Skip to content

Commit b6b0218

Browse files
authored
Glide query get user roles (#1010)
* Update readme.md Updated text to reflect changes to code * Update getUserRoles.js Use .reduce in place of .toArray return roles as array rather than the other meta data
1 parent 81c1a3c commit b6b0218

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
(function getUserRoles() {
2-
// sample user
3-
var userName = 'bow.ruggeri';
1+
function getUserRoles(userName) {
42

5-
var roleQuery = new GlideQuery('sys_user_has_role')
6-
.where('user.user_name', 'bow.ruggeri')
7-
.select(['role$DISPLAY', 'role.active', 'user$DISPLAY', 'user.email'])
8-
.toArray(100);
3+
var gqRoles = new global.GlideQuery('sys_user_has_role')
4+
.where('user.user_name', userName)
5+
.select('role$DISPLAY')
6+
// use .reduce() instead of .toArray() as it allows us to strip out only whats needed, and doesn't need to know the number of entries.
7+
.reduce(function (arr, e) {
8+
arr.push(e.role$DISPLAY);
9+
return arr;
10+
}, []);
911

10-
gs.log(JSON.stringify(roleQuery, null, 2));
11-
})();
12+
return gqRoles;
13+
};
14+
15+
gs.info(getUserRoles('bow.ruggeri'));

GlideQuery/Get User's Roles from User Name/readme.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,4 @@
22

33
GlideQuery's can greatly simplify extracting information from queries, especially when dot walking.
44

5-
This query retrieves a user's roles, and returns an array of objects with the the role's display
6-
value, the role's active status, the user's display value, and the user's email. Adding more
7-
fields is trivial with this format, and the query stream is very easy to follow.
5+
This code has been updated to simplify the function to return an array of roles for the given user. Shows the use of GlideQuery and .reduce() in place of .toArray().

0 commit comments

Comments
 (0)