Doen't work for existing nodes. (Must be "resaved")

Created on 14 December 2023, 6 months ago
Updated 18 December 2023, 6 months ago

2 years ago I built my own simplistic access control custom module that does more or less what is described in the Access Policy documentation ( group-content-by-department β†’ ). It works!
So, in our site all existing nodes and users settings are already populated with a proper "group" taxonomy.

A few days ago I discovered and installed Access Policy contrib module. (Beautifull module, congratulations. I dont understand why it is not more popular)
The big benefit is that I can now also control acces to medias

And now the issue.
After various atempts to make it work on our site I just realise that I have to edit and save all existing nodes to make Access Rules work.

Is there a simple straight way to do that, or should I use a dedicated piece of code.

πŸ’¬ Support request
Status

Closed: works as designed

Version

1.0

Component

Documentation

Created by

πŸ‡«πŸ‡·France gilbertdelyon

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @gilbertdelyon
  • πŸ‡«πŸ‡·France gilbertdelyon

    That's me again!
    Seems to work as follows:
    - Go to admin / content / filter published content / select all
    - Go to bottom of the page and save content/all selected elements
    A warning should be added to documentation because when a rule is not working as expected newbies like me can susspect a wrong setting while re-saving content will solve the issue.

  • πŸ‡ΊπŸ‡ΈUnited States partdigital

    Hi gilbertdelyon, thanks for reaching out!

    That is a clever workaround! Only risk with that approach is that it will assign the access policy based on the currently logged-in user. If you performed that action with a role that wasn't allowed to assign the "department" policy for example, it wouldn't change the policy, even if the department field had a value.

    This has been on my radar for a while, perhaps I can address this with a custom "Save default access policy" action? This would account for the original author.

    You can also achieve the same result with this code added to an update hook. Note that if you have a lot of nodes with missing policies you probably want to put this into a batch.

     $entityTypeManager = \Drupal::entityTypeManager();
      $selectionManager = \Drupal::service('access_policy.selection');
      $nids = $entityTypeManager->getStorage('node')->getQuery()
        ->accessCheck(FALSE)
        ->condition('access_policy', NULL, 'IS NULL')
        ->execute();
    
      $nodes = $entityTypeManager->getStorage('node')->loadMultiple($nids);
    
      foreach ($nodes as $node) {
        // Get the original author. This is necessary because different authors may
        // have access to different access policies.
        $owner = $node->getOwner();
        $policies = $selectionManager->getDefaultPolicy($node, $owner);
        if (!empty($policies)) {
          $node->set('access_policy', $policies);
          $node->save();
        }
      }
    
    
  • πŸ‡«πŸ‡·France gilbertdelyon

    Thank you for this prompt answer and for the piece of code.

    If you performed that action with a role that wasn't allowed to assign the "department" policy for example, it wouldn't change the policy, even if the department field had a value.

    As installing Acces Policy needs admin permissions I also resaved content with admin permissions, so I think this is not a big issue.

    BTW. It seems your code is dedicated to nodes. Would also work for existing medias? (only for my personal knowledge - I had no access control over medias before using Access Policy module)

  • πŸ‡ΊπŸ‡ΈUnited States partdigital

    Yes that code will work with media as well. If you want to do this dynamically you can use this method to fetch all the entity types that have access policies.

    \Drupal::service('access_policy.information')->getAllEnabledEntityTypes();
    
  • πŸ‡«πŸ‡·France gilbertdelyon

    Thank you, I appreciate
    So, I will close this issue.

  • Status changed to Closed: works as designed 6 months ago
  • πŸ‡«πŸ‡·France gilbertdelyon

    Closed

Production build 0.69.0 2024