2.1.2 • Published 7 years ago

role-hierarchy v2.1.2

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

Role Hierarchy Data Model

Build Status

This is a data model for a hierarchy of roles, suitable for mongodb users or meteor.

Given a definition of a role hierarchy :

  let hierarchyObj = {
      "name": "admin",
      "subordinates": [
        {
          "name": "user-admin",
          "subordinates": [
            {
              "name": "schoolAdmin",
              "subordinates": [
                {
                  "name": "teacher",
                  "subordinates": [
                    {
                      "name": "student",
                      // a student can see the following fields
                      "visibleUserFields": {
                        "_id": 1,
                        "username": 1,
                        "profile.name": 1,
                        "roles": 1
                      }
                    }
                  ],
                  // new users created by a teacher get the student role
                  "defaultNewUserRoles": [
                    "student"
                  ],
                  // new users created by a teacher get the teacher's profile.school and profile.classId
                  "profileFilters": [
                    "school",
                    "classId"
                  ],
                  // a teacher can see everything a student can see, also email addresses
                  "visibleUserFields": {
                    "emails": 1
                  }
                }
              ],
              "profileFilters": [
                "school"
              ]
            },
            {
              "name":"footballCoach",
              "subordinates":[
                {
                  "name": "footballCaptain",
                  "subordinates" :[
                    {
                      "name":"footballPlayer"
                    }
                  ]
                }
              ]
            }
          ],
          "defaultNewUserRoles": [
            "teacher"
          ]
        }
      ],
      "defaultNewUserRoles": [
        "teacher"
      ]
    }

    let roleHierarchy = new RoleHierarchy(
      {
        "rolesHierarchy": hierarchyObj),
        "loggingConfig": { level: "debug" },
        treeModelConfig: { "childrenPropertyName": "subordinates" },
      }      
    );

And a user :

  let myUserObj = {
    _id: 'abc123',
    profile: {
      organizations: ['springfield school', 'springfield football team']
    },
    "roles": {
      "springfield school": [
        "schoolAdmin",
        "footballCaptain"
      ],
      "springfield football team": [
        "footballCoach"
      ]

    }
  };

You can find information about the user's subordinates.

let subordinatesMap = roleHierarchy.getAllUserSubordinatesAsMap(myUserObj);
/*
{
        "springfield school": ["teacher", "student", "footballPlayer"],
        "springfield football team": ["footballCaptain", "footballPlayer"]
      }
*/

Or about a role's subordinates

let subordinatesArray = roleHierarchy.getAllSubordinateRolesAsArray('schoolAdmin');
/*
["teacher", "student"]
*/

Tests

Look in the test/test.js file, it gives you a pretty good idea of how to use this library.

To run the tests, simply :

npm test

API

docs generated with jsdoc2md

RoleHierarchy

Kind: global class

new RoleHierarchy(paramsObj)

create a new instance of RoleHierarchy

ParamTypeDescription
paramsObjObjectcontaining a hierarchy and a loggingConfig (optional) and a TreeModel config (optional): { hierarchy: {"name":"teacher", "subordinates": {"name":"student"} }, treeModelConfig: { "childrenPropertyName": "subordinates" }, loggingConfig: { "level": "debug"}}

roleHierarchy.reparse(rolesHierarchy)

re-create the hierarchy with a new object structure.

Kind: instance method of RoleHierarchy

ParamType
rolesHierarchyObject

roleHierarchy._getOrganizationsForUser(myUserObj)

Deprecated - use RoleHierarchy.getOrganizationsForUser instead.

Kind: instance method of RoleHierarchy

ParamType
myUserObj*

roleHierarchy.findRoleInHierarchy(roleName) ⇒ object

Find a role in the hierarchy by name

Kind: instance method of RoleHierarchy Returns: object - - the node in the tree that matches

ParamTypeDescription
roleNamestringthe name of the role to find

roleHierarchy.getRoleSubordinate(seniorRoleName, subordinateRoleName) ⇒ object

Return the subordinate roles of the given seniorRoleName

Kind: instance method of RoleHierarchy Returns: object - - the role of the subordinate, or false if not found.

ParamTypeDescription
seniorRoleNamestringthe name of the senior role
subordinateRoleNamestringthe name of the subordinate role

roleHierarchy.getAllSubordinateRolesAsArray(seniorRoleName) ⇒ Array

Get the names of subordinate roles as an array

Kind: instance method of RoleHierarchy Returns: Array - - the subordinate role names if any, otherwise undefined.

ParamTypeDescription
seniorRoleNamestringthe name of the senior role

roleHierarchy.getAllUserSubordinatesAsMap(myUserObj) ⇒ Object

Get a map of all of the role names that the provided user can administer, grouped by organization

Kind: instance method of RoleHierarchy Returns: Object - an object of subordinate {organization:roleName, roleName} arrays that the provided user can administer

ParamDescription
myUserObjthe user object of the provided user, with a roles property and a profile.organization or profile.organizations

roleHierarchy.getAllMyFieldsAsObject(myUserObj) ⇒ object

Get an object of all of the Meteor.user fields that the provided user can see

Kind: instance method of RoleHierarchy Returns: object - an object of the format {orgName: {field1: 1, field2: 2}}, the values being Meteor.user field names that the provided user can see, suitable for inclusion as a "fields" property in a mongodb Collection query.

ParamDescription
myUserObjthe user object of the provided user, with a roles property

roleHierarchy.isUserHasMoreSeniorRole(myUserObj, roleName, organizationName) ⇒ boolean

returns true if the given object is more senior than the given role in the given organization.

Kind: instance method of RoleHierarchy Returns: boolean - true if the user is more senior than the given role

ParamDescription
myUserObjthe user object of the provided user, with a roles property and an organization(s) property
roleNamethe name of the role to query
organizationNamethe name of the organization to query whether the user has the role

roleHierarchy.isUserDescendantOfUser(seniorUserObj, subordinateUserObj, organizationName) ⇒ boolean

returns true if the given senior user is higher in the hierarchy than the given subordinate user for the given organization.

Kind: instance method of RoleHierarchy Returns: boolean - true if the subordinateUser is below seniorUser in the hierarchy for at least one organization in common.

ParamDescription
seniorUserObjthe senior user we're checking, with roles property and organization(s) property
subordinateUserObjthe user we want to check see if they are subordinate to the senior user, with roles property and organization(s) property
organizationNamethe name of the organization whose roles to check

roleHierarchy.getProfileCriteriaFromUser(userWithProfile, profileFilterCriteria, organizationName) ⇒ object

Copy the given user's profile properties (as specified in roles hierarchy as profileFilters) as profile properties suitable for adding to a new user.

Kind: instance method of RoleHierarchy Returns: object - the query criteria, suitable for mongodb, to ensure only users with the same values for the specified fields will be returned.

ParamTypeDescription
userWithProfileobjectthe user object, with a profile property to copy
profileFilterCriteriaobjectexisting profileFilterCriteria. Note that if any properties are already specified, they may get overwritten.
organizationNamestringthe organization we're dealing with.

RoleHierarchy.getOrganizationsForUser(myUserObj) ⇒ Array.<String>

Get the organizations that the user belongs to, as an array.

Kind: static method of RoleHierarchy Returns: Array.<String> - an array of the organizations that the user belongs to.

ParamTypeDescription
myUserObjObjectan object containing an organization or organizations property.

RoleHierarchy.getRolesForUser(user, organization) ⇒ Array

Retrieve users roles

Kind: static method of RoleHierarchy Returns: Array - Array of user's roles, unsorted.

ParamTypeDescription
userObjectuser object
organizationStringOptional name of organization to restrict roles to. User's _GLOBAL_GROUP will also be included.

RoleHierarchy._getRolesForUser(user, organization)

Deprecated. Use RoleHierarchy.getRolesForUser instead.

Kind: static method of RoleHierarchy

ParamType
user*
organization*
2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

7 years ago

2.0.1

7 years ago

2.0.0

7 years ago

1.0.0

7 years ago