doufang7385 2012-12-03 14:58
浏览 85
已采纳

cakephp将ACL添加到功能登录系统

i have built upon this tutorial http://www.jamesfairhurst.co.uk/posts/view/creating_an_admin_section_with_cakephp_updated

and currently have a functional and quite well fleshed out admin section for my application.

Due to poor foresight I haven't taken into account regular users who need to be able to login to their own home page, where they can view bookings etc.

I have an appropriate database set up and have included a 'roles' field for authentication. I have also followed cakePHP's own 'auth' examples however have failed to get them to implement without throwing various errors, at this stage i'm not wanting to go changing the structure of the login system too much, that kind of thing can become a headache quick!!

I have spoken to the original author of the tutorial and he agrees that some simple logic added to the user_controller.php file should suffice.

basically i need something along the lines of an: "if user == 'user' THEN redirect to 'user_index.php' put simply. below is the current LOGIN function for user_controller.php

function login() {
        if(!empty($this->data)) {
            // unset unrequired validation rules
            unset($this->User->validate['username']['check_username_exists']);

            // validate form
            $this->User->set($this->data);
            if($this->User->validates()) {
                // update Last Login date
                $this->User->id = $this->User->_user['User']['id'];
                $this->User->saveField('last_login',date("Y-m-d H:i:s"));

                // save User to Session and redirect
                $this->Session->write('User', $this->User->_user);
                $this->Session->setFlash('You have successfully logged in.','default',array('class'=>'flash_good'));
                $this->redirect(array('action'=>'index','admin'=>TRUE));
            }
        }
    }

All validation is handled in the user.php model and there is some logic in app_controller.php to redirect authentication, it is included below;

app_controller.php

class AppController extends Controller {
    // class variables
    var $_User = array();


    /**
     * Before any Controller action
     */
    function beforeFilter() {
        // if admin url requested
        if(isset($this->params['admin']) && $this->params['admin']) {
            // check user is logged in
            if( !$this->Session->check('User') ) {
                $this->Session->setFlash('You must be logged in for that action.','flash_bad');
                $this->redirect('/login');
            }

            // save user data
            $this->_User = $this->Session->read('User');
            $this->set('user',$this->_User);

            // change layout
            $this->layout = 'admin';
        }
    }
}
  • 写回答

1条回答 默认 最新

  • dongwu3596 2013-01-22 21:27
    关注

    I faced a similar problem in my application. In my User model, I created a getRole() method which just pulled the role out of the database, and then I used a switch statement to redirect users to the correct controller.

    As a different approach, you could just add in an isAdmin column (default 0, 1 would indicate an admin) to your users table. Assuming there are not too many admins already, you could just manually set the admins. In your controller you would just need to check the field and the redirect accordingly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

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