Inactivity remove group membership
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
4 hours ago
Hi
I have the below script which should issue notifications when days inactive are reached however it dose not seem to work.
I cant see anything wrong but I am not JavaScripting Expert background in programming and SQL but not JavaScript any help would be appricated.
var UserInactivityManager = Class.create();
UserInactivityManager.prototype = {
initialize: function() {
this.inactivityThresholds = [30, 45, 59];
},
processUsers: function() {
var today = new GlideDateTime();
var userGR = new GlideRecord('sys_user');
userGR.addEncodedQuery('active=true^last_loginISNOTEMPTY');
userGR.query();
while (userGR.next()) {
var lastLogin = new GlideDateTime(userGR.getValue('last_login'));
var duration = GlideDateTime.subtract(today, lastLogin);
var daysInactive = parseInt(duration.getDayPart(), 10);
gs.info(
'[UserInactivityManager] ' +
userGR.user_name +
' inactive for ' +
daysInactive +
' days'
);
if (daysInactive >= 60) {
this.revokeAccess(userGR);
} else if (this.inactivityThresholds.indexOf(daysInactive) !== -1) {
this.sendNotification(userGR, daysInactive);
}
}
},
sendNotification: function(userGR, days) {
gs.eventQueue(
'user.inactivity.notification',
userGR,
days.toString(),
''
);
gs.info(
'[UserInactivityManager] Notification queued for ' +
userGR.user_name +
' (' + days + ' days)'
);
},
revokeAccess: function(userGR) {
gs.info(
'[UserInactivityManager] Revoking access for ' +
userGR.user_name
);
// Remove Roles
var roles = new GlideRecord('sys_user_has_role');
roles.addQuery('user', userGR.getUniqueValue());
roles.query();
while (roles.next()) {
roles.deleteRecord();
}
// Remove Group Memberships
var groups = new GlideRecord('sys_user_grmember');
groups.addQuery('user', userGR.getUniqueValue());
groups.query();
while (groups.next()) {
groups.deleteRecord();
}
this.sendFinalNotification(userGR);
// Optional
// userGR.setValue('active', false);
// userGR.update();
},
sendFinalNotification: function(userGR) {
gs.eventQueue(
'user.access.revoked',
userGR,
'',
''
);
gs.info(
'[UserInactivityManager] Access revoked notification queued for ' +
userGR.user_name
);
},
type: 'UserInactivityManager'
};
1 REPLY 1
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi,
Have you added log statements to see where your script is failing?
Additional, have you considered adding your date thresholds into your GlideRecord query? That would save needing to evaluate every user record and instead have the database do the filtering which is vastly more performative