yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

Compatibility with mongodb #1633

Closed indicalabs closed 10 years ago

indicalabs commented 10 years ago

I have started working with mongodb extension and able to work smoothly. But the default functionality like maintaining users , login, logout... needs changes in out of the box code. Is it possible to smoothly work with mongodb with defaults users functionality without many changes? Do we really need lot many changes to work with mongodb extension? Can we expect gii works with mongodb in future?

Thanks in advance

samdark commented 10 years ago

Which changes do you mean exactly?

indicalabs commented 10 years ago

Mongodb extension is expecting

  1. To have attributes() method in the model - which is okay.
  2. When we sign up, "status" field is not set with default value - So Not able to login
  3. There was an error sending email. - to reset password (because of empty/no STATUS field)
  4. Logout menu not enabled though I set status and succeed in Login.(Yii::$app->user->isGuest OR
    Yii::$app->user->identity->username was not set)
  5. $identity has not set, I think this is because mongodb will have "_id" but not "id" , If we add id, we also need to incorporate autoincrement of this "id" field.

I think if all this works by default, It would be great because out of the box "user" module is attractive and accelerate the development. I am not very good to fix the issue because I am a Java programmer and fall in love with Yii and learned PHP recently.

samdark commented 10 years ago

@klimov-paul can you look into it?

indicalabs commented 10 years ago

Paul, I think , there should be a way to deal with _id of mongodb, because having our own "id" returning as wrong id from ensureMongoId .

klimov-paul commented 10 years ago

When we sign up, "status" field is not set with default value

How do you attempt to setup default value for the “status”? Please provide the code example.

indicalabs commented 10 years ago

For now I am able to resolve status field issue, but user module is not setting identity , because yii expects "id" where mongodb having "_id".

I am just expecting out of the box user module to work with mongodb by changing ActiveRecord

klimov-paul commented 10 years ago

What is this “user module” you are mentioned?

indicalabs commented 10 years ago

Sorry I call user module which Yii framework has out of the box functionality for "signup", "login" ,"logout" and "rese-password".

klimov-paul commented 10 years ago

In that case it should be enough to change method “User::getId()” to be following:

public function getId()
{
    return $this->getPrimaryKey();
}
indicalabs commented 10 years ago

Thank Paul. That is working for me now. Currently "status" and "role" values are setting by the schema in the "user" table, that makes the new user Active by default. If we make change to the code to set these two fields with default values as part of core Yii framework, yii2-mongo will work for everyone without touching framework code.

samdark commented 10 years ago

Is it about advanced app template?

indicalabs commented 10 years ago

Yes

klimov-paul commented 10 years ago

@samdark, perhaps it is better you'll handle this.

samdark commented 10 years ago

OK. Will do when I'll be back home (mid-January).

indicalabs commented 10 years ago

samdark, Just to be clear here,

  1. status and role to be set through code with default values
  2. and these two should return in identity which will be used in PhpManager.php as in the below $this->assign(Yii::$app->user->identity->id, Yii::$app->user->identity->role); So that this Active user can be validated in custom rbac.php as soon as he Signed up
samdark commented 10 years ago

@Venu85 please check if it's better now. And thanks for your report.

indicalabs commented 10 years ago

@samdark Login is successful now, the mongodb database has the below record after signup

{
    "_id" : ObjectId("52c158e721182fb801d63af6"),
    "username" : "22",
    "email" : "xxxx@yahoo.com",
    "create_time" : 1388402919,
    "update_time" : 1388402919,
    "password_hash" : "$2y$13$0KFR9.oDPVU.t.t21IoZkOA1l/97HAkkCKpciLSEJVpYE1Tr22PdO",
    "auth_key" : "3IJfPVjzM404Heyv8-JTJ0Zy26FGOckm"
}

But the problem is, Since there is no status and role set,

  1. User is not able to login after first logout, (because application is checking "status" - In case of mysql, status is setting with default value by database)
  2. I have configured roles to authorize few actions, which is failed with below error (because it is expecting Yii::$app->user->identity->role to be set
Invalid Parameter – yii\base\InvalidParamException
Unknown authorization item ''.
at /Sites/advanced/vendor/yiisoft/yii2/yii/rbac/PhpManager.php
*/
    public function assign($userId, $itemName, $bizRule = null, $data = null)
    {
        if (!isset($this->_items[$itemName])) {
            throw new InvalidParamException("Unknown authorization item '$itemName'.");
samdark commented 10 years ago

Forgot to update scenarios. Should be OK now.