Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace appmodels;
- use yii;
- use yiidbActiveRecord;
- use yiiwebIdentityInterface;
- class User extends ActiveRecord implements IdentityInterface
- {
- public $id;
- public $username;
- public $email;
- public $password;
- public $phone_number;
- public $authkey;
- public static function tableName(){
- return 'user';
- }
- public function rules(){
- return [
- [['id','authkey'],'safe'],
- [['username', 'password','email'], 'required'],
- [['email'], 'email'],
- [['phone_number'],'string', 'max' => 13],
- [['username'], 'string', 'max' => 16],
- ];
- }
- /**
- * @inheritdoc
- */
- public static function findIdentity($id)
- {
- return static::findOne($id);
- }
- //Не нужен
- public static function findIdentityByAccessToken($token, $type = null)
- {
- throw new NotSupportedException('"findIdentityByAccessToken" is not implemented.');
- }
- /**
- * Finds user by username
- *
- * @param string $username
- * @return static|null
- */
- public static function findByUsername($username)
- {
- return static::find()->where('username = :username',[':username' => $username])->one();
- }
- /**
- * @inheritdoc
- */
- public function getId()
- {
- return $this->id;
- }
- /**
- * @inheritdoc
- */
- public function getAuthKey()
- {
- return $this->authKey;
- }
- /**
- * @inheritdoc
- */
- public function validateAuthKey($authKey)
- {
- return $this->authKey === $authKey;
- }
- /**
- * Validates password
- *
- * @param string $password password to validate
- * @return bool if password provided is valid for current user
- */
- public function validatePassword($password)
- {
- $hash = $this->password;
- return Yii::$app->getSecurity()->validatePassword($password,$hash);
- }
- public function beforeSave($insert){
- if (parent::beforeSave($insert)){
- if($this->isNewRecord){
- $this->authkey = Yii::$app->getSecurity()->generateRandomString();
- }
- return true;
- }
- return false;
- }
- }
- <?php
- namespace appmodels;
- use Yii;
- use yiibaseModel;
- use appmodelsUser;
- class SignupForm extends Model
- {
- public $username;
- public $email;
- public $password;
- public $confirmPassword;
- public $phone_number;
- public function rules()
- {
- return [
- [['username','email', 'password','confirmPassword'], 'required'],
- ['email', 'email'],
- ['phone_number', 'string', 'max' => 13],
- ['password', 'string', 'min' => 6],
- ['confirmPassword', 'compare', 'compareAttribute' => 'password']
- ];
- }
- public function signup()
- {
- $new_user = new User();
- $new_user->username = $this->username;
- $new_user->email = $this->email;
- $new_user->phone_number = $this->phone_number;
- $new_user->password = Yii::$app->getSecurity()->generatePasswordHash($this->password);
- Yii::trace($new_user->attributes;
- Yii::trace($new_user->getDirtyAttributes());
- Yii::trace($new_user->getOldAttributes());
- $status = $new_user->save();
- Yii::trace($status);
- return $status;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement