123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267 |
- <?php
- namespace app\models;
- use Yii;
- use yii\base\NotSupportedException;
- use yii\db\ActiveRecord;
- use yii\web\IdentityInterface;
- use app\models\behaviors\TimestampBehavior;
- use app\models\queries\UserQuery;
- /**
- * User model
- *
- * @property integer $id
- * @property string $username
- * @property string $is_email_verified
- * @property string $password_hash
- * @property string $password_reset_token
- * @property string $email_confirmation_token
- * @property string $email
- * @property string $auth_key
- * @property integer $role
- * @property integer $status
- * @property string $created_at
- * @property string $updated_at
- * @property string $password write-only password
- */
- class User extends ActiveRecord implements IdentityInterface
- {
- const STATUS_DELETED = 0;
- const STATUS_ACTIVE = 10;
- const ROLE_USER = 10;
- /**
- * 管理员
- */
- const ROLE_ADMIN = 1;
- /**
- * 开发者
- */
- const ROLE_DEV = 2;
- /**
- * @var string|null the current password value from form input
- */
- protected $_password;
- /**
- * @return UserQuery custom query class with user scopes
- */
- public static function find()
- {
- return new UserQuery(get_called_class());
- }
- /**
- * @inheritdoc
- */
- public function scenarios()
- {
- return array_merge(parent::scenarios(), [
- 'signup' => ['username','email','password','role'],
- ]);
- }
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'username' => '用户名',
- 'email' => '邮箱',
- 'password' => '密码',
- 'role' => '角色',
- ];
- }
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['username','email','password','role'], 'required', 'on'=>'signup'],
- ['status', 'default', 'value' => self::STATUS_ACTIVE],
- ['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
- ['role', 'default', 'value' => self::ROLE_DEV],
- ['role', 'in', 'range' => [self::ROLE_USER, self::ROLE_DEV, self::ROLE_ADMIN]],
- ['username', 'filter', 'filter' => 'trim'],
- ['username', 'unique'],
- ['username', 'string', 'min' => 2, 'max' => 255],
- ['email', 'filter', 'filter' => 'trim'],
- ['email', 'validateEmail'],
- ['email', 'email'],
- ['email', 'unique'],
- ];
- }
- public function validateEmail($attribute, $params) {
- if (!preg_match('/.*@social-touch.com$/', $this->$attribute)) {
- $this->addError($attribute, '我猜你丫是外星人,没有st邮箱不可注册:)');
- }
- }
- /**
- * @inheritdoc
- */
- public function behaviors()
- {
- return [
- 'timestamp' => [
- 'class' => TimestampBehavior::className(),
- ],
- ];
- }
- /**
- * @inheritdoc
- */
- public function beforeSave($insert)
- {
- if ($this->isNewRecord) {
- $this->generateAuthKey();
- $this->generateEmailConfirmationToken();
- }
- $this->realname = $this->username;
- $this->username = $this->email;
- return parent::beforeSave($insert);
- }
- /**
- * @inheritdoc
- */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- /**
- * @inheritdoc
- */
- public static function findIdentityByAccessToken($token, $type = null)
- {
- throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
- }
- /**
- * @inheritdoc
- */
- public function getId()
- {
- return $this->getPrimaryKey();
- }
- /**
- * @inheritdoc
- */
- public function getName()
- {
- return $this->realname;
- }
- /**
- * @inheritdoc
- */
- public function getAuthKey()
- {
- return $this->auth_key;
- }
- /**
- * @inheritdoc
- */
- public function validateAuthKey($authKey)
- {
- return $this->getAuthKey() === $authKey;
- }
- /**
- * Validates password
- *
- * @param string $password password to validate
- * @return boolean if password provided is valid for current user
- */
- public function validatePassword($password)
- {
- return Yii::$app->security->validatePassword($password, $this->password_hash);
- }
- /**
- * Generates password hash from password and sets it to the model
- *
- * @param string $password
- */
- public function setPassword($password)
- {
- $this->_password = $password;
- if (!empty($password)) {
- $this->password_hash = Yii::$app->security->generatePasswordHash($password);
- }
- }
- /**
- * @return string|null the current password value, if set from form. Null otherwise.
- */
- public function getPassword()
- {
- return $this->_password;
- }
- /**
- * Generates "remember me" authentication key
- */
- public function generateAuthKey()
- {
- $this->auth_key = Yii::$app->security->generateRandomString();
- }
- /**
- * Generates new email confirmation token
- */
- public function generateEmailConfirmationToken()
- {
- $this->email_confirmation_token = Yii::$app->security->generateRandomString() . '_' . time();
- }
- /**
- * Generates new password reset token
- */
- public function generatePasswordResetToken()
- {
- $this->password_reset_token = Yii::$app->security->generateRandomString() . '_' . time();
- }
- /**
- * Removes password reset token
- * @param bool $save whether to save the record. Default is `false`.
- * @return bool|null whether the save was successful or null if $save was false.
- */
- public function removePasswordResetToken($save = false)
- {
- $this->password_reset_token = null;
- if ($save) {
- return $this->save();
- }
- }
- /**
- * Removes email confirmation token and sets is_email_verified to true
- * @param bool $save whether to save the record. Default is `false`.
- * @return bool|null whether the save was successful or null if $save was false.
- */
- public function removeEmailConfirmationToken($save = false)
- {
- $this->email_confirmation_token = null;
- $this->is_email_verified = 1;
- if ($save) {
- return $this->save();
- }
- }
- }
|