matchPermission(items, data) {
    let matched = false,
      entityField,
      matchPerm;
    for (let i = 0, len = items.length; i < len; i++) {
      let iam = items[i];
      if (typeof iam !== 'string' || !iam) continue;
      try {
        iam = iam.split(':').splice(1); // remove first iam
      } catch (e) {
        continue;
      }
      // iam[0] - is category
      if (iam[0] !== data.category) {
        // Check if we want pattern matching or not.
        if (data.pattern !== true || typeof data.category !== 'string') continue;
        if (data.category.indexOf(iam[0]) !== 0) continue;
      }
      // check against any given actions.
      if (data.action) {
        let iamAct = this.fromPermissionBinary(iam[1]),
          dataAct = this.fromPermissionAction(data.action);
        if (dataAct.read && !iamAct.read) continue;
        if (dataAct.create && !iamAct.create) continue;
        if (dataAct.update && !iamAct.update) continue;
        if (dataAct.delete && !iamAct.delete) continue;
      }
      // check against any given entities.
      // By default, if the IAM does not have any entity, but we requested an entity, we stop.
      if (data.entity_type) {
        let entity = iam[3];
        if (!entity) continue;
        let tmp = entity.split('#'),
          entityField = tmp[0],
          entityId = tmp[1];
        if (data.entity_type !== entityField) continue;
        if (data.entity_id !== null) {
          if (typeof entityId === 'undefined') continue;
          if (data.entity_id !== entityId) continue;
        }
      }
      // IF we reached this, that means that we've passed all filters.
      matched = true;
      matchPerm = items[i];
      if (iam[3]) { // entity type.
        entityField = iam[3];
      }
      break;
    }
    let res = {
      match: matched
    };
    if (entityField) {  // we have the entityField.
      res.entity_field = entityField;
    }
    if (matched) {
      res.permission = matchPerm;
    }
    return res;
  }
/**
   * Given a list of permission UNLOQ IAMs, it will test to see
   * if the given input is match for any items.
   * The IAM pattern is:
   *  iam:$category:$permission:$userId:$entityField#entityId
   * ARGUMENTS
   *  - items - array of permission strings.
   *  - data.category - the category to match against.
   *  - data.action - any specific actions we want to match against
   *  - data.entity_type - if specified, the entity type we want to match against.
   *  - data.entity_id - if specified, the entity id we want to match against.
   * */
Given a list of permission UNLOQ IAMs, it will test to see
if the given input is match for any items.
Given a list of permission UNLOQ IAMs it will test to see if the given input is match for any items .
matchPermission(items, data) {
    let matched = false,
      entityField,
      matchPerm;
    for (let i = 0, len = items.length; i < len; i++) {
      let iam = items[i];
      if (typeof iam !== 'string' || !iam) continue;
      try {
        iam = iam.split(':').splice(1); 
      } catch (e) {
        continue;
      }
      if (iam[0] !== data.category) {
        if (data.pattern !== true || typeof data.category !== 'string') continue;
        if (data.category.indexOf(iam[0]) !== 0) continue;
      }
      if (data.action) {
        let iamAct = this.fromPermissionBinary(iam[1]),
          dataAct = this.fromPermissionAction(data.action);
        if (dataAct.read && !iamAct.read) continue;
        if (dataAct.create && !iamAct.create) continue;
        if (dataAct.update && !iamAct.update) continue;
        if (dataAct.delete && !iamAct.delete) continue;
      }
      if (data.entity_type) {
        let entity = iam[3];
        if (!entity) continue;
        let tmp = entity.split('#'),
          entityField = tmp[0],
          entityId = tmp[1];
        if (data.entity_type !== entityField) continue;
        if (data.entity_id !== null) {
          if (typeof entityId === 'undefined') continue;
          if (data.entity_id !== entityId) continue;
        }
      }
      matched = true;
      matchPerm = items[i];
      if (iam[3]) { 
        entityField = iam[3];
      }
      break;
    }
    let res = {
      match: matched
    };
    if (entityField) {  
      res.entity_field = entityField;
    }
    if (matched) {
      res.permission = matchPerm;
    }
    return res;
  }
matchPermission ( items , data ) { let matched = false , entityField , matchPerm ; for ( let i = 0 , len = items . length ; i < len ; i ++ ) { let iam = items [ i ] ; if ( typeof iam !== 'string' || ! iam ) continue ; try { iam = iam . split ( ':' ) . splice ( 1 ) ; } catch ( e ) { continue ; } if ( iam [ 0 ] !== data . category ) { if ( data . pattern !== true || typeof data . category !== 'string' ) continue ; if ( data . category . indexOf ( iam [ 0 ] ) !== 0 ) continue ; } if ( data . action ) { let iamAct = this . fromPermissionBinary ( iam [ 1 ] ) , dataAct = this . fromPermissionAction ( data . action ) ; if ( dataAct . read && ! iamAct . read ) continue ; if ( dataAct . create && ! iamAct . create ) continue ; if ( dataAct . update && ! iamAct . update ) continue ; if ( dataAct . delete && ! iamAct . delete ) continue ; } if ( data . entity_type ) { let entity = iam [ 3 ] ; if ( ! entity ) continue ; let tmp = entity . split ( '#' ) , entityField = tmp [ 0 ] , entityId = tmp [ 1 ] ; if ( data . entity_type !== entityField ) continue ; if ( data . entity_id !== null ) { if ( typeof entityId === 'undefined' ) continue ; if ( data . entity_id !== entityId ) continue ; } } matched = true ; matchPerm = items [ i ] ; if ( iam [ 3 ] ) { entityField = iam [ 3 ] ; } break ; } let res = { match : matched } ; if ( entityField ) { res . entity_field = entityField ; } if ( matched ) { res . permission = matchPerm ; } return res ; }
Given a list of permission UNLOQ IAMs, it will test to see
 if the given input is match for any items.
Given a list of permission UNLOQ IAMs it will test to see if the given input is match for any items .
// remove first iam // iam[0] - is category // Check if we want pattern matching or not. // check against any given actions. // check against any given entities. // By default, if the IAM does not have any entity, but we requested an entity, we stop. // IF we reached this, that means that we've passed all filters. // entity type. // we have the entityField.