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

RBAC unserialize issue #9986

Closed RSalo closed 9 years ago

RSalo commented 9 years ago

Hello everybody! Have issue with RBAC unserialize. I'm trying to check permission access but getting error "Rule not found : isAuthor". All actions i did step by step as in manual. I'll be grateful for any help! login2 login3

samdark commented 9 years ago

Do you have rule class?

RSalo commented 9 years ago

@samdark yes, i do.

namespace app\rbac;

use yii\rbac\Item;
use yii\rbac\Rule;

class AuthorRule extends Rule
{
    public $name = 'isAuthor';

    public function execute($user, $item, $params)
    {
        return isset($params['post']) ? $params['post']->createdBy == $user : false;
    }
}

But maybe i'm doing something wrong?

samdark commented 9 years ago

Doesn't seem so. Looks weird.

RSalo commented 9 years ago

I hope, this information have to help solved my problem: Data from auth_rule table:

name : isAuthor
data : O:19:"app\rbac\AuthorRule":3:{s:4:"name";s:8:"isAuthor";s:9:"createdAt";i:1444865585;s:9:"updatedAt";i:1444865585;}
created_at : 1444865585
updated_at : 1444865585

Yii 2.0.5 PHP 5.6.1 (cli) Loaded extensions:

Core
ctype
curl
date
dom
ereg
fileinfo
filter
hash
iconv
imap
intl
json
libxml
mbstring
mcrypt
mhash
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
Reflection
session
SimpleXML
SPL
sqlite3
standard
tokenizer
xdebug
xml
xmlreader
xmlwriter
zlib
SilverFire commented 9 years ago

@RSalo Could you try to change the serialized data in DB rules table to this: \app\rbac\AuthorRule (i added \ at the beggining of the class name)

RSalo commented 9 years ago

@SilverFire i added in DB "\" and right now get error login4

michaelnguyen2021 commented 9 years ago

can you post

on Gist ( https://gist.github.com/ )

RSalo commented 9 years ago

@duynguyen0511 yes of course https://gist.github.com/RSalo/1b97b52f1ccc011a63e7

michaelnguyen2021 commented 9 years ago

@RSalo

your AuthorRule class use "namespace app\rbac", but you place it under "/config/rbac/"

you can

or

The point is your namespace must match the file location or Yii2 cant auto-load your AuthorRule class

RSalo commented 9 years ago

@duynguyen0511 changed https://gist.github.com/RSalo/cf545b689f996aa33dcb and have the same error https://cloud.githubusercontent.com/assets/4490762/10672457/0ce20df0-78f9-11e5-94d5-0f17e83df409.png

michaelnguyen2021 commented 9 years ago

@RSalo

would you remove your rule and re-add them again ?

e.g. remove entry in auth_rule (the rule itself), remove entry in auth_item (permission that use the rule)

Then, create new rule, then add it to permission .

It should work.

RSalo commented 9 years ago

@duynguyen0511 no i don't it. one moment, i remove all tables and will create a new

RSalo commented 9 years ago

Probably, i found where is problem. In AuthorRule we have namespace "app\rbac\AuthorRule" that = "/srv/www/htdocs/shelter/config/rbac/AuthorRule.php", but yii in vendor have other path to my rule at the same namespace "/srv/www/htdocs/shelter/rbac/AuthorRule.php". login5

I see the only one way - hard write correct "@app" alias in bootstrap file. Or have you others ways to solved this issue?

SilverFire commented 9 years ago

You're wrong. @app alias points to /src/www/htdocs/shelter/, so app\rbac\AuthorRule namespace points to the /src/www/htdocs/shelter/rbac/AuthorRule.php

If you want to store the rule file in the config directory, you have to change file namespace to app\config\rbac

Is it clear for you?