<commit_before>'use strict'

var RoleAssessorAll = require('./acl/role-assessor/all');
var RoleAssessorAuthed = require('./acl/role-assessor/authed');
var RoleAssessorOwner = require('./acl/role-assessor/owner');

class ServerAcl
{
  constructor(options = {}) 
  {
    this.config = options;
    this.config['rules'] = this.config['rules'] ? this.config['rules'] : [];
    this.config['endpoints'] = this.config['endpoints'] ? this.config['endpoints'] : {};

    this.roleAssessor = {
      all: new RoleAssessorAll,
      authed: new RoleAssessorAuthed,
      owner: new RoleAssessorOwner
    };
  }
  populateContext(request, context)
  {
    // Initialise assessors
    var promises = [];
    for (var role in this.roleAssessor) {
      promises.push(this.roleAssessor[role].initContext(request, context));
    }
    return Promise.all(promises);
  }
  isPermitted(endpointName, context)
  {
    endpointName = endpointName ? endpointName : '';
    context = context ? context : {};

    // We append the end point rules to the global rules as the endpointrules should override the global rules
    var rules = this.getRules(endpointName);
    
    // We need to execute hasRole() for each rule in squence so we will start with a resolved promise
    var promise = Promise.resolve(true);
    rules.forEach(function(rule){
      // Each rule applies only the specified role
      // To test if a given rule will permit or deny the user we need to check two things
      // - Does the user have the role - if not the rule is no applicable and we can move on
      // - Does the rule permit or deny the user
      promise = promise.then(function(initResult){
        var role = rule['role'];
        return this.hasRole(role, context).then(function(userHasRole){
          var result = undefined;
          var initConditions = (typeof initResult == 'object') ? initResult : null;
          if (typeof userHasRole == 'object') {
            if (initConditions) {
              for (var condition in userHasRole) {
                initConditions[condition] = userHasRole[condition];
              }
              result = initConditions;
            } else {
              result = userHasRole;
            }
          } else if (userHasRole === true) {
            if (initConditions) {
              result = initConditions;
            } else {
              result = rule['allow'] !== undefined ? rule['allow'] : true;
            }
          }
          return result;
        });
      }.bind(this));
    }.bind(this));
    
    return promise.then(function(finalResult){
      return finalResult;
    });
  }
  hasRole(role, context)
  {
    // If a role assessor has been defined for this role we delegate
    // Otherwise we check if the role was staticaly assigned to the user
    var promise = Promise.resolve(false);
    var roleAssessor = this.roleAssessor[role];
    if (roleAssessor) {
      promise = roleAssessor.hasRole(context);
    } else {
      var staticRoles = (context['user'] && context['user']['acl'] && context['user']['acl']['roles']) ? context['user']['acl']['roles'] : [];
      promise = Promise.resolve(staticRoles.indexOf(role) !== -1);
    }
    <commit_msg>Add comments<commit_after>'use strict'

var RoleAssessorAll = require('./acl/role-assessor/all');
var RoleAssessorAuthed = require('./acl/role-assessor/authed');
var RoleAssessorOwner = require('./acl/role-assessor/owner');

class ServerAcl
{
  constructor(options = {}) 
  {
    this.config = options;
    this.config['rules'] = this.config['rules'] ? this.config['rules'] : [];
    this.config['endpoints'] = this.config['endpoints'] ? this.config['endpoints'] : {};

    this.roleAssessor = {
      all: new RoleAssessorAll,
      authed: new RoleAssessorAuthed,
      owner: new RoleAssessorOwner
    };
  }
  populateContext(request, context)
  {
    // Initialise assessors
    var promises = [];
    for (var role in this.roleAssessor) {
      promises.push(this.roleAssessor[role].initContext(request, context));
    }
    return Promise.all(promises);
  }
  isPermitted(endpointName, context)
  {
    endpointName = endpointName ? endpointName : '';
    context = context ? context : {};

    // We append the end point rules to the global rules as the endpointrules should override the global rules
    var rules = this.getRules(endpointName);
    
    // We need to execute hasRole() for each rule in squence so we will start with a resolved promise
    var promise = Promise.resolve(true);
    rules.forEach(function(rule){
      // Each rule applies only the specified role
      // To test if a given rule will permit or deny the user we need to check two things
      // - Does the user have the role - if not the rule is no applicable and we can move on
      // - Does the rule permit or deny the user
      promise = promise.then(function(initResult){
        var role = rule['role'];
        return this.hasRole(role, context).then(function(userHasRole){
          var result = undefined;
          var initConditions = (typeof initResult == 'object') ? initResult : null;
          if (typeof userHasRole == 'object') {
            // An object was returned - this represents the conditions under which the user has the role 
            if (initConditions) {
              // If we already had role ownership conditions we need to merge the new conditions
              for (var condition in userHasRole) {
                initConditions[condition] = userHasRole[condition];
              }
              result = initConditions;
            } else {
              result = userHasRole;
            }
          } else if (userHasRole === true) {
            if (initConditions) {
              // The previous rol assessor returned conditions - these still apply so we return them
              result = initConditions;
            } else {
              result = rule['allow'] !== undefined ? rule['allow'] : true;
            }
          }
          return result;
        });
      }.bind(this));
    }.bind(this));
    
    return promise.then(function(finalResult){
      return finalResult;
    });
  }
  hasRole(role, context)
  {
    // If a role assessor has been defined for this role we delegate
    // Otherwise we check if the role was staticaly assigned to the user
    var promise = Promise.resolve(false);
    var roleAssessor = this.roleAssessor[role];
    if (roleAssessor) {
      promise = roleAssessor.hasRole(context);
    } else {
      var staticRoles = (context['user'] && context['user']['acl'] && context['user']['acl']['roles']) ? context['user']['acl']['roles'] : [];
      promise = Promise.resolve(staticRoles.indexOf(role) !== -1);
    }
    
{'fraction_non_alphanumeric': 0.11664423508299686, 'fraction_numerical': 0.0002990877822640945, 'mean_word_length': 2.5052410901467503, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5575827', 'n_tokens_mistral': 1842, 'n_tokens_neox': 1834, 'n_words': 549}