Cakephp 3 中的超级用户或管理员 - 带管理员的电子商务

Superuser or Admin in Cakephp 3 - E-Commerce with Admin

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

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

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

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

提前致谢

您可以通过不同的 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 的用户模型 table 中编写以下代码。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”。

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

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