Skip to content
Snippets Groups Projects
process.js 29.9 KiB
Newer Older
import("system.logging");
import("system.vars");
S.Leipold's avatar
S.Leipold committed
import("system.tools");
import("system.SQLTYPES");
import("system.db");
import("Sql_lib");

/**
 * Provides functions to work with permissions.
 * Don't instanciate this!
 * 
 * @class
 */
function PermissionUtil () {}

{ //block where variables declared with let are available to avoid unexpected side-effects

    let alias = SqlUtils.getSystemAlias();
    let sqlHelper = new SqlMaskingUtils(alias);
    /**
     * Returns the default empty condition string for the given entity.
     * 
     * @param {String} pEntity name of the entity
     * 
     * @result {String} default empty condition string ({\"entity\":\"" + pEntity + "\",\"filter\":{\"type\":\"group\",\"operator\":\"AND\",\"childs\":[]}})
     */
    PermissionUtil.getEmptyCondString = function (pEntity) 
    {   
        return "{\"entity\":\"" + pEntity + "\",\"filter\":{\"type\":\"group\",\"operator\":\"AND\",\"childs\":[]}}";
    }
 * Returns the ids of all subordinated permission sets of a given parent permission set.
 * 
 * @param {String} pSetId id of the parent permission set
 * @result {String[]} array with the ids of every subordinated permission set. The result can never be null.
    PermissionUtil.getChildSetsOfSet = function (pSetId) 
        return newSelect("ASYS_PERMISSIONSETID", alias)
        .from("ASYS_PERMISSIONSET")
        .whereIfSet("ASYS_PERMISSIONSET.ASYS_PERMISSIONSET_ID", pSetId)
        .arrayColumn(true);
 * Returns all subordinated permission actions of a given permission set.
 * 
 * @param {String} pSetId id of the parent permission set
 * @result {String[]} array with the ids of every subordinated permission action. The result can never be null.
    PermissionUtil.getActionsOfSet = function (pSetId)
        return newSelect("ASYS_PERMISSIONACTION.ASYS_PERMISSIONACTIONID", alias)
        .from("ASYS_PERMISSIONACTION")
        .join("ASYS_PERMISSION", "ASYS_PERMISSION.ASYS_PERMISSIONID = ASYS_PERMISSIONACTION.ASYS_PERMISSION_ID")
        .whereIfSet("ASYS_PERMISSION.ASYS_PERMISSIONSET_ID", pSetId)
        .arrayColumn(true);
 * Returns all permission actions of the given permissions.
 * @param {String[]} pPermIds the ids of the permissions
 * @result {String[]} returns ids of all permission actions. The result can never be null.
    PermissionUtil.getActions = function(pPermIds) {
        if (pPermIds.length <= 0)
            return [];
        return newSelect("ASYS_PERMISSIONACTIONID", alias)
        .from("ASYS_PERMISSIONACTION")
        .whereIfSet("ASYS_PERMISSIONACTION.ASYS_PERMISSION_ID", pPermIds, SqlBuilder.IN())
        .arrayColumn(true);
 * Returns the action of a given permission action id.
 * @param {String} pActionId id of the action
 * @result {String} title of action as readable string of the given action id. Never 'null', empty string if there is no result.
    PermissionUtil.resolveActionId = function (pActionId)
        return newSelect("ASYS_PERMISSIONACTION.ACTION", alias)
        .from("ASYS_PERMISSIONACTION")
        .whereIfSet("ASYS_PERMISSIONACTION.ASYS_PERMISSIONACTIONID", pActionId)
        .cell(true);
S.Leipold's avatar
S.Leipold committed
    * Converts a given array to an object with properties permissionid, entity, role, field, cond, action, accesstype, condtype. 
    * 
    * @param {String[]} pArr the array which should be converted to an object. Order of array: permid, entity, role, field, cond, action, accesstype, condtype.
    * 
    * @result {{}} converted object
    */
    PermissionUtil.convertArrToObj = function(pArr) {
        var ret = pArr.map(function(x) {
            return {    
                "permissionid": x[0],
                "entity": x[1],
                "role": x[2],
                "field": x[3],
                "cond": x[4],
                "action": x[5],
                "accesstype": x[6],
                "condtype": x[7]
            }
        });
        return ret;
    }
 * Returns the first index at which a given permissionid can be found in an array, or -1 if it is not present.
 * 
 * @param {String[]} pPermTable permission table
 *  
 * @param {String} pPermId id of the permission
 *
 * @result {int} returns position (index) of the searched permission in the table, otherwise returns -1 if not found
    PermissionUtil.indexOfPermId = function(pPermTable, pPermId) {
        var notFound = -1;
        for (var i = 0; i < pPermTable.length; i++) {
            if(pPermTable[i].permissionid == pPermId)
                return i;
        }
        return notFound;
 * Checks a permission if the given actions are different to the actions in the database.
 * @param {String} pPermId permission id to which the actions are linked to
 * @param {String[]} pActionNew array of strings of new actions
 * 
 * @result {String[]} returns the different elements
 */
    PermissionUtil.getActionDiff = function(pPermId, pActionNew) {
        var actionOld = newSelect("ACTION", alias)
        .from("ASYS_PERMISSIONACTION")
        .whereIfSet("ASYS_PERMISSIONACTION.ASYS_PERMISSION_ID", pPermId)
        .arrayColumn(true);
        return arrDiff(actionOld, pActionNew);
    }
 * Checks if the given string is different to the string of a column in the given database table.
 * 
 * @param {String} pId id of DB entry
 * 
 * @param {String} pString string which has to be checked if different
 * 
 * @param {String} pDbCol column to which the string is compared
 * 
 * @param {String} pDbTable database table
 * 
 * @result {Boolean} returns true if different, otherwise false
 */
    PermissionUtil.isDiff = function(pId, pString, pDbCol, pDbTable) {
        var stringDb = newSelect(pDbCol, alias)
        .from(pDbTable)
        .whereIfSet([pDbTable, pDbTable + "ID"], pId)
        .cell(true);
        return stringDb != pString ? true : false;
    }
 * Updates the value of the column in table if the values are different.
 * 
 * @param {String} pId id of DB entry
 * 
 * @param {String} pValue string which gets checked if different
 * 
 * @param {String} pDbCol column to which the string is compared
 * 
 * @param {String} pDbTable database table
 * 
 * @result {Integer} number of records that were updated
 */
    PermissionUtil.updateIfDiff = function(pId, pValue, pDbCol, pDbTable) {
        if (PermissionUtil.isDiff(pId, pValue, pDbCol, pDbTable)) {
            var cols = [pDbCol];
            var vals = [pValue];
            var cond = new SqlBuilder(alias).whereIfSet([pDbTable, pDbTable + "ID"], pId);
            return cond.updateData(true, pDbTable, cols, null, vals)
     * Gets the number of permissions which are linked to the given entity-role-combination. 
     * It is possible to set one parameter to null/undefined to get the number of permissions of a role or entity. 
     * Both parameter null/undefined gets the number of all permissions in the system.
     *  
     * @param {String} [pEntityName] name of the entity, can be null
     * 
     * @param {String} [pRole] name of the role, can be null
     * 
     * @result {Integer} returns number of permissions linked to the entity-role-combination.
     */
    PermissionUtil.getNumberOfPermissions = function(pEntityName, pRole) {
        return newSelect("COUNT(*)", alias)
        .from("ASYS_PERMISSIONSET")
        .whereIfSet("ASYS_PERMISSIONSET.ENTITY_ID", pEntityName)
        .andIfSet("ASYS_PERMISSIONSET.ROLE_ID", pRole)
        .cell(true, "0");
 * Gets the default permission of the root permission set.
 * 
 * @param {String} pPermId id of the permission
 * @result {String} returns id of the default permission of the root permission set. Never 'null', empty string if there is no result.
    PermissionUtil.getPermissionRoot = function(pPermId) {
        var parentSet = [PermissionUtil.getParentSet(pPermId)];
            parentSet = newSelect("ASYS_PERMISSIONSET_ID, ASYS_PERMISSIONSETID", alias)
            .from("ASYS_PERMISSIONSET")
            .where("ASYS_PERMISSIONSET.ASYS_PERMISSIONSETID", parentSet[0])
            .arrayRow();
        return PermissionUtil.getPermissionWithoutCond(parentSet[1]);
     * get the entity for a specific permissionset.
     * 
     * @param {String} pSetId the id of the permission set
     * 
     * @return {String} the entity name or an empty string if not found
     */
    PermissionUtil._getEntity = function(pSetId) {
        return newSelect("ENTITY_ID", alias)
        .from("ASYS_PERMISSIONSET")
        .whereIfSet("ASYS_PERMISSIONSET.ASYS_PERMISSIONSETID", pSetId)
    }

    /**
     * Gets the default permission of a given permission set.
     * 
     * @param {String} pSetId id of the permission set
     * 
     * @result {String} returns id of default permission of given set. Never 'null', empty string if there is no result.
     */
    PermissionUtil.getPermissionWithoutCond = function(pSetId) {
        var emptyCond = PermissionUtil.getEmptyCondString(PermissionUtil._getEntity(pSetId));

        return newSelect("ASYS_PERMISSIONID", alias)
        .from("ASYS_PERMISSION")
        .whereIfSet("ASYS_PERMISSION.ASYS_PERMISSIONSET_ID", pSetId)
        .and(new SqlBuilder(alias).where()
            .or("COND is null")
            .or("ASYS_PERMISSION.COND", emptyCond, sqlHelper.castLob("#", 254) +" = ?")
            )
        .cell();
     * Gets the permissions with conditions of a given permission set.
     * 
     * @param {String} pSetId the id of the permission set
     * 
     * @result {String[]} returns the ids of permissions with conditions of a given permission set. The result can never be null.
     */
    PermissionUtil.getPermissionWithCond = function(pSetId) {
        var emptyCond = PermissionUtil.getEmptyCondString(PermissionUtil._getEntity(pSetId));
        return newSelect("ASYS_PERMISSIONID", alias)
        .from("ASYS_PERMISSION")
        .whereIfSet("ASYS_PERMISSION.ASYS_PERMISSIONSET_ID", pSetId)
        .and(new SqlBuilder(alias).where()
            .or("COND is not null")
            .and("ASYS_PERMISSION.COND", emptyCond, sqlHelper.castLob("#", 254) +" != ?")
     * Gets the permission set id of a given role-entity-accesstype-combination.
     * 
     * @param {String} pRole name of the role
     * 
     * @param {String} pEntity name of the entity
     * 
     * @param {String} pAccessType name of the access type (E,R,F)
     * 
     * @param {String} [pField] name of the field
     * 
     * @result {String} returns id of the matching permission set. The result can never be null.
     */
    PermissionUtil.getSet = function(pRole, pEntity, pAccessType, pField) {
        
        var query = newSelect("ASYS_PERMISSIONSETID", alias)
        .from("ASYS_PERMISSIONSET")
        .where("ASYS_PERMISSIONSET.ROLE_ID", pRole)
        .and("ASYS_PERMISSIONSET.ENTITY_ID", pEntity)
        .and("ASYS_PERMISSIONSET.ACCESSTYPE", pAccessType);
        
        if (pField) {
            query.and("ASYS_PERMISSIONSET.FIELD_ID", pField)
        return query.cell();
     * Gets the root permission set of a entity-role-combination.
     * 
     * @param {String} pRole id of a role
     * 
     * @param {String} pEntity id of an entity
     * 
     * @result {String} returns id of the root permission set of the given entity-role-combination. Never 'null', empty string if there is no result.
     */
    PermissionUtil.getSetRoot = function(pRole, pEntity) {
        return newSelect("ASYS_PERMISSIONSETID", alias)
        .from("ASYS_PERMISSIONSET")
        .where("ASYS_PERMISSIONSET.ROLE_ID", pRole)
        .and("ASYS_PERMISSIONSET.ENTITY_ID", pEntity)
        .and("ASYS_PERMISSIONSET.ACCESSTYPE", "E")
        .cell();
     * Gets the parent permission set of a permission.
     * 
     * @param {String} pPermId id of the permission
     * 
     * @result {String} returns id of the parent set of the given permission. Never 'null', empty string if there is no result.
     */
    PermissionUtil.getParentSet = function(pPermId) {
        return newSelect("ASYS_PERMISSIONSET_ID", alias)
        .from("ASYS_PERMISSION")
        .where("ASYS_PERMISSION.ASYS_PERMISSIONID", pPermId)
        .cell();
     * Gets the parent permission set of a set.
     * 
     * @param {String} pSetId id of the permission set
     * 
     * @result {String} returns id of the parent permission set of the given set. Never 'null', empty string if there is no result.
     */
    PermissionUtil.getParentSetOfSet = function(pSetId) {
        return newSelect("ASYS_PERMISSIONSET_ID", alias)
        .from("ASYS_PERMISSIONSET")
        .where("ASYS_PERMISSIONSET.ASYS_PERMISSIONSETID", pSetId)
        .cell();
     * Checks if the given permission set has any children left.
     * 
     * @param {String} pSetId id of the permission set
     * 
     * @result {Boolean} returns true if permission set has no children, otherwise false
     */
    PermissionUtil.setIsEmpty = function(pSetId) {
        var subSets = PermissionUtil.getChildSetsOfSet(pSetId);
        var subPerms = PermissionUtil.getPermissions([pSetId]);
        var subActions = PermissionUtil.getActionsOfSet(pSetId);
        if (subActions.length == 0 && subPerms.length == 0 && subSets.length == 0) 
            return true;
        return false;
    }
     * Returns all subordinated permissions of the given permission sets.
     * 
     * @param {String[]} pSetIds ids of the permission sets
     * 
     * @result {String[]} array with ids of all subordinated permissions. The result can never be null.
     */
    PermissionUtil.getPermissions = function(pSetIds) {
        return newSelect("ASYS_PERMISSIONID", alias)
        .from("ASYS_PERMISSION")
        .whereIfSet("ASYS_PERMISSION.ASYS_PERMISSIONSET_ID", pSetIds, SqlBuilder.IN())
        .arrayColumn(true); // returns empty array if pSetIds is an empty array
     * Returns the condition type of the given permission.
     * 
     * @param {String} pPermId the id of the permission, mandatory
     * 
     * @result {String} returns the value of condtype (1 or 0). Never 'null', empty string if there is no result.
     */
    PermissionUtil.getCondType = function(pPermId) {
        return newSelect("CONDTYPE", alias)
        .from("ASYS_PERMISSION")
        .where("ASYS_PERMISSION.ASYS_PERMISSIONID", pPermId)
        .cell();
     * Inserts a new instance of a permission set into ASYS_PERMISSIONSET.
     * 
     * @param {String} pParentPermSetId parent permission set, empty if root node
     * 
     * @param {String} pEntity entity to which the PermissionSet is linked, mandatory
     * 
     * @param {String} pRole Role to which the PermissionSet is linked, mandatory
     * 
     * @param {String} pField Field to which the PermissionSet is linked, empty if no field permission
     * 
     * @param {String} pAccessType Entity, Record or Field (E, R, F), mandatory
     * 
     * @result {Integer} returns id of the inserted permission set
     */
    PermissionUtil.insertSet = function(pParentPermSetId, pEntity, pRole, pField, pAccessType) {
        var table = "ASYS_PERMISSIONSET";
        var cols = [
Simon Leipold's avatar
Simon Leipold committed
        "FIELD_ID",
        "ACCESSTYPE",
        "ROLE_ID",
        "ASYS_PERMISSIONSETID",
        "ASYS_PERMISSIONSET_ID",
        "ENTITY_ID"
Simon Leipold's avatar
Simon Leipold committed
        var vals = [pField, pAccessType, pRole, setId, pParentPermSetId, pEntity];
        db.insertData(table, cols, null, vals, alias);
     * Inserts a new instance of a permission into ASYS_PERMISSION.
     * 
     * @param {String} pParentSetId parent permission set, mandatory
     * 
     * @param {String} pCond condition of the permission, empty if no condition
     * 
     * @param {String} pCondType condition Type of the permission, should nearly always be "true"
     * 
     * @param {String} pPermId id of the new permission (can be empty/null)
     * 
     * @result {Integer} returns id of the inserted permission
     */
    PermissionUtil.insertPermission = function(pParentSetId, pCond, pCondType, pPermId) {
        var table = "ASYS_PERMISSION";
Simon Leipold's avatar
Simon Leipold committed
        "ASYS_PERMISSIONSET_ID",
        "ASYS_PERMISSIONID",
        "CONDTYPE",
        "COND"
        var permId;
        if (pPermId != null && pPermId != "" && pPermId != undefined) {
            permId = pPermId;
        } else {
            permId = util.getNewUUID(); 
        }
Simon Leipold's avatar
Simon Leipold committed
        var vals = [pParentSetId, permId, pCondType, pCond];
        db.insertData(table, cols, null, vals, alias);  
        return permId;
    }
     * Inserts a new instance of a permission action into ASYS_PERMISSIONACTION.
     * 
     * @param {String} pParentPermId parent permission, mandatory
     * 
     * @param {String} pAction title of action (view, create,...), mandatory
     * 
     * @param {String} pActionId id of the new permission action
     * 
     * @result {Integer} returns id of the inserted permission action, returns null if insert was not possible
     */
    PermissionUtil.insertAction = function(pParentPermId, pAction, pActionId) {
        var table = "ASYS_PERMISSIONACTION";
Simon Leipold's avatar
Simon Leipold committed
        "ASYS_PERMISSIONACTIONID",
        "ASYS_PERMISSION_ID",
        "ACTION"
        
        var actionNotExists = newSelect("ASYS_PERMISSIONACTIONID", alias)
        .from("ASYS_PERMISSIONACTION")
        .whereIfSet("ASYS_PERMISSIONACTION.ASYS_PERMISSIONACTIONID", pActionId)
        .cell(true) == "";
        if (pActionId != null & pActionId != "" && pActionId != undefined && actionNotExists) {
            actionId = util.getNewUUID(); // if same id is already in db -> create new UID
Simon Leipold's avatar
Simon Leipold committed
        var vals = [actionId, pParentPermId, pAction];
        if (db.insertData(table, cols, null, vals, alias) == 0) {
            return null;
        }
    
    /**
     * Returns the condition of a permission.
     * 
     * @param {String} pPermId id of the permission which condition should be returned, mandatory
     * 
     * @result {String} returns the condition of a permission
     */
    PermissionUtil.getCond = function(pPermId) {
        return newSelect("COND", alias)
        .from("ASYS_PERMISSION")
        .where("ASYS_PERMISSION.ASYS_PERMISSIONID", pPermId)
        .cell();
     * Returns true if the permission exists, otherwise false.
     * 
     * @param {String} pPermId The permission id
     * 
     * @result {Boolean} true if permission exists, otherwise false
     */
    PermissionUtil.permissionExists = function(pPermId) {
        var permissionCount = newSelect("COUNT(*)", alias)
        .from("ASYS_PERMISSION")
        .whereIfSet("ASYS_PERMISSION.ASYS_PERMISSIONID", pPermId)
        .cell(true, "0");
        
        return permissionCount != "0";
    /**
     * Returns true if the action exists, otherwise false.
     * 
     * @param {String} pAction The title of the action (e.g. view, create, read, update, delete)
     * 
     * @param {String} pPermId The ID of the permission to which the action is linked
     * 
     * @result {Boolean} true if action exists, otherwise false
     */
    PermissionUtil.actionExists = function(pAction, pPermId) {
        var permissionActionCount = newSelect("COUNT(*)", alias)
        .from("ASYS_PERMISSIONACTION")
        .where("ASYS_PERMISSIONACTION.ACTION", pAction)
        .and("ASYS_PERMISSIONACTION.ASYS_PERMISSION_ID", pPermId)
        .cell();

        return permissionActionCount != 0;
     * Returns permissionid of the permission with fitting parameters, otherwise returns empty string
     * 
     * @param {String} pRole name of the role
     * @param {String} pEntity name of the entity
     * @param {String} pField name of the field
     * @param {String} pAccesstype accesstype (E,F,R)
     * @param {String} pCondition condition (Filter in JSON-format)
     * @param {String} pCondtype type of the condition (true/false)
     * 
     * @result {String} Returns the id of the permission with fitting parameters, otherwise returns empty string, can never be null
     *
     */
    PermissionUtil.getPermission = function(pRole, pEntity, pField, pAccesstype, pCondition, pCondtype) {
        var permissionSelect = newSelect("ASYS_PERMISSION.ASYS_PERMISSIONID", alias)
        .from("ASYS_PERMISSIONSET")
        .join("ASYS_PERMISSION",  "ASYS_PERMISSION.ASYS_PERMISSIONSET_ID = ASYS_PERMISSIONSET.ASYS_PERMISSIONSETID")
        .where("ASYS_PERMISSIONSET.ENTITY_ID", pEntity)
        .and("ASYS_PERMISSIONSET.ROLE_ID", pRole)
        .and("ASYS_PERMISSIONSET.ACCESSTYPE", pAccesstype)
        var emptyCond = PermissionUtil.getEmptyCondString(pEntity);
        if (checkInput([pCondition])) {
            if (pCondition == emptyCond) {
                permissionSelect.and(new SqlBuilder(alias).where()
                    .or("ASYS_PERMISSION.COND", emptyCond, sqlHelper.castLob("#", 254) + " = ?")
                    .or("COND is null")
                    );
                permissionSelect.and("ASYS_PERMISSION.COND", pCondition, sqlHelper.cast("#", SQLTYPES.VARCHAR, pCondition.length) + " = ?")
        if (checkInput([pField])) {
            permissionSelect.and("ASYS_PERMISSIONSET.FIELD_ID", pField)
        if (checkInput([pCondtype])) {
            permissionSelect.and("ASYS_PERMISSION.CONDTYPE", pCondtype)

        return permissionSelect.cell();
     * Deletes a permission action from ASYS_PERMISSIONACTION.
     * 
     * @param {String} pActionId permission action id which should be deleted, mandatory
     * @result {Integer} returns number of deleted records
    PermissionUtil.deleteAction = function(pActionId) {
        return newWhereIfSet("ASYS_PERMISSIONACTION.ASYS_PERMISSIONACTIONID", pActionId, undefined, undefined, alias)
        .deleteData(true, "ASYS_PERMISSIONACTION");
S.Leipold's avatar
S.Leipold committed
    /**
Simon Leipold's avatar
Simon Leipold committed
     * Converts the role title to the unqiue role name.
S.Leipold's avatar
S.Leipold committed
     * 
     * @param {String} pRoleTitle title of a role, mandatory
     * 
Simon Leipold's avatar
Simon Leipold committed
     * @result {String} returns role name, empty string if no fitting role name exists
S.Leipold's avatar
S.Leipold committed
     */
    PermissionUtil.resolveRoleTitle = function(pRoleTitle) {
        var allRoles = tools.getAllRoles();
        var roleName = "";
        for each (role in allRoles) {
            if (role[0] == pRoleTitle) {
                roleName = role[3];
            }
        }
        return roleName;
    }
    
Simon Leipold's avatar
Simon Leipold committed
    /**
     * Converts the unqiue role name to the role title.
     * 
     * @param {String} pRoleName name of a role, mandatory
     * 
     * @result {String} returns title of a role, empty string if no fitting role title exists
     */
    PermissionUtil.resolveRoleName = function(pRoleName) {
        var allRoles = tools.getAllRoles();
        var roleTitle = "";
        for each (role in allRoles) {
            if (role[3] == pRoleName) {
                roleTitle = role[0];
            }
        }
        return roleTitle;
    }
    
    /**
     * Checks if an given action is linked to any permission of the current user.
     * 
     * @param {String} pEntity name of the entity, mandatory
     * 
     * @param {String} pAccesstype type of accesslevel ("E", "R" or "F"), mandatory
     * 
     * @param {String} pAction action to be checked (view, create, read, update, delete), mandatory
     * 
     * @param {String} pField name of the field, nullable
     * 
     * @param {String} pCondition condition of the permissions, nullable
     * 
     * @result {String} returns true if user has the given action linked to a permission, otherwise false
     */
    PermissionUtil.userHasAction = function(pEntity, pAccesstype, pAction, pField, pCondition) {
        var userRoles = tools.getRoles(vars.get("$sys.user"));
        var permissions = [];
        var condition;
        
        if (checkInput(pCondition)) {
            condition = pCondition;
        } else {
            condition = PermissionUtil.getEmptyCondString(pEntity);
        }
        
        for each(let role in userRoles) {
            permissions.push(PermissionUtil.getPermission(role, pEntity, pField, pAccesstype, condition, "1"))
        }
        
        if (permissions.length > 0) {
            for each(let perm in permissions) {
                var actionsOfPerm = PermissionUtil.getActions([perm])
                for each(let action in actionsOfPerm) {
                    if (PermissionUtil.resolveActionId(action) == pAction) {
                        return true;
                        break;
                    }
                }
            }
        }
        return false;
    }

    /**
     * Delete all links to child roles of pRole.
     * 
     * @param {String} pRole role which should be deleted, mandatory
     * 
     * @result {Integer} returns number of deleted records
     */
    PermissionUtil.removeParentRoleLinks = function(pRole) {
        return newWhereIfSet("ASYS_ROLES_CHILDREN.PARENT_ROLE", pRole, undefined, undefined, alias)
        .deleteData(true, "ASYS_ROLES_CHILDREN");
    }
    
    /**
     * Delete all links to parent roles of pRole.
     * 
     * @param {String} pRole role which should be deleted, mandatory
     * 
     * @result {Integer} returns number of deleted records
     */
    PermissionUtil.removeChildRoleLinks = function(pRole) {
        return newWhereIfSet("ASYS_ROLES_CHILDREN.CHILD_ROLE", pRole, undefined, undefined, alias)
        .deleteData(true, "ASYS_ROLES_CHILDREN");
    }
    
    /**
     * Removing all  all links to parent roles.
     * 
     * @param {String} pRole role which should be deleted, mandatory
     * 
     * @result {Integer} returns number of deleted records
     */
    PermissionUtil.removeHirarchyRoleLinks = function(pRole) {
        var affectedEntrys = 0;
        affectedEntrys += this.removeParentRoleLinks(pRole);
        affectedEntrys += this.removeChildRoleLinks(pRole);
        return affectedEntrys;
    }
    
    /**
     * Deletes a permission set from ASYS_PERMISSIONSET.
     * 
     * @param {String} pPermSetId permission set id which should be deleted, mandatory
     * 
     * @result {Integer} returns number of deleted records
     */
    PermissionUtil.deleteSet = function(pPermSetId) {
        return newWhereIfSet("ASYS_PERMISSIONSET.ASYS_PERMISSIONSETID", pPermSetId, undefined, undefined, alias)
        .deleteData(true, "ASYS_PERMISSIONSET");
    }

    
    /**
     * Deletes a permission from ASYS_PERMISSION.
     * 
     * @param {String} pPermId permission id which should be deleted, mandatory
     * 
     * @result {Integer} returns number of deleted records
     */
    PermissionUtil.deletePerm = function(pPermId) {
        return newWhereIfSet("ASYS_PERMISSION.ASYS_PERMISSIONID", pPermId, undefined, undefined, alias)
        .deleteData(true, "ASYS_PERMISSION");
    }
    
    /**
     * Deletes all permissions sets, permissions and permission actions linked to a role.
     * 
     * @param {String} pRole name of the role, mandatory
     * 
     * @result {String} returns number of deleted entrys
     */
    PermissionUtil.deleteEverythingLinkedToRole = function(pRole) {
        var sets = newSelect("ASYS_PERMISSIONSETID", alias)
        .from("ASYS_PERMISSIONSET")
        .where("ASYS_PERMISSIONSET.ROLE_ID", pRole).arrayColumn(true);
        var perms = this.getPermissions(sets);
        var actions = this.getActions(perms);
        var affectedEntrys = 0;
        
        for each(let action in actions) {
            affectedEntrys += this.deleteAction(action);
        }
        
        for each(let perm in perms) {
            affectedEntrys += this.deletePerm(perm);
        }
        
        for each(let set in sets) {
            affectedEntrys += this.deleteSet(set);
        }
        
        affectedEntrys += this.removeHirarchyRoleLinks(pRole);
       
        return affectedEntrys;
    }
// arrDiff calculates different elements of two arrays and returns them as array, otherwise empty array
function arrDiff (arr1, arr2) {
    var helperArr = [], diff = [];

    for (let i = 0; i < arr1.length; i++) {
        helperArr[arr1[i]] = true;
    }

    for (let i = 0; i < arr2.length; i++) {
        if (helperArr[arr2[i]]) {
            delete helperArr[arr2[i]];
        } 
        else {
            helperArr[arr2[i]] = true;
        }
    }

    for (var k in helperArr) {
        diff.push(k);
    }

    return diff;
}

// checks input array if each element is a valid input, returns true if valid, otherwise false
function checkInput(pInputArr) {
Johannes Hörmann's avatar
Johannes Hörmann committed
    for each (var input in pInputArr) { // TODO: replace "for each (.. in ..)" with for (.. of ..) when the designer supports it (the server already supports it). As it's deprecated in js https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Statements/for_each...in
        if (input == undefined || input == null || input == "")
            return false;
    }
    return true;