【发布时间】:2017-05-31 08:30:28
【问题描述】:
通常,cakephp 中有 Auth 组件来帮助用户登录,并且有 Auth->Allow() 函数可以使访客用户仍然可以访问某些页面,例如索引。但现在我希望只有激活的帐户才能访问网络的几乎所有功能,但仍然除了一些正常的页面,如索引、视图等。 我在 Appcontroller 中有一个功能
public function is_activated(){
$userId = $this->Auth->user('id');
$user = $this->Users->find('all', [
'conditions' => ['id' => $userId],
'fields' => ['id', 'email', 'activated']
])->first();
$activated = $user->activated;
if($activated !== 1){
$this->Flash->error(__('Your account is not yet activated'));
return $this->redirect('/users/activate');
}
}
我在 BeforeFilter 和 ProjectsController 中的 Auth->allow() 中调用它:
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
$this->Auth->allow(['index', 'getMyProjects']);
$this->is_activated();
}
但是通过这种方式,每个页面都会受到影响,并且 Auth->allow() 不再起作用。任何人都可以向我展示我的 is_activated() 函数的更好方法,我想这种方式我重定向网络不是一个好方法。
【问题讨论】: