我会直截了当地说。
我有用户,具有隐私权。隐私可以是公开的也可以是私人的。 我希望能够允许未登录的用户能够查看其他用户,只要其他用户是公共的,当然如果他们是私人检查该用户是否是当前用户。
我无法想到最好的方法,因为isAuthorized仅针对登录用户调用,我不能只允许操作,因为isAuthorized根本没有被调用。
非常感谢任何帮助。
答案 0 :(得分:1)
您需要在控制器操作中添加一些逻辑,以验证配置文件是公共的还是私有的,如果它是私有的,那么当前登录的用户(如果有的话)是该配置文件的所有者。这样的事情可以解决问题:
// app/Controller/UsersController.php
public function view($id) {
// Check the privacy setting
if ($this->User->isPrivate($id)) {
// Private profile, check if user is logged in and id matches
if (empty($this->Auth->user()) || $this->Auth->user('id') != $id) {
// User is either not logged in or not the person we want to view
$this->Session->setFlash(
// Inspired by Cartman rage... you might want to change it ;-)
__('No kitty, this is my profile!')
);
// Redirect the user back to our index action (or anywhere else)
$this->redirect(array('action' => 'index'));
}
}
}
并确保在模型中创建isPrivate检查方法:
// app/Model/User.php
public function isPrivate($id) {
$privacy = $this->field('privacy', array('id' => $id));
if ($privacy == 'private') {
// This is a private profile
return true;
}
// This is a public profile
return false;
}