WIP Roles and permissions access control.

This commit is contained in:
Ahmed Bouhuolia
2019-09-11 21:40:09 +02:00
parent 9a8de9ca7d
commit cba17739d6
24 changed files with 847 additions and 20 deletions

View File

@@ -20,7 +20,7 @@ const Account = bookshelf.Model.extend({
balances() {
return this.hasMany('AccountBalance', 'accounnt_id');
}
},
});
export default bookshelf.model('Account', Account);

View File

@@ -12,9 +12,7 @@ const AccountBalance = bookshelf.Model.extend({
*/
hasTimestamps: false,
/**
*
*/
account() {
return this.belongsTo('Account', 'account_id');
},

View File

@@ -0,0 +1,25 @@
import bookshelf from './bookshelf';
const Permission = bookshelf.Model.extend({
/**
* Table name of Role model.
* @type {String}
*/
tableName: 'permissions',
/**
* Timestamp columns.
*/
hasTimestamps: false,
role() {
return this.belongsTo('Role', 'role_id');
},
resource() {
return this.belongsTo('Resource', 'resource_id');
},
});
export default bookshelf.model('Permission', Permission);

View File

@@ -0,0 +1,21 @@
import bookshelf from './bookshelf';
const Resource = bookshelf.Model.extend({
/**
* Table name.
*/
tableName: 'resources',
/**
* Timestamp columns.
*/
hasTimestamps: false,
permissions() {
},
roles() {
},
});
export default bookshelf.model('Resource', Resource);

View File

@@ -20,11 +20,18 @@ const Role = bookshelf.Model.extend({
return this.belongsToMany('Permission', 'role_has_permissions', 'role_id', 'permission_id');
},
/**
* Role may has many resources.
*/
resources() {
return this.belongsToMany('Resource', 'role_has_permissions', 'role_id', 'resource_id');
},
/**
* Role model may has many users.
*/
users() {
return this.belongsTo('User');
return this.belongsToMany('User', 'user_has_roles');
},
});

View File

@@ -21,6 +21,13 @@ const User = bookshelf.Model.extend({
verifyPassword(password) {
return bcrypt.compareSync(password, this.get('password'));
},
/**
* User model may has many associated roles.
*/
roles() {
return this.belongsToMany('Role', 'user_has_roles', 'user_id', 'role_id');
},
});
export default bookshelf.model('User', User);