Skip to content
This repository has been archived by the owner on Aug 30, 2021. It is now read-only.

Roles per model #983

Closed
ervinebalo opened this issue Oct 13, 2015 · 2 comments
Closed

Roles per model #983

ervinebalo opened this issue Oct 13, 2015 · 2 comments

Comments

@ervinebalo
Copy link

Is there a way with the current feature set that I can apply roles specifically to a model, not app wide?

Here's what I'd like to do considering 2 models: Program and Article.

A user creates a Program and is the owner or admin of that program. User can then create Articles that belong to that program. He can also give other users of the app "editor", a role, rights to his Program that allows those "editors" create/edit articles under said program. Another role would be just a 'reader' role that only allows other users to view articles

@mleanos
Copy link
Member

mleanos commented Oct 13, 2015

It sounds like you're talking about permission based roles. I solved a similar design consideration with a project based off MEANJS.

The way that I would approach it is to have a permissions field in your Program model. Something like this..

var ProgramSchema = new Schema({
  created: {
    type: Date,
    default: Date.now
  },
  name: {
    type: String
  },
  description: {
    type: String
  },
  user: {
    type: Schema.ObjectId,
    ref: 'User'
  },
  permissions: [{
      user: {
        type: Schema.ObjectId,
        ref: 'User'
      },
      roles: {
        type: [{
            type: String,
            enum: ['reader', 'editor', 'admin']
          }]
      }
    }]
});

That's a basic example. But it may be a model that works for your use case. In your server.policy file you can check for that role, and set specific routes to only allow users with the required role.

If you need additional help, or would like to discuss further, I'm on Gitter https://gitter.im/meanjs/mean

@ervinebalo
Copy link
Author

Thanks @mleanos. I will try that. If I have any other Qs, I will definitely ask. Thanks again

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants