Skip to content
This repository was archived by the owner on May 27, 2021. It is now read-only.

apis for add and remove roles #53

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions server/controllers/users.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const jwt = require('jsonwebtoken');

const User = require('../models').User;
const Role = require('../models').Role;
const UserRole = require('../models').UserRole;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't Babel configured for this project?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's Babel and what do we need it for?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Babel is a JavaScript transpiler, it converts modern JavaScript into the older versions that most browsers understand

module.exports = {
create(req, res) {
Expand Down Expand Up @@ -38,5 +40,47 @@ module.exports = {
}
})
.catch(error => res.status(400).json({ data: error, message: 'No such user found' }));
}
};
},

addUserRole(req,res){
const { value } = req.body;
const id = req.params.id;
return User
.findOne({ where: { id } })
.then(user => {
const id = user.id;
Role
.findOne({ where: { value } })
.then(role => {
UserRole.create({
UserId : id,
RoleId : role.id,
});
res.status(200).json({message:'Role \'' + value +'\' added to user' });
} )
.catch(error => res.status(400).json({ data: error, message: 'No such role found' }))
} )
.catch(error => res.status(400).json({ data: error, message: 'No such user found' }));
},

removeUserRole(req,res){
const { value } = req.body;
const id = req.params.id;
return User
.findOne({ where: { id } })
.then(user => {
Role
.findOne({ where: { value } })
.then(role => {
const id = user.id;
UserRole
.destroy({ where: { UserId:id, RoleId:role.id }
})
.then(userrole => {res.status(200).json({message:'Role \'' + value +'\' removed from user' })})
.catch(error => res.status(400).json({ data: error, message: 'No such role assigned to user' }));
} )
.catch(error => res.status(400).json({ data: error, message: 'No such role found' }))
} )
.catch(error => res.status(400).json({ data: error, message: 'No such user found' }));
},
}
27 changes: 27 additions & 0 deletions server/migrations/20190914234409-create-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('Roles', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
value: {
type: Sequelize.ENUM('admin', 'candidate')
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('Roles');
}
};
40 changes: 40 additions & 0 deletions server/migrations/20191008190353-create-user-role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable('UserRoles', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
UserId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'Users',
key: 'id'
}
},
RoleId: {
type: Sequelize.INTEGER,
onDelete: 'CASCADE',
references: {
model: 'Roles',
key: 'id'
}
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable('UserRoles');
}
};
11 changes: 11 additions & 0 deletions server/models/role.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const Role = sequelize.define('Role', {
value: DataTypes.ENUM('admin', 'candidate')
}, {});
Role.associate = function(models) {
Role.belongsToMany(models.User, {through: 'UserRole'});

};
return Role;
};
2 changes: 1 addition & 1 deletion server/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = (sequelize, DataTypes) => {
},
});
User.associate = function(models) {
// associations can be defined here
User.belongsToMany(models.Role, {through: 'UserRole'});
};
return User;
};
10 changes: 10 additions & 0 deletions server/models/userrole.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
'use strict';
module.exports = (sequelize, DataTypes) => {
const UserRole = sequelize.define('UserRole', {

}, {});
UserRole.associate = function(models) {
// associations can be defined here
};
return UserRole;
};
7 changes: 7 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,11 @@ module.exports = (app) => {
app.post('/api/users', usersController.create);
app.get('/api/users', usersController.list);
app.get('/api/user', passport.authenticate('jwt', { session: false }), usersController.get);


app.patch('/api/users/:id/roles', usersController.addUserRole);
app.delete('/api/users/:id/roles', usersController.removeUserRole);



};