robregonm / yii2-auth

Yii 2 User Authentication & Role Based Access Control (RBAC) Module
BSD 3-Clause "New" or "Revised" License
77 stars 37 forks source link

Login with username #23

Closed cansozeri closed 10 years ago

cansozeri commented 10 years ago

Functionality to login with username ... If you have a proper solution for this please provide or you can merge with this code . Thanx : )

robregonm commented 10 years ago

Looks fine, however, could you please merge the "orWhere" lines into one? That way will be avaluated as one instruction instead of separated conditions (when yii generates sql) Thanks in advance.

cansozeri commented 10 years ago

is this what you want ??

$user->orWhere(["email" => $this->username])
         ->orWhere(["username" => $this->username]);
robregonm commented 10 years ago

I mean $user->orWhere(['email' => $this->username, 'email' => $this->username]); Thanks in advance.

cansozeri commented 10 years ago

Yeah I tried it first but the yii add an 'And' operator I don't know why ?? So I can not login with username...

$user->orWhere(['email' => $this->username, 'username' => $this->username]);

Than the query is ( yii2 debug tool)

SELECT * FROM `user` WHERE (`email`='cansozeri') AND (`username`='cansozeri')

This should be to login with username or email with 'Or' not 'And'

If I use it like this

$user->orWhere(["email" => $this->username])
                 ->orWhere(["username" => $this->username]);

The query is ok with 'Or'

SELECT * FROM `user` WHERE (`email`='cansozeri') OR (`username`='cansozeri')
robregonm commented 10 years ago

Right, please, change it to: $user->where(['or', 'email' => $this->username, 'username' => $this->username]);

cansozeri commented 10 years ago

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'cansozeri' in 'where clause' The SQL being executed was: SELECT * FROM user WHERE (cansozeri) OR (cansozeri)

:))) I am lookin into it from google too it is strange but I can not add 'or' operator without errors.

robregonm commented 10 years ago

Have you "composer.phar update" your project?

cansozeri commented 10 years ago

Yes I did it ...

cansozeri commented 10 years ago

I think the right way to do is the first one I send over here as this example ..

$model = User::find()
    ->where('userid > :userid', [':userid' => $userid])
    ->orWhere('primary_user = :primary_user', [':primary_user' => $primary_user])
    ->andWhere('status = :status', [':status' => $status])
    ->all();

I think like propel this ->orWhere and ->andWhere evaluated as one instruction...

So we should use

$user->orWhere(["email" => $this->username])->orWhere(["username" => $this->username]);

or

$user->where(["email" => $this->username])->orWhere(["username" => $this->username]);

or with one code...

$this->_user = User::find()
        ->where(["email" => $this->username])
        ->orWhere(["username" => $this->username])
        ->one();
robregonm commented 10 years ago

After some tests, I realize the best alternative :) Please change models/User.php Line 94 to: return static::find()->andWhere(['and', ['or', ['username' => $username], ['email' => $username]], ['status' => static::STATUS_ACTIVE]])->one();

Then I will accept the pull request. Thanks.

cansozeri commented 10 years ago

: )))) Ok I did it but I think this is your pull request not mine : ))

robregonm commented 10 years ago

Since pull request #24 fixes this issue, I will close it.

robregonm commented 10 years ago

Thanks.