【问题标题】:Superuser or Admin in Cakephp 3 - E-Commerce with AdminCakephp 3 中的超级用户或管理员 - 带管理员的电子商务
【发布时间】:2016-09-29 01:07:16
【问题描述】:

我正在使用 CakePHP 3 创建一个电子商务网站

我需要创建一个允许管理员上传的管理员页面 产品并可能查看一些 KPI 等。

Cake 中有没有办法同时拥有一个用户(一般客户在网站上购物)和一个超级用户(或管理员)?我的用户表中有一个“is_admin”列来区分管理员和用户。我是否只需要在我的 addProducts 函数中有这样的东西,或者有更好的方法吗?:

public function addProducts(){
    $user = $this->Auth->user();
    if($user['is_admin']) {
        //allow access
    } else {
      //throw anauthorised exception
    }
}

提前致谢

【问题讨论】:

    标签: cakephp authorization e-commerce


    【解决方案1】:

    您可以通过管理员和前台用户的不同 URL 对其进行管理。这可以通过路由和 APP 控制器进行管理。 我的一个应用程序使用如下:

    在 routes.php 文件中

    Router::prefix('admin', function ($routes) {
        // All routes here will be prefixed with `/admin`
        // And have the prefix => admin route element added.
        $routes->fallbacks('DashedRoute');
        $routes->connect('/', array('controller' => 'Users', 'action' => 'login'));
        /* Here you can define all the routes for the admin */
    });
    
    Router::scope('/', function ($routes) {
    
        $routes->connect('/', array('controller' => 'Users', 'action' => 'login', 'home'));
        /* Here you can define all the routes for the frontend */
    });
    

    请注意,对于管理员,您需要在所有 /src/Controller、/src/Template 中创建一个名为“Admin”的目录,并且在这些目录中,您可以使用我们在代码中使用的相同结构。

    现在是需要写在/src/Controller/AppController.php中的代码

    public $prefix = '';
    public function initialize()
    {
    
        $this->prefix = (!empty($this->request->params['prefix'])?$this->request->params['prefix']:'');
        $this->set('prefix',$this->prefix);
        if( !empty($this->prefix) && $this->prefix==='admin' )
        {   
    
            $this->loadComponent('Auth', [
    
                'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login',
                    'prefix'=>'admin'
                ],
                'loginRedirect' => [
                    'controller' => 'Users',
                    'action' => 'index',
                    'prefix'=>'admin'
                ],
                'logoutRedirect' => [
                    'controller' => 'Users',
                    'action' => 'login',
                    'prefix'=>'admin'
                ],
                'authError' => 'Did you really think you are allowed to see that?',
                'authenticate' => [
                    'Form' => [
                        'finder' => 'admin',
                        'fields' => ['username' => 'email', 'password' => 'password']
                    ]
                ],
                'storage' => ['className' => 'Session', 'key' => 'Auth.Admin']
            ]);
        }
    
        else
        {
    
            $this->loadComponent('Auth', [
    
                'loginAction' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'loginRedirect' => [
                    'controller' => 'Users',
                    'action' => 'myaccount'
                ],
                'logoutRedirect' => [
                    'controller' => 'Users',
                    'action' => 'login'
                ],
                'authError' => 'Did you really think you are allowed to see that?',
                'authenticate' => [
                    'Form' => [
                        'finder' => 'user',
                        'fields' => ['username' => 'email', 'password' => 'password']
                    ]
                ],
                'storage' => ['className' => 'Session', 'key' => 'Auth.User']
            ]);
        }
    }
    

    在这里您可以看到我们为存储 Auth.User 和 Auth.Admin 使用了不同的密钥

    对于查找器,您需要在位于 src\Model\Table\UsersTable.php 的用户模型表中编写以下代码

    public function findAdmin(\Cake\ORM\Query $query, array $options)
    {
        $query
            ->select(array('Users.email', 'Users.password','Users.id','Users.role_id'))
            ->where(array('Users.role_id' => 1));
    
        return $query;
    }
    public function findUser(\Cake\ORM\Query $query, array $options)
    {
        $query
            ->select(array('Users.email', 'Users.password','Users.id','Users.role_id'))
            ->where(array('Users.status' => 1,'Users.role_id' => 3));
    
        return $query;
    }
    

    注意,这里我为管理员保留 role_id “1”,为前台用户保留“3”。

    通过这种方式,即使您可以在同一浏览器中设置两者的登录,因为两种用户类型的键不同。

    希望这可以帮助您相应地设置结构。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2016-03-05
    • 1970-01-01
    • 2013-08-04
    • 1970-01-01
    • 1970-01-01
    • 2014-12-13
    相关资源
    最近更新 更多