<commit_before>/**
 * Created by joeltobey on 4/14/17.
 */
component
  extends="coldbox.system.Interceptor"
  displayname="Security Interceptor"
  output="false"
{
  variables['_instance'] = {};

  public void function configure() {
    // configure the interceptor
    _instance['initialized'] = false;

    // Create the internal properties now
    setProperty( 'rules', [] );
    setProperty( 'rulesLoaded', false );
  }

  private void function loadRules() {
    /*lock name="security.loadrules.#controller.getAppHash()#" type="exclusive" timeout="30" throwontimeout="true" {
      var oModel = getModel( getproperty('rulesModel') );

      if (propertyExists('rulesModelArgs') && len(getProperty('rulesModelArgs'))) {
        var qRules = evaluate("oModel.#getproperty('rulesModelMethod')#( #getProperty('rulesModelArgs')# )");
      } else {
        var qRules = evaluate("oModel.#getproperty('rulesModelMethod')#");
      }

      validateRulesQuery(qRules);
      setProperty('rules', queryToArray(qRules));
      setProperty('rulesLoaded', true);
    }*/
  }

  private void function validateRulesQuery() {

  }

  public void function afterConfigurationLoad( event, interceptData, buffer, rc, prc ) {
    loadRules();

    if ( propertyExists('validatorModel') ) {
      try {
        var validator = getInstance( getProperty( 'validatorModel' ) );
        registerValidator( validator );
      } catch(any ex){
        if ( ex.type == "Security.validatorException" )
          rethrow;
        throw("Error creating validatorModel: #getProperty('validatorModel')#", ex.message & ex.detail & ex.tagContext.toString(), "interceptors.Security.validatorCreationException");
      }
    }

    _instance['initialized'] = true;
  }

  public void function preProcess( event, interceptData, buffer, rc, prc ) {
    //if (!_instance.initialized)
      //afterConfigurationLoad( argumentCollection:arguments );

    //processRules( argumentCollection:arguments );
  }

  private void function registerValidator(required any validator) {
    if ( structKeyExists(arguments.validator,"userValidator") ) {
      _instance['validator'] = arguments.validator;
    } else {
      throw(message="Validator object does not have a 'userValidator' method, I can only register objects with this interface method.", type="Security.validatorException");
    }
  }

}<commit_msg>singleton<commit_after>/**
 * Created by joeltobey on 4/14/17.
 */
component singleton
  extends="coldbox.system.Interceptor"
  displayname="Security Interceptor"
  output="false"
{
  variables['_instance'] = {};

  public void function configure() {
    // configure the interceptor
    _instance['initialized'] = false;

    // Create the internal properties now
    setProperty( 'rules', [] );
    setProperty( 'rulesLoaded', false );
  }

  private void function loadRules() {
    /*lock name="security.loadrules.#controller.getAppHash()#" type="exclusive" timeout="30" throwontimeout="true" {
      var oModel = getModel( getproperty('rulesModel') );

      if (propertyExists('rulesModelArgs') && len(getProperty('rulesModelArgs'))) {
        var qRules = evaluate("oModel.#getproperty('rulesModelMethod')#( #getProperty('rulesModelArgs')# )");
      } else {
        var qRules = evaluate("oModel.#getproperty('rulesModelMethod')#");
      }

      validateRulesQuery(qRules);
      setProperty('rules', queryToArray(qRules));
      setProperty('rulesLoaded', true);
    }*/
  }

  private void function validateRulesQuery() {

  }

  public void function afterConfigurationLoad( event, interceptData, buffer, rc, prc ) {
    loadRules();

    if ( propertyExists('validatorModel') ) {
      try {
        var validator = getInstance( getProperty( 'validatorModel' ) );
        registerValidator( validator );
      } catch(any ex){
        if ( ex.type == "Security.validatorException" )
          rethrow;
        throw("Error creating validatorModel: #getProperty('validatorModel')#", ex.message & ex.detail & ex.tagContext.toString(), "interceptors.Security.validatorCreationException");
      }
    }

    _instance['initialized'] = true;
  }

  public void function preProcess( event, interceptData, buffer, rc, prc ) {

    if (!_instance.initialized)
      afterConfigurationLoad( argumentCollection:arguments );

    // TODO: Create processRules() here that can do majority of security via rules
    // rather than relying on application model to manage everything.
    _instance.validator.processRules( argumentCollection:arguments );
  }

  private void function registerValidator(required any validator) {
    if ( structKeyExists(arguments.validator,"userValidator") ) {
      _instance['validator'] = arguments.validator;
    } else {
      throw(message="Validator object does not have a 'userValidator' method, I can only register objects with this interface method.", type="Security.validatorException");
    }
  }

}
{'fraction_non_alphanumeric': 0.12795436022819887, 'fraction_numerical': 0.0028524857375713123, 'mean_word_length': 3.817468105986261, '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': '7428482', 'n_tokens_mistral': 1337, 'n_tokens_neox': 1323, 'n_words': 310}