/** * Handle middleware * * @param Request $request * @param callable $next * @return mixed */ public function handle(Request $request, Closure $next) { //Get account $account = $this->getAccountFromRouting(); //Set account in context $this->context->setAccount($account); //If the owner type is User if ($this->authorizer->getResourceOwnerType() == 'user') { //Find the user $user = $this->userRepository->find($this->authorizer->getResourceOwnerId()); //If we have account in the route if ($account) { //Check if the user has access to the account if (!$user->isAssociateToAccount($account)) { return $this->response->errorUnauthorized("You don't have access to the account {$account->uuid}"); } } //Add context processor to log $this->log->addProcessors([new ContextProcessor($user, isset($account) ? $account : null)]); //Set the user in context $this->context->setUser($user); } // Set application locale $this->setApplicationLocale(); return $next($request); }
public function testHasAccount() { $account = new Account(); $context = new Context(); $context->setAccount($account); $this->assertTrue($context->hasAccount()); $this->assertSame($account, $context->account()); }
public function testValidationFailed() { $account = new Account(); $context = new Context(); $context->setAccount($account); $userRepository = m::mock(UserRepository::class); $userRepository->shouldReceive('isEmailExistForThisAccount')->once()->with($account, '*****@*****.**')->andReturn(true); $rule = new ValidateUserIsUnique($context, $userRepository); $this->assertFalse($rule->validate('', '*****@*****.**', '')); }