Skip to content

Commit

Permalink
Remove login from user logic, use email
Browse files Browse the repository at this point in the history
  • Loading branch information
zenn1989 committed Jan 13, 2019
1 parent c2af769 commit a0e4e37
Show file tree
Hide file tree
Showing 28 changed files with 52 additions and 115 deletions.
19 changes: 1 addition & 18 deletions Apps/ActiveRecord/User.php
Expand Up @@ -13,7 +13,6 @@
* Class User. Active record model for user auth data
* @package Apps\ActiveRecord
* @property int $id
* @property string $login
* @property string $email
* @property string $password
* @property int $role_id
Expand All @@ -32,17 +31,15 @@ class User extends ActiveModel implements iUser

protected $casts = [
'id' => 'integer',
'login' => 'string',
'email' => 'string',
'role_id' => 'integer',
'approve_token' => 'string'
];

protected $searchable = [
'columns' => [
'login' => 2,
'email' => 3,
'nick' => 1
'nick' => 2
],
'joins' => [
'profiles' => ['users.id', 'profiles.user_id']
Expand Down Expand Up @@ -164,20 +161,6 @@ public static function isMailExist(?string $email = null): bool
return self::where('email', $email)->count() > 0;
}

/**
* Check if user with $login is exist
* @param string $login
* @return bool
*/
public static function isLoginExist(?string $login = null): bool
{
if (!Any::isStr($login) || Any::isEmpty($login) || Str::length($login) < 2) {
return false;
}

return self::where('login', $login)->count() > 0;
}

/**
* Get user person like a object via email
* @param string|null $email
Expand Down
9 changes: 1 addition & 8 deletions Apps/Console/MainAdduserCommand.php
Expand Up @@ -29,7 +29,6 @@ public function configure()
{
$this->setName('main:adduser')
->setDescription('Add new user into database')
->addOption('login', 'login', InputOption::VALUE_OPTIONAL, 'Set user login. Should be unique!')
->addOption('email', 'email', InputOption::VALUE_OPTIONAL, 'Set user email. Should be unique!')
->addOption('password', 'password', InputOption::VALUE_OPTIONAL, 'Set user password')
->addOption('role', 'role', InputOption::VALUE_OPTIONAL, 'Define user role_id. Should be integer (see prefix_roles table). By default: 1=guest, 2=user, 3=moder, 4=admin');
Expand All @@ -44,11 +43,6 @@ public function configure()
*/
public function execute(InputInterface $input, OutputInterface $output)
{
// get login and check validity
$login = $this->optionOrAsk('login', 'User login');
if (Str::length($login) < 2) {
throw new \Exception('Login is too short');
}
// get email and check validity
$email = $this->optionOrAsk('email', 'User email');
if (!Str::isEmail($email)) {
Expand All @@ -64,14 +58,13 @@ public function execute(InputInterface $input, OutputInterface $output)
}

// check if user is always exists
if (User::isLoginExist($login) || User::isMailExist($email)) {
if (User::isMailExist($email)) {
$output->writeln('User is always exists');
return;
}

// create new user instance in prefix_users table
$user = new User();
$user->login = $login;
$user->email = $email;
$user->password = Crypt::passwordHash($password);
$user->role_id = $roleId;
Expand Down
5 changes: 1 addition & 4 deletions Apps/Controller/Admin/User/ActionIndex.php
Expand Up @@ -30,10 +30,7 @@ public function index(): ?string
// check if search query passed
$query = $this->request->query->get('search', null);
if ($query && Any::isStr($query) && Str::length($query) > 1) {
$record = $record->where(function ($db) use ($query) {
$db->where('login', 'like', '%' . $query . '%')
->orWhere('email', 'like', '%' . $query . '%');
});
$record = $record->where('email', 'like', '%' . $query . '%');
}

// set current page num and offset
Expand Down
5 changes: 2 additions & 3 deletions Apps/Controller/Admin/User/Boot.php
Expand Up @@ -32,9 +32,8 @@ public static function boot(): void
/** @var User[]|Collection $records */
$records->each(function($item) use ($model) {
/** @var User $item */
$title = $item->login . '(' . $item->email . ')';
$text = App::$Translate->get('User', 'Login: %login%, email: %email%, nick: %nick%', [
'login' => $item->login,
$title = $item->email . '(id=' . $item->id . ')';
$text = App::$Translate->get('User', 'Email: %email%, nick: %nick%', [
'email' => $item->email,
'nick' => $item->profile->nick ?? 'id' . $item->id
]);
Expand Down
2 changes: 1 addition & 1 deletion Apps/Controller/Front/User/ActionSignup.php
Expand Up @@ -93,7 +93,7 @@ public function signup(): ?string
App::$Event->run(static::EVENT_USER_REGISTER_FAIL, [
'model' => $registerForm
]);
App::$Session->getFlashBag()->add('error', __('Login or email is always used on website'));
App::$Session->getFlashBag()->add('error', __('Email is always used on website'));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Apps/Controller/Front/User/ActionSocialAuth.php
Expand Up @@ -73,7 +73,7 @@ public function socialauth(string $provider)
$loginModel->openSession($model->_userObject);
$this->response->redirect('/'); // session is opened, refresh page
} else { // something gonna wrong, lets notify user
App::$Session->getFlashBag()->add('error', __('Login or email is always used on website'));
App::$Session->getFlashBag()->add('error', __('Email is always used on website'));
}
}

Expand Down
1 change: 0 additions & 1 deletion Apps/Model/Admin/User/FormUserDelete.php
Expand Up @@ -53,7 +53,6 @@ public function labels(): array
{
return [
'email' => __('Email'),
'login' => __('Login'),
'delete' => __('Delete user content')
];
}
Expand Down
27 changes: 3 additions & 24 deletions Apps/Model/Admin/User/FormUserUpdate.php
Expand Up @@ -18,7 +18,6 @@
class FormUserUpdate extends Model
{
public $email;
public $login;
public $password;
public $newpassword;
public $role_id;
Expand Down Expand Up @@ -63,7 +62,6 @@ public function labels(): array
{
return [
'email' => __('Email'),
'login' => __('Login'),
'newpassword' => __('New password'),
'role_id' => __('Role'),
'approved' => __('Approved')
Expand All @@ -77,12 +75,10 @@ public function labels(): array
public function rules(): array
{
return [
[['email', 'login', 'role_id', 'approved'], 'required'],
[['email', 'role_id', 'approved'], 'required'],
['newpassword', 'used'],
['email', 'email'],
['login', 'length_min', 3],
['email', 'Apps\Model\Admin\User\FormUserUpdate::isUniqueEmail', $this->_user->getParam('id')],
['login', 'Apps\Model\Admin\User\FormUserUpdate::isUniqueLogin', $this->_user->getParam('id')]
];
}

Expand Down Expand Up @@ -137,26 +133,9 @@ public function save()
* @param int|null $userId
* @return bool
*/
public static function isUniqueEmail($email, $userId = null)
public static function isUniqueEmail($email, $userId = null): bool
{
$find = User::where('email', '=', $email);

if ($userId && Any::isInt($userId)) {
$find->where('id', '!=', $userId);
}

return $find->count() === 0;
}

/**
* Check if new login is always exist
* @param string $login
* @param int|null $userId
* @return bool
*/
public static function isUniqueLogin($login, $userId = null)
{
$find = User::where('login', '=', $login);
$find = User::where('email', $email);

if ($userId && Any::isInt($userId)) {
$find->where('id', '!=', $userId);
Expand Down
3 changes: 1 addition & 2 deletions Apps/Model/Front/Profile/FormSettings.php
Expand Up @@ -97,8 +97,7 @@ public function rules(): array
['sex', 'in', [0, 1, 2]],
['hobby', 'length_max', '50'],
['phone', 'phone'],
['url', 'url'],
['nick', 'notequal', $this->_user->login]
['url', 'url']
];

// custom profile fields
Expand Down
17 changes: 8 additions & 9 deletions Apps/Model/Front/User/FormLogin.php
Expand Up @@ -15,7 +15,7 @@
*/
class FormLogin extends Model
{
public $login;
public $email;
public $password;
public $captcha;

Expand All @@ -39,9 +39,10 @@ public function __construct($captcha = false)
public function rules(): array
{
$rules = [
[['login', 'password'], 'required'],
['login', 'length_min', '2'],
[['email', 'password'], 'required'],
['email', 'length_min', '2'],
['password', 'length_min', '3'],
['email', 'email'],
['captcha', 'used']
];
if ($this->_captcha) {
Expand All @@ -57,7 +58,7 @@ public function rules(): array
public function labels(): array
{
return [
'login' => __('Login or email'),
'email' => __('Email'),
'password' => __('Password'),
'captcha' => __('Captcha')
];
Expand All @@ -70,12 +71,10 @@ public function labels(): array
public function tryAuth(): bool
{
/** @var User $user */
$user = App::$User->where(function ($q) {
$q->where('login', $this->login)
->orWhere('email', $this->login);
})->first();
$user = User::where('email', $this->email)
->first();

// login found, check if approved and compare password
// row found, check if approved and compare password
if ($user && !$user->approve_token) {
// check if legacy password hash used (ffcms 3.0 or early)
if (Crypt::isOldPasswordHash($user->password) && App::$Security->password_hash($this->password) === $user->password) {
Expand Down
5 changes: 3 additions & 2 deletions Apps/Model/Front/User/FormPasswordChange.php
Expand Up @@ -12,7 +12,6 @@
*/
class FormPasswordChange extends Model
{
public $login;
public $password;
public $repassword;
public $captcha;
Expand All @@ -27,7 +26,6 @@ class FormPasswordChange extends Model
public function __construct(iUser $user)
{
$this->_user = $user;
$this->login = $user->getParam('login');
parent::__construct(true);
}

Expand Down Expand Up @@ -58,6 +56,9 @@ public function labels(): array
];
}

/**
* Save on submit
*/
public function make()
{
$this->_user->password = Crypt::passwordHash($this->password);
Expand Down
2 changes: 1 addition & 1 deletion Apps/Model/Front/User/FormRecovery.php
Expand Up @@ -63,6 +63,7 @@ public function make()
throw new SyntaxException('You must approve your account');
}

/** @var UserRecovery $rows */
$rows = UserRecovery::where('user_id', '=', $user->getId())
->orderBy('id', 'DESC')
->first();
Expand Down Expand Up @@ -93,7 +94,6 @@ public function make()
if (App::$Mailer) {
// send recovery email
App::$Mailer->tpl('user/_mail/recovery', [
'login' => $user->login,
'email' => $this->email,
'token' => $token,
'id' => $rObject->id
Expand Down
16 changes: 5 additions & 11 deletions Apps/Model/Front/User/FormRegister.php
Expand Up @@ -15,7 +15,6 @@
class FormRegister extends Model
{
public $email;
public $login;
public $password;
public $repassword;
public $captcha;
Expand Down Expand Up @@ -44,15 +43,14 @@ public function __construct($captcha = false)
public function rules(): array
{
$rules = [
[['login', 'password', 'repassword', 'email'], 'required'],
['login', 'length_min', '2'],
[['password', 'repassword', 'email'], 'required'],
['password', 'length_min', '3'],
['email', 'email'],
['repassword', 'equal', $this->getRequest('password', $this->getSubmitMethod())],
['captcha', 'used']
];

if (true === $this->_captcha) {
if ($this->_captcha) {
$rules[] = ['captcha', 'App::$Captcha::validate'];
}

Expand All @@ -66,7 +64,6 @@ public function rules(): array
public function labels(): array
{
return [
'login' => __('Login'),
'password' => __('Password'),
'repassword' => __('Repeat password'),
'email' => __('Email'),
Expand All @@ -79,18 +76,16 @@ public function labels(): array
* @param bool $activation
* @return bool
*/
public function tryRegister($activation = false)
public function tryRegister($activation = false): bool
{
$check = App::$User->where('login', '=', $this->login)
->orWhere('email', '=', $this->email)
$check = App::$User->where('email', $this->email)
->count();
if ($check !== 0) {
return false;
}

// create row
$user = new User();
$user->login = $this->login;
$user->email = $this->email;
$user->password = Crypt::passwordHash($this->password);
// if need to be approved - make random token and send email
Expand All @@ -100,8 +95,7 @@ public function tryRegister($activation = false)
if (App::$Mailer) {
App::$Mailer->tpl('user/_mail/approve', [
'token' => $user->approve_token,
'email' => $user->email,
'login' => $user->login
'email' => $user->email
])->send($this->email, (new \Swift_Message(App::$Translate->get('Default', 'Registration approve', []))));
}
}
Expand Down
6 changes: 2 additions & 4 deletions Apps/Model/Install/Main/FormInstall.php
Expand Up @@ -61,7 +61,6 @@ public function labels(): array
'mail.password' => __('Password'),
'singleLanguage' => __('Default language'),
'multiLanguage' => __('Multi language'),
'user.login' => __('Login'),
'user.email' => __('Email'),
'user.password' => __('Password'),
'user.repassword' => __('Repeat password'),
Expand All @@ -77,15 +76,15 @@ public function rules(): array
{
return [
[['db.driver', 'db.host', 'db.username', 'db.password', 'db.database', 'db.prefix', 'singleLanguage', 'mainpage'], 'required'],
[['user.login', 'user.email', 'user.password', 'user.repassword'], 'required'],
[['user.email', 'user.password', 'user.repassword'], 'required'],
['mail.enable', 'required'],
['mail.enable', 'int'],
[['mail.host', 'mail.port', 'mail.user', 'main.encrypt', 'mail.password'], 'used'],
['mail.user', 'email'],
['mail.port', 'int'],
['mail.encrypt', 'in', ['ssl', 'tls', 'none']],
['mainpage', 'in', ['none', 'news', 'about']],
[['user.login', 'user.password'], 'length_min', 4],
['user.password', 'length_min', 4],
['user.repassword', 'equal', $this->getRequest('user.password', $this->getSubmitMethod())],
['user.email', 'email'],
['multiLanguage', 'used'],
Expand Down Expand Up @@ -127,7 +126,6 @@ public function make()
// insert admin user
$user = new User();
$user->setConnection('install');
$user->login = $this->user['login'];
$user->email = $this->user['email'];
$user->role_id = 4;
$user->password = Crypt::passwordHash($this->user['password']);
Expand Down

0 comments on commit a0e4e37

Please sign in to comment.