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 plotBAPC画图出错
  • ¥30 关于#opencv#的问题:使用大疆无人机拍摄水稻田间图像,拼接成tif图片,用什么方法可以识别并框选出水稻作物行
  • ¥15 Python卡尔曼滤波融合
  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理