Skip to content

Instantly share code, notes, and snippets.

@elchele
Last active June 20, 2016 15:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elchele/9925867 to your computer and use it in GitHub Desktop.
Save elchele/9925867 to your computer and use it in GitHub Desktop.
Sidecar: Retrieving User Roles
/* Author: Angel Magaña - cheleguanaco@cheleguanaco.com
*
* This Gist can be used within a Sugar 7 Controller to retrieve a user's
* security Roles
*
*/
/* To retrieve the Roles, we create a User object then
* retrieve the User's related Role records via the default Users <--> Roles relationship
*/
var user = app.data.createBean('Users', {id: app.user.id}); //the JSON object contains the ID of the record to load
user.fetch(); //this retrieves the record from the Users module with the ID specified in the JSON object
//this retrieves the related records for the previously loaded User record, using the 'link' name (from vardefs)
var user_roles = user.getRelatedCollection('aclroles');
user_roles.fetch({relate:true});
// At this point, user_roles.models represents all the Role records associated with the given user, in this case, the current user
@matthewpoer
Copy link

If you just need the names of the roles for the current user, that's stored in-session at app.user.attributes.roles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment