12

早在五月份,我就发布了这个问题。我试图在不同的应用程序上再次做同样的事情,但我还没有找到解决这个问题的方法。我确实有更多的信息和更好的代码,所以我希望你们能帮助我解决这个问题。

用例:医生办公室有一个包含管理员用户的网站。User用户通过模型和.CakePHP 的 Auth 成功登录UsersController

医生有完全不同的个人资料和行动的转诊医生。医生需要通过 登录example.com/physicians/login。但是,此登录失败

authError => 'You are not authorized to access that location.'

这是我的代码AppController

class AppController extends Controller {
public $helpers = array('Form', 'Html', 'Time', 'Session', 'Js' => array('Jquery')); 

public $components = array(
    'Session',
    'Auth' => array(
        'autoRedirect' => false,
        'authorize' => 'Controller'
    )
);

public function beforeFilter() {
    $this->Auth->allow('index', 'view', 'edit', 'display', 'featured', 'events', 'contact', 'signup', 'search', 'view_category', 'view_archive', 'addComment', 'schedule', 'login');
}

}

这是我UsersController的工作:

class UsersController extends AppController {

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'User',
                'fields' => array(
                    'username' => 'username',
                    'password' => 'password'
                )
            )
        ),
        'loginRedirect' => array('controller' => 'users', 'action' => 'admin'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'loginAction' => array('controller' => 'users', 'action' => 'login'),
        'sessionKey' => 'Admin'
    )
);


public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'login', 'logout');
}

function isAuthorized() {
    return true;
}

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

public function logout() {
    $this->Session->destroy();
    $this->redirect($this->Auth->logout());
}

这是我的PhysiciansController代码不起作用:

class PhysiciansController extends AppController {

public $components = array(
    'Auth' => array(
        'authenticate' => array(
            'Form' => array(
                'userModel' => 'Physician',
                'fields' => array(
                    'username' => 'username',
                    'password' => 'password'
                )
            )
        ),
        'loginRedirect' => array('controller' => 'physicians', 'action' => 'dashboard'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'loginAction' => array('controller' => 'physicians', 'action' => 'login'),
        'sessionKey' => 'Physician'
    )
);

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

    $this->Auth->authorize = array(
        'Actions' => array(
            'userModel' => 'Physician',
            'actionPath' => 'physicians'
        )
    );

    $this->Auth->allow('login', 'logout');
// $this->Session->write('Auth.redirect','/physicians/index');
}

function isAuthorized() {   
    return true;    
}   

public function login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            $this->redirect(array('controller' => 'physicians', 'action' => 'dashboard'));
        } else {
            $this->Session->read();
            debug($this->Auth);
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

public function logout() {
    $this->Session->destroy();
    $this->redirect($this->Auth->logout());
}

我真的不想重新开始并切换到 ACL —— 我不确定这对于两次登录是否是必要的。非常感谢您的帮助!

编辑:Joshua 在下面的回答很棒而且非常有帮助。我实现了它,但是当我尝试通过/phys/physican/login(前缀/控制器/操作)以医师身份登录时,我仍然收到未经授权的错误。管理员设置效果很好。这是我尝试登录时的调试代码:

object(AuthComponent) {
    components => array(
    (int) 0 => 'Session',
    (int) 1 => 'RequestHandler'
)
authenticate => array(
    'Form' => array(
        'userModel' => 'Physician'
    )
)
authorize => false
ajaxLogin => null
flash => array(
    'element' => 'default',
    'key' => 'auth',
    'params' => array()
)
loginAction => array(
    'controller' => 'physicians',
    'action' => 'phys_login'
)
loginRedirect => null
logoutRedirect => '/'
authError => 'You are not authorized to access that location.'
allowedActions => array()
request => object(CakeRequest) {
    params => array(
        'prefix' => '*****',
        'plugin' => null,
        'controller' => 'physicians',
        'action' => 'phys_login',
        'named' => array(),
        'pass' => array(),
        'phys' => true,
        '_Token' => array(
            'key' => 'ad1ea69c3b2c7b9e833bbda03ef18b04079b23c3',
            'unlockedFields' => array()
        ),
        'isAjax' => false
    )
    data => array(
        'Physician' => array(
            'password' => '*****',
            'username' => 'deewilcox'
        )
    )
    query => array()
    url => 'phys/physicians/login'
    base => ''
    webroot => '/'
    here => '/phys/physicians/login'
}
response => object(CakeResponse) {

}
settings => array()

}

4

2 回答 2

19

好的,我有办法做到这一点。你知道前缀路由吗?如果没有,请在此处阅读我的答案:CakePHP/MVC 管理函数放置该答案描述了如何设置单个路由前缀('admin')。但是你可以有任何数字——就像这样:

Configure::write('Routing.prefixes', array('admin','phys','member','user'));
// now we have admin, phys, member and user prefix routing enabled.

你可以做的是让所有医生的方法都使用'admin'前缀路由,所有医生方法都使用'phys'前缀路由。

所以下面是我很快就破解的代码,所以它可能并不完美,但它应该显示这个概念。这是应用程序控制器的前置过滤器方法的伪代码:

if (USER IS TRYING TO ACCESS AN ADMIN PREFIXED METHOD) {
    Then use the users table for auth stuff
} else if (USER IS TRYING TO ACCESS A PHYS PREFIXED METHOD) {
    Then use the physicians table for auth stuff
} else {
    It's neither an admin method, not a physicians method. So just always allow access. Or always deny access - depending on your site
}

这是我的应用程序控制器代码:

App::uses('Controller', 'Controller');

class AppController extends Controller {

    public $components = array('Security','Cookie','Session','Auth','RequestHandler');
    public $helpers = array('Cache','Html','Session','Form');

    function beforeFilter() {

        if ($this->request->prefix == 'admin') {
            $this->layout = 'admin';
            // Specify which controller/action handles logging in:
            AuthComponent::$sessionKey = 'Auth.Admin'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session
            $this->Auth->loginAction = array('controller'=>'administrators','action'=>'login');
            $this->Auth->loginRedirect = array('controller'=>'some_other_controller','action'=>'index');
            $this->Auth->logoutRedirect = array('controller'=>'administrators','action'=>'login');
            $this->Auth->authenticate = array(
                'Form' => array(
                    'userModel' => 'User',
                )
            );
            $this->Auth->allow('login');

        } else if ($this->request->prefix == 'phys') {
            // Specify which controller/action handles logging in:
            AuthComponent::$sessionKey = 'Auth.Phys'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session
            $this->Auth->loginAction = array('controller'=>'users','action'=>'login');
            $this->Auth->logoutRedirect = '/';

            $this->Auth->authenticate = array(
                'Form' => array(
                    'userModel' => 'Physician',
                )
            );
        } else {
            // If we get here, it is neither a 'phys' prefixed method, not an 'admin' prefixed method.
            // So, just allow access to everyone - or, alternatively, you could deny access - $this->Auth->deny();
            $this->Auth->allow();           
        }
    }

    public function isAuthorized($user){
        // You can have various extra checks in here, if needed.
        // We'll just return true though. I'm pretty certain this method has to exist, even if it just returns true.
        return true;
    }

}

注意这些行:

AuthComponent::$sessionKey = 'Auth.Admin'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session

AuthComponent::$sessionKey = 'Auth.Phys'; // solution from https://stackoverflow.com/questions/10538159/cakephp-auth-component-with-two-models-session

这样做是允许一个人在一个浏览器中同时作为医生和管理员登录,而不会干扰彼此的会话。您可能在实时站点中不需要它,但在测试时它肯定很方便。

现在,在您各自的控制器中,您将需要带有适当前缀的直接登录/注销方法。

因此,对于管理员前缀,在您的用户控制器中:

public function admin_login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

public function admin_logout() {
    $this->Session->setFlash('Successfully Logged Out');
    $this->redirect($this->Auth->logout());
}

在您的医生控制器中:

public function phys_login() {
    if ($this->request->is('post')) {
        if ($this->Auth->login()) {
            return $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth');
        }
    }
}

public function phys_logout() {
    $this->Session->setFlash('Successfully Logged Out');
    $this->redirect($this->Auth->logout());
}

就像我说的,我很快就将所有代码拼凑在一起,所以它可能无法逐字运行,但它应该显示这个概念。如果您有任何问题,请告诉我。

于 2012-10-31T22:07:17.570 回答
0

代替

$this->Session->write('Auth.redirect','/physicians/index'); 

你应该使用

setcookie("keys", value);
于 2015-03-25T06:21:14.303 回答