【问题标题】:How to assign role to admin user Programmatically in Magento?如何在 Magento 中以编程方式将角色分配给管理员用户?
【发布时间】:2013-07-24 05:15:03
【问题描述】:

我需要在 Magento 中以编程方式将用户角色分配给管理员用户。我怎样才能?我尝试了一些方法,例如,

try {
$user->setRoleIds(array(<role_id>))
->setRoleUserId($user->getUserId())
->saveRelations();

} catch (Exception $e) {
echo $e->getMessage();
exit;
}

但是当我们应用上述代码时,数据库会损坏并出现错误 - "Parent Role id 'G5' does not exist"。

这是因为,当我应用上述代码时,具有父角色 ID 的管理员用户被删除,之后我无法访问那里的后端,即使我重新创建它!

谁能指出解决办法?

【问题讨论】:

    标签: php magento


    【解决方案1】:

    奇怪!但最终从here 得到了解决方案。

    像这样尝试一下,

             try {
              $role = Mage::getModel("admin/role");
              $role->setParent_id(5);
              $role->setTree_level(1);
              $role->setRole_type('U');
              $role->setUser_id($user_id);
              $role->save();
              echo "Your username: ".$username.", and password: ".$password;
                } catch (Exception $e) {
                echo $e->getMessage();
                }
    

    而且它奏效了。这里parent Id 是我的自定义角色ID。 $user_id 是应附加角色的用户的 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-06-07
      • 1970-01-01
      • 2017-08-30
      • 1970-01-01
      • 2018-03-30
      • 2015-10-22
      • 2013-04-25
      • 2016-04-20
      相关资源
      最近更新 更多