via fix script
Users
– 10 in total |
|
||||||||||||||||||||||
Groups
– 2 in total |
TestGrp1 TestGrp2 |
||||||||||||||||||||||
Group
memberships |
All above users added to each group |
||||||||||||||||||||||
Group
roles – 2 in total |
itil agent_workspace_user |
||||||||||||||||||||||
Stand
alone user roles – 1 in total |
Catalog (added to all above users) |
||||||||||||||||||||||
Last
login default |
'2020-01-01'; |
||||||||||||||||||||||
Last
login time default |
'2020-01-01
15:47:31'; |
Fix script name “set up test users"
To be run once only
Script code:
var lastLogin = '2020-01-01';
var lastLoginTime = '2020-01-01 15:47:31';
setUpUsers(lastLogin, lastLoginTime);
setUpGroups();
AddRolesToGroups();
AddStandaloneRolesToUsers();
AddUsersToGroups();
function setUpUsers(lastLogin, lastLoginTime) {
setUpUser('test', 'TEST1', lastLogin, lastLoginTime);
setUpUser('test', 'TEST2', lastLogin, lastLoginTime);
setUpUser('test', 'TEST3', lastLogin, lastLoginTime);
setUpUser('test', 'TEST4', lastLogin, lastLoginTime);
setUpUser('test', 'TEST5', lastLogin, lastLoginTime);
setUpUser('test', 'TEST6', lastLogin, lastLoginTime);
setUpUser('test', 'TEST7', lastLogin, lastLoginTime);
setUpUser('test', 'TEST8', lastLogin, lastLoginTime);
setUpUser('test', 'TEST9', lastLogin, lastLoginTime);
setUpUser('test', 'TEST10', lastLogin, lastLoginTime);
}
function setUpUser(firstName, lastName, lastLogin, lastLoginTime) {
var grUser = new GlideRecord('sys_user');
grUser.newRecord();
grUser.first_name = firstName;
grUser.last_name = lastName;
grUser.last_login_time = lastLoginTime;
grUser.last_login = lastLogin;
var combinedNames = firstName + '_' + lastName;
grUser.user_name = combinedNames.toLowerCase();
grUser.insert();
}
function setUpGroups() {
setUpGroup('TestGrp1');
setUpGroup('TestGrp2');
}
function setUpGroup(groupName) {
var grGroup = new GlideRecord('sys_user_group');
grGroup.newRecord();
grGroup.name = groupName;
grGroup.insert();
}
function AddRolesToGroups() {
AddRoleToGroup('TestGrp1', 'itil');
AddRoleToGroup('TestGrp1', 'agent_workspace_user');
AddRoleToGroup('TestGrp2', 'itil');
AddRoleToGroup('TestGrp2', 'agent_workspace_user');
}
function AddRoleToGroup(groupName, groupRole) {
var grGrpRole = new GlideRecord('sys_group_has_role');
grGrpRole.newRecord();
grGrpRole.setDisplayValue('group', groupName);
grGrpRole.setDisplayValue('role', groupRole);
grGrpRole.insert();
}
function AddStandaloneRolesToUsers() {
var grUsers = new GlideRecord('sys_user');
var sQuery = 'last_nameTEST';
grUsers.addQuery(sQuery);
grUsers.query();
while (grUsers.next()) {
AddStandaloneRoleToUser(grUsers.sys_id, 'catalog');
AddStandaloneRoleToUser(grUsers.sys_id, 'catalog');
}
}
function AddStandaloneRoleToUser(userSysID, roleName) {
var grUserRole = new GlideRecord('sys_user_has_role');
grUserRole.newRecord();
grUserRole.user = userSysID;
grUserRole.setDisplayValue('role', roleName);
grUserRole.insert();
}
function AddUsersToGroups() {
var grUsers = new GlideRecord('sys_user');
var sQuery = 'last_nameLIKETEST';
grUsers.addQuery(sQuery);
grUsers.query();
while (grUsers.next()) {
AddUserToGroup(grUsers.sys_id, 'TestGrp1');
AddUserToGroup(grUsers.sys_id, 'TestGrp2');
}
}
function AddUserToGroup(userSysID, groupName) {
var grGrpRole = new GlideRecord('sys_user_grmember');
grGrpRole.newRecord();
grGrpRole.user = userSysID;
grGrpRole.setDisplayValue('group', groupName);
grGrpRole.insert();
}
Comments
Post a Comment