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
  • app.user.get('roles') is working in Sugar 7.6.0.0

    Thanks Alan!
  • Hi GaelleFernandez,

    First of all I am wondering why do you want to put quotes for 0, because the deleted field is of type integer not character!!
    Now to make it work.Could you please try writing the rolesArray api returning to browser console?

    (function(app) {
        app.events.on("app:sync:complete", function(){
              app.api.call('GET', app.api.buildURL('ins_ins_courses/userRole'), null, { 
                               success: function(userRoleArray) {
                                     console.log(userRoleArray);
                             },
                });
       });

    And please check whether you are able to get the roles for the logged in user.Let me know.

    Thanks!
  • Hello again Shijin,

    And thanks again for your time :)

    I know it is strange to put quotes to an integer in the query, but i can assure you : i've tested your query with and without quotes, and it gives me roles'id when i put quotes to the 0.

    My browser console returned me a "app is undefined", so, replacing every app by App, i've got your code like this :

    (function(App) {  
        App.events.on("App:sync:complete", function(){
            App.api.call('GET', App.api.buildURL('ins_ins_courses/userRole'), null, {
               success: function(userRoleArray) {
                    console.log(_.indexOf(userRoleArray,"1ffe6d3f-897e-8a2d-1b00-558969e06287") );
                     if (_.indexOf(userRoleArray,"1ffe6d3f-897e-8a2d-1b00-558969e06287") > -1) {
                         console.log(userRoleArray);
                         var acls = App.user.getAcls();
                         acls.Accounts.create = 'no'; 
                         App.user.set("acls", acls);
                         isCounsellor = true;
                     }else{
                         console.log("not inarray");
                     }
                 },
            });
        });
    })(SUGAR.App);

    But it gives me nothing. absolutely nothing in the console...
    I can't see the id for the current user's role...

    Do we have to replace App by SUGAR.App ?
    (and what is isCounsellor ?)

    Thanks a lot to help me, the newbie who does not understand lots of things (and sorry for that), and thank you for your patience !

  • Hello again Shijin,

    And thanks again for your time :)

    I know it is strange to put quotes to an integer in the query, but i can assure you : i've tested your query with and without quotes, and it gives me roles'id when i put quotes to the 0.

    My browser console returned me a "app is undefined", so, replacing every app by App, i've got your code like this :

    (function(App) {  
        App.events.on("App:sync:complete", function(){
            App.api.call('GET', App.api.buildURL('ins_ins_courses/userRole'), null, {
               success: function(userRoleArray) {
                    console.log(_.indexOf(userRoleArray,"1ffe6d3f-897e-8a2d-1b00-558969e06287") );
                     if (_.indexOf(userRoleArray,"1ffe6d3f-897e-8a2d-1b00-558969e06287") > -1) {
                         console.log(userRoleArray);
                         var acls = App.user.getAcls();
                         acls.Accounts.create = 'no'; 
                         App.user.set("acls", acls);
                         isCounsellor = true;
                     }else{
                         console.log("not inarray");
                     }
                 },
            });
        });
    })(SUGAR.App);

    But it gives me nothing. absolutely nothing in the console...
    I can't see the id for the current user's role...

    Do we have to replace App by SUGAR.App ?
    (and what is isCounsellor ?)

    Thanks a lot to help me, the newbie who does not understand lots of things (and sorry for that), and thank you for your patience !

  • Try changing this line:
    app.user.set("acl", acls);
    Note the change from "acls" to "acl". This fixed the issue for me. However, I also noticed that when you use the API call, the delay to make that call results in the Create button being rendered before this code is run. If the user then clicks on Create, they'll get an error message and subsequent loads of the List View will omit the Create button. Reloading the page will start that process over. Just something worth keeping in mind.
  • Hi,

    You can omit isCounsellor, which is just one variable I was using in my code.
    I hope you have created the api file in custom/clients/base/api, then the api url you should change like this
    app.api.call('GET', app.api.buildURL('userRole'), 

    then endpoint should be
    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' => '',
                  ),
                );
            } 

    if you are keeping api file in custom/modules/Accounts/clients/base/api you can use the url like below
    app.api.call('GET', app.api.buildURL('Accounts/userRole'), 
    then end point should be
    public function registerApiRest(){
                    return array(
                          'userRole' => array(
                          'reqType' => 'GET',
                          'path' => array('Accounts','userRole'),
                          'pathVars' => array(),
                          'method' => 'getCurrentUserRole',
                          'shortHelp' => 'Custom Api to get the current user role',
                          'longHelp' => '',
                  ),
                );
            } 

    ins_ins_courses was my module name!! Sorry for that......:)
  • HI Shijin !

    Again, thanks a lot, i think yesterday i wasn't fully aware.
    So, i've erased every reference to your module in my code. My custom Api is set under custom/clients/base/api.

    But i still can't get my user role.
    I've added a little 
    $GLOBALS['log']->error('User roles :'.$user_roles);
    in my getCurrentUserRoleApi.php before the return $user_roles, but my log (set in Error Level) does not show anything.

    The api is well included and interprted as an api, because when testing in http://<sugar>/rest/v10/help , i have the GET userRole.

    I've tested the url : http://<sugar>/rest/v10/userRole , and it returned an error message : 
    {"error":"need_login","error_message":"No valid authentification for user."}

    So maybe i should add a curl call before, to add the credentials?

    i'm browsing the internet to try to understand what is wrong, but that is kind of tough, because my knowledges in Webservices and api calls are limitfull ...

    So thanks again to share your knowledges ! :)
  • Hey, you don't need to do authentication explicitly as you are calling the api inside suger.This error you are getting because you are trying to access directly in browser.

    Please paste you are api file code here.

    Thanks!
  • So, here is my <sugar>/custom/clients/base/api/getCurrentUserRoleApi.php :

    <?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('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'];
            }
           $GLOBALS['log']->error('User roles :'.$user_roles);
            return $user_roles;
        }
    }

    Thanks again for the time you take Shijin !

  • Hi GaelleFernandez,

    I couldn't find any issue with the API, everything seems to be proper......:(