Find users having role snc-internal or no role in Business rule

SNOW44
Mega Guru

Hello Everyone,

I have an interesting requirement where end users are  not allowed to rename/delete attachment in portal. End user having both scenario which they have snc_internal or no role to user. And for user who don't have any role have solution like !gs.hasRole() == true.

My question is how to we find the user who have exactly snc_internal role?

can some one help me on this. @Ankur Bawiskar 

1 ACCEPTED SOLUTION

Amit Gujarathi
Giga Sage
Giga Sage

HI @SNOW44 ,
I trust you are doing great.
PLease find the below solution for the same.

// Check if the current user has the snc_internal role
function hasSNCInternalRole() {
    var hasRole = false;
    var user = gs.getUser();
    var roles = user.getRoles();
    
    for (var i = 0; i < roles.size(); i++) {
        if (roles.get(i) == 'snc_internal' && roles.size() == 1) {
            hasRole = true;
            break;
        }
    }
    
    return hasRole;
}

// Example usage
if (hasSNCInternalRole()) {
    // User has only the snc_internal role
    gs.info("User has exactly the snc_internal role.");
} else {
    // User does not have exactly the snc_internal role
    gs.info("User does not have exactly the snc_internal role.");
}

Was this answer helpful?


Please consider marking it correct or helpful.


Your feedback helps us improve!


Thank you!


Regards,


Amit Gujrathi



View solution in original post

8 REPLIES 8

Harish KM
Kilo Patron
Kilo Patron

Hi @SNOW44 you can use gs.hasRole('snc_internal'). This will return true if loggedin user have snc_internal role,

Alternatively you use delete operation ACL instead of Business rule.

in ACL just give role snc_internal and advance script answer = false; // this will not allow them to delete attachment

Regards
Harish

Hi Harish,

When I try gs.hasRole('snc_internal') it return true for both ITIL user and user having(snc_internal) users.

ACL is not working in portal side, I already given a try.

Hi @SNOW44 the use gs.hasRoleExactly('snc_internal');

if that doesnt work, use the below method

    var role= gs.hasRole('snc_internal');
hasRoleExactly(role);

    function hasRoleExactly(role) {
    var au = new ArrayUtil();
    var roles = gs.getSession().getRoles() + '';
    var roleArray = roles.split(",");
    var isAuthorized = au.contains(roleArray, role);
    gs.info("role"+isAuthorized);
    return isAuthorized;
}
Regards
Harish

Thanks harish, will check and comeback!