I like the names for the roles - GOD, SUPER HERO and MAN , which are easy to understand.
Since you are using the MEAN stack, and most of the route checking happens on node , I would rather just keep the role table.
Roles
{ _id : 1, name : GOD, golbalPerms : true }, { _id : 2, name : SUPER HERO, privatePerms : true }, { _id : 3, name : MAN }
Users:
{ _id : 111, name : Jesus, roleId : 1 }, { _id : 222, name : BatMan, roleId : 2 }, { _id : 333, name : Jack, roleId : 3 }
When a user logs in and sends the user object back to the client, be sure to replace roleId with the corresponding role object from the database.
Switching to Node JS code:
Fully understanding your usecase, we can divide them into the following methods -
Createuser
CreateTodo
DeleteTodo
ReadTodo
- UpdateTodo
CommentTodo
Assigntodo
Step by step, CreateUser .
Route code snippet:
app.all('/users', users.requiresLogin); // Users Routes app.route('/users') .post(users.hasPerms('globalPerms'), users.create);
In your controller, you can check based on the input globalPerms if the verified ones allow you to create a user by calling next() else return with the corresponding error message.
Now CreateTodo && & DeleteTodo :
Both of them pretty much work with the same logic with a little trick.
Route code snippet:
app.all('/todos', users.requiresLogin); // Users Routes app.route('/todos') .post(users.hasPerms('globalPerms','privatePerms'), todos.create); .delete(users.hasPerms('globalPerms','privatePerms'), todos.delete);
To create Todo, globalPerms have GOD and privatePerms with SUPER HERO , both of which can be enabled.
The todos.delete here will be in the todos.delete method, just make sure user.id === todos.createById else SUPER HERO can continue to delete Todos created by the GOD.
ReadTodo :
When a TODO is created, it must have createById , stored similarly, when someone is assigned a TODO , then assignedTo and assignedBy must also be written.
This simplifies the processing of many other operations.
user.role.globalPerms - provide GOD to all TODO data.
user.role.privatePerms - give TODO either created by him or assigned to him / her.
user.role.globalPerms === undefined && user.role.privatePerms === undefined - its MAN and give TODOs that are assigned to it only.
UpdateTodo and CommentTodo :
This is an exact copy of what ReadTODO DIY does.
Last, AssignTodo :
Simple, loggedInUser.id === todos.createdById , then he can assign it to anyone.
Two things to keep in mind here:
Since the purpose of the element mainly occurs on your UI (Angular) interface, I gave this approach of checking loggedInUser.id === todos.createdById . Any way recorded by the user will see all TODOs through a read operation and can assign it to anyone who likes it.
Make sure that SUPER HERO can only assign TODO to itself or another SUPER HERO or MAN, but not to the GOD. As you show Assign for options on the user interface, is beyond the scope of this question. It is just a head.
Hope this was clear.
NOTE. There was no need to grant permissions for MAN in roles, and we coped with all possible operations.