/** * Регистрация нового пользователя * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $randLength = mt_rand(6, 9); $this->password = Yii::$app->security->generateRandomString($randLength); $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { //$profile = new Profile(); //$profile->user_id = $user->id; //$profile->name = $this->name; ////если в куках есть id аффилиата, сохраняем его //$affiliateId = (int) Yii::$app->request->cookies['affiliate']; //if ($affiliateId > 0 && User::findIdentity($affiliateId)) { //$profile->user_affiliate_id = $affiliateId; //} //$profile->save(); // Присвоить роль пользователю можно при создании нового пользователя. // присвоить Роль "user" новому пользователю // `auth_assignment` $userRole = Yii::$app->authManager->getRole('user'); Yii::$app->authManager->assign($userRole, $user->getId()); return $this->sendRegistrationEmail(); } } return null; }
public function reg() { $user = new User(); $user->phone = $this->phone; $user->email = $this->email; $user->status = $this->status; $user->setPassword($this->password); $user->generateAuthKey(); if ($this->scenario === 'emailActivation') { $user->generateSecretKey(); } $transaction = Yii::$app->db->beginTransaction(); try { if ($user->save()) { $modelProfile = new Profile(); $modelProfile->user_id = $user->id; if ($modelProfile->save()) { $transaction->commit(); return RbacHelper::assignRole($user->getId()) ? $user : null; } } else { return false; } } catch (Exception $e) { $transaction->rollBack(); } }
/** * @param User $model * @return User */ public function setModel($model) { $this->username = $model->username; $this->email = $model->email; $this->status = $model->status; $this->model = $model; $this->roles = ArrayHelper::getColumn(\Yii::$app->authManager->getRolesByUser($model->getId()), 'name'); return $this->model; }
/** * Assigns the appropriate role to the registered user. * If this is the first registered user in our system, he will get the * root role (this should be you), if not, he will get the user role. * * @param User $user User object. * @return string Role name. */ public static function assignRole(User $user) { $id = $user->getId(); // make sure there are no leftovers Role::deleteAll(['user_id' => $id]); $auth = Yii::$app->authManager; $role = $auth->getRole($user->user_role); $auth->assign($role, $id); // return assigned role name in case you want to use this method in tests return $role->name; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->save(); $user->afterSignup(); $auth = Yii::$app->authManager; $auth->revokeAll($user->getId()); if ($this->roles && is_array($this->roles)) { foreach ($this->roles as $role) { $auth->assign($auth->getRole($role), $user->getId()); } } return $user; } return null; }
/** * Signs up the user. * If scenario is set to "rna" (registration needs activation), this means * that user need to activate his account using email confirmation method. * * @return User|null The saved model or null if saving fails. */ public function signup() { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); $user->status = $this->status; // if scenario is "rna" ( Registration Needs Activation ) we will generate account activation token if ($this->scenario === 'rna') { $user->generateAccountActivationToken(); } // if user is saved and role is assigned return user object return $user->save() && RbacHelper::assignRole($user->getId()) ? $user : null; }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return string|\yii\web\Response */ public function actionCreate() { $user = new User(['scenario' => 'create']); $role = new Role(); if ($user->load(Yii::$app->request->post()) && $role->load(Yii::$app->request->post()) && Model::validateMultiple([$user, $role])) { $user->setPassword($user->password); $user->generateAuthKey(); if ($user->save()) { $role->user_id = $user->getId(); $role->save(); } return $this->redirect('index'); } else { return $this->render('create', ['user' => $user, 'role' => $role]); } }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { $userRole = Yii::$app->authManager->getRole('user'); Yii::$app->authManager->assign($userRole, $user->getId()); return $user; } } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); $user->save(false); // the following three lines were added: $auth = Yii::$app->authManager; $authorRole = $auth->getRole('author'); $auth->assign($authorRole, $user->getId()); return $user; } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->parent_id = $this->parent_id; $user->username = $this->username; $user->email = $this->email; $user->setPassword($this->password); $user->generateAuthKey(); $user->save(false); // нужно добавить следующие три строки: $auth = Yii::$app->authManager; $userRole = $auth->getRole('user'); $auth->assign($userRole, $user->getId()); return $user; } return null; }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->username = $this->username; $user->email = $this->email; $user->acceso_externo = false; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { // Aca es donde se asigna el rol "sinRol" a los nuevos usuarios $auth = Yii::$app->authManager; $sinRol = $auth->getRole('sinRol'); $auth->assign($sinRol, $user->getId()); return $user; } } return null; }
/** * Creates a new User model. * If creation is successful, the browser will be redirected to the 'view' page. * * @return string|\yii\web\Response */ public function actionCreate() { $user = new User(['scenario' => 'create']); if (!$user->load(Yii::$app->request->post())) { return $this->render('create', ['user' => $user]); } $user->setPassword($user->password); $user->generateAuthKey(); if (!$user->save()) { return $this->render('create', ['user' => $user]); } $auth = Yii::$app->authManager; $role = $auth->getRole($user->item_name); $info = $auth->assign($role, $user->getId()); if (!$info) { Yii::$app->session->setFlash('error', Yii::t('app', 'There was some error while saving user role.')); } return $this->redirect('index'); }
/** * Signs user up. * * @return User|null the saved model or null if saving fails */ public function signup() { if ($this->validate()) { $user = new User(); $user->first_name = $this->first_name; $user->last_name = $this->last_name; $user->username = $this->username; $user->email = $this->email; $user->avatar = $this->avatar; $user->setPassword($this->password); $user->generateAuthKey(); if ($user->save()) { /* Give user access to client area */ $auth = Yii::$app->authManager; $authorRole = $auth->getRole('client'); $auth->assign($authorRole, $user->getId()); return $user; } } return null; }
/** * Заполнение данных пользователя. * * @param User $user */ public function fillUserData(User $user) { $this->setUserId($user->getId()); $this->setCompanyId($user->getCompanyId()); $this->setEmail($user->getEmail()); $this->setFullName($user->getFullName()); $this->setRole($user->getRole()); $this->setStatus($user->getStatus()); $this->setPhoto($user->getPhoto()); }