dousuitang5239 2014-02-12 00:10
浏览 17
已采纳

Cakephp管理员和用户登录分开

I want to make an aplication where users can login normally and back-end where admins can login.

So far I created this:

routes.php

$prefix = 'admin';

Router::connect(
    "/{$prefix}/:plugin/:controller",
    array('action' => 'index', 'prefix' => $prefix, $prefix => true)
);
Router::connect(
    "/{$prefix}/:plugin/:controller/:action/*",
    array('prefix' => $prefix, $prefix => true)
);
Router::connect(
    "/{$prefix}/:controller",
    array('action' => 'index', 'prefix' => $prefix, $prefix => true)
);
Router::connect(
    "/{$prefix}/:controller/:action/*",
    array('prefix' => $prefix, $prefix => true)
);

AppController:

public $components = array(
    'DebugKit.Toolbar',
    'Session',
    'Auth' => array(
        'loginRedirect' => array(
            'controller' => 'pages',
            'action' => 'display'
        ),
        'logoutRedirect' => array(
            'controller' => 'pages',
            'action' => 'display',
            'home'
        ),
        'authorize' => 'Controller',
        'authError' => 'Access denied! Did you really think that you can access that?'
    )
);

public function isAuthorized($user) {
    // Admin can access every action
    if (isset($user['role']) && $user['role'] === 'admin') {
        return true;
    }

    // Default deny
    return false;
}

public function beforeFilter() {
    $this->Auth->allow('display');
    //$this->recordActivity();
    if($this->request->prefix == 'admin'){
        $this->layout = 'admin';
    }   
}

With this when I try to access pages on front-end that needs auth it gives me login() action but when a try to access /admin it redirects me to /users/login.

I want to have two separate login systems with diferrent layouts. One for normal users and one for admin users.

Can anybody help please?

  • 写回答

1条回答 默认 最新

  • doujiao1180 2014-02-12 14:46
    关注

    I don't recommend two login() actions just for the sake of a different view. You can move the if statement from your beforeFilter() to UsersController::login() to set the layout correctly. However, if you do want to proceed with separate actions, set the AuthComponent::loginAction property in AppController::beforeFilter() like:

    if($this->request->prefix == 'admin'){
        $this->Auth->loginAction = array('controller' => 'users', 'action' => 'admin_login', 'plugin' => false);
    }
    

    where admin_login will be another action that you create in UsersController.

    On a side note, I recommend using cake's default prefix routing as mentioned in the book. It is very similar to what you have done but you won't have to manually create the routes. Also, as mentioned in there, to access /admin you will need to define a route like:

    Router::connect(
        '/admin',
        array('controller' => 'pages', 'action' => 'index', 'admin' => true)
    );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 用51单片机控制急停。
  • ¥15 孟德尔随机化结果不一致
  • ¥15 在使用pyecharts时出现问题
  • ¥15 深度学习残差模块模型
  • ¥50 怎么判断同步时序逻辑电路和异步时序逻辑电路
  • ¥15 差动电流二次谐波的含量Matlab计算
  • ¥15 Can/caned 总线错误问题,错误显示控制器要发1,结果总线检测到0
  • ¥15 C#如何调用串口数据
  • ¥15 MATLAB与单片机串口通信
  • ¥15 L76k模块的GPS的使用