Skip to content

Commit 442152d

Browse files
Utility : Assign a role to user for certain time after that this will revoke after a day. (#1115)
* Utility : Assign a role to user for certain time after that this will revoke. Utility : Assign a role to user for certain time after that this will revoke. * Add readme.md file for the utility of AssignRoleToUserForADay
1 parent 5d49cd2 commit 442152d

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Utility/AssignRoleToUserForADay.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
function assignRoleToUserForADay(userSysId, roleSysId) {
2+
try {
3+
var user = getUserById(userSysId);
4+
var role = getRoleById(roleSysId);
5+
var userRoleSysId = assignRoleToUser(user.sys_id, role.sys_id);
6+
scheduleRoleRemoval(userRoleSysId, 1); // Schedule for 1 day later
7+
} catch (error) {
8+
gs.error(error.message);
9+
}
10+
}
11+
12+
function getUserById(userSysId) {
13+
var user = new GlideRecord('sys_user');
14+
if (!user.get(userSysId)) {
15+
throw new Error('User not found: ' + userSysId);
16+
}
17+
return user;
18+
}
19+
20+
function getRoleById(roleSysId) {
21+
var role = new GlideRecord('sys_user_role');
22+
if (!role.get(roleSysId)) {
23+
throw new Error('Role not found: ' + roleSysId);
24+
}
25+
return role;
26+
}
27+
28+
function assignRoleToUser(userSysId, roleSysId) {
29+
var userRole = new GlideRecord('sys_user_has_role');
30+
userRole.initialize();
31+
userRole.user = userSysId;
32+
userRole.role = roleSysId;
33+
34+
var userRoleSysId = userRole.insert();
35+
if (!userRoleSysId) {
36+
throw new Error('Failed to assign role to user');
37+
}
38+
return userRoleSysId;
39+
}
40+
41+
function scheduleRoleRemoval(userRoleSysId, days) {
42+
var job = new GlideRecord('sys_trigger');
43+
job.initialize();
44+
job.name = 'Remove user role after ' + days + ' days';
45+
job.script = 'var userRole = new GlideRecord("sys_user_has_role"); userRole.get("' + userRoleSysId + '"); userRole.deleteRecord();';
46+
job.next_action = new GlideDateTime();
47+
job.next_action.addDaysUTC(days);
48+
job.insert();
49+
}

Utility/readme.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Utility: AssignRoleToUserForADay.js
2+
3+
## Overview
4+
`AssignRoleToUserForADay.js` is a utility script designed to temporarily assign a role to a user for a single day. This can be useful for granting temporary permissions or access within an application.
5+
6+
## Features
7+
- Assign a role to a user for 24 hours.
8+
- Automatically revoke the role after the time period expires.
9+
- Log actions for auditing purposes.
10+
11+
## Usage
12+
1. **Import the script**:
13+
```javascript
14+
var assignRoleToUserForADay = require('./Utility/AssignRoleToUserForADay');
15+
```
16+
17+
2. **Call the function**:
18+
```javascript
19+
assignRoleToUserForADay(userId, roleId)
20+
.then(() => {
21+
console.log('Role assigned successfully.');
22+
})
23+
.catch((error) => {
24+
console.error('Error assigning role:', error);
25+
});
26+
```
27+
28+
## Parameters
29+
- `userId` (String): The ID of the user to whom the role will be assigned.
30+
- `roleId` (String): The ID of the role to be assigned.
31+
32+
## Example
33+
```javascript
34+
var assignRoleToUserForADay = require('./Utility/AssignRoleToUserForADay');
35+
36+
var userId = '12345';
37+
var roleId = 'admin';
38+
39+
assignRoleToUserForADay(userId, roleId)
40+
.then(() => {
41+
console.log('Role assigned successfully.');
42+
})
43+
.catch((error) => {
44+
console.error('Error assigning role:', error);
45+
});
46+
```

0 commit comments

Comments
 (0)