/** * @return string */ public function actionIndex() { /** @var \cookyii\modules\Account\resources\Account $AccountModel */ $AccountModel = \Yii::createObject(\cookyii\modules\Account\resources\Account::className()); $AccountEditForm = \Yii::createObject(['class' => Account\backend\forms\AccountEditForm::className(), 'Account' => $AccountModel]); return $this->render('index', ['AccountEditForm' => $AccountEditForm]); }
/** * @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]); } /** @var \cookyii\modules\Account\resources\Account $Account */ $Account = \Yii::createObject(\cookyii\modules\Account\resources\Account::className()); $Account->setAttributes(['name' => $name, 'email' => $email, 'password' => $pass, 'activated_at' => time()]); $Account->save(); if (!$Account->hasErrors()) { AuthManager()->assign(RbacFactory::Role(\common\Roles::USER), $Account->id); AuthManager()->assign(RbacFactory::Role(\common\Roles::ADMIN), $Account->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($Account->getFirstErrors()); if (!empty($error)) { $this->stdout("\t> {$error}\n", \yii\helpers\Console::FG_RED); } return static::EXIT_CODE_ERROR; } return static::EXIT_CODE_NORMAL; }
/** * @return \cookyii\modules\Account\resources\Account */ private function getAccount() { if ($this->_Account === null) { /** @var \cookyii\modules\Account\resources\Account $AccountModel */ $AccountModel = \Yii::createObject(\cookyii\modules\Account\resources\Account::className()); $this->_Account = $AccountModel::find()->byEmail($this->email)->one(); } return $this->_Account; }
/** * @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(); }
/** * @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']); } } }
/** * @return array */ public static function getGroupedPermissionValues() { /** @var \cookyii\modules\Account\resources\Account $AccountModel */ $AccountModel = \Yii::createObject(\cookyii\modules\Account\resources\Account::className()); $permissions = $AccountModel::getAllPermissions(); $result = ['items' => [], 'children' => []]; if (!empty($permissions)) { foreach ($permissions as $permission => $description) { if (empty($permission)) { continue; } $part = explode('.', $permission); if (empty($part) || count($part) < 1) { continue; } $count = count($part); if ($count === 1) { if (!in_array($permission, $result['items'], true)) { $result['items'][$permission] = $description; } } else { $g1 = sprintf('%s.*', $part[0]); if (!isset($result['children'][$g1])) { $result['children'][$g1] = ['items' => []]; } if (!in_array($permission, $result['children'][$g1]['items'], true)) { $result['children'][$g1]['items'][$permission] = $description; } } } } return $result; }
/** * @param \cookyii\modules\Account\resources\Account $Account * @return array */ private function decryptData($Account) { if (empty($this->hash)) { throw new \yii\base\InvalidParamException('Empty hash.'); } $data = Security()->decryptByKey(base64_decode($this->hash), $Account->getEncryptKey()); if (empty($data)) { throw new \yii\base\InvalidParamException('Invalid hash.'); } return Json::decode($data); }