/**
* Created by z on 30.10.2015.
*/
'use strict';
/**
* Provides functionality useful for state authorisation.
* @param {Array} states - Array of state names you need to be logged in
* to access. Given a state name "project" matches using regexp /^project/
* e.g. "project" as well as "project-new".
* @constructor
*/
// TODO Roles
SUXESS.SecureStateCheck = function ( states ) {
this._regexp = this._buildRegExp( states );
};
SUXESS.SecureStateCheck.prototype = {
/**
* Builds regexp array used for state matching.
* @param {Array} states - array of state names
* @returns {Array}
* @private
*/
_buildRegExp : function ( states ) {
var ar = [];
for( var i = 0; i < states.length; i++ ) {
ar.push( new RegExp('^'+states[i]) );
}
return ar;
},
/**
* Returns true if user is allowed to visit a given state.
* @param state - from $stateProvider service
* @param user
* @returns {boolean}
*/
hasAccess : function ( state, user ) {
var u = state.name;
for( var i = 0; i < this._regexp.length; i++){
if( u.match(this._regexp[i]) ) {
return user.email !== undefined;
}
}
return true;
}
};