Exemple #1
0
 /**
  * Execute the controller.
  *
  * @return  string  The rendered view.
  *
  * @since   1.0
  * @throws  \UnexpectedValueException
  */
 public function execute()
 {
     /* @type \JTracker\Application $application */
     $application = $this->getContainer()->get('app');
     $id = $application->input->getUint('id');
     if (!$id) {
         throw new \UnexpectedValueException('No id given');
     }
     if (!$application->getUser()->check('admin')) {
         if ($application->getUser()->id != $id) {
             $application->enqueueMessage(g11n3t('You are not authorized to refresh this user.'), 'error')->redirect($application->get('uri.base.path') . 'user/' . $id);
         }
     }
     /* @type \Joomla\Github\Github $github */
     $gitHub = $this->getContainer()->get('gitHub');
     $loginHelper = new GitHubLoginHelper($this->getContainer());
     $gitHubUser = $gitHub->users->getAuthenticatedUser();
     $user = new GithubUser($application->getProject(), $this->getContainer()->get('db'));
     $user->loadGitHubData($gitHubUser)->loadByUserName($user->username);
     // Refresh the avatar
     $loginHelper->refreshAvatar($user->username);
     try {
         $loginHelper->setEmail($user->id, $gitHubUser->email);
     } catch (\RuntimeException $e) {
         $application->enqueueMessage(g11n3t('An error has occurred during email refresh.'), 'error');
     }
     $application->enqueueMessage(g11n3t('The profile has been refreshed.'), 'success')->redirect($application->get('uri.base.path') . 'user/' . $id);
     return parent::execute();
 }
 public static function getLoginUser()
 {
     if (empty(self::$loginUser)) {
         $jstr = Session::get(self::$sessionKey);
         self::$loginUser = GithubUser::loadFromJson($jstr);
     }
     return self::$loginUser;
 }
Exemple #3
0
 /**
  * Execute the controller.
  *
  * @return  string  The rendered view.
  *
  * @since   1.0
  * @throws  \Exception
  */
 public function execute()
 {
     /* @type \JTracker\Application $app */
     $app = $this->getContainer()->get('app');
     $user = $app->getUser();
     if ($user->id) {
         // The user is already logged in.
         $app->redirect(' ');
     }
     $error = $app->input->get('error');
     if ($error) {
         // GitHub reported an error.
         throw new \Exception($error);
     }
     $code = $app->input->get('code');
     if (!$code) {
         // No auth code supplied.
         throw new \Exception('Missing login code');
     }
     // Do login
     /*
      * @todo J\oAuth scrambles our redirects - investigate..
      *
     
     $options = new Registry(
     	array(
     		'tokenurl' => 'https://github.com/login/oauth/access_token',
     		'redirect_uri' => $app->get('uri.request'),
     		'clientid' => $app->get('github.client_id'),
     		'clientsecret' => $app->get('github.client_secret')
     	)
     );
     
     $oAuth = new oAuthClient($options);
     
     $token = $oAuth->authenticate();
     
     $accessToken = $token['access_token'];
     */
     $loginHelper = new GitHubLoginHelper($this->getContainer());
     $accessToken = $loginHelper->requestToken($code);
     // Store the token into the session
     $app->getSession()->set('gh_oauth_access_token', $accessToken);
     // Get the current logged in GitHub user
     $options = new Registry();
     $options->set('gh.token', $accessToken);
     // GitHub API works best with cURL
     $transport = HttpFactory::getAvailableDriver($options, array('curl'));
     $http = new Http($options, $transport);
     // Instantiate Github
     $gitHub = new Github($options, $http);
     $gitHubUser = $gitHub->users->getAuthenticatedUser();
     $user = new GithubUser($app->getProject(), $this->getContainer()->get('db'));
     $user->loadGitHubData($gitHubUser)->loadByUserName($user->username);
     // Save the avatar
     $loginHelper->saveAvatar($user->username);
     // Set the last visit time
     $loginHelper->setLastVisitTime($user->id);
     // User login
     $app->setUser($user);
     $redirect = $app->input->getBase64('usr_redirect');
     $redirect = $redirect ? base64_decode($redirect) : '';
     // Set a "remember me" cookie.
     $app->setRememberMe(true);
     $app->redirect($redirect);
 }