Check if logged user has a rol in widget. Service portal

jacc_99
Giga Guru

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

1 ACCEPTED SOLUTION

VigneshMC
Mega Sage

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

View solution in original post

4 REPLIES 4

Ct111
Giga Sage

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

Hi creativethinker,

 

thanks for your answer!

 

I checked if it is possible with 

g_user.hasRole

but it doesn´t works.

 

Jose

VigneshMC
Mega Sage

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

dvp
Mega Sage
Mega Sage

Replace the if condition with below

<div ng-if="category.sys_id == 'f1efc621db77d700b730fd651d961978' && ::c.respuesta == 'si'">

" quote ending should be after 'si'