i've encountered issue several times , tried avoid removing calling flash method. lately, want show error flash non-logged in user tries log out. however, when test action(by accessing localhost:8765/users/logout without being logged in), works fine except 2 error messages "you not authorized access location". how can fix issue?
here codes
in appcontroller:
public function initialize() { parent::initialize(); $this->loadcomponent('requesthandler'); $this->loadcomponent('flash'); $this->loadcomponent('auth', [ 'authorize' => ['controller'], //for user authorize checking, tells app let each controller decides own rules authorize 'loginredirect' => ['controller' => 'articles', 'action' => 'index'], 'logoutredirect' => ['controller' => 'users', 'action' => 'index'] ]); } public function beforefilter(event $event) { //this applied every controller $this->auth->allow(['index', 'view', 'display']); } ... public function isauthorized($user) { //admin can access every action if(isset($user['role']) && $user['role'] === 'admin'){ return true; } //default deny return false; }
in userscontroller:
public function isauthorized($user) { //all registered users can add articles if($this->request->action === 'add'){ return true; } //the self user can edit , delete account if(in_array($this->request->action, ['edit', 'delete'])){ //get id of targeted user $targetuserid = (int)$this->request->params['pass'][0]; //check if current user targeted user if($this->users->selfuser($targetuserid, $user['id'])){ return true; }else{ $this->flash->error(__('you not authorized action')); } } return parent::isauthorized($user); } public function beforefilter(event $event) { parent::beforefilter($event); $this->auth->allow(['add']); } ... public function logout() { return $this->redirect($this->auth->logout()); }
in userstable
public function selfuser($targeteduserid, $userid) { return $targeteduserid == $userid; }
in default.ctp
$cakedescription = 'cakephp: rapid development php framework'; ?> <!doctype html> <html> <head> <?= $this->html->charset() ?> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title> <?= $cakedescription ?>: <?= $this->fetch('title') ?> </title> <?= $this->html->meta('icon') ?> <?= $this->html->css('base.css') ?> <?= $this->html->css('cake.css') ?> <?= $this->fetch('meta') ?> <?= $this->fetch('css') ?> <?= $this->fetch('script') ?> </head> <body> <nav class="top-bar expanded" data-topbar role="navigation"> <ul class="title-area large-3 medium-4 columns"> <li class="name"> <h1><a href=""><?= $this->fetch('title') ?></a></h1> </li> </ul> <div class="top-bar-section"> <ul class="right"> <li><a target="_blank" href="http://book.cakephp.org/3.0/">documentation</a></li> <li><a target="_blank" href="http://api.cakephp.org/3.0/">api</a></li> </ul> </div> </nav> <?= $this->flash->render() ?> <div class="container clearfix"> <?= $this->fetch('content') ?> </div> <footer> </footer> </body> </html>
in login.ctp
<div class="users form"> <?= $this->flash->render('auth') ?> <?= $this->form->create() ?> <fieldset> <legend><?= __('please enter username , password') ?></legend> <?= $this->form->input('username') ?> <?= $this->form->input('password') ?> </fieldset> <?= $this->form->button(__('login')); ?> <?= $this->form->end() ?> </div>
could post excerpt ctp file? possible within page layout flash rendered twice.
Comments
Post a Comment