Skip to content

Instantly share code, notes, and snippets.

@Spriz
Created February 25, 2018 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Spriz/efa8a9a774bf20fd3b7737eff679e2bd to your computer and use it in GitHub Desktop.
Save Spriz/efa8a9a774bf20fd3b7737eff679e2bd to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace Api\Controller\Traits;
use App\Exception\NoAccountSelected;
use Cake\Event\Event;
/**
* Trait RequiresActiveAccountTrait
*
* @package Api\Controller\Traits
*/
trait RequiresActiveAccountTrait
{
/**
* @return array
*/
public function implementedEvents(): array
{
return ['Controller.initialize' => 'checkForAccount'] + parent::implementedEvents();
}
/**
* @param \Cake\Event\Event $e
*/
public function checkForAccount(Event $e)
{
if (empty($this->Auth->user('active_account_id'))) {
throw new NoAccountSelected();
}
parent::beforeFilter($e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment