AjaxModelBehavior::className(), ]; } /** * @inheritdoc */ public static function tableName() { return 'userV'; } public function scenarios() { return array( 'activation' => ['login', 'key'], 'update' => ['phone', 'name', 'email', 'password', 'birthDate', 'preferredLanguage', 'repassword', 'enabled', 'roleId', 'login'], 'insert' => ['phone', 'name', 'email', 'password', 'birthDate', 'preferredLanguage', 'repassword', 'enabled', 'roleId', 'login'], 'signup' => ['phone', 'name', 'login', 'email', 'password', 'repassword'], 'reset' => ['password', 'repassword'], 'default' => ['phone', 'name', 'login', 'email', 'password', 'repassword'], 'login' => ['login', 'password'], 'view' => ['id', 'name', 'roleId', 'email', 'phone', 'birthDate', 'preferredLanguage', 'enabled'], 'list' => [ 'id', 'roleId', 'name', 'phone', 'enabled', 'added', 'changed', 'addedUserId', 'changedUserId', 'addedUserName', 'changedUserName', 'addedIp', 'changedIp'], ); } public function attributeLabels() { return array( 'login' => Yii::t('api', 'Login'), 'password' => Yii::t('api', 'Password'), 'email' => Yii::t('api', 'Email'), 'repassword' => Yii::t('api', 'Confirm Password'), 'name' => Yii::t('api', 'Name'), ); } public function rules() { return array( array('id', 'required', 'on' => ['update']), array('name', 'required'), array('login', 'filter', 'filter' => 'trim'), array('login', 'required'), array('login', 'string', 'min' => 3, 'max' => 50), array('login', 'unique', 'message' => Yii::t('yii', '{attribute} "{value}" has already been taken.', array('{attribute}' => Yii::t('api', 'Login'), '{value}' => $this->login)) ), array('phone', 'unique', 'message' => Yii::t('yii', '{attribute} "{value}" has already been taken.', array('{attribute}' => Yii::t('api', 'Phone'), '{value}' => $this->phone)) ), array('phone', 'required'), array('phone', 'yii\validators\RegularExpressionValidator', 'pattern' => '/^\+380\d{9}$/'), array('email', 'filter', 'filter' => 'trim'), array('email', 'required'), array('email', 'email'), array('password', 'required', 'on' => array('signup', 'login', 'insert')), array('password', 'string', 'min' => 6, 'max' => 30, 'tooShort' => Yii::t('api', 'Password should contain at least 6 characters.'), 'tooLong' => Yii::t('api', 'Password should contain at most 30 characters.'), 'on' => ['signup', 'login', 'insert'], ), //array('password', 'validateUpdatePassword', 'on' => 'update'), array('repassword', 'required', 'on' => array('signup', 'insert')), array('repassword', 'string', 'min' => 6, 'max' => 30, 'tooShort' => Yii::t('api', 'Password should contain at least 6 characters.'), 'tooLong' => Yii::t('api', 'Password should contain at most 30 characters.'), 'on' => ['signup', 'update', 'insert'], ), array('repassword', 'compare', 'compareAttribute'=>'password', 'on' => array('signup', 'update', 'insert'), 'message' => Yii::t('api', 'Password confirmation does not match') ) ); } public function propertyMapping() { return [ 'user_id_' => 'id', 'name_' => 'name', 'phone_' => 'phone', 'email_' => 'email', 'login_' => 'login', 'password_' => 'password', 'role_id_' => 'roleId', 'enabled_' => 'enabled', 'birthdate_' => 'birthDate', 'preferred_language_' => 'preferredLanguage', 'token_' => 'token', ]; } public function internalServiceAttributes() { return [ 'repassword', 'reset_email_', 'reset_birthdate_', 'reset_preferred_language_', 'reset_token_' ]; } public function crudSqlInterface() { return [ 'insert' => 'user_Create', 'update' => 'user_Update', 'delete' => 'user_Delete', ]; } public static function getPublicAttrNames() { return [ 'id', 'name', 'phone', 'email', 'login', 'enabled', 'roleId', 'birthDate', 'preferredLanguage' ]; } public static function getActiveStatus($model, $index, $this) { return $model->enabled == 'y' ? 'Yes' : 'No'; } public function activateUser() { $url = \Yii::$app->urlManager->createAbsoluteUrl( 'users/default/activation', array('login' => $this['login'], 'key' => $this['emailConfirmationCode']) ); $message = \Yii::t('module', 'Thank you for registering.') . '' . Yii::t('module', 'Your activation url') . '.'; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; return mail($this['email'], Yii::t('module', 'Сonfirmation email'), $message, $headers); } public static function getUserData() { Yii::$app->user->setIdentity(false); /** @var \app\models\Users $identity */ $identity = \Yii::$app->user->identity; $result = $identity->getAttributes(self::getPublicAttrNames()); if (isset($result['birthDate'])) { $result['birthDate'] = (new DateTime($result['birthDate']))->format(DateTime::W3C); } $result['isGuest'] = Yii::$app->user->isGuest; return $result; } /*public function validateUpdatePassword() { if (!empty($this->password)) { if (empty($this->repassword)) $this->addError('repassword', \Yii::t ('yii', '{attribute} cannot be blank.', array('{attribute}' => \Yii::t('api', 'Confirm Password')))); } }*/ public static function findIdentity($id) { return static::find($id); } public function getId() { return $this->id; } public function getAuthKey() { return $this->auth_key; } public function validateAuthKey($authKey) { return $this->auth_key === $authKey; } public function beforeSave($insert) { if (parent::beforeSave($insert)) { if ($this->isNewRecord) { if (!empty($this->password)) { $this->password = Security::generatePasswordHash($this->password); } if (!isset($this->enabled)) { $this->enabled = self::$activeAfterRegistration ? 'y' : 'n'; } } else { if ($this->scenario === 'activation') $this->emailConfirmationCode = Security::generateRandomKey(); if ($this->scenario === 'update' && !empty($this->password) && strlen($this->password) <= 32) { // long password - is already hashed $this->password = Security::generatePasswordHash($this->password); } } if ($this->scenario === 'update') { $this->checkAttributeForReset('reset_email_', 'email'); $this->checkAttributeForReset('reset_birthdate_', 'birthDate'); $this->checkAttributeForReset('reset_preferred_language_', 'preferredLanguage'); $this->checkAttributeForReset('reset_token_', 'token'); if(!isset($this['token'])) { $this->setAttribute('token', null); } } return true; } return false; } public function validatePassword($password) { return Security::validatePassword($password, $this->getAttribute('password')); } public static function findByLogin($login) { return static::find(array('login' => $login)); } public function getModule() { if ($this->_module === NULL){ $this->_module = \Yii::$app->getModule('users'); } return $this->_module; } public function afterSave($insert) { if ($this->scenario == 'insert') { $this->emailConfirmationCode = Security::generateRandomKey(); $confirmationData = array( 'user_id_' => $this->getPrimaryKey(), 'phone_confirmation_code_' => null, 'phone_confirmation_expiry_' => null, 'reset_phone_code_' => 'y', 'email_confirmation_code_' => $this->emailConfirmationCode, 'email_confirmation_expiry_' => null, 'reset_email_code_' => 'n' ); $this->execFunction('user_SetConfirmationCode', $confirmationData); $event = new ModelEvent; $this->trigger(self::EVENT_NEW_USER, $event); } parent::afterSave($insert); } public function getUserRoleIds() { $query = new Query(); $query->from('userRoleT')->select(array('id')); $column = $query->column(); return array_combine($column, array_map('ucfirst', $column)); } public static function checkEditable($model) { return $model->id != \Yii::$app->user->identity->getId(); } public function resetCheckCodes($type = 'full') { //default values for full type: $params = [ 'user_id_' => $this->getId(), 'phone_confirmation_code_' => null, 'phone_confirmation_expiry_' => null, 'reset_phone_code_' => 'y', 'email_confirmation_code_' => null, 'email_confirmation_expiry_' => null, 'reset_email_code_' => 'y' ]; switch ($type) { case 'phone': $params['email_confirmation_code_'] = $this->getAttribute('emailConfirmationCode'); $params['email_confirmation_expiry_'] = $this->getAttribute('emailConfirmationExpiry'); $params['reset_email_code_'] = 'n'; break; case 'email': $params['phone_confirmation_code_'] = $this->getAttribute('phoneConfirmationCode'); $params['phone_confirmation_expiry_'] = $this->getAttribute('phoneConfirmationExpiry'); $params['reset_phone_code_'] = 'n'; break; } return (int) $this->execFunction('user_SetConfirmationCode' , $this->_getResetCheckCodesParams($type)); } public function checkCode($phoneCode = null, $emailCode = null) { return $this->execFunction('user_CheckConfirmationCode' , [ 'user_id_' => \Yii::$app->user->getId(), 'phone_confirmation_code_' => (($phoneCode) ? static::getDb()->quoteValue($phoneCode) : 'NULL'), 'email_confirmation_code_' => (($emailCode) ? static::getDb()->quoteValue($emailCode) : 'NULL'), ]); } public function sendCode($userId, $type) { return ($type == 'phone') ? $this->_sendPhoneCode($userId) : $this->_sendEmailCode($userId); } protected function _sendPhoneCode($userId) { $code = uniqid(); $data = [ 'user_id_' => $userId, 'phone_confirmation_code_' => $code, 'phone_confirmation_expiry_' => $this->_getCodeExpireDate(), 'reset_phone_code_' => 'n', 'email_confirmation_code_' => $this->getAttribute('emailConfirmationCode'), 'email_confirmation_expiry_' => $this->getAttribute('emailConfirmationExpiry'), 'reset_email_code_' => 'n' ]; $this->execFunction('user_SetConfirmationCode', $data); $this->_sendPhoneCodeNotification($code); return true; } protected function _sendEmailCode($userId) { $code = $this->_generateCode(); $data = [ 'user_id_' => $userId, 'phone_confirmation_code_' => $this->getAttribute('phoneConfirmationCode'), 'phone_confirmation_expiry_' => $this->getAttribute('phoneConfirmationExpiry'), 'reset_phone_code_' => 'n', 'email_confirmation_code_' => $code, 'email_confirmation_expiry_' => $$this->_getCodeExpireDate(), 'reset_email_code_' => 'n' ]; $this->execFunction('user_SetConfirmationCode', $data); return $this->_sendEmailCodeNotification($code); } protected function _generateCode($length = 8) { $code = ''; for ($i=0; $i<$length; $i++) { $code .= rand(0, 9); } return $code; } protected function _getCodeExpireDate() { return date('Y-m-d H:i:s', time() + self::CODE_EXPIRE_DAY_COUNT * 24 * 60 * 60); } protected function _sendPhoneCodeNotification($code) { //TODO:: привязати відсилку СМС через шлюз return true; } protected function _sendEmailCodeNotification($code) { if (!$this->email) { return false; } \Yii::$app->mail->compose('user/codeSend', array('code' => $code)) ->setFrom('from@domain.com') ->setTo($this->email) ->setSubject('Email verification') ->send(); return true; } public static function findIdentityByAccessToken($token) { throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.'); } public static function setLanguage($languageId) { if (array_key_exists($languageId, LanguageHelper::$db2jsMap) && !yii::$app->user->isGuest) { $user = Users::find(yii::$app->user->identity->id); $user->scenario = 'update'; $user['preferredLanguage'] = $languageId; return $user->update(false); } return false; } }