/** * {@inheritdoc} * * @param string $module * @param string $controller * @param array $params * @return void */ protected function preDispatch($module, $controller, $params = array()) { // auth as CLI user $cliUser = Table::findRowWhere(['login' => 'system']); Auth::setIdentity($cliUser); parent::preDispatch($module, $controller, $params); }
/** * Test user with correct password */ public function testSigninWithCorrectPassword() { $this->dispatch('users/signin', ['login' => 'admin', 'password' => 'admin'], 'POST'); // $this->assertModule('users'); // $this->assertController('signin'); $this->assertNotNull(Auth::getIdentity()); }
/** * Test sign out user */ public function testSignOut() { $this->assertNotNull(Auth::getIdentity()); $this->dispatch('users/signout'); $this->assertModule('users'); $this->assertController('signout'); $this->assertNull(Auth::getIdentity()); }
/** * Test Auth Identity clear * * @covers \Bluz\Auth\Auth::getIdentity * @covers \Bluz\Auth\Auth::clearIdentity */ public function testAuthClearIdentityWithWrongUserAgent() { $adminIdentity = new UserAdmin(); Session::set('auth:agent', 'agent:php'); Session::set('auth:identity', $adminIdentity); $_SERVER['HTTP_USER_AGENT'] = 'agent:cli'; $this->assertNull(Auth::getIdentity()); }
protected function setUp() { parent::setUp(); $this->hybridAuthMock = $this->getMockBuilder('\\Hybrid_Auth')->setMethods(['authenticate'])->disableOriginalConstructor()->getMock(); $this->authAdapterMock = $this->getMockBuilder('\\Hybrid_Provider_Adapter')->setMethods(['getUserProfile'])->disableOriginalConstructor()->getMock(); Db::insert('users')->setArray(['id' => 2, 'login' => 'Bill', 'email' => '*****@*****.**', 'status' => 'active'])->execute(); Db::insert('auth')->setArray(['provider' => 'facebook', 'userId' => 2, 'foreignKey' => 112233])->execute(); Auth::setIdentity(new \Application\Users\Row()); }
/** * Check user access by pair module-privilege * * @param string $module * @param string $privilege * * @return bool */ public function isAllowed($module, $privilege) { if ($privilege) { $user = Auth::getIdentity(); if (!$user || !$user->hasPrivilege($module, $privilege)) { return false; } } return true; }
/** * {@inheritdoc} * * @return void */ public function beforeInsert() { $this->created = gmdate('Y-m-d H:i:s'); /* @var \Application\Users\Row $user */ if ($user = Auth::getIdentity()) { $this->userId = $user->id; } else { $this->userId = Users\Table::SYSTEM_USER; } }
/** * Reset layout and Request */ protected static function resetApp() { if (self::$app) { self::$app->useLayout(true); } Proxy\Auth::clearIdentity(); Proxy\Messages::popAll(); Proxy\Request::setInstance(new Http\Request()); Proxy\Response::setInstance(new Http\Response()); Proxy\Response::setPresentation(null); }
public function testUserStatusActive() { $provider = new AuthProvider('Facebook'); $provider->setResponse($this->getApp()); $authRow = new Row(); $authRow->userId = 2; try { $provider->alreadyRegisteredLogic($authRow); } catch (RedirectException $e) { } $this->assertNotNull(Auth::getIdentity()); }
/** * setUp * * @return void */ public function setUp() { parent::setUp(); $this->getApp()->useLayout(false); Auth::setIdentity(new UserHasPermission(UserFixtureContainer::$fixture)); }
/** * Reset layout and Request */ protected static function resetApp() { if (self::$app) { self::$app->useLayout(true); self::$app->resetRouter(); } Proxy\Auth::clearIdentity(); Proxy\Messages::popAll(); Proxy\Request::setInstance(RequestFactory::fromGlobals()); Proxy\Response::setInstance(new Bluz\Response\Response()); }
/** * Setup user with all privileges * * @return void */ protected function setupSuperUserIdentity() { Auth::setIdentity(new UserHasPermission()); }
/** * Can entity login * * @return void */ public function login() { Auth::setIdentity($this); }
/** * Call Verify Function * * @param string $password * @param string $hash * @throws \Application\Exception * @return string */ protected function callVerifyFunction($password, $hash) { /** @var \Bluz\Auth\Auth $auth */ $auth = Auth::getInstance(); $options = $auth->getOption(self::PROVIDER_EQUALS); if (!isset($options['verify']) or !is_callable($options['verify'])) { throw new Exception("Verify function for 'equals' adapter is not callable"); } // verify password with hash return call_user_func($options['verify'], $password, $hash); }
* * @route /api/{$resource}/{$id} * @param string $resource * @param string $id * * @route /api/{$resource} * @param string $resource * * @return mixed */ return function ($resource, $id, $relation, $relationId) { /** * @var Controller $this */ $this->useJson(); Auth::clearIdentity(); try { // authentication by api token if ($token = Request::getParam('token')) { Table::getInstance()->authenticateToken($token); } $params = []; foreach ([$id, $relation, $relationId] as $param) { if (!is_null($param)) { $params[] = $param; } } return $this->dispatch('api', 'resources/' . $resource, $params); } catch (\Exception $e) { // process exceptions here Response::setStatusCode($e->getCode());
/** * Can entity login * * @throws Exception * @throws AuthException * @return void */ public function tryLogin() { switch ($this->status) { case Table::STATUS_PENDING: throw new AuthException("Your account is pending activation", 403); case Table::STATUS_DISABLED: throw new AuthException("Your account is disabled by administrator", 403); case Table::STATUS_ACTIVE: // all ok // regenerate session if (PHP_SAPI !== 'cli') { Session::regenerateId(); } // save user to new session Auth::setIdentity($this); break; default: throw new Exception("User status is undefined in system"); } }
/** * Try with permissions */ public function testAllow() { Auth::setIdentity(new UserHasPermission()); $this->getApp()->widget('test', 'acl-denied'); }
<?php /** * @copyright Bluz PHP Team * @link https://github.com/bluzphp/skeleton */ /** * @namespace */ namespace Application\Layout\Helper; use Bluz\Proxy\Auth; return function () { /** * @var \Application\Users\Row $user */ if ($user = Auth::getIdentity()) { return $user->login; } else { return __('Guest'); } };
/** * Denied access * @param ForbiddenException $exception * @return \Bluz\Controller\Controller|null */ public function forbidden(ForbiddenException $exception) { if (AuthProxy::getIdentity()) { $message = Translator::translate("You don't have permissions to access this page"); } else { $message = Translator::translate("You don't have permissions, please sign in"); } // for AJAX and API calls (over JSON) $jsonOrApi = Request::isXmlHttpRequest() || Request::getAccept([Request::TYPE_HTML, Request::TYPE_JSON]) == Request::TYPE_JSON; // for guest, for requests if (!AuthProxy::getIdentity() && !$jsonOrApi) { // save URL to session and redirect make sense if presentation is null Session::set('rollback', Request::getUri()->__toString()); // add error notice Messages::addError($message); // redirect to Sign In page $url = Router::getUrl('users', 'signin'); return $this->redirect($url); } else { return $this->error(new ForbiddenException($message, 403, $exception)); } }
<?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ /** * @namespace */ namespace Bluz\View\Helper; use Bluz\View\View; use Bluz\Proxy\Auth; return function () { return Auth::getIdentity(); };
/** * Test deny access */ public function testDeny() { Proxy\Auth::setIdentity(new UserGuest()); $this->assertFalse(Proxy\Acl::isAllowed('any', 'any')); }