Error: Missing TinyAuth role id field (Auth.User.role_id) in user session
dwesolowski opened this issue · 6 comments
Could you please advise. Having a cakephp 3.5 issue? Can't seem to find what I am missing.
Error: Missing TinyAuth role id field (Auth.User.role_id) in user session
Appcontroller
$this->loadComponent('TinyAuth.Auth', [
'loginAction' => [
'controller' => 'Users',
'action' => 'login',
'prefix' => false
],
'loginRedirect' => [
'controller' => 'Auctions',
'action' => 'index',
'prefix' => false
],
'logoutRedirect' => [
'controller' => 'Auctions',
'action' => 'index',
'prefix' => false
],
'flash' => [
'element' => 'autherror'
],
'authError' => 'Did you really think you are allowed to see that?',
'unauthorizedRedirect '=> false,
'authenticate' => [
'Form' => [
'fields' => [
'username' => 'username',
'password' => 'password'
],
'contain' => ['Roles'],
'scope' => ['Users.active' => 1]
]
],
'authorize' => [
'TinyAuth.Tiny' => [
'roleColumn' => 'role_id', // Name of column in user table holding role id (only used for single-role per user/BT)
'rolesTable' => 'Roles', // Name of Configure key holding all available roles OR class name of roles database table
'roleAlias ' => 'alias ', // Name of the column for the alias
'multiRole' => true, // True will enable multi-role/HABTM authorization (requires a valid join table)
'pivotTable' => 'RolesUsers',
'superAdminRole' => null, // Id of the super admin role. Users with this role will have access to ALL resources
'authorizeByPrefix' => false, // If prefixed routes should be auto-handled by their matching role name
'prefixes' => [], // A list of authorizeByPrefix handled prefixes
'allowUser' => false, // True will give authenticated users access to all resources except those using the adminPrefix
'adminPrefix' => null, // Name of the prefix used for admin pages. Defaults to admin
'autoClearCache' => true // True will generate a new ACL cache file every time
]
]
]
);
appview
public function initialize()
{
parent::initialize();
$this->loadHelper('TinyAuth.AuthUser');
$this->loadHelper('User');
$this->loadHelper('Ip');
}
userstable
public function initialize(array $config)
{
$this->table('users');
$this->displayField('username');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Search.Searchable');
$this->hasMany('Addresses', [
'foreignKey' => 'user_id'
]);
$this->belongsToMany('Roles', [
'foreignKey' => 'user_id',
'targetForeignKey' => 'role_id',
'joinTable' => 'roles_users'
]);
$this->belongsTo('Limits', [
'foreignKey' => 'limit_id',
'joinType' => 'INNER'
]);
}
rolestable
public function initialize(array $config)
{
$this->table('roles');
$this->displayField('name');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->addBehavior('Search.Searchable');
$this->belongsToMany('Users', [
'foreignKey' => 'role_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'roles_users'
]);
}
rolesuserstable
public function initialize(array $config)
{
$this->table('roles_users');
$this->displayField('role_id');
$this->primaryKey(['role_id', 'user_id']);
$this->addBehavior('Timestamp');
$this->belongsTo('Roles', [
'foreignKey' => 'role_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Users', [
'foreignKey' => 'user_id',
'joinType' => 'INNER'
]);
}
The error message is quite clear and descriptive
Missing TinyAuth role id field (Auth.User.role_id) in user session
Make sure your data in the session looks as expected/configured. What is in dd($this->request->session->read('Auth.User.')) ?
The output is below.
[
'id' => (int) 3,
'username' => 'admin',
'first_name' => 'Daren',
'last_name' => 'Wesolowski',
'email' => 'daren.wesolowski@icloud.com',
'ip' => '127.0.0.1',
'active' => (int) 1,
'limit_id' => (int) 2,
'last_login' => object(Cake\I18n\Time) {
'time' => '2018-02-10T07:08:02-05:00',
'timezone' => 'America/Indiana/Indianapolis',
'fixedNowTime' => false
},
'created' => object(Cake\I18n\Time) {
'time' => '2015-07-21T13:55:01-04:00',
'timezone' => 'America/Indiana/Indianapolis',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2018-02-10T07:08:02-05:00',
'timezone' => 'America/Indiana/Indianapolis',
'fixedNowTime' => false
},
'canceled' => false,
'deleted' => false,
'roles' => [
(int) 0 => [
'id' => (int) 1,
'name' => 'Administrator',
'alias' => 'admin',
'created' => object(Cake\I18n\Time) {
'time' => '2015-07-21T04:03:04-04:00',
'timezone' => 'America/Indiana/Indianapolis',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2015-08-16T22:42:07-04:00',
'timezone' => 'America/Indiana/Indianapolis',
'fixedNowTime' => false
},
'_joinData' => [
'role_id' => (int) 1,
'user_id' => (int) 3,
'created' => null,
'modified' => null
]
],
(int) 1 => [
'id' => (int) 2,
'name' => 'Member',
'alias' => 'member',
'created' => object(Cake\I18n\Time) {
'time' => '2015-07-21T04:03:04-04:00',
'timezone' => 'America/Indiana/Indianapolis',
'fixedNowTime' => false
},
'modified' => object(Cake\I18n\Time) {
'time' => '2015-08-16T22:42:18-04:00',
'timezone' => 'America/Indiana/Indianapolis',
'fixedNowTime' => false
},
'_joinData' => [
'role_id' => (int) 2,
'user_id' => (int) 3,
'created' => null,
'modified' => null
]
]
],
'bids' => [
(int) 1 => [
'total_bids' => (int) 7,
'current_pack' => (int) 1,
'current_pack_balance' => (int) 7
],
(int) 2 => [
'total_bids' => (int) 100,
'current_pack' => (int) 4,
'current_pack_balance' => (int) 100
]
]
]
As you can see you dont have a single role, but multi role setup.
Thus, you need to configure it accordingly as documented.
Closing as user error.
Last question, but above my user_id and role_id in roles_users looks right. What am I missing please?
Depending on how you use it https://github.com/dereuromark/cakephp-tinyauth/tree/master/docs#configuration comes into play.
Central config avoids having them configured for one part this way, and forgetting them on a different part.
I use it myself for 3.5 in both ways (single and multi role) just fine.
Thanks works, should all config options to app.php also?