Example of usage use Bluz\Proxy\Auth; $user = Auth::getIdentity();
See also: Instance::setIdentity()
See also: Instance::getIdentity()
See also: Instance::clearIdentity()
Author: Anton Shevchuk
Inheritance: use trait ProxyTrait
示例#1
0
 /**
  * {@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);
 }
示例#2
0
 /**
  * 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());
 }
示例#3
0
 /**
  * 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());
 }
示例#4
0
 /**
  * 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());
 }
示例#5
0
 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());
 }
示例#6
0
文件: Acl.php 项目: dezvell/mm.local
 /**
  * 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;
 }
示例#7
0
文件: Row.php 项目: bluzphp/skeleton
 /**
  * {@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;
     }
 }
示例#8
0
 /**
  * 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);
 }
示例#9
0
 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());
 }
示例#10
0
 /**
  * setUp
  *
  * @return void
  */
 public function setUp()
 {
     parent::setUp();
     $this->getApp()->useLayout(false);
     Auth::setIdentity(new UserHasPermission(UserFixtureContainer::$fixture));
 }
示例#11
0
 /**
  * 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());
 }
示例#12
0
 /**
  * Setup user with all privileges
  *
  * @return void
  */
 protected function setupSuperUserIdentity()
 {
     Auth::setIdentity(new UserHasPermission());
 }
示例#13
0
 /**
  * Can entity login
  *
  * @return void
  */
 public function login()
 {
     Auth::setIdentity($this);
 }
示例#14
0
 /**
  * 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);
 }
示例#15
0
 *
 * @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());
示例#16
0
文件: Row.php 项目: bluzphp/skeleton
 /**
  * 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");
     }
 }
示例#17
0
 /**
  * Try with permissions
  */
 public function testAllow()
 {
     Auth::setIdentity(new UserHasPermission());
     $this->getApp()->widget('test', 'acl-denied');
 }
示例#18
0
<?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');
    }
};
示例#19
0
 /**
  * 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));
     }
 }
示例#20
0
文件: User.php 项目: dezvell/mm.local
<?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();
};
示例#21
0
 /**
  * Test deny access
  */
 public function testDeny()
 {
     Proxy\Auth::setIdentity(new UserGuest());
     $this->assertFalse(Proxy\Acl::isAllowed('any', 'any'));
 }