Skip to content

Commit

Permalink
feat(general): added permissions model
Browse files Browse the repository at this point in the history
  • Loading branch information
serge1peshcoff committed Feb 3, 2020
1 parent 0630881 commit 7825467
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 1 deletion.
44 changes: 44 additions & 0 deletions migrations/20200203160355-create-permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('permissions', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
scope: {
type: Sequelize.STRING,
allowNull: false
},
action: {
type: Sequelize.STRING,
allowNull: false
},
object: {
type: Sequelize.STRING,
allowNull: false
},
combined: {
type: Sequelize.STRING,
allowNull: false
},
description: {
type: Sequelize.TEXT,
allowNull: false
},
filters: {
type: Sequelize.ARRAY(Sequelize.STRING),
allowNull: false,
defaultValue: []
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => queryInterface.dropTable('permissions')
};
35 changes: 35 additions & 0 deletions migrations/20200203171809-create-circle-permissions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = {
up: (queryInterface, Sequelize) => queryInterface.createTable('circle_permissions', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
circle_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'circles',
key: 'id'
}
},
permission_id: {
type: Sequelize.INTEGER,
allowNull: false,
references: {
model: 'permissions',
key: 'id'
}
},
created_at: {
allowNull: false,
type: Sequelize.DATE
},
updated_at: {
allowNull: false,
type: Sequelize.DATE
}
}),
down: (queryInterface) => queryInterface.dropTable('circle_permissions')
};
10 changes: 10 additions & 0 deletions models/CirclePermission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const { sequelize } = require('../lib/sequelize');

const CirclePermission = sequelize.define('circle_permission', {}, {
underscored: true,
tableName: 'circle_permissions',
createdAt: 'created_at',
updatedAt: 'updated_at',
});

module.exports = CirclePermission;
63 changes: 63 additions & 0 deletions models/Permission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const { Sequelize, sequelize } = require('../lib/sequelize');

const Permission = sequelize.define('permission', {
scope: {
type: Sequelize.ENUM('global', 'global'),
allowNull: false,
defaultValue: '',
validate: {
isIn: {
args: [['global', 'local']],
msg: 'Permission scope should be one of these: "global", "local".'
}
}
},
action: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Action should be set.' },
},
},
object: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Object should be set.' },
},
},
combined: {
type: Sequelize.STRING,
allowNull: true
},
description: {
type: Sequelize.STRING,
allowNull: false,
defaultValue: '',
validate: {
notEmpty: { msg: 'Description should be set.' },
},
},
filters: {
type: Sequelize.ARRAY(Sequelize.STRING),
allowNull: false,
defaultValue: []
}
}, {
underscored: true,
tableName: 'permissions',
createdAt: 'created_at',
updatedAt: 'updated_at',
});

Permission.afterValidate((permission) => {
permission.combined = [
permission.scope,
permission.action,
permission.object
].join(':');
});

module.exports = Permission;
8 changes: 7 additions & 1 deletion models/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const AccessToken = require('./AccessToken');
const RefreshToken = require('./RefreshToken');
const Body = require('./Body');
const Circle = require('./Circle');
const Permission = require('./Permission');
const CirclePermission = require('./CirclePermission');

Campaign.hasMany(User, { foreignKey: 'campaign_id' });
User.belongsTo(Campaign, { foreignKey: 'campaign_id' });
Expand All @@ -24,12 +26,16 @@ Circle.belongsTo(Body, { foreignKey: 'body_id' });
Circle.hasMany(Circle, { foreignKey: 'parent_circle_id', as: 'child_circles' });
Circle.belongsTo(Circle, { foreignKey: 'parent_circle_id', as: 'parent_circle' });

Circle.belongsToMany(Permission, { through: CirclePermission, foreignKey: 'circle_id', as: 'permissions' });
Permission.belongsToMany(Circle, { through: CirclePermission, foreignKey: 'permission_id', as: 'circles' });

module.exports = {
User,
Campaign,
MailConfirmation,
AccessToken,
RefreshToken,
Body,
Circle
Circle,
Permission
};

0 comments on commit 7825467

Please sign in to comment.