guild/frontend/models/SignupForm.php

72 lines
1.8 KiB
PHP
Raw Normal View History

2018-10-11 11:15:09 +03:00
<?php
namespace frontend\models;
2020-01-29 11:26:01 +03:00
use common\classes\Debug;
2024-02-05 23:58:48 +03:00
use common\models\Status;
2020-01-29 11:26:01 +03:00
use Yii;
2018-10-11 11:15:09 +03:00
use yii\base\Model;
use common\models\User;
/**
* Signup form
*/
class SignupForm extends Model
{
public $username;
public $email;
public $password;
2024-02-05 23:58:48 +03:00
public $is_partner = 0;
2018-10-11 11:15:09 +03:00
/**
* {@inheritdoc}
*/
public function rules()
{
return [
['username', 'trim'],
['username', 'required'],
['username', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This username has already been taken.'],
['username', 'string', 'min' => 2, 'max' => 255],
['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\User', 'message' => 'This email address has already been taken.'],
['password', 'required'],
['password', 'string', 'min' => 6],
2024-02-05 23:58:48 +03:00
['is_partner', 'integer']
2018-10-11 11:15:09 +03:00
];
}
/**
* Signs user up.
*
* @return User|null the saved model or null if saving fails
*/
public function signup()
{
if (!$this->validate()) {
return null;
}
$user = new User();
$user->username = $this->username;
$user->email = $this->email;
$user->setPassword($this->password);
$user->generateAuthKey();
2020-01-29 11:26:01 +03:00
$user->save();
$auth = Yii::$app->authManager;
$authorRole = $auth->getRole('user');
$auth->assign($authorRole, $user->id);
2023-10-18 10:48:38 +03:00
2024-02-05 23:58:48 +03:00
User::createSimpleProfile($user->email, $this->is_partner ? Status::getByName("Партнер") : Status::getByName("Аутстафинг"));
2023-10-18 13:12:02 +03:00
return $user->save() ? $user : null;
2018-10-11 11:15:09 +03:00
}
}