/** * @inheritdoc */ protected function getValue($event) { if ($this->value instanceof Expression) { return $this->value; } else { return is_callable($this->value) ? call_user_func($this->value, $event) : Security()->generateRandomString(32); } }
/** * @return string */ public function getDefaultValue() { $length = $this->length; $length = is_array($length) ? (int) rand(array_shift($length), array_shift($length)) : (int) $length; $length = $length <= 0 ? 12 : $length; $length = $length > 32 ? 32 : $length; return Security()->generateRandomString($length); }
/** * @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(); }
/** * @param null|string $name * @param null|string $email * @param null|string $password * @return AccountModel * @throws \yii\base\Exception */ public function create($name = null, $email = null, $password = null) { $Client = $this->Model; if ($Client->isNewRecord) { throw new \yii\base\Exception(\Yii::t('cookyii.client', 'You can not create an account from unsaved client.')); } $name = empty($name) ? $Client->name : $name; $email = empty($email) ? $Client->email : $email; $password = empty($password) ? Security()->generateRandomString(10) : $password; /** @var AccountModel $AccountModel */ $AccountModel = \Yii::createObject(AccountModel::class); $Account = $AccountModel::find()->byEmail($email)->one(); if (empty($Account)) { $Account = $AccountModel; $Account->setAttributes(['name' => $name, 'email' => $email, 'password' => $password, 'activated_at' => time()]); $Account->validate() && $Account->save(); } if (!$Account->hasErrors() && !$Account->isNewRecord) { $Client->account_id = $Account->id; $Client->validate() && $Client->save(); } return $Account; }
private function events() { $this->on(self::EVENT_BEFORE_INSERT, function (\yii\base\ModelEvent $Event) { /** @var self $Model */ $Model = $Event->sender; $Model->password_hash = Security()->generatePasswordHash($this->password); $Model->auth_key = Security()->generateRandomString(); $Model->token = Security()->generateRandomString(); }); $this->on(self::EVENT_BEFORE_UPDATE, function (\yii\base\ModelEvent $Event) { /** @var self $Model */ $Model = $Event->sender; if (!empty($this->password)) { $Model->password_hash = Security()->generatePasswordHash($this->password); } }); }
/** * @param AccountModel $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); }
<?php /** * account-user.php * @author Revin Roman http://phptime.ru * * @var $faker \Faker\Generator * @var $index integer */ use frontend\modules\Account; return ['id' => $index + 1, 'username' => $faker->userName, 'gender' => Account\models\User::MALE, 'activated' => Account\models\User::ACTIVATED, 'deleted' => Account\models\User::NOT_DELETED, 'email' => $faker->email, 'auth_key' => Security()->generateRandomString(), 'password_hash' => Security()->generatePasswordHash('password_' . $index), 'created_at' => time(), 'updated_at' => time()];
/** * @param \yii\base\ModelEvent $Event * @throws \yii\base\Exception */ public function updatePasswordHashBeforeUpdate(\yii\base\ModelEvent $Event) { /** @var static $Model */ $Model = $Event->sender; if (!empty($this->password)) { $Model->password_hash = Security()->generatePasswordHash($this->password); } }