/** * 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); }