Disable record's creation

Hello everybody !

I have a really urgent problem to solve :

How can i disable the "creation" of new records (in standdard modules like Accounts, but in custom too) for few users?

I was thinking about roles, but there is nothing on the role management.
I would like to disable the creation of new Accounts for the role "lambda user".

of course, we have to take care of disable "duplication" too.

By disable, i mean, disallowed access to the button "create" and "duplicate" and to disallowed the action.

Do you know how to disable the action of creation/duplication and how to hide the "create"/"duplicate" buttons ?

Thanks a lot !

ps : Im on Sugar 7.5.1 Pro
  • I would add it to JSGroupings so that it gets downloaded and run after user has logged in and not in Record.js, since your code would only run once Record view is loaded.

    See example below (though I haven't tested it for myself)

    http://support.sugarcrm.com/02_Documentation/04_Sugar_Developer/Sugar_Developer_Guide_7.6/50_Extensi...
    /*** 
    * Included via JSGroupings Extension
    */
    (function(app) {
        app.events.on("app:sync:complete", function(){
            var acls = app.user.getAcls();
            acls.Accounts.create = 'no';
            app.user.set("acls", acls);
        });
    })(SUGAR.App);

    App Ecosystem @ SugarCRM

  • Does JSGroupings exist only from 7.6 ? Because my client is still on 7.5.1 and want to build the whole application on this version ...

    Thanks for your help anyway @Matthew Marum that is a good track to test :)

  • JSGroupings extension point has existed for a long time.  All 7.x versions and in Sugar 6.5.x at least.

    App Ecosystem @ SugarCRM

  • Thanks for the help, but i'm not able to make it work, i'm not sure i'm doing it well. 

    First of all, is there any knowledge base about sugar X backbone ?

    And secondly, you responded how to disable the create action to a user, but not how to know WHICH roles are defined for this user... could you give me a hint, please?

    Thanks a lot for your help Matthew Marum !
  • Gaelle,

    Try checking app.user.attributes.roles for the appropriate role. You can then only execute Matthew's code if the role you specify is found.

    I hope that helps!

    -Alan
  • Combining what Matthew mentioned above and the app.user.attributes.roles array, here is how I got it working on Accounts for the Sales Administrator role.

    ./custom/Extension/application/Ext/JSGroupings/acl_hide_create.php
    <?php foreach ($js_groupings as $key => $groupings) {     foreach  ($groupings as $file => $target) {         if ($target == 'include/javascript/sugar_grp7.min.js') {             $js_groupings[$key]['custom/javascript/acl_hide_create.js'] = 'include/javascript/sugar_grp7.min.js';         }         break;     } }
    ./custom/javascript/acl_hide_create.js
    (function(app) {     app.events.on("app:sync:complete", function(){         var roles = app.user.attributes.roles;         for(var i = 0; i < roles.length; i++) {           if(roles[i] == 'Sales Administrator') {             var acls = app.user.getAcls();             acls.Accounts.create = 'no';             app.user.set("acls", acls);             break;           }         }     }); })(SUGAR.App);
    Follow this with a Quick Repair and it should work!
  • Hi GaelleFernandez,

    Please try the code Alan Beam and Matthew Marum has given.
    Then navigate to admin and do
    ->Repair and Rebuild
    ->Rebuild GS Groupings
    ->Repair and Rebuild

    If you ask me a knowledge base for sugar X backbone, I will suggest you to read the component.js files we are having in cache/javascript/base and jsgroupings files we have in cache/include/javascript.

    You can get a lot of stuffs and idea about Sugar sidecar framework if you have a look into these files

    Thanks!

  • Okay, i've tested your code, and it won't work for me. The JSGrouping is working, in fact, but not the search for the role.

    There is no App.user.attributes.roles object, for any user.
    In the attributes, i can have :

    _hash,acl,address_city/country/postalcode/state/street, full_name, id, is_manager, is_password_expired, is_top_level_manager, module_list, my_teams, password_expired_message, picture, preferences, reports_to_id, reports_to_name, show_wizard, user_name and _proto_ 


    After a little digging, i have App.user.attributes.acl.ACLRoles.admin = 'no' for standard users.
    But i think this is not their role, only they are not defined as Administrator in Admin > Users.

    It could do the trick (do you agree?), i'm still trying to figure out how to use this information.
  • Hi GaelleFernandez,

    I too couldn't find the list of roles for a user in app.user object.Here is another approach you can use

    create a custom entrypoint userRole to get the current user roles
    create a file custom/clients/base/api/getCurrentUserRoleApi.php and add the below code.
    <?php if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
             class getCurrentUserRoleApi extends SugarApi{
                   public function registerApiRest(){
                       return array(
                              'userRole' => array(
                              'reqType' => 'GET',
                              'path' => array('ins_ins_courses','userRole'),
                              'pathVars' => array(),
                              'method' => 'getCurrentUserRole',
                              'shortHelp' => 'Custom Api to get the current user role',
                              'longHelp' => '',
                      ),
                   );
              } 
              public function getCurrentUserRole($api,$args){
                    global $current_user;global $db;
                   $query = "SELECT acl_roles.id ".
                    "FROM acl_roles ".
                    "INNER JOIN acl_roles_users ON acl_roles_users.user_id = '".$current_user->id."' ".
                        "AND acl_roles_users.role_id = acl_roles.id AND acl_roles_users.deleted = 0 ".
                    "WHERE acl_roles.deleted=0 ";

                $result = $db->query($query);
                $user_roles=array();
       while($row = $db->fetchByAssoc($result) ){
                    $user_roles[] = $row['id'];
                }
                 return $user_roles;
              }
    }

    This entrypoint will return the current user's role ids.(name can be edited in future, so its good to compare with sugar db id for the role).
    Now you can call this api in GS grouping extension.

    (function(app) {
        app.events.on("app:sync:complete", function(){
              app.api.call('GET', app.api.buildURL('ins_ins_courses/userRole'), null, {
                               success: function(userRoleArray) {
                                      if (_.indexOf(userRoleArray, "<your roles sugar id>") > -1) {
                                          var acls = app.user.getAcls();
                                          acls.Accounts.create = 'no'; 
                                         app.user.set("acls", acls); isCounsellor = true;
                                     }
                             },
                });
       });
    })(SUGAR.App);  Hope it helps!  Thanks:)