Mongoose schema for multi-user application

I need to create a schema for a multi-user application that I need to develop using MongoDB, Express, AngularJS and NodeJS. I have 4 types of users in this application, GUEST , REGISTERED_USER , SUBSCRIBER , ADMIN . When a user has registered an application, I need to display forms and information based on the role (s) of a particular user.

Can someone give me some idea on how to handle the schema for different user roles, since I could subsequently use it for role functions and access level?

For example, for REGISTERED_USER , SUBSCRIBER , ADMIN users, registeredUserSchema is usually used:

 var registeredUserSchema = mongoose.Schema({ userId: String, fullName: String , email: String, password: String, created: { type: Date, default: Date.now }, roles: [String] }); 

But then I need a lot of information requested from a user with the SUBSCRIBER role, so as soon as he registered in the application, I would like to display a lot of additional information to show that it is filled in his acocunt than the user with just REGISTERED_USER .

Can anyone help me with this?

EDIT: More explanation

For example, a REGISTERED_USER will have userId , but a user with the SUBSCRIBER role will have subscriberId . Therefore, I need to decide how to structure this data, since there is data common to all users, and then for each user there is different data based on his role. I need help with a data structure selection strategy. For example, in the Java Persistence API, we have various inheritance strategies for structuring data in relational database tables.

+6
source share
2 answers

I use these collections for this part:

 var permissions_schema = mongoose.Schema({ name : String, title : String }); // this are mostly static data var roles_schema = mongoose.Schema({ name : String, title : String, _permissions : Array });// _permissions is an array that contain permissions _id var contacts_schema = mongoose.Schema({ mobile : String, password : String, first_name : String, last_name : String, _role : Number, _enabled : Boolean, _deleted : Boolean, _verify_code : Number, _groups_id : Array, _service_id : String }); // and at last _role is _id of the role which this user owns. 

With these collections, you can easily manage your users. Hope they have some good ideas for you.

UPDATE: A more flexible scheme may have a role_id array in the contact object, and the contact may have many roles, and its permissions are the merging of all role permissions.

+6
source

Here's a custom diagram from the MEAN.JS yeoman generator:

  var UserSchema = new Schema({ firstName: { type: String, trim: true, default: '', validate: [validateLocalStrategyProperty, 'Please fill in your first name'] }, lastName: { type: String, trim: true, default: '', validate: [validateLocalStrategyProperty, 'Please fill in your last name'] }, displayName: { type: String, trim: true }, email: { type: String, trim: true, default: '', validate: [validateLocalStrategyProperty, 'Please fill in your email'], match: [/.+\@.+\..+/, 'Please fill a valid email address'] }, username: { type: String, unique: 'testing error message', required: 'Please fill in a username', trim: true }, password: { type: String, default: '', validate: [validateLocalStrategyPassword, 'Password should be longer'] }, salt: { type: String }, provider: { type: String, required: 'Provider is required' }, providerData: {}, additionalProvidersData: {}, roles: { type: [{ type: String, enum: ['user', 'admin'] }], default: ['user'] }, updated: { type: Date }, created: { type: Date, default: Date.now }, /* For reset password */ resetPasswordToken: { type: String }, resetPasswordExpires: { type: Date } }); 
+1
source

Source: https://habr.com/ru/post/976627/


All Articles