<commit_before>var _ = require('lodash');
var IndexMixin = require('./index_mixin');
var _debug = require('debug');
var logId = 'serverbone:collections:acl_index_mixin';
var acl = require('../acl');
var errors = require('../errors');
var when = require('when');
var debug = {
  trace: _debug(logId + ':trace'),
  log: _debug(logId + ':log'),
  warn: _debug(logId + ':warn'),
  error: _debug(logId + ':error')
};


var ACLIndexMixin = _.extend({}, IndexMixin, {
  initACL: function() {
    this.acl = new acl.ACL(this.permissions);
  },

  addToIndex: function(model, options) {
    options = options || {};
    options.action = 'update';
    options.actor = options.actor || this.actor;
    debug.trace('ACLCollection(%s).create %s actor present: %d',
      this.type||'', options.action, options.actor !== undefined);
    var err = this.checkAccess(options, model);
    if (err) return when.reject(err);
    return IndexMixin.addToIndex.call(this, model, options);
  },

  removeFromIndex: function(models, options) {
    options = options || {};
    options.action = 'update';
    options.actor = options.actor || this.actor;
    var model = !_.isArray(models) ? models : null;
    var err = this.checkAccess(options, model);
    if (err) return when.reject(err);
    return IndexMixin.removeFromIndex.call(this, models, options);
  },

  destroyAll: function(options) {
    options = options || {};
    options.action = 'destroy';
    options.actor = options.actor || this.actor;
    var err = this.checkAccess(options);
    if (err) return when.reject(err);
    return this._callAdapter('removeIndex', options);
  },

  canAccess: function(action, actor, model) {
    return this.acl.assert(this.getRoles(actor, model), action);
  },

  getRoles: function(actor, model) {
    var roles = [];
    if (!actor || !model) return roles;
    if (model && actor && model.id === actor.id) {
      roles.push('owner');
    }
    roles = _.uniq((actor.roles || []).concat(roles));
    return roles;
  },

  checkAccess: function(options, model) {
    var canAccess = this.canAccess(options.action, options.actor, model);
    if (!canAccess) {
      debug.log('no access:' + options.action + ', ' + this.type + ', ' + this.indexKey);
      var error = new errors.ForbiddenError('No access to ' + options.action);<commit_msg>ACLIndex can override action<commit_after>var _ = require('lodash');
var IndexMixin = require('./index_mixin');
var _debug = require('debug');
var logId = 'serverbone:collections:acl_index_mixin';
var acl = require('../acl');
var errors = require('../errors');
var when = require('when');
var debug = {
  trace: _debug(logId + ':trace'),
  log: _debug(logId + ':log'),
  warn: _debug(logId + ':warn'),
  error: _debug(logId + ':error')
};


var ACLIndexMixin = _.extend({}, IndexMixin, {
  initACL: function() {
    this.acl = new acl.ACL(this.permissions);
  },

  addToIndex: function(model, options) {
    options = options || {};
    options.action = options.action || 'update';
    options.actor = options.actor || this.actor;
    debug.trace('ACLIndexMixin(%s).addToIndex %s actor present: %d',
      this.type||'', options.action, options.actor !== undefined);
    var err = this.checkAccess(options, model);
    if (err) return when.reject(err);
    return IndexMixin.addToIndex.call(this, model, options);
  },

  removeFromIndex: function(models, options) {
    options = options || {};
    options.action = options.action || 'update';
    options.actor = options.actor || this.actor;
    var model = !_.isArray(models) ? models : null;
    var err = this.checkAccess(options, model);
    if (err) return when.reject(err);
    return IndexMixin.removeFromIndex.call(this, models, options);
  },

  destroyAll: function(options) {
    options = options || {};
    options.action = options.action || 'destroy';
    options.actor = options.actor || this.actor;
    var err = this.checkAccess(options);
    if (err) return when.reject(err);
    return this._callAdapter('removeIndex', options);
  },

  canAccess: function(action, actor, model) {
    return this.acl.assert(this.getRoles(actor, model), action);
  },

  getRoles: function(actor, model) {
    var roles = [];
    if (!actor || !model) return roles;
    if (model && actor && model.id === actor.id) {
      roles.push('owner');
    }
    roles = _.uniq((actor.roles || []).concat(roles));
    return roles;
  },

  checkAccess: function(options, model) {
    var canAccess = this.canAccess(options.action, options.actor, model);
    if (!canAccess) {
      debug.log('no access:' + options.action + ', ' + this.type + ', ' + this.indexKey);
      var error = new errors.ForbiddenError('No access to ' + options.action);
{'fraction_non_alphanumeric': 0.16847248778415128, 'fraction_numerical': 0.0, 'mean_word_length': 3.693918245264207, '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': '4534537', 'n_tokens_mistral': 1514, 'n_tokens_neox': 1489, 'n_words': 347}