role management for custom record action

Hi,

I created a custom action for a custom module, but i need restrict the access to this action if the current user is in a specific role. How can i do that?

<?php
array (
  'type' => 'actiondropdown',
  'name' => 'main_dropdown',
  'primary' => true,
  'showOn' => 'view',
  'buttons' => 
    array (
      0 => 
      array (
        'type' => 'rowaction',
        'event' => 'button:edit_button:click',
        'name' => 'edit_button',
        'label' => 'LBL_EDIT_BUTTON_LABEL',
        'acl_action' => 'edit',
      ),
      1 => 
      array (
        'type' => 'rowaction',
        'event' => 'button:custom_action:click',
        'name' => 'custom_action',
        'label' => 'LBL_CUSTOM_ACTION',
        'acl_action' => 'view',
      ),
      2 => 
      array (
        'type' => 'pdfaction',
        'name' => 'download-pdf',
        'label' => 'LBL_PDF_VIEW',
        'action' => 'download',
        'acl_action' => 'view',
      ),
?>

Thanks for the help

  • Hi,

    You can create sugar API to get user role.

    <?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;
        }
    }

    After getting current user's role you can apply condition according your requirements.

    Regards!

  • Thanks for reply,

    After create the sugar api, how i can restrict the access to the custom action?

  • Hi Oscar Castro,

    You can try this in your record.js

    initialize:function(options){

            app.view.invokeParent(this, {type: 'view', name: 'record', method: 'initialize', args:[options]});

            this.on('render',this.disableFields,this);

            this.model.on('render', this.disableFields, this);

    },

    disableFields: function(){

            var roles = app.user.get('roles');

            for (var i = 0; i < roles.length; i++) {

                if (roles[i] == 'Agent'){ //replace with your specific role

                    $('[name=edit_button]').addClass('disabled');

                   //add fields you want to disable through jquery

                }

            }

        },

    Regards,

    Roland

  • Hi Roland Cadavos,

    I'm facing problem in mobile. Becouse js file is not work in mobile applications. #sugarCRM 8.3

  • Hi Juned Rawoot,

    Which area of the mobile app requires restriction? Have you created a custom action button in mobile app? Or you want to have restriction on default actions.

    For default action restriction, we can make use of Role Management to restrict Edit, View, Delete actions based on the permissions granted to each user. We can also restrict access at fields level.

    For custom action, if you have added it by extending the apk files, then you can use your custom js before building back the apk file.

    Let us know what you are trying to achieve in mobile which is already accomplished in web.

    Regards.