node.js - How to check which type of access user has (Nodejs and mongodb) -


i creating alarm tracking management system remote smart devices. using nodejs , mongodb. idea when device has alarm want user has permission see alarm error (or history of errors) after logs in. let have 3 types of user, first type can track 1 group of devices, second group , third devices. demo version thinking create user in mongodb , add array 3 types of devices group , assign true of false each device group depending on access urer have. have log in system, want advise how check if user has access group 1 can redirected after login particular route , etc. think create function checks somehow 'if(user nameofgroup1 true) redirect nameofgroup1 view' not sure how write code condition. user schema:

    var userschema = mongoose.schema({     local: {         username: string,         password: string,         access:[{          nameofgroup1: string,         available: boolean     },     {            nameofgroup2: string,         available: boolean     },     {             nameofgroup3: string,         available: boolean     }]}}); 

it simpler store array of group ids user should have access to. way you'll able add more groups in future , checking given group easy.

for example, if user's doc in db like:

{ "username" : "name", "groups" : [ 0, 2 ] } 

then it's easy check groups has access check specific group with:

function check(user, group) {   return user.groups.indexof(group) > -1; } 

also it's easy search users access given group in database:

db.users.find({groups:2}); 

instead of numbers can use names or objectids or whatever convenient you.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -