With that in mind, lets talk about a recent situation I came across that very much speaks to this point. In short, in the course of talking about the 7.x technology, the question arose of how one would go about retrieving a list of Roles assigned to a given user. Because we have to do this through a JavaScript based controller, techniques we might have used in the past are no longer applicable.
For the purposes of our example we will assume that we wish to retrieve the list of Roles assigned to the currently logged in user, in turn allowing us to see some details about the manner in which we interact with the current user, also a common need and one that also differs greatly.
The following Gist will allow us to retrieve the Roles:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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 |
NOTE: Roles are only accessible to Admin level users and as such, the above code would only function if executed by a user with Admin privileges. In turn, it would also be best to specify a particular user ID rather than relying on the current user.
Hola Angel, sabe si existe alguna manera de gestionar en Roles las zolapas del ZuckerReports 2.1?
ReplyDeleteNo, lo siento pero no sé.
DeleteCan we include this logic into a SugarLogic Expression in the javascript function?
ReplyDeleteisMemberOfRole would be the new SugarLogic Expression
Hola Ángel hay una manera de restringir una vista personalizada desde roles, o bien obtener los roles de un usuario, no importando si no es administrador, solamente los roles de ese usuario?
ReplyDelete