skip
skip

Reputation: 12653

Mongoose schema for Multi-User application

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

Could somebody give me some idea on how to handle the schema for different roles of a user, for I could later use it for role and access level functionalities?

For example, the following registeredUserSchema is common for REGISTERED_USER, SUBSCRIBER, ADMIN users:

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 to be asked from a user with SUBSCRIBER role so once he has registered to the application, I would like to display a lot many extra information to show him to be filled up in his acocunt information, than a user with just REGISTERED_USER.

Could somebody help me with it?

EDIT: More Explaination

For example, a REGISTERED_USER will have userId, but a user with SUBSCRIBER role is going to have subscriberId. So I need to decide on how to structure that data as there is data that common for all the users and then there is data different for each user based on his role. I need help on the strategy to choose to structure that data. For example, in Java Persistence API we have various Inheritance Strategies to structure data in relational database tables.

Upvotes: 7

Views: 11312

Answers (2)

Elyas74
Elyas74

Reputation: 548

I'm using this collections for this part :

var permissions_schema = new mongoose.Schema({
    name : String,
    title : String
}); // this are mostly static data

var roles_schema = new mongoose.Schema({
    name : String,
    title : String,
    _permissions : Array
});// _permissions is an array that contain permissions _id

var contacts_schema = new 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 something like these collections, you can manage your users easily. I hope these have good ideas for you.

UPDATE : A more flexible schema can have an Array of role_id in contact object, and contact can have many roles and his /her permissions are a merge of all roles permissions.

Upvotes: 6

MEAN Developer
MEAN Developer

Reputation: 277

Here's the user schema from the MEAN.JS yeoman scaffolding 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
    }
});

Upvotes: 8

Related Questions