You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							81 lines
						
					
					
						
							2.2 KiB
						
					
					
				
			
		
		
	
	
							81 lines
						
					
					
						
							2.2 KiB
						
					
					
				<?php | 
						|
/** | 
						|
 * Created by PhpStorm. | 
						|
 * User: Egorka | 
						|
 * Date: 11.08.2017 | 
						|
 * Time: 15:07 | 
						|
 */ | 
						|
 | 
						|
namespace console\controllers; | 
						|
 | 
						|
use core\forms\user\UserForm; | 
						|
use yii\helpers\ArrayHelper; | 
						|
use core\services\user\UserManageService; | 
						|
use yii\console\Controller; | 
						|
use Yii; | 
						|
 | 
						|
class UserController extends Controller | 
						|
{ | 
						|
    private $_service; | 
						|
 | 
						|
    public function __construct($id, $module, UserManageService $service, $config = []) | 
						|
    { | 
						|
        parent::__construct($id, $module, $config); | 
						|
        $this->_service = $service; | 
						|
    } | 
						|
 | 
						|
    public function actionAdd() | 
						|
    { | 
						|
        $username = $this->prompt('Username:', ['required' => true]); | 
						|
        $email    = $this->prompt('Email:', ['required' => true]); | 
						|
        $password = $this->prompt('Password:', ['required' => true]); | 
						|
        $role     = $this->select('Role:', ArrayHelper::map(Yii::$app->authManager->getRoles(), 'name', 'description')); | 
						|
 | 
						|
        //$this->findModel($username, $email); | 
						|
 | 
						|
        $form           = new UserForm(); | 
						|
        $form->username = $username; | 
						|
        $form->email    = $email; | 
						|
        $form->password = $password; | 
						|
        $form->role     = $role; | 
						|
 | 
						|
        if ($form->validate()) { | 
						|
            $this->_service->create($form); | 
						|
            $this->stdout('Done!' . PHP_EOL); | 
						|
 | 
						|
            return; | 
						|
        } | 
						|
 | 
						|
        $this->stdout('Errors found!' . PHP_EOL); | 
						|
        foreach ($form->errors as $error) { | 
						|
            $this->stdout(is_string($error) ? $error : $error[0] . PHP_EOL); | 
						|
        } | 
						|
        //$this->stdout(print_r($form->errors, true) . PHP_EOL); | 
						|
 | 
						|
 | 
						|
        /* | 
						|
        $user = User::create($username, $email, $phone, $password); | 
						|
        $user->save();*/ | 
						|
    } | 
						|
 | 
						|
    public function actionAddAdmin($username, $email, $password) | 
						|
    { | 
						|
        $form           = new UserForm(); | 
						|
        $form->username = $username; | 
						|
        $form->email    = $email; | 
						|
        $form->password = $password; | 
						|
        $form->role     = 'admin'; | 
						|
 | 
						|
        if ($form->validate()) { | 
						|
            $this->_service->create($form); | 
						|
            return; | 
						|
        } | 
						|
    } | 
						|
 | 
						|
    /*private function findModel(string $username, string $email): void | 
						|
    { | 
						|
        if ($model = User::find()->where(['username' => $username])->orWhere(['email' => $email])->one()) { | 
						|
            throw new Exception('User is already exists.'); | 
						|
        } | 
						|
    }*/ | 
						|
}
 | 
						|
 |