View source
<?php
namespace Drupal\media_acquiadam\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Datetime\DateFormatterInterface;
use Drupal\Core\Link;
use Drupal\Core\Routing\TrustedRedirectResponse;
use Drupal\Core\Routing\UrlGeneratorInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\media_acquiadam\OauthInterface;
use Drupal\user\UserDataInterface;
use Drupal\user\UserInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class OauthController extends ControllerBase {
protected $webdamApiBase = "https://apiv2.webdamdb.com";
protected $oauth;
protected $request;
protected $userData;
protected $dateFormatter;
protected $urlGenerator;
public function __construct(OauthInterface $oauth, RequestStack $request_stack, UserDataInterface $user_data, AccountProxyInterface $currentUser, DateFormatterInterface $dateFormatter, UrlGeneratorInterface $urlGenerator) {
$this->oauth = $oauth;
$this->request = $request_stack
->getCurrentRequest();
$this->userData = $user_data;
$this->currentUser = $currentUser;
$this->dateFormatter = $dateFormatter;
$this->urlGenerator = $urlGenerator;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('media_acquiadam.oauth'), $container
->get('request_stack'), $container
->get('user.data'), $container
->get('current_user'), $container
->get('date.formatter'), $container
->get('url_generator'));
}
public function authPage(UserInterface $user) {
if ($user
->id() !== $this
->currentUser()
->id()) {
throw new NotFoundHttpException();
}
$result = [];
$media_acquiadam_settings = $this
->config('media_acquiadam.settings');
$redirect_url = sprintf('/user/%d/acquiadam', $this
->currentUser()
->id());
$access_token = $this->userData
->get('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token');
$refresh_token = $this->userData
->get('media_acquiadam', $this->currentUser
->id(), 'acquiadam_refresh_token');
$access_token_expiration = $this->userData
->get('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token_expiration');
$is_expired = empty($access_token) || $access_token_expiration <= time();
$is_authenticated = !$is_expired || $is_expired && !empty($refresh_token);
$is_no_credentials = !$media_acquiadam_settings
->get('secret') || !$media_acquiadam_settings
->get('client_id');
if ($is_no_credentials) {
$result[] = [
'#markup' => '<p>' . $this
->t('The Acquia DAM module is not fully configured.') . '</p>',
];
if ($this
->currentUser()
->hasPermission('administer site configuration')) {
$config_url = $this->urlGenerator
->generateFromRoute('media_acquiadam.config', [], [
'query' => [
'destination' => $redirect_url,
],
]);
$result[] = [
'#markup' => '<p>' . $this
->t('Please <a href="@configure">configure</a> the Acquia DAM module to continue.', [
'@configure' => $config_url,
]) . '</p>',
];
}
else {
$result[] = [
'#markup' => '<p>' . $this
->t('Please contact a site administrator to continue.') . '</p>',
];
}
}
elseif ($is_expired) {
$this->userData
->delete('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token');
$this->userData
->delete('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token_expiration');
$this->userData
->delete('media_acquiadam', $this->currentUser
->id(), 'acquiadam_refresh_token');
$link = Link::createFromRoute('Authenticate', 'media_acquiadam.auth_start', [
'auth_finish_redirect' => $redirect_url,
]);
$result[] = [
'#markup' => '<p>' . $this
->t('You are <strong>not</strong> authenticated with Acquia DAM.') . '</p>',
];
$result[] = [
'#markup' => '<p>' . $link
->toString() . '</p>',
];
}
elseif ($is_authenticated) {
$logout_link = Link::createFromRoute('Logout from DAM', 'media_acquiadam.logout', [
'auth_finish_redirect' => $redirect_url,
]);
$reauthenticate_link = Link::createFromRoute('Reauthenticate', 'media_acquiadam.auth_start', [
'auth_finish_redirect' => $redirect_url,
]);
$result[] = [
'#markup' => '<p>' . $this
->t('You are authenticated with Acquia DAM.') . '</p>',
];
$result[] = [
'#markup' => '<p>' . $this
->t('Your authentication expires on @date.', [
'@date' => $this->dateFormatter
->format($access_token_expiration),
]) . '</p>',
];
$result[] = [
'#markup' => '<p>' . $reauthenticate_link
->toString() . ' | ' . $logout_link
->toString() . '</p>',
];
}
return !empty($result) ? $result : NULL;
}
public function authStart() {
$authFinishRedirect = $this->request->query
->get('auth_finish_redirect');
$this->oauth
->setAuthFinishRedirect($authFinishRedirect);
return new TrustedRedirectResponse($this->oauth
->getAuthLink());
}
public function logout() {
$this->userData
->delete('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token');
$this->userData
->delete('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token_expiration');
$this->userData
->delete('media_acquiadam', $this->currentUser
->id(), 'acquiadam_refresh_token');
$authFinishRedirect = $this->request->query
->get('auth_finish_redirect');
return new TrustedRedirectResponse($authFinishRedirect);
}
public function authFinish() {
$authFinishRedirect = $this->request->query
->get('auth_finish_redirect');
if ($original_path = $this->request->query
->get('original_path', FALSE)) {
$authFinishRedirect .= '&original_path=' . $original_path;
}
$this->oauth
->setAuthFinishRedirect($authFinishRedirect);
if (!$this->oauth
->authRequestStateIsValid($this->request
->get('state'))) {
throw new AccessDeniedHttpException();
}
$access_token = $this->oauth
->getAccessToken($this->request
->get('code'));
$this->userData
->set('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token', $access_token['access_token']);
$this->userData
->set('media_acquiadam', $this->currentUser
->id(), 'acquiadam_access_token_expiration', $access_token['expire_time']);
$this->userData
->set('media_acquiadam', $this->currentUser
->id(), 'acquiadam_refresh_token', $access_token['refresh_token']);
return new RedirectResponse($authFinishRedirect);
}
}