/** * 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(); }
/** * Fetch avatars. * * @return $this * * @since 1.0 * @throws \UnexpectedValueException */ private function fetchAvatars() { /* @type \Joomla\Database\DatabaseDriver $db */ $db = $this->getContainer()->get('db'); $usernames = $db->setQuery($db->getQuery(true)->from($db->quoteName('#__activities'))->select('DISTINCT ' . $db->quoteName('user'))->order($db->quoteName('user')))->loadColumn(); if (!count($usernames)) { throw new \UnexpectedValueException(g11n3t('No users found in database.')); } $this->logOut(sprintf(g11n4t('Processing avatars for one user.', 'Processing avatars for %d users.', count($usernames)), count($usernames))); $progressBar = $this->getProgressBar(count($usernames)); $this->usePBar ? $this->out() : null; $base = JPATH_THEMES . '/images/avatars/'; $adds = 0; $loginHelper = new GitHubLoginHelper($this->getContainer()); foreach ($usernames as $i => $username) { if (!$username) { continue; } if (file_exists($base . '/' . $username . '.png')) { $this->debugOut(sprintf(g11n3t('User avatar already fetched for user %s'), $username)); $this->usePBar ? $progressBar->update($i + 1) : $this->out('-', false); continue; } $this->debugOut(sprintf(g11n3t('Fetching avatar for user: %s'), $username)); try { $loginHelper->saveAvatar($username); ++$adds; } catch (\DomainException $e) { $this->debugOut($e->getMessage()); $this->debugOut(sprintf(g11n3t('Copy default image for user: %s'), $username)); copy(JPATH_THEMES . '/images/avatars/user-default.png', JPATH_THEMES . '/images/avatars/' . $username . '.png'); } $this->usePBar ? $progressBar->update($i + 1) : $this->out('+', false); } return $this->out()->logOut(sprintf(g11n3t('Added %d new user avatars'), $adds)); }
/** * 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); }
/** * Get a renderer object. * * @param string|array $templatesPaths A path or an array of paths where to look for templates. * * @return RendererInterface * * @since 1.0 * @throws \RuntimeException */ protected function fetchRenderer($templatesPaths) { /* @type \JTracker\Application $application */ $application = $this->getContainer()->get('app'); $rendererName = $application->get('renderer.type'); $className = 'JTracker\\View\\Renderer\\' . ucfirst($rendererName); // Check if the specified renderer exists in the application if (false == class_exists($className)) { $className = 'Joomla\\View\\Renderer\\' . ucfirst($rendererName); // Check if the specified renderer exists in the Framework if (false == class_exists($className)) { throw new \RuntimeException(sprintf('Invalid renderer: %s', $rendererName)); } } $config = array(); switch ($rendererName) { case 'twig': $config['templates_base_dir'] = JPATH_TEMPLATES; $config['environment']['debug'] = JDEBUG ? true : false; break; case 'mustache': $config['templates_base_dir'] = JPATH_TEMPLATES; // . '/partials'; $config['partials_base_dir'] = JPATH_TEMPLATES; $config['environment']['debug'] = JDEBUG ? true : false; break; case 'php': $config['templates_base_dir'] = JPATH_TEMPLATES . '/php'; $config['debug'] = JDEBUG ? true : false; break; default: throw new \RuntimeException('Unsupported renderer: ' . $rendererName); break; } // Load the renderer. /* @type RendererInterface $renderer */ $renderer = new $className($config); // Register tracker's extension. $renderer->addExtension(new TrackerExtension($this->getContainer())); // Register additional paths. if (!empty($templatesPaths)) { $renderer->setTemplatesPaths($templatesPaths, true); } $gitHubHelper = new GitHubLoginHelper($this->getContainer()); $renderer->set('loginUrl', $gitHubHelper->getLoginUri())->set('user', $application->getUser()); // Retrieve and clear the message queue $renderer->set('flashBag', $application->getMessageQueue()); $application->clearMessageQueue(); // Add build commit if available if (file_exists(JPATH_ROOT . '/current_SHA')) { $data = trim(file_get_contents(JPATH_ROOT . '/current_SHA')); $renderer->set('buildSHA', $data); } else { $renderer->set('buildSHA', ''); } return $renderer; }