<commit_before>
var mongoose = require("mongoose");
var util = require('util');
var access = require('../lib/access');
var _ = require('underscore');

var accesslevels = {
guest:0, //anyone has this right
user:1,  //authenticated user
verifieduser:2, //recently authenticated user (note: stateful, same user can change)
//group:3,  //only principles who members of the group referred to by the "group" property
//owner:4,  //only owner (principle matches 'owner' property)
admin:5,  //only principle with admin role
system: 6 //denied to all except system code
};

var defaultrole = accesslevels.user;

var policy = new access.Policy({
  'any': undefined,
  'read': 'any',
  'write': 'any',
  'edit':'write',
  'create':'write',
  'remove': 'write',
  'view': 'read',
});
policy.defaultaccesscontrol = {'any': 'admin'};
policy.hasPermission = function(user, object, rel, guards) {
  var role = user.role ? accesslevels[user.role] : defaultrole; //XXX
  var neededLevel = Math.max.apply(null, _(guards).map(function(g){ return accesslevels[g]; }));
  //console.log('neededLevel', neededLevel, 'user role level', role);
  return role >= neededLevel;
}


var _currentPolicy = policy;
function getAccessControlPolicy() {
  return _currentPolicy;
}
function setAccessControlPolicy(p) {
  _currentPolicy = p;
}

function guardSchemaPlugin(schema, options) {
  var accesscontrol = options.map;
  if (!options.base) {
    //we have to do the check as properties are set because rules can depend
    //on the values of the properties so we need to check with the original value
    schema.pre('markModified', function (next, key) {;
      var user = this.getPrinciple();
      if (user) {
        var op = this.isNew ? 'create' : 'edit';
        if (!(key == '_id' && this.isNew)) {
          this.ensure(op, key);
        }
      }
      next();
    });
    schema.pre('save', function (next) {
      var user = this.getPrinciple();
      if (user) {
        try {
           var op = this.isNew ? 'create' : 'edit';
           var This = this;
           var typeCheck = false;
           //if we haven't already done a check, do one now
           this.modifiedPaths().forEach(function(path) {
             if (path == '_id' && This.isNew)
               return;
             typeCheck = true;
           });
           if (!typeCheck) {//there were no modified paths
             This.schema.statics.ensure(user, op); //need the most derived schema
           }
        } catch (err) {
           next(err);
        }
      }
      next();
    });<commit_msg>a more efficient access check<commit_after>
var mongoose = require("mongoose");
var util = require('util');
var access = require('../lib/access');
var _ = require('underscore');

var accesslevels = {
guest:0, //anyone has this right
user:1,  //authenticated user
verifieduser:2, //recently authenticated user (note: stateful, same user can change)
//group:3,  //only principles who members of the group referred to by the "group" property
//owner:4,  //only owner (principle matches 'owner' property)
admin:5,  //only principle with admin role
system: 6 //denied to all except system code
};

var defaultrole = accesslevels.user;

var policy = new access.Policy({
  'any': undefined,
  'read': 'any',
  'write': 'any',
  'edit':'write',
  'create':'write',
  'remove': 'write',
  'view': 'read',
});
policy.defaultaccesscontrol = {'any': 'admin'};
policy.hasPermission = function(user, object, rel, guards) {
  var role = user.role ? accesslevels[user.role] : defaultrole; //XXX
  var neededLevel = Math.max.apply(null, _(guards).map(function(g){ return accesslevels[g]; }));
  //console.log('neededLevel', neededLevel, 'user role level', role);
  return role >= neededLevel;
}


var _currentPolicy = policy;
function getAccessControlPolicy() {
  return _currentPolicy;
}
function setAccessControlPolicy(p) {
  _currentPolicy = p;
}

function guardSchemaPlugin(schema, options) {
  var accesscontrol = options.map;
  if (!options.base) {
    //we have to do the check as properties are set because rules can depend
    //on the values of the properties so we need to check with the original value
    schema.pre('markModified', function (next, key) {;
      var user = this.getPrinciple();
      if (user) {
        var op = this.isNew ? 'create' : 'edit';
        if (!(key == '_id' && this.isNew)) {
          this.ensure(op, key);
        }
      }
      next();
    });
    schema.pre('save', function (next) {
      var user = this.getPrinciple();
      if (user) {
        try {
           //if we haven't already done a check, do one now
           var paths = this.modifiedPaths();
           var count = 0;
           if (this.isNew && paths.indexOf('_id') > -1)
             count++; //ignore _id
           if (paths.length <= count) {//there were no paths that were checked
             var op = this.isNew ? 'create' : 'edit';
             this.schema.statics.ensure(user, op); //need the most derived schema
           }
        } catch (err) {
           next(err);
        }
      }
      next();
    });
{'fraction_non_alphanumeric': 0.14170279858100118, 'fraction_numerical': 0.0031533307055577452, 'mean_word_length': 2.7760416666666665, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 6, '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': '3664672', 'n_tokens_mistral': 1534, 'n_tokens_neox': 1500, 'n_words': 486}