Exemple #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);
 }
Exemple #2
0
 public function beforeUpdate()
 {
     $this->updatedAt = $this->updatedAt ?: time();
     $user = new LoginModel();
     if ($user->isUserLoggedIn()) {
         $userinfo = LoginModel::getCurrentUser();
         $this->userId = $this->userId ? $this->userId : $userinfo['id'];
         $this->username = $this->username ? $this->username : $userinfo['username'];
     }
 }
Exemple #3
0
 private function getUserInfo()
 {
     $user = new LoginModel();
     if ($user->isUserLoggedIn()) {
         $userinfo = $user->getCurrentUser();
         return $userinfo;
     } else {
         return false;
     }
 }
Exemple #4
0
 public function requestChangeEmail($newEmail, $forceSend = false)
 {
     $me = Login::getCurrentUser();
     $userId = $me['id'];
     if (!$userId) {
         throw new Exception\UnauthorizedException('ERR_USER_NOT_LOGIN');
     }
     $user = self::findFirst("id = {$userId}");
     if (!$user) {
         throw new Exception\ResourceNotFoundException('ERR_USER_NOT_EXIST');
     }
     return $this->sendChangeEmailVerificationEmail($user->username, $newEmail);
 }
Exemple #5
0
 public function indexAction()
 {
     if (!$this->request->isPost()) {
         return;
     }
     if ($this->request->isAjax()) {
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             return $this->showInvalidMessagesAsJson($form);
         }
         $user = new Login();
         try {
             $loginUser = $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             if ($this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $this->cookies->set(Login::LOGIN_COOKIE_REMEMBER_KEY, $token, time() + $user->getRememberMeTokenExpires());
                 }
             }
             return $this->showResponseAsJson(Login::getCurrentUser());
         } catch (\Exception $e) {
             return $this->showExceptionAsJson($e, $user->getMessages());
         }
     } else {
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             $this->showInvalidMessages($form);
             return $this->redirectHandler($this->getDI()->getConfig()->user->loginFailedRedirectUri, 'error');
         }
         $user = new Login();
         try {
             $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             if ($this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $this->cookies->set('realm', $token, time() + $user->getRememberMeTokenExpires());
                 } else {
                     $this->flashSession->error($user->getMessages());
                 }
             }
             //$this->flashSession->success('SUCCESS_USER_LOGGED_IN');
             return $this->redirectHandler($this->getDI()->getConfig()->user->loginSuccessRedirectUri);
         } catch (\Exception $e) {
             $this->showException($e, $user->getMessages());
             return $this->redirectHandler($this->getDI()->getConfig()->user->loginFailedRedirectUri, 'error');
         }
     }
 }
Exemple #6
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;
 }
Exemple #7
0
 /**
  * @operationName("users mobile binding")
  * @operationDescription("users mobile binding")
  */
 public function bindMobileAction()
 {
     $bindingForm = new MobileBindingForm(new User());
     $curUser = Login::getCurrentUser();
     $data = $this->request->getPut();
     $data['userId'] = $curUser['id'];
     if (!$bindingForm->isValid($data)) {
         return $this->showInvalidMessagesAsJson($bindingForm);
     }
     try {
         if (!User::bindMobile($data['mobile'], $data['captcha'], $data['userId'])) {
             return $this->showErrorMessageAsJson(400, 'BIND_MOBILE_FAILURE');
         }
     } catch (Exception\LogicException $e) {
         return $this->showExceptionAsJson($e);
     }
     return $this->showResponseAsJson(['mobile' => $data['mobile'], 'status' => true]);
 }
Exemple #8
0
 /**
  * @operationName("取消收藏文章")
  * @operationDescription("取消收藏文章")
  */
 public function deleteAction()
 {
     $user = Login::getCurrentUser();
     $userId = $user['id'];
     $postId = $this->dispatcher->getParam('id', 'int');
     if ($userId < 1 || $postId < 1) {
         return;
     }
     $star = Stars::findFirst("userId = {$userId} AND postId = {$postId}");
     if ($star) {
         $star->delete();
     } else {
         $star = new Stars();
         $star->userId = $userId;
         $star->postId = $postId;
     }
     return $this->response->setJsonContent($star);
 }
 /**
  * @param $validator
  * @param string $attribute
  * @return bool
  */
 public function validate($validator, $attribute)
 {
     $value = $validator->getValue($attribute);
     $usr = Login::getCurrentUser();
     if (!$usr['id']) {
         $validator->appendMessage(new Validation\Message('ERR_USER_NOT_LOGIN', $attribute));
         return false;
     }
     /**
      * @var $usr User
      */
     $usr = User::findFirst('id = ' . $usr['id']);
     if (!Login::passwordVerify($value, $usr->password)) {
         $message = $this->getOption('message');
         if (!$message) {
             //$message = 'The old password provided is incorrect.';
             $message = 'ERR_USER_OLD_PASSWORD_NOT_MATCH';
         }
         $validator->appendMessage(new Validation\Message($message, $attribute, null, null));
         return false;
     }
     return true;
 }
Exemple #10
0
 public function loginAction()
 {
     if (!$this->request->isPost()) {
         return;
     }
     $user = new OAuthModels\Login();
     if ($this->request->isAjax()) {
         try {
             $user->connectWithPassword($this->request->getPost('identify'), $this->request->getPost('password'), OAuthManager::getAccessToken());
             OAuthManager::removeAccessToken();
             return $this->showResponseAsJson(UserModels\Login::getCurrentUser());
         } catch (\Exception $e) {
             OAuthManager::removeAccessToken();
             return $this->showExceptionAsJson($e, $user->getMessages());
         }
     } else {
         try {
             $accessToken = OAuthManager::getAccessToken();
             $user->connectWithPassword($accessToken);
             OAuthManager::removeAccessToken();
             return $this->redirectHandler($this->getDI()->getConfig()->oauth->loginSuccessRedirectUri);
         } catch (\Exception $e) {
             $this->showException($e, $user->getMessages());
             return $this->redirectHandler($this->getDI()->getConfig()->oauth->loginFailedRedirectUri);
         }
     }
 }
 public function checkAction()
 {
     $username = $this->request->get('username');
     $email = $this->request->get('email');
     $mobile = $this->request->get('mobile');
     if ($this->hasQQ($username)) {
         $this->response->setStatusCode('409', 'User Already Exists');
     }
     $loginedUser = Models\Login::getCurrentUser();
     $extraCondition = '';
     // 已登录用户表示当前为修改用户名,允许与当前用户名相同
     if ($loginedUser['id'] > 0) {
         $extraCondition .= ' AND id != ' . $loginedUser['id'];
     }
     if ($username) {
         $userinfo = Models\Login::findFirst(array("username = '******' {$extraCondition}"));
     } elseif ($email) {
         $userinfo = Models\Login::findFirst(array("email = '{$email}' {$extraCondition}"));
     } elseif ($mobile) {
         $userinfo = Models\Login::findFirst(array("mobile = '{$mobile}' {$extraCondition}"));
     } else {
         $userinfo = array();
     }
     $this->view->disable();
     if ($userinfo) {
         $this->response->setStatusCode('409', 'User Already Exists');
     }
     return $this->response->setJsonContent(array('exist' => $userinfo ? true : false, 'id' => $userinfo ? $userinfo->id : 0, 'status' => $userinfo ? $userinfo->status : null));
 }
Exemple #12
0
 public function indexAction()
 {
     return $this->response->setJsonContent(Login::getCurrentUser());
 }
Exemple #13
0
 /**
  * Creates a new Comment for the Thread from the submitted data.
  *
  * @param string $uniqueKey The id of the thread
  * @throws \Exception
  */
 public function postThreadCommentsAction($uniqueKey)
 {
     $threadManager = new ThreadManager();
     $thread = $threadManager->findThreadByUniqueKey($uniqueKey);
     if (!$thread) {
         throw new \Exception(sprintf('Thread with identifier of "%s" does not exist', $uniqueKey));
     }
     //        if (!$thread->isCommentable()) {
     //            throw new \Exception(sprintf('Thread "%s" is not commentable', $uniqueKey));
     //        }
     $parentId = $this->request->getPost('parentId');
     $parent = $this->getValidCommentParent($thread, $parentId);
     $content = $this->request->getPost('content');
     $username = $this->request->getPost('username');
     $commentManager = new CommentManager();
     $comment = $commentManager->createComment($thread, $parent);
     //        if ($form->isValid()) {
     $comment->content = $content;
     //        if(!empty($username)) $comment->username = $username;
     $user = new LoginModel();
     if ($user->isUserLoggedIn()) {
         $userinfo = $user->getCurrentUser();
         $comment->userId = $userinfo['id'];
         $comment->username = $userinfo['username'];
     }
     $commentManager->filterContent($comment);
     //政治敏感词过滤
     if ($commentManager->saveComment($comment) !== false) {
         $errors = $comment->getMessages();
         p($errors);
         //                return $this->getViewHandler()->handle($this->onCreateCommentSuccess($form, $id, $parent));
     }
     $this->view->pick('thread/comment');
     $this->view->setVars(array('comment' => $comment, 'thread' => $thread));
 }
Exemple #14
0
 /**
  * @operationName("用户收藏文章列表")
  * @operationDescription("用户收藏文章列表")
  */
 public function starsAction()
 {
     $me = Login::getCurrentUser();
     $user = User::findFirstById($me['id']);
     $this->view->setVar('item', $user);
     $userId = $user->id;
     $query = array('page' => $this->request->getQuery('page', 'int', 1));
     $star = new Star();
     $starsItemQuery = $star->getStars($userId);
     $paginator = new \Eva\EvaEngine\Paginator(array("builder" => $starsItemQuery, "limit" => 5, "page" => $query['page']));
     $paginator->setQuery($query);
     $pager = $paginator->getPaginate();
     $this->view->setVar('pager', $pager);
 }
Exemple #15
0
 public function indexAction()
 {
     if (!$this->request->isPost()) {
         return;
     }
     if ($this->request->isAjax() || $this->request->get('ajax')) {
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             return $this->showInvalidMessagesAsJson($form);
         }
         $user = new Login();
         try {
             $loginUser = $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             $cookieDomain = $this->getDI()->getConfig()->session->sso_domain;
             if ($loginUser->id && $this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $cookies = $this->cookies->set(Login::LOGIN_COOKIE_REMEMBER_KEY, $token, time() + $user->getRememberMeTokenExpires());
                     if ($cookieDomain) {
                         $cookie = $cookies->get(Login::LOGIN_COOKIE_REMEMBER_KEY);
                         $cookie->setDomain($cookieDomain);
                     }
                 }
             }
             if (!empty($_SERVER['HTTP_ORIGIN'])) {
                 $this->response->setHeader('Access-Control-Allow-Credentials', 'true');
                 $this->response->setHeader('Access-Control-Allow-Origin', $_SERVER['HTTP_ORIGIN']);
                 $this->response->setHeader('Access-Control-Allow-Methods', 'POST');
                 $this->response->setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, X-Requested-With');
             }
             return $this->showResponseAsJson(Login::getCurrentUser());
         } catch (\Exception $e) {
             return $this->showExceptionAsJson($e, $user->getMessages());
         }
     } else {
         $loginFailedRedirectUri = $this->dispatcher->getParam('loginFailedRedirectUri');
         $loginFailedRedirectUri = $loginFailedRedirectUri ? $loginFailedRedirectUri : $this->getDI()->getConfig()->user->loginFailedRedirectUri;
         $loginFailedRedirectUri = $loginFailedRedirectUri ? $loginFailedRedirectUri : $this->request->getURI();
         $form = new Forms\LoginForm();
         if ($form->isValid($this->request->getPost()) === false) {
             $this->showInvalidMessages($form);
             return $this->redirectHandler($loginFailedRedirectUri, 'error');
         }
         $user = new Login();
         try {
             $user->loginByPassword($this->request->getPost('identify'), $this->request->getPost('password'));
             if ($this->request->getPost('remember')) {
                 $token = $user->getRememberMeToken();
                 if ($token) {
                     $ssoDomain = $this->getDI()->getConfig()->session->sso_domain;
                     $this->cookies->set('realm', $token, time() + $user->getRememberMeTokenExpires());
                     if ($ssoDomain) {
                         $cookie = $this->cookies->get(Login::LOGIN_COOKIE_REMEMBER_KEY);
                         $cookie->setDomain($ssoDomain);
                     }
                 } else {
                     $this->flashSession->error($user->getMessages());
                 }
             }
             //$this->flashSession->success('SUCCESS_USER_LOGGED_IN');
             $loginSuccessRedirectUri = $this->dispatcher->getParam('loginSuccessRedirectUri');
             if (empty($loginSuccessRedirectUri)) {
                 $loginSuccessRedirectUri = '/';
             }
             return $this->response->redirect($loginSuccessRedirectUri);
         } catch (\Exception $e) {
             $this->showException($e, $user->getMessages());
             // $this->getDI()->getConfig()->user->loginFailedRedirectUri
             return $this->response->redirect($loginFailedRedirectUri, 'error');
         }
     }
 }