【发布时间】:2015-01-06 08:48:17
【问题描述】:
我想做一个页面,这个页面只能打开一个用户。我想做这样的事情:如果用户 id = 1 则打开,否则抛出错误。
我已经试过了:
if (in_array($this->action, array('controller' => 'users', 'action' => 'admin'))) {
$postId = (int) $this->request->params['pass'][0];
if ($this->User->isOwnedBy($postId, $users['id'] = 1)) {
return true;
}else{echo "You are not admin!";}
}
然后我想,也许这更容易一些?
public function admin($id = null) {
$this->User->id = $id;
if ($id == 1) {
echo 'You are admin';
}
else {
throw new NotFoundException(__('You are not admin !'));
}
}
但它不起作用,我如何让这个用户 ID 进入这个 if。第二个解决方案只抛出这个错误,但我不想要它,如果用户 ID 为 1,我想要访问。
这是用户图片
感谢您提供任何线索或解决方案。
【问题讨论】:
-
您遇到什么错误?将 id 与会话数据匹配。你在使用身份验证组件吗?
-
我收到此错误:
else {throw new NotFoundException(__('You are not admin !'));} -
你在使用auth组件登录吗?
-
你是说这个? :
if ($this->Auth->login()) { return $this->redirect($this->Auth->redirectUrl()); } -
添加了答案。您可以检查已登录用户。