<?
namespace app\models;
use Yii;
use yii\base\Model;
class RegForm extends Model
{
public $login;
public $email;
public $password;
public $password_repeat;
public $wmr;
public $skype;
public $status;
public $created_at;
public $updated_at;
public function rules(){
return[
[['login', 'email', 'password', 'wmr', 'skype'], 'filter','filter' => 'trim'],
['login','unique','targetClass'=>'app\models\User', 'message' => 'Этот логин занят'],
['password','string','min'=>6,'max'=>12],
['password_repeat','compare','compareAttribute'=>'password'],
[['email','login','password','wmr','skype'], 'required', 'message' => 'Введите данные'],
['email','unique','targetClass'=>'app\models\User', 'message' => 'Этот емайл занят'],
['wmr','string','min'=>10,'max'=>12],
['wmr','unique','targetClass'=>'app\models\User'],
['status', 'default', 'value'=>USER::STATUS_ACTIVE, 'on'=>'default'],
['status','in','range'=>
[
USER::STATUS_NOT_ACTIVE,
USER::STATUS_ACTIVE]],
];
}
public function attributeLabels()
{
return[
'login' => 'Введите Логин',
'password' => 'Пароль',
'password_repeat' => 'Повторите пароль',
'email' => 'Введите Емайл',
'wmr' => 'Введите WMR кошелек',
'skype' => 'Введите свой скайп',
];
}
public function reg()
{
$user = new User;
$user->login = $this->login;
$user->email = $this->email;
$user->wmr = $this->wmr;
$user->skype = $this->skype;
$user->status = $this->status;
$user->setPassword($this->password);
$user->generateAuthKey();
return $user->save();
}
}
User.php
<?php
namespace app\models;
use Yii;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
use yii\behaviors\TimestampBehavior;
class User extends ActiveRecord implements IdentityInterface
{
const STATUS_DELETED = 0;
const STATUS_NOT_ACTIVE = 1;
const STATUS_ACTIVE = 10;
public static function tableName()
{
return 'user';
}
public function behaviors()
{
return
[
TimestampBehavior::className()
];
}
public static function findByUsername($login)
{
return static::findOne([
'login' => $login
]);
}
public function setPassword($password)
{
$this->password = Yii::$app->getSecurity()->generatePasswordHash($password);
}
public function generateAuthKey(){
$this->auth_key = Yii::$app->security->generateRandomString();
}
public function validatePassword($password)
{
return Yii::$app->security->validatePassword($password, $this->password);
}
/*Аутентификация пользователя*/
public static function findIdentity($id)
{
return static::findOne([
'id' => $id,
'status' => self::STATUS_ACTIVE
]
);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->auth_key;
}
public function validateAuthKey($authKey)
{
return $this->auth_key === $authKey;
}
}
SiteController
public function actionReg()
{
$model = new RegForm();
if ($model->load(Yii::$app->request->post()) && $model->validate()):
if ($model->reg()):
return $this->goHome();
endif;
endif;
return $this->render(
'reg',
['model' => $model]
);
}
Вопрос задан
более трёх лет назад
281 просмотр