douzhi6365 2016-03-10 19:06
浏览 13
已采纳

获取magento中特定角色的用户

Is there any way to get users of a particular role (say staff) in Magento? I tried with this

$roles_users = Mage::getResourceModel('admin/roles_user_collection');

But dont know how to add filter for a particular role.

thanks in advance

  • 写回答

2条回答 默认 最新

  • doumu2172 2016-03-10 20:19
    关注

    If you look carefully at how magento stores admin roles and users, you would understand this better.

    Say you create a role staff, magento stores this role in the admin_role table. When you create a new user, the user data is stored in the admin_user table, which has no association at all to the admin_role table. But, when you assign this user the role of staff, this assignment creates a new admin role in itself again. Essentially, the user itself is treated as an admin role.

    This should work perfectly:

    $output = []; // just an array to hold all the users, you may not need this
    
    // instance of the admin_role
    $model = Mage::getModel('admin/role');
    
    // fetch all roles with name of 'Staff', but get only the first item since two roles cannot have same name
    $role = $model->getCollection()
        ->addFieldToFilter('role_name', ['eq' => 'Staff'])
        ->getFirstItem();
    
    // check to make sure the role exists
    if ($roleId = $role->getId())
    {
        // get a collection of all the user roles having the Staff role id as a parent_id
        $staffUsers = $model->getCollection()
            ->addFieldToFilter('parent_id', ['eq' => $roleId]);
    
        // ensure the collection has size
        if ($staffUsers->getSize())
        {
            // loop through each object and get the user_id values
            foreach ($staffUsers as $staffUser)
            {
                // you can still check to make sure the user_id field is not null
                if ($staffUser->getUserId())
                {
                    // get the user object and do anything with it
                    $user = Mage::getModel('admin/user')->load($staffUser->getUserId());
                    $output[$user->getId()] = $user->getFirstname() . " " . $user->getLastname();
                }
            }
        }
    }
    
    
    var_dump($output); die;
    

    Hope it helps.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法