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
Parents
  • Hi GaelleFernandez

    You can set access for Edit to none in roles management for Accounts module for this particular role.
    Then users come under that role won't be able to edit,create or duplicate an account

    Thanks!
  • Creation of records is governed by the "Edit" ACL on backend.  So you need to do some customizations in order to disable Create actions but not Edit actions.  However, on the front end, we've already started adding some support for a "Create" ACL.  You can modify the User's ACLs object to utilize this.  Or you could override the /me API endpoint that returns the User's ACLs to include additional "create" permissions for any "lambda user".

    Try this in your browser.
    var acls = SUGAR.App.user.getAcls();
    acls.Accounts.create = 'no';
    SUGAR.App.user.set("acls", acls);
    And then navigate away and then back to Accounts module to force a re-render.  The "Create" button at the top will disappear and if you navigate into an Account record you will find the "Copy" or "Duplicate" buttons no longer appear in action dropdown.

    Of course, you can always just manipulate DOM directly.  For example, if you could add CSS "disabled" class to these buttons which will disable them visually and then remove any jQuery event listeners that might be on these elements to prevent actions from being triggered.  Or just remove these elements all together.

    App Ecosystem @ SugarCRM

  • PROBLEM SOLVED !

    After a big while, and with the huge Help, Time, Knowledges and Patience of Alan BeamShijin Krishna and Matthew Marum, I got it running !
    So, because they were so kind to me, and because i know someone will someday face this situation, i put here the code wrote !

    How to write the custom API to retrieve current user roles :
    (be carefull, your file must have the same name as you class)
    in <sugar>/custom/clients/base/api/getCurrentUserRoleApi.php

    <?phpif(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class getCurrentUserRoleApi extends SugarApi{
    public function registerApiRest(){
      return array(
       'userRole' => array(
        'reqType' => 'GET',
        'path' => array('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;
    }
    }
      
    Then, add your js, which will call the api and act in return :
    (be careful, every app should be in minus letters, but the last one, SUGAR.App has to be given a capital !)
    (be careful, <YourRoleID> must be replaced by your chosen role id)
    in <sugar>/custom/javascript/acl_hide_create.js

    (function(app) {
        app.events.on("app:sync:complete", function(){
    /*call to the custom api to disallow create for Standard Users.*/
      app.api.call('GET', app.api.buildURL('userRole'), null, { 
       success: function(userRoleArray) {
        if (_.indexOf(userRoleArray,"<YourRoleID>") > -1) {
         console.log(userRoleArray);
         var acls = app.user.getAcls();
         acls.Accounts.create = 'no'; 
         app.user.set("acls", acls);
        }else{
         console.log("not inarray");
        }
       },
      });
        });
    })(SUGAR.App);

    Finally, to add your JS t the JSGroupings, 
    (this one adds your js to the sugr7.min.js)
    (be careful : your file.php has to be named the same as your precedent js file)
    under <sugar>/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;
        }
    }

    After that, do a QRR (quick repair and rebuild) and a repair the Grouping JS
    (go to admin > repair  and you'll see the first item and the 13th)


    So, this is brought to you by the unification of the forces and knowledges of all 3 great men :  Alan BeamShijin Krishna and Matthew Marum !

    Many big thanks to them !!


Reply
  • PROBLEM SOLVED !

    After a big while, and with the huge Help, Time, Knowledges and Patience of Alan BeamShijin Krishna and Matthew Marum, I got it running !
    So, because they were so kind to me, and because i know someone will someday face this situation, i put here the code wrote !

    How to write the custom API to retrieve current user roles :
    (be carefull, your file must have the same name as you class)
    in <sugar>/custom/clients/base/api/getCurrentUserRoleApi.php

    <?phpif(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
    class getCurrentUserRoleApi extends SugarApi{
    public function registerApiRest(){
      return array(
       'userRole' => array(
        'reqType' => 'GET',
        'path' => array('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;
    }
    }
      
    Then, add your js, which will call the api and act in return :
    (be careful, every app should be in minus letters, but the last one, SUGAR.App has to be given a capital !)
    (be careful, <YourRoleID> must be replaced by your chosen role id)
    in <sugar>/custom/javascript/acl_hide_create.js

    (function(app) {
        app.events.on("app:sync:complete", function(){
    /*call to the custom api to disallow create for Standard Users.*/
      app.api.call('GET', app.api.buildURL('userRole'), null, { 
       success: function(userRoleArray) {
        if (_.indexOf(userRoleArray,"<YourRoleID>") > -1) {
         console.log(userRoleArray);
         var acls = app.user.getAcls();
         acls.Accounts.create = 'no'; 
         app.user.set("acls", acls);
        }else{
         console.log("not inarray");
        }
       },
      });
        });
    })(SUGAR.App);

    Finally, to add your JS t the JSGroupings, 
    (this one adds your js to the sugr7.min.js)
    (be careful : your file.php has to be named the same as your precedent js file)
    under <sugar>/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;
        }
    }

    After that, do a QRR (quick repair and rebuild) and a repair the Grouping JS
    (go to admin > repair  and you'll see the first item and the 13th)


    So, this is brought to you by the unification of the forces and knowledges of all 3 great men :  Alan BeamShijin Krishna and Matthew Marum !

    Many big thanks to them !!


Children
No Data