Пример #1
0
 /**
  *
  * @SWG\Api(
  *   path="/users/me",
  *   description="User API",
  *   produces="['application/json']",
  *   @SWG\Operations(
  *     @SWG\Operation(
  *       method="GET",
  *       summary="Get current user info",
  *     )
  *   )
  * )
  */
 public function indexAction()
 {
     Login::setLoginMode(Login::LOGIN_MODE_TOKEN);
     $storage = Login::getAuthStorage();
     $userinfo = Login::getCurrentUser();
     return $this->response->setJsonContent($userinfo);
 }
Пример #2
0
 public function afterLogin($event, $loginUser)
 {
     if (!$loginUser->id) {
         return;
     }
     $storage = Login::getAuthStorage();
     if (Login::getLoginMode() == Login::LOGIN_MODE_TOKEN) {
         $apikey = new Apikey();
         $userId = $loginUser->id;
         $token = $apikey->findFirst("userId = {$userId}");
         if (!$token) {
             $token = $apikey->generateToken($userId);
         }
         $storage->setId($token->apikey);
         $storage->set(Login::AUTH_KEY_TOKEN, $token);
     }
     $defaultRoles = $loginUser->getRoles();
     $roles = $loginUser->roles;
     $authRoles = array();
     if ($roles) {
         foreach ($roles as $role) {
             $authRoles[] = $role->roleKey;
         }
     }
     $authRoles = array_unique(array_merge($defaultRoles, $authRoles));
     $storage->set(Login::AUTH_KEY_ROLES, $authRoles);
 }
Пример #3
0
 public function indexAction()
 {
     $this->cookies->delete(Login::LOGIN_COOKIE_KEY);
     $this->cookies->delete(Login::LOGIN_COOKIE_REMEMBER_KEY);
     Login::getAuthStorage()->remove(Login::AUTH_KEY_LOGIN);
     Login::getAuthStorage()->remove(Login::AUTH_KEY_TOKEN);
     Login::getAuthStorage()->remove(Login::AUTH_KEY_ROLES);
     return $this->response->redirect('/');
 }
Пример #4
0
 public function getRoles()
 {
     $user = Login::getCurrentUser();
     if (!$user['id']) {
         return array('GUEST');
     }
     $storage = Login::getAuthStorage();
     $authRoles = $storage->get(Login::AUTH_KEY_ROLES);
     $authRoles = $authRoles ?: array();
     //Add default roles
     if ($user['status'] == 'active') {
         $authRoles[] = 'USER';
         $authRoles = array_unique($authRoles);
     }
     return $authRoles;
 }
Пример #5
0
 /**
  *
  * @SWG\Api(
  *   path="/login",
  *   description="User Login API",
  *   produces="['application/json']",
  *   @SWG\Operations(
  *     @SWG\Operation(
  *       method="POST",
  *       summary="Login by password",
  *       @SWG\Parameters(
  *         @SWG\Parameter(
  *           name="Login json",
  *           description="{ identify : username or email, password : password}",
  *           paramType="body",
  *           required=true,
  *           type="string"
  *         )
  *       )
  *     )
  *   )
  * )
  */
 public function indexAction()
 {
     Login::setLoginMode(Login::LOGIN_MODE_TOKEN);
     $data = $this->request->getRawBody();
     if (!$data) {
         throw new Exception\InvalidArgumentException('No data input');
     }
     if (!($data = json_decode($data, true))) {
         throw new Exception\InvalidArgumentException('Json data parsing failed');
     }
     $form = new LoginForm();
     if ($form->isValid($data) === false) {
         return $this->showInvalidMessagesAsJson($form);
     }
     $user = new Login();
     $apikey = new Apikey();
     $loginUser = $user->loginByPassword($data['identify'], $data['password']);
     $userinfo = $loginUser->dump(User::$simpleDump);
     $userinfo['roles'] = Login::getAuthStorage()->get(Login::AUTH_KEY_ROLES);
     $userinfo['token'] = Login::getAuthStorage()->get(Login::AUTH_KEY_TOKEN);
     return $this->response->setJsonContent($userinfo);
 }
Пример #6
0
 public function saveUserToStorage(Entities\Users $userinfo)
 {
     $authIdentity = $this->userToAuthIdentity($userinfo);
     $storage = Login::getAuthStorage();
     $storage->set(Login::AUTH_KEY_LOGIN, $authIdentity);
     return $authIdentity;
 }
Пример #7
0
 public static function logout()
 {
     /** @var \Phalcon\HTTP\ResponseInterface $response */
     $response = IoC::get('response');
     $response->setHeader('P3P', 'CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR');
     $config = IoC::get('config');
     /** @var \Phalcon\Http\Response\Cookies $cookies */
     $cookies = IoC::get('cookies');
     $cookieDomain = $config->session->cookie_params->domain;
     $sso_ticket_name = $config->session->sso_ticket_name;
     $cookies->get(Login::LOGIN_COOKIE_KEY)->setDomain($cookieDomain)->delete();
     $cookies->get(Login::LOGIN_COOKIE_REMEMBER_KEY)->setDomain($cookieDomain)->delete();
     $cookies->get($sso_ticket_name)->setDomain($cookieDomain)->delete();
     Login::getAuthStorage()->remove(Login::AUTH_KEY_LOGIN);
     Login::getAuthStorage()->remove(Login::AUTH_KEY_TOKEN);
     Login::getAuthStorage()->remove(Login::AUTH_KEY_ROLES);
     Login::removeBadges();
 }