基于角色,每个控制器(Auth / ACL)

时间:2014-02-23 14:42:15

标签: cakephp authentication acl

我有这个数据库:

Roles(id,name) - > hasMany Users

Users(id,role_id,用户名,密码) - > belongsTo Roles

所以,通常我有三个角色:管理员,转售和客户。我为每个使用一个控制器,因为它更容易管理视图..然后我曾经使用纯Session,你知道,我只是存储在Session所有用户数据然后在每个控制器中我只是检查角色是否对应于那个作用。

现在我想使用Auth / ACL Component因为我厌倦了重新发明轮子..问题是:我没有想法如何编写角色的东西!如果你给我踩踏板石头我会很高兴。

这是我的代码:

AppController

class AppController extends Controller {
public $components = array(
    'Acl',
    'Auth' => array(
        'authorize' => array(
            'Actions' => array('actionPath' => 'controllers')
        )
    ),
    'Session'
);

public function beforeFilter() {
    $this->Auth->loginAction = array(
      'controller' => 'users',
      'action' => 'login'
    );
    $this->Auth->logoutRedirect = array(
      'controller' => 'users',
      'action' => 'login'
    );
    $this->Auth->loginRedirect = array(
      'controller' => 'HERE SHOULD BE THE ROLE CONTROLLER??',
      'action' => 'add'
    );
}

UsersController

class UsersController extends AppController{
public $name = 'Users';

function index(){ }

function login(){
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        }
        $this->Session->setFlash(__('Your username or password was incorrect.'));
    }
}

function logout(){
    $this->Session->setFlash('Good-Bye');
    $this->redirect($this->Auth->logout());
}

AdministratorsController(类似于Resales或ClientsController)

class AdministratorsController extends AppController{

public function beforeFilter() {
    parent::beforeFilter();

    $this->Auth->allow();
}

0 个答案:

没有答案