问题描述
有什么方法可以吸引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.
预先感谢
推荐答案
如果仔细看一下magento如何存储管理员角色和用户,您会更好地理解这一点.
If you look carefully at how magento stores admin roles and users, you would understand this better.
假设您创建角色staff
,则magento将该角色存储在admin_role
表中.创建新用户时,用户数据存储在admin_user
表中,该表与admin_role
表完全没有关联.但是,当您为该用户分配staff
角色时,此分配本身会再次创建一个新的管理员角色.本质上,用户本身被视为管理员角色.
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.
这应该很好地工作:
$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;
希望有帮助.
这篇关于获取在magento中具有特定角色的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!