All files / src/domain/PrivilegeChecker index.js

100% Statements 10/10
100% Branches 10/10
100% Functions 2/2
100% Lines 9/9
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24            9x 1x     8x 1x     7x 6x 3x     3x 5x      
import is from 'is_js';
import { List } from 'immutable';
import Config from 'domain/Config';
 
export default class PrivilegeChecker {
  static hasPrivilege(state, privilege) {
    if (Config.get('featureLogin') !== true) {
      return true;
    }
 
    if (is.not.string(privilege) || is.empty(privilege)) {
      return false;
    }
 
    const privileges = state.get('privileges');
    if (is.not.object(privileges) || ! List.isList(privileges.items)) {
      return false;
    }
 
    const regEx = new RegExp(`${privilege}$`, 'i');
    return !! privileges.items.find((item) => regEx.test(item));
  }
}