首页 > 解决方案 > 如何在 Perfex CRM 中隐藏用户角色的数据?

问题描述

我需要一个隐藏 Perfex CRM for User Roles 某些部分的功能。示例:折扣区。

我试过这段代码:

function hide_fields_nonusers()
{
    if ($role != '4') {    
        <style type="text/css">
            #sconto {
                display: none !important;
            }
            #discount_area {display: none !important;}
        </style>
 }

但是没有任何效果,它为每个人隐藏了那部分,包括管理员。

编辑

这个正在工作,但只是为了通过 ID 获取员工角色。

/**
     * Get employee role by id
     * @param  mixed $id Optional role id
     * @return mixed     array if not id passed else object
     */
    public function get($id = '')
    {
        if (is_numeric($id)) {

            $role = $this->app_object_cache->get('role-' . $id);

            if ($role) {
                return $role;
            }

            $this->db->where('roleid', $id);

            $role              = $this->db->get(db_prefix() . 'roles')->row();
            $role->permissions = !empty($role->permissions) ? unserialize($role->permissions) : [];

            $this->app_object_cache->add('role-' . $id, $role);

            return $role;
        }

        return $this->db->get(db_prefix() . 'roles')->result_array();
    }

标签: javascriptphpcodeigniter

解决方案


它可能hide_fields_nonusers()会产生 PHP 错误,因为此函数不是完全有效的 PHP 代码。

要回答您的问题 - 根据您提供的内容 - 如果您在$role不等于 4 时将样式标签作为 PHP 代码回显,这应该可以工作。

注意: 如果您没有收到任何错误消息,则表明该函数根本没有执行。正如 Marcel 评论的那样,如果您可以发布您调用此函数的位置,我们可以看得更近一些。

PHP

public function hide_fields_nonusers()
{
  
    if ($role != '4') {    
        echo '<style type="text/css">
              #sconto { display:none !important; }
              #discount_area { display:none !important; }
              </style>';
    }
}

或者对于 JavaScript

if ($role != 4) {
    var css   = '#sconto { display:none !important; }
                 #discount_area { display:none !important; }',
        head  = document.head || document.getElementsByTagName('head')[0],
        style = document.createElement('style');

    head.appendChild(style);

    style.type = 'text/css';
    if (style.styleSheet) {
       // This is required for IE8 and below.
       style.styleSheet.cssText = css;
    } else {
       style.appendChild(document.createTextNode(css));
    }
}

推荐阅读