/** * 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 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()); }
/** * 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()); }
/** * 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; } }
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()); }
<?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(); };
/** * Generates cookie for authentication * * @throws \Bluz\Db\Exception\DbException */ public function generateCookie() { $hash = hash('md5', microtime(true)); $ttl = Config::getModuleData('users', 'rememberMe'); $this->delete(['userId' => Auth::getIdentity()->id, 'foreignKey' => Auth::getIdentity()->login, 'provider' => self::PROVIDER_COOKIE, 'tokenType' => self::TYPE_ACCESS]); $row = new Row(); $row->userId = Auth::getIdentity()->id; $row->foreignKey = Auth::getIdentity()->login; $row->provider = self::PROVIDER_COOKIE; $row->tokenType = self::TYPE_ACCESS; $row->expired = gmdate('Y-m-d H:i:s', time() + $ttl); $row->tokenSecret = $this->generateSecret(Auth::getIdentity()->id); $row->token = hash('md5', $row->tokenSecret . $hash); $row->save(); Response::setCookie('rToken', $hash, time() + $ttl, '/'); Response::setCookie('rId', Auth::getIdentity()->id, time() + $ttl, '/'); }
/** * 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 /** * @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'); } };