Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save zachelrath/6783274 to your computer and use it in GitHub Desktop.
Save zachelrath/6783274 to your computer and use it in GitHub Desktop.
Skuid Snippets for adding/removing PermissionSetAssignments based on the value of a field in Custom Setting record(s)
// Row Action snippet
skuid.snippet.registerSnippet('SyncPermSets_RowAction',function(){
skuid.snippet.getSnippet('UpdatePermissionSetAssignmentsForUsers')({
rows:[arguments[0].item.row]
});
});
// Mass Action snippet
skuid.snippet.registerSnippet('SyncPermSets_MassAction',function(){
var rows = [];
skuid.$.each(arguments[0].list.getSelectedItems(),function(){
rows.push(this.row);
});
skuid.snippet.getSnippet('UpdatePermissionSetAssignmentsForUsers')({
rows:rows
});
});
//
// Utility snippet --- the meat of the operation.
// Called by both of the above Snippets.
//
skuid.snippet.registerSnippet('UpdatePermissionSetAssignmentsForUsers',function(){
var customSettingRows = arguments[0].rows,
$ = skuid.$,
Assignments = skuid.model.getModel('PermissionSetAssignments'),
PermissionSets = skuid.model.getModel('PermissionSets'),
Users = skuid.model.getModel('Users'),
permSetsByName = {};
if (PermissionSets && PermissionSets.data && PermissionSets.data.length){
$.each(PermissionSets.data,function(){
permSetsByName[this.Name]=this;
});
}
// We want to take whatever's in the Accessible Components field
// and use this to assign the specified user the corresponding Permission Sets.
var permSetNamesByComponent = {
'College Counseling':'Education',
'Education':'Education',
'Mental Health':'Mental_Health',
'Full Medical & Dental Care':'Full_Medical_and_Dental_Care',
'Lifetime Individual Sports':'Lifetime_Individual_Sports',
'Self Expression':'Self_Expression',
'Job Club':'Job_Club',
'FLSE':'FLSE',
'Community Organizer Comments':'Community_Organizer',
'Community Organizer Outreach':'Community_Organizer',
'Program Coordinator Comments':'Program_Director'
};
// Build a unique Perm Set Names list
var uniquePermSetNames = {};
$.each(permSetNamesByComponent,function(k,v){uniquePermSetNames[v]=1;});
$.each(customSettingRows,function(i,cs){
// console.log('got a Cs row');
// First, make sure that this is a user-level custom setting
if (cs.SetupOwnerId.substring(0,3)!=='005') return true;
//console.log('got a user record');
// Get the corresponding User record.
var userRecord = Users.getRowById(cs.SetupOwnerId);
// Get the list of Perm Set Assignments
var existingAssignments = userRecord.PermissionSetAssignments
? userRecord.PermissionSetAssignments.records
: [];
if (existingAssignments == null) existingAssignments = [];
var permSetsUserDoesHave = {};
$.each(existingAssignments,function(){
permSetsUserDoesHave[this.PermissionSet.Name]=this;
});
// Split the user's new Accessible Components array into an Array of Accessible Components
var newComponents = (cs.Accessible_Components__c || '').split(','),
permSetsUserShouldHave = {};
$.each(newComponents,function(){
// If the user already has this
permSetsUserShouldHave[permSetNamesByComponent[this]]=1;
});
// Loop over all Perm Set possibilities.
$.each(uniquePermSetNames,function(permSetName,v){
// SHOULD the user have this Perm Set?
var userShouldHaveIt = (permSetName in permSetsUserShouldHave);
// DOES the user currently have it?
var userDoesHaveIt = (permSetName in permSetsUserDoesHave);
// Case 1: User SHOULD Have it, and DOES Have it.
// ----> DO NOTHING
// Case 2: User SHOULD NOT Have it, and DOES NOT Have it.
// ----> DO NOTHING
// Case 3: User SHOULD Have it, and DOES NOT Have it.
// ----> Create a new row in the PermissionSetAssignments model
if (userShouldHaveIt && !userDoesHaveIt) {
// console.log('adding Perm Set: ' + permSetName + ', for ' + userRecord.Name);
Assignments.createRow({
additionalConditions: [
{field:'AssigneeId',operator:'=',value:userRecord.Id},
{field:'PermissionSetId',operator:'=',value:permSetsByName[permSetName].Id}
]
});
} else if (!userShouldHaveIt && userDoesHaveIt) {
// console.log('got Perm Set to DELETE: ' + permSetName + ', for ' + userRecord.Name);
// Case 4: User SHOULD NOT Have it, and DOES Have it.
// -----> Create a new row to the PermissionSetAssignments model,
// corresponding to this existing record,
// and flag it for DELETION.
var psaRowToDelete = {Id: permSetsUserDoesHave[permSetName].Id};
Assignments.adoptRow(psaRowToDelete);
Assignments.deleteRow(psaRowToDelete);
}
});
});
// IF stuff has changed, commit all changes, for all selected users, to the database.
if (Assignments.hasChanged){
var body = $('body');
body.block({
message: 'Saving changes...'
});
Assignments.save({callback:function(result){
if (result.totalsuccess){
body.block({
message: 'All changes saved successfully! Now refreshing User data...'
});
Users.updateData(function(){
body.unblock();
});
} else {
body.block({
message: 'There was an error saving changes. Please try again.',
timeout: 2000
});
console.log(result);
}
}});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment