【问题标题】:CakePHP Multiple User Roles/PRO Membership RoleCakePHP 多用户角色/PRO 成员角色
【发布时间】:2019-10-13 05:26:26
【问题描述】:

我有一个 CakePHP 应用程序,目前有 2 个 user_type(s)。

  1. admin(管理员)
  2. 帐号(普通用户)

我正在开发一种用户可以支付额外费用的东西,它可以让他们访问更多的内容和页面。我打算创建一个名为 PRO 的新 user_type 并使用它,但 PRO 成员仍然需要访问拥有 user_type 帐户的人可以访问的所有内容。

基本上,最简单的设置方法是什么,我可以授予用户使用与我当前使用的代码类似的代码查看某些内容和页面的权限:

<?php if( $currentUser['user_type'] == 'account' ) { ?><?php } ?> 用于限制内容

if( $this->User['user_type'] == 'account')) {

            switch ($this->request->getParam('action')) {
                case 'PAGE':
                    return true;
                default:
                    return false;
                    break;
            }
        }

用于控制页面访问

另外,我需要一种简单的方法来让用户通过前端管理面板进行访问。我目前使用下面的代码,我需要一种方法来调整它以适用于这个新的“PRO”选项。

if( $user->user_type != 'admin' && $currentUser['id'] = 1 )
              echo $this->Form->postLink(__('Make Admin'), ['action' => 'make_admin', $user->id], ['confirm' => __('Are you sure you want to make admin?', $user->name), 'class'=>'btn btn-danger btn-md']);
            else 
               echo $this->Form->postLink(__('Revoke Admin'), ['action' => 'revoke_admin', $user->id], ['confirm' => __('Are you sure you want to remove admin rights?', $user->name), 'class'=>'btn btn-danger btn-md']);

我是 cakephp 的菜鸟,因此非常感谢任何帮助。我没有制作基础应用程序,我聘请了一个开发人员来做,所以我正在学习 cakephp,以便我可以调整应用程序以更好地满足我的需求。到目前为止,这个社区一直很有帮助,我希望我能继续让一切正常运行!

【问题讨论】:

标签: php cakephp authorization cakephp-3.x


【解决方案1】:

我的观点是我将在 AppController 中进行所有更改并从那里获得许可。然后,我可以毫不费力地轻松处理其余的情况。

例如在 AppController beforeFilter() 中

$this->authenticatedUser = AuthComponent::user();
//$this->authenticatedUser['superadmin'] = false; //if you set true, this will be your root user

并在此处设置您的新角色

$this->authenticatedUser['role'] = 'PRO' //or your `account` with your condition

如果您将角色account 更改为PRO 角色,您可以设置$this->authenticatedUser['role'] 来设置您的新角色。

 if(in_array($this->authenticatedUser['role'],['account','PRO'])) {

        switch ($this->request->getParam('action')) {
            case 'PAGE':
                return true;
            default:
                return false;
                break;
        }
    }

或者你可以改变类似的东西

if(!$this->authenticatedUser['superadmin']) 

if($this->authenticatedUser['superadmin'])

如果我能看到更多您的 AppController (BaseController),我可以为您提供更多帮助。现在,有很多未知数,并做出了很多假设来寻求帮助。无论如何,我的方法可能很容易帮助您添加新角色。

【讨论】:

    猜你喜欢
    • 2013-03-31
    • 2011-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-19
    • 2018-07-27
    • 1970-01-01
    相关资源
    最近更新 更多