超级用户或管理员在Cakephp 3 - 电子商务与管理 [英] Superuser or Admin in Cakephp 3 - E-Commerce with Admin

查看:277
本文介绍了超级用户或管理员在Cakephp 3 - 电子商务与管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



有一种方法在Cake有一个用户(一般客户在网站上购物)和超级用户同时?我在我的用户表中有一个'is_admin'列,以区分管理员和用户。我只需要在我的addProducts函数中有这样的东西,或有更好的方法吗?

  public function addProducts(){
$ user = $ this-> Auth-> user();
if($ user ['is_admin']){
//允许访问
} else {
//抛出授权异常
}
} $提前感谢

解决方案 / div>

您可以通过管理员和前面用户的不同网址进行管理。这可以通过路由和APP控制器来管理。
我使用的应用程序之一如下:



在routes.php文件

  Router :: prefix('admin',function($ routes){
//将以`/ admin`作为前缀
//添加前缀=>管理路由元素
$ routes-> fallbacks('DashedRoute');
$ routes-> ; connect('/',array('controller'=>'Users','action'=>'login'));
/ *在这里你可以定义所有的路由admin * /
});

Router :: scope('/',function($ routes){

$ routes-> connect('/',array('controller' '
/ *在这里你可以定义前端的所有路由* /
});

请注意,管理员需要在所有/ src / Controller,/ src /模板命名为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'=&认为你可以看到吗?',
'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'=>你真的认为你可以看到吗? b $ b'authenticate'=> [
'Form'=> [
'finder'=>'user',
'fields'=> ['username'= >'email','password'=>'password']
]
],
'storage'=> ['className'=>'Session' '=>'Auth.User']
]);
}
}

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



对于finder,您需要在您的用户模型表中编写以下代码,位于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 ('Users.email','Users.password','Users.id','Users.role_id'))
- > where(array('Users.status'=> 1,'Users。 role_id'=> 3));

return $ query;注意,在这里我保持role_id1为管理员和3
}



<



以这种方式,即使您可以在同一浏览器中设置登录名,因为两种用户类型都不同。 strong>



希望这有助于您相应地设置结构。


I'm creating an E-Commerce website using CakePHP 3

I need to create an Admin page that will allow the Admin to upload products and possibly view a few KPI's etc..

Is there a way in Cake to have a User (general customer shopping on the site) and a Superuser (or Admin) at the same time? I have an 'is_admin' column in my Users table to differentiate between admin and user. Do I just need to have something like this in my addProducts function or is there a better way?:

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

Thanks in advance

解决方案

You can manage it via different URL's for admin and front User. This can be managed via the routes and the APP Controller. What I am using for one of my appplication is as below:

In the routes.php file

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 */
});

Please note for the Admin you need to create a directory in all /src/Controller, /src/Template named as "Admin" and within these directories you can use the same structure that we use in our code.

Now comes the code that needs to be written in /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']
        ]);
    }
}

Here you can see that we are using different keys for the storage Auth.User and Auth.Admin

For the finder you need to write the below code in your user model table located at 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;
}

Note, Here I am keeping role_id "1" for Admin and "3" for front Users.

In this manner, even you can set the login for both in the same browser as key for both the user types is different.

Hope this helps you setup the structure accordingly.

这篇关于超级用户或管理员在Cakephp 3 - 电子商务与管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆