Пример #1
0
 /**
  * Registers a new user account.
  * @return bool
  */
 public function register()
 {
     if (!$this->validate()) {
         return false;
     }
     $this->user->setAttributes(['email' => $this->email, 'username' => $this->username, 'password' => $this->password, 'firstName' => $this->firstName, 'lastName' => $this->lastName]);
     return $this->user->register();
 }
Пример #2
0
 public function actionRegister()
 {
     $errors = null;
     $model = new UserForm();
     if (Yii::$app->request->isPost) {
         $model->setAttributes(Yii::$app->request->post());
         if ($model->validate()) {
             $user = new User();
             $user->setAttributes($model->getAttributes());
             $user->setPassword($model->password);
             $user->generateAuthKey();
             $save = $user->save();
             if ($save) {
                 $purse = new Purse();
                 $purse->user_id = $user->id;
                 $purse->active = 1;
                 $purse->balance = 0;
                 $purse->name = "Основной";
                 $purse->save();
                 $login = Yii::$app->user->login($user, 3600 * 24 * 14);
                 if ($login) {
                     return $this->goHome();
                 }
             }
         } else {
             $errors = $model->getErrors();
         }
     }
     return $this->renderPartial('register', ['errors' => $errors, 'model' => $model]);
 }
Пример #3
0
 /**
  * @param $data array
  * @param $password string
  * @return User
  */
 public function create($data, $password)
 {
     $user = new User();
     $user->setAttributes($data);
     $user->group = User::GROUP_USER;
     $user->setPassword($password);
     $user->authKey = Yii::$app->security->generateRandomString();
     $this->saveOrFail($user);
     return $user;
 }
Пример #4
0
 public function actionCreate()
 {
     $model = new User();
     $model->setAction('admin-insert');
     if (isset($_POST['save'])) {
         $model->setAttributes($_POST['User']);
         if ($model->validate() && $model->adminCreate()) {
             Messages::get()->success("User created!");
             $this->getRequest()->goToPage('users', 'index');
         }
     }
     $this->assign('model', $model);
 }
Пример #5
0
 /**
  * @return bool
  */
 public function register()
 {
     if ($this->validate()) {
         $user = new User();
         if (User::SCENARIO_PERSON == $this->status) {
             $user->setScenario($this->status);
         } elseif (User::SCENARIO_COMPANY == $this->status) {
             $user->setScenario($this->status);
         } else {
             return false;
         }
         $user->setAttributes($this->getAttributes());
         $user->setPassword($this->password);
         return $user->save();
     }
     return false;
 }
Пример #6
0
 public function actionCreateUser()
 {
     $this->stdout("Create a new user...\n", Console::BOLD);
     $username = $this->prompt("Username: "******"Username can only contain alphabet characters, numbers, dots and underscores.";
         return false;
     }]);
     $name = trim($this->prompt("Display name: ", ['validator' => function ($input, &$error) {
         $input = trim($input);
         if (preg_match('/^[\\w\\d_\\. ]+$/', $input)) {
             return true;
         }
         $error = "Display name must contain at least 1 visible character, and can only contain alphabet characters, numbers, dots, underscores and spaces. Leading and trailing spaces will be trimmed.";
         return false;
     }]));
     $password = $this->prompt("Password: "******"Password must have at least 8 characters";
         return false;
     }]);
     $email = $this->prompt("Email: ", ['validator' => function ($input, &$error) {
         if (filter_var($input, FILTER_VALIDATE_EMAIL)) {
             return true;
         }
         $error = "Invalid email format";
         return false;
     }]);
     $role = $this->prompt("Role (administrator/moderator): ", ['validator' => function ($input, &$error) {
         if (preg_match('/^(administrator|moderator)$/', $input)) {
             return true;
         }
         $error = "Role must be either 'administrator' or 'moderator'";
         return false;
     }]);
     if ($this->confirm("Do you want to proceed with creating this user?")) {
         $model = new User();
         $model->scenario = User::SCENARIO_CONSOLE_CREATE;
         $model->setAttributes(['username' => $username, 'name' => $name, 'email' => $email, 'status' => User::STATUS_ACTIVE]);
         $model->setPassword($password);
         $model->generateAuthKey();
         $model->generateAccessToken();
         if ($model->save()) {
             $auth = Yii::$app->authManager;
             $role = $auth->getRole($role);
             $auth->assign($role, $model->id);
             $this->stdout("User '{$username}' has been created\n", Console::BOLD);
         } else {
             foreach ($model->errors as $key => $errors) {
                 foreach ($errors as $error) {
                     $this->stdout("'{$key}': {$error}", Console::BOLD);
                 }
             }
         }
     } else {
         $this->stdout("Aborting...\n", Console::BOLD);
     }
     return 0;
 }