0
votes

I am working on an app using loopback . Need help and suggestions on models relationship and role.

  1. Multiple organisations
  2. An organisation have multiple admin and multiple user.

I'm using relation like

  1. Organisation hasMany. User
  2. User belongs to organisation
  3. Created a admin role.Don't want use $owner because in future may owner is not available.
  4. created two models users and organization

How can I list all user belongs to a organisation. How to differentiate b/w Admins/Users of different org.

Do I have to create custom filter for that?

//user.json
"properties": {
    "email": {
      "type": "string",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "organization": {
      "type": "belongsTo",
      "model": "Organization",
      "foreignKey": "orgUserId"
    },
    "templates": {
      "type": "hasMany",
      "model": "Template",
      "through": "Share"
      }
    }
  }


  //organization.json
 "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "users": {
      "type": "hasMany",
      "model": "user",
      "foreignKey": "orgUserId",
      "properties" :{
        "name": "realm"
      }
    },
    "templates": {
      "type": "hasMany",
      "model": "Template",
      "through": "Share"
    }
  },

Please help. Thanks

1

1 Answers

0
votes

How can I list all user belongs to a organization ?

Since you've defined User belongsTo and Organization hasMany relationship you could simply make the following request : GET api\Organization\{ID}\users

How to differentiate b/w Admins/Users of different org.`

First you should setup admin and team member (for instance) roles, then check in a role resolver script (example) that any user with admin role for a given organizationID is well indeed trying to make admin operation for that organization and not an other.

This is well documented in there

https://docs.strongloop.com/display/public/LB/Defining+and+using+roles

You should also check and study all this github repository, it has most of the information you're looking for:

https://github.com/strongloop/loopback-example-access-control