- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2019 08:33 AM
Hello,
In widget, I want to check if the logged user have one role, for example "#example01".
If the logged user has the role #example01, in portal page will be shown a link through HTML with Angular code.
If the logged user hasn´t the role #example01, that link should not show in portal page.
I make next development:
- Server script:
// Query to check if the logged user has the #example01 role. data.messages variable keep the result.
data.result = 'false';
var gr = new GlideRecord('sys_user_has_role');
gr.addQuery('user', gs.getUserID());
gr.addQuery('role', '#example01');
gr.query();
if (gr.next()) {
data.result= 'true';
}
- Client controller:
// With the value in the server script variable data.result I make the variable c.respuesta to check in HTML
if($scope.data.result){
c.respuesta = 'si';
}else{
c.respuesta = 'no';
}
- Body HTML template:
<!-- ng-if="category.sys_id because is a category that I can filter depends on if the logged user has the #example01 role. -->
<div ng-if="category.sys_id == 'f1efc621db77d700b730fd651d961978'" && ::c.respuesta == 'si' >
<span class="class_name" ng-repeat="category in data.nameoflist track by category.sys_id">
// Code with the link to shown or not
</span>
</div>
This doesn´t work good. It doesn´t check if logged user has the role or not.
The problem is that I don´t know how to pass the result if the logged user has the role or not from server script to client script and after to HTML.
Somebody could help me with this?
Thanks in advance
Jose
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- 5,260 Views

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2019 08:38 AM
In server code, instead of gliderecording to sys_user_has_role table, you can use below
data.result = false;
if (gs.hasRole("admin")){
data.result= true;
}
then directly use it in HTML script like
<div ng-if=data.result && ::c.respuesta == 'si' >
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2019 08:36 AM
Try something like this directly in client script
g_user.hasRole('client_script_admin');
LEt me know if it helps for this you wont need to depend on server.
Mark my ANSWER as CORRECT and HELPFUL oif iit helPED
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-29-2019 02:37 AM
Hi creativethinker,
thanks for your answer!
I checked if it is possible with
g_user.hasRole
but it doesn´t works.
Jose

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2019 08:38 AM
In server code, instead of gliderecording to sys_user_has_role table, you can use below
data.result = false;
if (gs.hasRole("admin")){
data.result= true;
}
then directly use it in HTML script like
<div ng-if=data.result && ::c.respuesta == 'si' >
Thanks

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-28-2019 08:46 AM
Replace the if condition with below
<div ng-if="category.sys_id == 'f1efc621db77d700b730fd651d961978' && ::c.respuesta == 'si'">
" quote ending should be after 'si'