In the last two posts, we saw how to access item’s fields and ownership information. We are not done talking about Item object. There are still many things we would like to cover. But before then, I am going to wrap up another object which was mentioned when we were introducing the Master property in Item. That’s the Security object. The Security object contains some utility functions dealing with users, groups and roles. And we have seen the examples using its loadUser() and loadGroup() functions. We are going to see what else the Security object has.
Security.inGroup()
Security.inGroup(userId, groupName) returns a Boolean object to indicate if the group specified by groupName (String) contains the user specified by userId (String). For example,
if (Security.inGroup(userID, ‘Admin Group’)) {
// the current user is in Admin Group
} else {
// the current user is NOT in Admin Group
}
Notes: The userID is one of the pre-loaded variables for each script indicating the user whose action triggers the script. Usually it is the current login user’s ID.
Security.inRole()
Security.inRole(userId, roleName) checks if the user specified by the userId is in the role specified by roleName (String) by returning a Boolean object. The roleName is the displayed name for each role in Roles page [Administration –> Security –> Roles].
Security.loadUser()
Security.loadUser(userId) returns the User specified by the userId. If no user can be found with the userId, then an empty User will be returned. For example,
var user = Security.loadUser(‘Abc’);
if (user.id === null) {
println(“The user cannot be found.”);
}
Security.loadGroup()
Security.loadGroup(groupName) returns the Group specified by the groupName. Like loadUser if no group cannot be found with the groupName, an empty Group object will be returned.
Security.listGroups()
Security.listGroups(userId) returns an Array of group names (String) which the user belongs to.
Security.listRoles()
Security.listRoles(userId) returns an Array of role names (String) which the user has. Roles don’t have direct relationship with users. Group is the middle connector with them. So to get all the roles for a user, first the server will find all the groups the user belongs to. Next all the roles in each of those groups will be collected. Then after removing all the duplicates (one role could be added into multiple groups), the collection will be returned to you.
Security.listUsersInGroup()
Security.listUsersInGroup(groupName) returns an Array of users (User objects) that in the group.
Security.listUsersInRole()
Security.listUsersInRole(roleName) returns an Array of users (User objects) that in the role.
Security.searchUsers()
Security.searchUsers(criteria) returns an Array of users (User objects) matching the criteria. For example, the below function will return all the users with professional licenses and located in Toronto.
function getAllProfessionalUsersInToronto() {
var criteria = {
city : 'Toronto',
licenseCode: 'S'
};
return Security.searchUsers(criteria);
}
--Michal