amnah / yii2-user

Yii2 user authentication module
MIT License
253 stars 104 forks source link

Getting unknown property: amnah\yii2\user\models\Role::can_? #195

Closed atiberius closed 7 years ago

atiberius commented 7 years ago

I am receiving this when using a controller that allows certain actions to be accessed by guest users, like this:

public function behaviors() {
    return [
      'access' => [
        'class' => AccessControl::className(),
        'rules' => [
          [
            'actions' => ['action1', 'action2', ...],
            'allow'   => TRUE,
            'roles'   => ['?'],
          ],
        ....
        ]
      ]
    ];
}

I suspect this happens because yiisoft/yii2/filters/AccessRule.php changed the way it verifies permissions. In function 'matchRole', if current user is not guest, it forwards the permission check to its Role:

...
if (!isset($roleParams)) {
                $roleParams = $this->roleParams instanceof Closure ? call_user_func($this->roleParams, $this) : $this->roleParams;
            }
...

and it executes Role::checkPermission('?'), which throws the Exception:

Exception (Unknown Property) 'yii\base\UnknownPropertyException' with message 'Getting unknown property: amnah\yii2\user\models\Role::can_?'

amnah commented 7 years ago

It seems to be working fine for me.

The code you posted

if (!isset($roleParams)) {
    $roleParams = $this->roleParams instanceof Closure ? call_user_func($this->roleParams, $this) : $this->roleParams;
}

is checking for $this->roleParams. By default that value is public $roleParams = []; so it's not calling any function there.

I think you left something out ... Can you post your entire public function behaviors() { code

atiberius commented 7 years ago

This is my full behaviors() function:

public function behaviors() {
    return [
      'access' => [
        'class' => AccessControl::className(),
        'only'  => ['action1', 'action2', 'action3', 'action4'],
        'rules' => [
          [
            'actions' => ['action1', 'action2'],
            'allow'   => TRUE,
            'roles'   => ['?'],
          ],
          [
            'actions' => ['action3', 'action4'],
            'allow'   => TRUE,
            'roles'   => ['@'],
          ],
        ],
      ],
    ];
  }
amnah commented 7 years ago

I see what happened, released a new 5.0.8 version for it (though you may need to wait a bit for composer to pick it up)

Thanks for the heads up

atiberius commented 7 years ago

Thank you!