/** * метод создает пользователя и назначает его админом * @param string $email * @param string $name * @param string $pass * @return int */ public function actionAdd($email = '', $name = '', $pass = '') { if (empty($email)) { $email = $this->prompt('Enter user email:', ['required' => true]); } if (empty($name)) { $name = $this->prompt('Enter user name:', ['required' => true]); } if (empty($pass)) { $pass = $this->prompt('Enter user password:'******'required' => true]); } $User = new \resources\User(['name' => $name, 'email' => $email, 'password' => $pass, 'activated' => \resources\User::ACTIVATED, 'deleted' => \resources\User::NOT_DELETED]); $User->save(); if (!$User->hasErrors()) { AuthManager()->assign(RF::Role(\frontend\Permissions::ROLE_USER), $User->id); AuthManager()->assign(RF::Role(\frontend\Permissions::ROLE_ADMIN), $User->id); $this->stdout("User have been successfully added\n", \yii\helpers\Console::FG_GREEN); } else { $this->stdout("ERROR creating user\n", \yii\helpers\Console::FG_RED); $error = array_shift($User->getFirstErrors()); if (!empty($error)) { $this->stdout("\t> {$error}\n", \yii\helpers\Console::FG_RED); } return static::EXIT_CODE_ERROR; } return static::EXIT_CODE_NORMAL; }
/** * @param string $email * @param string $name * @param string $pass * @return int */ public function actionAdd($email = '', $name = '', $pass = '') { if (empty($email)) { $email = $this->prompt('Enter user email:', ['required' => true]); } if (empty($name)) { $name = $this->prompt('Enter user name:', ['required' => true]); } if (empty($pass)) { $pass = $this->prompt('Enter user password:'******'required' => true]); } $User = new \resources\Account(['name' => $name, 'email' => $email, 'password' => $pass, 'activated_at' => time()]); $User->save(); if (!$User->hasErrors()) { AuthManager()->assign(RbacFactory::Role(\common\Roles::USER), $User->id); AuthManager()->assign(RbacFactory::Role(\common\Roles::ADMIN), $User->id); $this->stdout("User have been successfully added\n", \yii\helpers\Console::FG_GREEN); } else { $this->stdout("ERROR creating user\n", \yii\helpers\Console::FG_RED); $error = array_shift($User->getFirstErrors()); if (!empty($error)) { $this->stdout("\t> {$error}\n", \yii\helpers\Console::FG_RED); } return static::EXIT_CODE_ERROR; } return static::EXIT_CODE_NORMAL; }
/** * @param \yii\authclient\ClientInterface $Client * @return bool * @throws \yii\base\InvalidConfigException */ public function save(\yii\authclient\ClientInterface $Client) { /** @var Account\backend\Module $Module */ $Module = \Yii::$app->getModule($this->accountModule); $roles = $Module->roles; /** @var AccountModel $Account */ $Account = \Yii::createObject(AccountModel::class); $Account->appendClientAttributes($Client); $Account->setAttributes(['email' => $this->email]); $Account->validate() && $Account->save(); $AuthResponse = AccountAuthResponseModel::createLog($Client); if ($Account->hasErrors()) { $AuthResponse->result = Json::encode($Account->getErrors()); } else { $AuthResponse->result = (string) $Account->id; $Account->pushSocialLink($Client); AuthManager()->assign(RbacFactory::Role($roles['user']), $Account->id); $SignInFormModel = \Yii::createObject(SignInForm::class); User()->login($Account, $SignInFormModel::REMEMBER_TIME); } $AuthResponse->validate() && $AuthResponse->save(); if ($Account->hasErrors()) { $this->populateErrors($Account, 'name'); } return !$Account->hasErrors(); }
/** * @param string $email * @param string $name * @param string $pass * @return int */ public function actionAdd($email = '', $name = '', $pass = '') { /** @var Account\backend\Module $Module */ $Module = \Yii::$app->getModule($this->accountModule); $roles = $Module->roles; if (empty($email)) { $email = $this->prompt('Enter user email:', ['required' => true]); } if (empty($name)) { $name = $this->prompt('Enter user name:', ['required' => true]); } if (empty($pass)) { $pass = $this->prompt('Enter user password:'******'required' => true]); } /** @var AccountModel $Account */ $Account = \Yii::createObject(AccountModel::class); $Account->setAttributes(['name' => $name, 'email' => $email, 'password' => $pass, 'activated_at' => time()]); $Account->save(); if (!$Account->hasErrors()) { AuthManager()->assign(RbacFactory::Role($roles['user']), $Account->id); AuthManager()->assign(RbacFactory::Role($roles['admin']), $Account->id); $this->stdout("User have been successfully added\n", Console::FG_GREEN); } else { $this->stdout("ERROR creating user\n", Console::FG_RED); $error = array_shift($Account->getFirstErrors()); if (!empty($error)) { $this->stdout("\t> {$error}\n", Console::FG_RED); } return static::EXIT_CODE_ERROR; } return static::EXIT_CODE_NORMAL; }
/** * @inheritdoc */ public function extraFields() { $fields = parent::extraFields(); $fields['roles'] = function (Model $Model) { $result = []; $roles = AuthManager()->getRolesByUser($Model->id); foreach ($roles as $role => $conf) { $result[$role] = true; } return $result; }; $fields['permissions'] = function (Model $Model) { $result = []; $permissions = AuthManager()->getPermissionsByUser($Model->id); foreach ($permissions as $permission => $conf) { $result[$permission] = true; } return $result; }; $fields['properties'] = function (Model $Model) { return $Model->properties(); }; $fields['alerts'] = function (Model $Model) { $Alerts = $this->alerts; return empty($Alerts) ? [] : ArrayHelper::getColumn($Alerts, function (AccountAlertModel $Model) { return $Model->toArray(); }); }; ApiAttribute::datetimeFormat($fields, 'created_at'); ApiAttribute::datetimeFormat($fields, 'updated_at'); ApiAttribute::datetimeFormat($fields, 'activated_at'); ApiAttribute::datetimeFormat($fields, 'deleted_at'); return $fields; }
/** * @return bool */ public function save() { $Client = $this->Client; \Yii::warning($Client->isNewRecord); $Client->name = $this->name; $Client->email = $this->email; $Client->phone = $this->phone; $result = $Client->validate() && $Client->save(); if ($Client->hasErrors()) { $this->populateErrors($Client, 'name'); } if (AuthManager() instanceof \yii\rbac\DbManager) { AuthManager()->invalidateCache(); } $this->Client = $Client; return $result; }
/** * @param \yii\authclient\ClientInterface $Client * @return bool * @throws \yii\base\InvalidConfigException */ public function save(\yii\authclient\ClientInterface $Client) { /** @var \cookyii\modules\Account\resources\Account $Account */ $Account = \Yii::createObject(\cookyii\modules\Account\resources\Account::className()); $Account->appendClientAttributes($Client); $Account->setAttributes(['email' => $this->email, 'password' => Security()->generateRandomString(10)]); $Account->validate() && $Account->save(); if (!$Account->hasErrors()) { $Account->notificationHelper->sendSignUpEmail(); AuthManager()->assign(RbacFactory::Role(\common\Roles::USER), $Account->id); $SignInFormModel = \Yii::createObject(SignInForm::className()); User()->login($Account, $SignInFormModel::REMEMBER_TIME); } if ($Account->hasErrors()) { $this->populateErrors($Account, 'name'); } return !$Account->hasErrors(); }
/** * @return bool */ public function register() { /** @var \cookyii\modules\Account\resources\Account $Account */ $Account = \Yii::createObject(\cookyii\modules\Account\resources\Account::className()); $Account->setAttributes(['name' => $this->name, 'email' => $this->email, 'password' => $this->password, 'activated_at' => time()]); $Account->validate() && $Account->save(); if (!$Account->hasErrors()) { $Account->notificationHelper->sendSignUpEmail(); AuthManager()->assign(RbacFactory::Role(\common\Roles::USER), $Account->id); if ($this->loginAfterRegister) { $SignInFormModel = \Yii::createObject(SignInForm::className()); User()->login($Account, $SignInFormModel::REMEMBER_TIME); } } if ($Account->hasErrors()) { $this->populateErrors($Account, 'name'); } return !$Account->hasErrors(); }
/** * @return bool */ public function save() { $Account = $this->Account; $Account->name = $this->name; $Account->email = $this->email; $Account->gender = $this->gender; if (!empty($this->new_password)) { $Account->password = $this->new_password; } $result = $Account->validate() && $Account->save(); if ($Account->hasErrors()) { $this->populateErrors($Account, 'name'); } if (AuthManager() instanceof \yii\rbac\DbManager) { AuthManager()->invalidateCache(); } $this->Account = $Account; return $result; }
/** * @return bool */ public function register() { /** @var Account\backend\Module $Module */ $Module = \Yii::$app->getModule($this->accountModule); $roles = $Module->roles; /** @var AccountModel $Account */ $Account = \Yii::createObject(AccountModel::class); $Account->setAttributes(['name' => $this->name, 'email' => $this->email, 'password' => $this->password, 'activated_at' => time()]); $Account->validate() && $Account->save(); if (!$Account->hasErrors()) { $Account->notificationHelper->sendSignUpEmail(); AuthManager()->assign(RbacFactory::Role($roles['user']), $Account->id); if ($this->loginAfterRegister) { $SignInFormModel = \Yii::createObject(SignInForm::class); User()->login($Account, $SignInFormModel::REMEMBER_TIME); } } if ($Account->hasErrors()) { $this->populateErrors($Account, 'name'); } return !$Account->hasErrors(); }
public function testMain() { ob_start(); dump(['test' => rand()]); // это тест! expect('Функция дампа не отдает результат', ob_get_clean())->notEmpty(); $this->assertInstanceOf(yii\log\Dispatcher::class, YiiLog()); $this->assertInstanceOf(yii\db\Connection::class, DB()); $this->assertInstanceOf(yii\base\ErrorHandler::class, ErrorHandler()); $this->assertInstanceOf(yii\caching\Cache::class, Cache()); $this->assertInstanceOf(yii\i18n\Formatter::class, Formatter()); $this->assertInstanceOf(yii\base\View::class, View()); $this->assertInstanceOf(yii\i18n\I18N::class, I18N()); $this->assertInstanceOf(yii\rbac\ManagerInterface::class, AuthManager()); $this->assertInstanceOf(yii\web\AssetManager::class, AssetManager()); $this->assertInstanceOf(yii\web\User::class, User()); $this->assertInstanceOf(yii\base\Request::class, Request()); $this->assertInstanceOf(yii\base\Response::class, Response()); $this->assertInstanceOf(yii\web\Session::class, Session()); $this->assertInstanceOf(yii\web\UrlManager::class, UrlManager()); $this->assertInstanceOf(yii\mail\MailerInterface::class, Mailer()); $this->assertInstanceOf(services\File\Service::class, FileService()); $this->assertInstanceOf(services\Activity\Service::class, ActivityService()); }
/** * @return array * @throws \yii\web\BadRequestHttpException * @throws \yii\web\NotFoundHttpException */ public function run() { $result = ['result' => false, 'message' => \Yii::t('account', 'Unknown error')]; $account_id = (int) Request()->post('account_id'); $roles = (array) Request()->getBodyParam('roles', []); if (empty($account_id)) { throw new \yii\web\BadRequestHttpException('Empty account id'); } /** @var $modelClass \cookyii\modules\Account\resources\Account */ $modelClass = $this->modelClass; $Account = $modelClass::find()->byId($account_id)->one(); if (empty($Account)) { throw new \yii\web\NotFoundHttpException('Account not found'); } AuthManager()->revokeAll($Account->id); if (!empty($roles)) { foreach ($roles as $role => $flag) { if ($flag === true) { AuthManager()->assign(RbacFactory::Role($role), $Account->id); } } } return ['result' => true, 'message' => \Yii::t('account', 'Roles successfully saved.')]; }
/** * @return bool */ public function save() { $Account = $this->Account; $Account->name = $this->name; $Account->email = $this->email; $Account->gender = $this->gender; if ($Account->isNewRecord) { $Account->activated_at = time(); } if (!empty($this->new_password)) { $Account->password = $this->new_password; } $result = $Account->validate() && $Account->save(); if ($Account->hasErrors()) { $this->populateErrors($Account, 'name'); } else { AuthManager()->revokeAll($Account->id); $roles = $this->roles; if (!empty($roles)) { foreach ($roles as $role => $checked) { if ($checked === true) { AuthManager()->assign(RbacFactory::Role($role), $Account->id); } } } } if (AuthManager() instanceof \yii\rbac\DbManager) { AuthManager()->invalidateCache(); } $this->Account = $Account; return $result; }
/** * @param \yii\authclient\ClientInterface $Client * @throws \yii\web\ForbiddenHttpException */ public function authSuccessCallback(\yii\authclient\ClientInterface $Client) { $AuthResponse = new \cookyii\modules\Account\resources\Account\Auth\Response(); $AuthResponse->client = $Client->getId(); $attributes = $Client->getUserAttributes(); $AuthResponse->response = Json::encode($attributes); /** @var \cookyii\modules\Account\resources\Account $AccountModel */ $AccountModel = \Yii::createObject(\cookyii\modules\Account\resources\Account::className()); $AccountQuery = $AccountModel::find(); switch ($Client->getId()) { case 'facebook': $AccountQuery->byFacebookId($attributes['id']); break; case 'github': $AccountQuery->byGithubId($attributes['id']); break; case 'google': $AccountQuery->byGoogleId($attributes['id']); break; case 'linkedin': $AccountQuery->byLinkedinId($attributes['id']); break; case 'live': $AccountQuery->byLiveId($attributes['id']); break; case 'twitter': $AccountQuery->byTwitterId($attributes['id']); break; case 'vkontakte': $AccountQuery->byVkontakteId($attributes['id']); break; case 'yandex': $AccountQuery->byYandexId($attributes['id']); break; } $Account = $AccountQuery->one(); if ($Account instanceof \cookyii\modules\Account\resources\Account) { if (true !== ($reason = $Account->isAvailable())) { switch ($reason) { default: case true: break; case 'not-activated': $Account->addError('activated', \Yii::t('account', 'Account is not activated.')); break; case 'deleted': $Account->addError('deleted', \Yii::t('account', 'Account removed.')); break; } $AuthResponse->result = Json::encode($Account->getErrors()); } else { $AuthResponse->result = Json::encode($Account->id); } } else { $Account = $AccountModel; $Account->appendClientAttributes($Client); if (!empty($Account->email)) { $SearchAccount = $AccountModel::find()->byEmail($Account->email)->one(); if (!empty($SearchAccount)) { $Account = $SearchAccount; $Account->appendClientAttributes($Client); } } else { Session()->set('OAuthResponseClient', $Client); Response()->redirect(['/account/sign/fill'])->send(); exit; } if ($Account->save()) { $Account->createSocialLink($Client); $AuthResponse->result = Json::encode($Account->id); if (!$Account->can(\common\Roles::USER)) { AuthManager()->assign(RbacFactory::Role(\common\Roles::USER), $Account->id); } } else { $AuthResponse->result = Json::encode($Account->getErrors()); } } $AuthResponse->save(); if ($Account instanceof \cookyii\modules\Account\resources\Account && !$Account->isNewRecord && !$Account->hasErrors()) { $Account->save(); User()->login($Account, 86400); } else { $errors = $Account->getFirstErrors(); if (isset($errors['activated'])) { throw new \yii\web\ForbiddenHttpException($errors['activated']); } if (isset($errors['deleted'])) { throw new \yii\web\ForbiddenHttpException($errors['deleted']); } } }
protected function switchTestDb() { $this->traitSwitchTestDb(); AuthManager()->db = $this->db; }
/** * @return array */ public function getRoles() { $result = []; $roles = static::getAllRoles(); $Assignments = AuthManager()->getAssignments($this->id); foreach (array_keys($Assignments) as $role) { $result[$role] = $roles[$role]; } unset($result['user']); return $result; }
/** * @param \yii\authclient\ClientInterface $Client * @throws \yii\base\NotSupportedException */ public function authSuccessCallback(\yii\authclient\ClientInterface $Client) { $AuthResponse = new \resources\User\Auth\Response(); $AuthResponse->client = $Client->getId(); $attributes = $Client->getUserAttributes(); $AuthResponse->response = Json::encode($attributes); $UserQuery = \resources\User::find(); switch ($Client->getId()) { case 'facebook': $UserQuery->byFacebookId($attributes['id']); break; case 'github': $UserQuery->byGithubId($attributes['id']); break; case 'google': $UserQuery->byGoogleId($attributes['id']); break; case 'linkedin': $UserQuery->byLinkedinId($attributes['id']); break; case 'live': $UserQuery->byLiveId($attributes['id']); break; case 'twitter': $UserQuery->byTwitterId($attributes['id']); break; case 'vkontakte': $UserQuery->byVkontakteId($attributes['id']); break; case 'yandex': $UserQuery->byYandexId($attributes['id']); break; } /** @var \resources\User $User */ $User = $UserQuery->one(); if ($User instanceof \resources\User) { $AuthResponse->result = Json::encode($User->id); } else { $User = new \resources\User(); $User->appendClientAttributes($Client); if ($User->save()) { $User->createSocialLink($Client); $AuthResponse->result = Json::encode($User->id); AuthManager()->assign(RbacFactory::Role(\frontend\Permissions::ROLE_USER), $User->id); } else { $AuthResponse->result = Json::encode($User->getErrors()); } } $AuthResponse->save(); if ($User instanceof \resources\User && !$User->isNewRecord) { $User->save(); User()->login($User, 86400); } }
/** * @param \yii\authclient\ClientInterface $Client * @throws \yii\web\ForbiddenHttpException */ public function socialAuthCallback(\yii\authclient\ClientInterface $Client) { /** @var Account\backend\Module $Module */ $Module = \Yii::$app->getModule($this->accountModule); $roles = $Module->roles; $AuthResponse = AccountAuthResponseModel::createLog($Client); $attributes = $Client->getUserAttributes(); /** @var AccountModel $AccountModel */ $AccountModel = \Yii::createObject(AccountModel::class); $AccountQuery = $AccountModel::find(); switch ($Client->getId()) { case 'facebook': $AccountQuery->byFacebookId($attributes['id']); break; case 'instagram': $AccountQuery->byInstagramId($attributes['id']); break; case 'github': $AccountQuery->byGithubId($attributes['id']); break; case 'google': $AccountQuery->byGoogleId($attributes['id']); break; case 'linkedin': $AccountQuery->byLinkedinId($attributes['id']); break; case 'live': $AccountQuery->byLiveId($attributes['id']); break; case 'twitter': $AccountQuery->byTwitterId($attributes['id']); break; case 'vkontakte': $AccountQuery->byVkontakteId($attributes['id']); break; case 'yandex': $AccountQuery->byYandexId($attributes['id']); break; case 'odnoklassniki': $AccountQuery->byOdnoklassnikiId($attributes['id']); break; } $Account = $AccountQuery->one(); if ($Account instanceof AccountModel) { $Account->pushSocialLink($Client); if (true !== ($reason = $Account->isAvailable())) { switch ($reason) { default: case true: break; case 'not-activated': $Account->addError('activated', \Yii::t('cookyii.account', 'Account is not activated.')); break; case 'deleted': $Account->addError('deleted', \Yii::t('cookyii.account', 'Account removed.')); break; } $AuthResponse->result = Json::encode($Account->getErrors()); } else { $AuthResponse->result = Json::encode($Account->id); } } else { $Account = $AccountModel; $Account->appendClientAttributes($Client); if (!empty($Account->email)) { $SearchAccount = $AccountModel::find()->byEmail($Account->email)->one(); if (!empty($SearchAccount)) { $Account = $SearchAccount; $Account->appendClientAttributes($Client); } } $Account->activated_at = time(); $Account->validate() && $Account->save(false); if ($Account->hasErrors()) { $AuthResponse->result = Json::encode($Account->getErrors()); } else { $Account->pushSocialLink($Client); $AuthResponse->result = Json::encode($Account->id); if (!$Account->can($roles['user'])) { AuthManager()->assign(RbacFactory::Role($roles['user']), $Account->id); } } } $AuthResponse->validate() && $AuthResponse->save(); if ($Account instanceof AccountModel && !$Account->isNewRecord && !$Account->hasErrors()) { $Account->save(); User()->login($Account, 86400); } else { $errors = $Account->getFirstErrors(); if (isset($errors['activated'])) { throw new \yii\web\ForbiddenHttpException($errors['activated']); } if (isset($errors['deleted'])) { throw new \yii\web\ForbiddenHttpException($errors['deleted']); } } }
/** * @return array */ public static function getAllPermissions() { return ArrayHelper::map(AuthManager()->getPermissions(), 'name', 'description'); }
public function init() { AuthManager()->db = DB(); }