PhotosAccess.php in Album Photos 8.5
File
photos_access/src/Plugin/views/access/PhotosAccess.php
View source
<?php
namespace Drupal\photos_access\Plugin\views\access;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\views\Plugin\views\access\AccessPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Route;
use Drupal\Core\Session\AccountInterface;
class PhotosAccess extends AccessPluginBase implements CacheableDependencyInterface {
protected $entityTypeManager;
protected $routeMatch;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_manager, RouteMatchInterface $route_match) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_manager;
$this->routeMatch = $route_match;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('current_route_match'));
}
public function summaryTitle() {
return $this
->t('Album privacy settings');
}
public function access(AccountInterface $account) {
$access = FALSE;
photos_access_request_album_password();
$nid = $this->routeMatch
->getRawParameter('node');
$photosAccessNode = _photos_access_pass_type($nid);
$uid = FALSE;
if (isset($photosAccessNode['node'])) {
$uid = $photosAccessNode['node']->uid;
}
elseif (isset($photosAccessNode['view'])) {
$uid = $photosAccessNode['view']->uid;
}
if ($uid && $account
->id() == $uid) {
$access = TRUE;
}
if ($account
->hasPermission('view photo')) {
if (isset($photosAccessNode['node']->viewid) && $photosAccessNode['node']->viewid != 3) {
$node = $this->entityTypeManager
->getStorage('node')
->load($photosAccessNode['node']->nid);
$access = $node
->access('view');
}
elseif (isset($photosAccessNode['view']->pass)) {
$access = FALSE;
if (isset($_SESSION[$photosAccessNode['view']->nid . '_' . session_id()]) && $photosAccessNode['view']->pass == $_SESSION[$photosAccessNode['view']->nid . '_' . session_id()] || !photos_access_pass_validate($photosAccessNode)) {
$access = TRUE;
}
}
else {
$access = $account
->hasPermission('view photo');
}
}
if ($access == FALSE) {
throw new AccessDeniedHttpException();
}
return $access;
}
public function alterRouteDefinition(Route $route) {
$route
->setRequirement('_access', 'TRUE');
}
public function getCacheMaxAge() {
return Cache::PERMANENT;
}
public function getCacheContexts() {
return [
'user.node_grants:view',
];
}
public function getCacheTags() {
$nid = $this->routeMatch
->getRawParameter('node');
return [
'photos:album:' . $nid,
'node:' . $nid,
];
}
}