Commit 61090c4e authored by Nathaniel Burnett's avatar Nathaniel Burnett
Browse files

Issue #3105818 by Nathaniel: Cleanup admin settings and access checks. Some other code cleanup.

parent f39cfc96
Loading
Loading
Loading
Loading
+3 −4
Changes for config/install/photos.settings.yml: 3 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -5,20 +5,19 @@ photos_display_full_imagesize: large
photos_display_full_viewnum: 10
photos_display_imageorder: weight|asc
photos_display_list_imagesize: large
photos_display_page_display: 1
photos_display_page_display: 0
photos_display_page_user: true
photos_display_teaser_display: 1
photos_display_teaser_display: 0
photos_display_teaser_imagesize: medium
photos_display_teaser_user: true
photos_display_teaser_viewnum: 5
photos_display_user: true
photos_display_user: false
photos_display_view_imagesize: large
photos_display_viewpager: 10
photos_image_count: true
photos_num: 5
photos_pager_imagesize: thumbnail
photos_plupload_status: false
photos_private_file_styles: automatic
photos_size:
  thumbnail: Small
  large: Large
+0 −3
Changes for config/schema/photos.schema.yml: 0 added lines, 3 removed lines.
Original line number Diff line number Diff line
@@ -82,9 +82,6 @@ photos.settings:
    photos_plupload_status:
      type: boolean
      label: 'Enable plupload module integration.'
    photos_private_file_styles:
      type: string
      label: 'Private image style setting.'
    photos_size:
      type: sequence
      label: 'Image style options available in album node settings.'
+11 −149
Changes for photos.module: 11 added lines, 149 removed lines.
Original line number Diff line number Diff line
@@ -10,16 +10,14 @@ use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\File\Exception\FileException;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\exif\ExifContent;
use Drupal\image\Entity\ImageStyle;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\photos\Form\PhotosMediaLibraryForm;
use Drupal\photos\PhotosAlbum;
@@ -27,134 +25,6 @@ use Drupal\photos\Entity\PhotosImage;
use Drupal\photos\PhotosImageInterface;
use Drupal\user\UserInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Photos access checks for different operations.
 *
 * @param string $op
 *   Operation to check access.
 * @param mixed $value
 *   $node, $user, $file->id() OR $node->id().
 *
 * @return bool
 *   TRUE if user is allowed access content, otherwise FALSE.
 */
function _photos_access($op, $value) {
  $user = \Drupal::currentUser();
  switch ($op) {

    case 'viewUser':
      return ($value->id() && $value->hasPermission('create photo') || \Drupal::currentUser()->hasPermission('access user profiles') && \Drupal::currentUser()->hasPermission('view photo'));

    case 'imageOrig':
      if (!\Drupal::currentUser()->hasPermission('view original')) {
        return FALSE;
      }
      else {
        return TRUE;
      }

    case 'imageView':
      // Value is fid, check if user can view this photo's album.
      if ($user->id() == 1) {
        return TRUE;
      }
      if (\Drupal::config('photos.settings')->get('photos_access_photos')) {
        // Check if album password is required.
        photos_access_request_album_password();
        $node = _photos_access_pass_type($value);
        $uid = FALSE;
        // Check if user is node author.
        if (isset($node['node'])) {
          $uid = $node['node']->uid;
        }
        elseif (isset($node['view'])) {
          $uid = $node['view']->uid;
        }
        if ($uid && $user->id() == $uid) {
          return TRUE;
        }
        if (\Drupal::currentUser()->hasPermission('view photo')) {
          if (isset($node['node']->viewid) && $node['node']->viewid < 3) {
            // Check node access.
            $node = Node::load($node['node']->nid);
            return $node->access('view');
          }
          elseif (isset($node['node']) && $node['node']->viewid == 4) {
            // @todo move logic.
            // Check role access.
            $accountRoles = \Drupal::currentUser()->getRoles();
            $node = Node::load($node['node']->nid);
            if ($node && isset($node->privacy) && isset($node->privacy['roles'])) {
              if (count(array_intersect($accountRoles, $node->privacy['roles'])) !== 0) {
                return \Drupal::currentUser()->hasPermission('view photo');
              }
            }
          }
          elseif (isset($node['view']) && $node['view']->viewid == 3 && isset($node['view']->pass)) {
            // Check password.
            if (isset($_SESSION[$node['view']->nid . '_' . session_id()]) && $node['view']->pass == $_SESSION[$node['view']->nid . '_' . session_id()] || !photos_access_pass_validate($node)) {
              return TRUE;
            }
          }
        }
        return \Drupal::currentUser()->hasPermission('view photo');
      }
      else {
        return \Drupal::currentUser()->hasPermission('view photo');
      }

    case 'album':
      if (\Drupal::config('photos.settings')->get('photos_access_photos')) {
        // Check if album password is required.
        photos_access_request_album_password();
      }
      return ($value->getType() == 'photos') && $value->access('view');

    case 'editAlbum':
      if ($value->getType() == 'photos') {
        return $value->access('update');
      }
      break;

    case 'imageEdit':
      // @todo use entity id instead of fid.
      // @todo move to entity access.
      if (!is_object($value)) {
        $db = \Drupal::database();
        $query = $db->select('node', 'n');
        $query->join('photos_image_field_data', 'p', 'p.album_id = n.nid');
        $query->fields('n', ['nid'])
          ->condition('p.id', $value);
        $nid = $query->execute()->fetchField();
        if ($nid) {
          $value = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
        }
        else {
          // Not found.
          throw new NotFoundHttpException();
        }
      }
      return $value->access('update') || $value->access('delete');

    case 'imageDelete':
      // @todo use entity id instead of fid.
      // @todo move to entity access.
      if (!is_object($value)) {
        $db = \Drupal::database();
        $query = $db->select('node', 'n');
        $query->join('photos_image_field_data', 'p', 'p.album_id = n.nid');
        $query->fields('n', ['nid'])
          ->condition('p.id', $value);
        $nid = $query->execute()->fetchField();
        $value = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
      }
      return $value->access('delete');

  }
  return FALSE;
}

/**
 * Implements hook_photos_access().
@@ -248,6 +118,7 @@ function photos_form_node_form_alter(&$form, FormStateInterface $form_state, $fo
    $view_imagesize = \Drupal::config('photos.settings')->get('photos_display_view_imagesize');
    $full_imagesize = \Drupal::config('photos.settings')->get('photos_display_full_imagesize');
    $teaser_imagesize = \Drupal::config('photos.settings')->get('photos_display_teaser_imagesize');
    $legacy_display_settings = \Drupal::config('photos.settings')->get('photos_legacy_view_mode');

    if (isset($node->album['list_imagesize'])) {
      $style_name = $node->album['list_imagesize'];
@@ -281,7 +152,8 @@ function photos_form_node_form_alter(&$form, FormStateInterface $form_state, $fo
      }
    }

    if ($photos_global || $photos_page || $photos_teaser) {
    // @todo this is deprecated. Use display modes instead.
    if ($legacy_display_settings && ($photos_global || $photos_page || $photos_teaser)) {
      $form['photos'] = [
        '#type' => 'details',
        '#title' => t('Album settings'),
@@ -857,7 +729,6 @@ function photos_user_view(array &$build, EntityInterface $account, EntityViewDis
    if (\Drupal::currentUser()->hasPermission('view photo') || $account->hasPermission('create photo')) {
      $user = \Drupal::currentUser();
      if ($display->getComponent('photos_album_count')) {
        $description = '';
        if ($account->album['album']['count']) {
          $url = Url::fromUri('base:photos/user/' . $account->id() . '/album');
          $user_albums = \Drupal::translation()->formatPlural($account->album['album']['count'], '@count album', '@count albums');
@@ -1123,22 +994,7 @@ function photos_preprocess_photos_image_html(&$variables, $hook) {
 */
function photos_cron() {
  // Update photos count.
  PhotosAlbum::resetCount(1);
  // Delete temporary private image styles.
  if (\Drupal::config('photos.settings')->get('photos_private_file_styles') == 'automatic') {
    // @todo set duration e.g. 1 month / week / day?
    // @todo image styles are created now in private://styles/... This feature
    // may be obsolete?
    if (file_exists('private://photos/tmp_images')) {
      try {
        \Drupal::service('file_system')->deleteRecursive('private://photos/tmp_images');
        // @todo add new photos-album-private cache tag and invalidate here.
      }
      catch (FileException $e) {
        watchdog_exception('photos', $e);
      }
    }
  }
  PhotosAlbum::resetCount(TRUE);
}

/**
@@ -1451,7 +1307,13 @@ function template_preprocess_photos_image(array &$variables) {
    unset($variables['elements']['title']);
  }

  try {
    $variables['url'] = $photos_image->toUrl('canonical')->toString();
  }
  catch (EntityMalformedException $e) {
    watchdog_exception('photos', $e);
    $variables['url'] = '';
  }

  $variables['page'] = photos_image_is_page($photos_image);

+0 −7
Changes for photos.routing.yml: 0 added lines, 7 removed lines.
Original line number Diff line number Diff line
@@ -96,13 +96,6 @@ photos.album.recent:
  requirements:
    _permission: 'view photo'

photos.image.update:
  path: '/photos/image/update'
  defaults:
    _controller: '\Drupal\photos\Controller\PhotosEditController::ajaxEditUpdate'
  requirements:
    _permission: 'create photo'

photos.image.recent:
  path: '/photos/image'
  defaults:
+0 −23
Changes for photos_access/photos_access.module: 0 added lines, 23 removed lines.
Original line number Diff line number Diff line
@@ -319,7 +319,6 @@ function photos_access_move_files(Node $node, $public = TRUE) {
    $new_scheme = $default_scheme;
  }
  $cache_tags = [];
  $clear_private_styles = [];
  // @todo add a warning that changing privacy settings will move files.
  foreach ($results as $result) {
    $photosImage = NULL;
@@ -370,34 +369,12 @@ function photos_access_move_files(Node $node, $public = TRUE) {
          $dirname = $file_system->dirname($new_uri);
          $file_system->prepareDirectory($dirname, FileSystemInterface::CREATE_DIRECTORY);
          file_move($file, $new_uri);
          if ($new_scheme == 'public') {
            $path_info = pathinfo($uri);
            $ext = strtolower($path_info['extension']);
            $clear_private_styles[] = 'image_' . $fid . '.' . $ext;
          }
          // Clear image page cache.
          $cache_tags[] = 'photos:image:' . $fid;
        }
      }
    }
  }
  // Check private image style settings.
  $private_styles_setting = \Drupal::config('photos.settings')->get('photos_private_file_styles');
  if (!empty($clear_private_styles) && $private_styles_setting == 'automatic') {
    // Delete private image styles.
    $private_image_styles_path = $private_scheme . '://photos/tmp_images';
    foreach ($clear_private_styles as $basename) {
      // Find all derivatives for this image.
      if (file_exists($private_image_styles_path)) {
        $file_uris = \Drupal::service('file_system')
          ->scanDirectory($private_image_styles_path, '~\b' . $basename . '\b~');
        foreach ($file_uris as $uri => $data) {
          // Delete.
          \Drupal::service('file_system')->delete($uri);
        }
      }
    }
  }
  // Clear album page cache.
  $cache_tags[] = 'photos:album:' . $nid;
  // Invalidate image page and album page cache as needed.
Loading