Commit bb145f50 by Alexander Makarov

Fixes #3179: removed duplicate validation rules from User model in advanced app,…

Fixes #3179: removed duplicate validation rules from User model in advanced app, removed User::create()
parent 9b69e2d1
......@@ -29,26 +29,6 @@ class User extends ActiveRecord implements IdentityInterface
const ROLE_USER = 10;
/**
* Creates a new user
*
* @param array $attributes the attributes given by field => value
* @return static|null the newly created model, or null on failure
*/
public static function create($attributes)
{
/** @var User $user */
$user = new static();
$user->setAttributes($attributes);
$user->setPassword($attributes['password']);
$user->generateAuthKey();
if ($user->save()) {
return $user;
} else {
return null;
}
}
/**
* @inheritdoc
*/
public function behaviors()
......@@ -181,28 +161,4 @@ class User extends ActiveRecord implements IdentityInterface
{
$this->password_reset_token = null;
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
['role', 'default', 'value' => self::ROLE_USER],
['role', 'in', 'range' => [self::ROLE_USER]],
['username', 'filter', 'filter' => 'trim'],
['username', 'required'],
['username', 'unique'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'filter', 'filter' => 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'unique'],
];
}
}
......@@ -120,8 +120,7 @@ class SiteController extends Controller
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
$user = $model->signup();
if ($user) {
if ($user = $model->signup()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
......
......@@ -43,7 +43,13 @@ class SignupForm extends Model
public function signup()
{
if ($this->validate()) {
return User::create($this->attributes);
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
$user->save(false);
return $user;
}
return null;
......
......@@ -268,18 +268,19 @@ After executing the command we'll get the following hierarchy:
Author can create post, admin can update post and do everything author can.
If your application allows user signup you need to assign roles to these new users once. For example, in order for all
signed up users to become authors you in advanced application template you need to modify `common\models\User::create()`
signed up users to become authors you in advanced application template you need to modify `frontend\models\SignupForm::signup()`
as follows:
```php
public static function create($attributes)
public function signup()
{
/** @var User $user */
$user = new static();
$user->setAttributes($attributes);
$user->setPassword($attributes['password']);
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
if ($user->save()) {
$user->save(false);
// the following three lines were added:
$auth = Yii::$app->authManager;
......@@ -287,9 +288,9 @@ public static function create($attributes)
$auth->assign($authorRole, $user->getId());
return $user;
} else {
return null;
}
return null;
}
```
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment