Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow some users to create sub-objects only in objects they’re the authors of #115

Closed
arkhi opened this issue Mar 15, 2021 · 3 comments
Closed
Assignees
Labels
bug Something isn't working fixed Issue has already been fixed

Comments

@arkhi
Copy link

arkhi commented Mar 15, 2021

This Issue is detailing the discussion I initiated on Discord.

With this structure as example:

root
  |- home
  |- students
    |- joe's page 
# user/config/groups.yaml
[…]
students:
  groupname: students
  […]
  access:
    admin:
      super: 'false'
      login: 'true'
      pages:
        create: 'false'
        read: 'false'
        update: 'false'
        delete: 'false'
        list: 'true'
[…]
  1. I log in with joe who belongs to the students group (I can only list pages).
  2. A super admin has set up the following permissions to joe’s page:
    permissions:
        authors:
            - joe
        groups:
            authors:
                create: true
                read: true
                update: true
  3. I’m trying to create first post into joe’s page (like a personal journal).
  4. first post is temporarily created, waiting to be saved (:princess:).
  5. I click the Save button.
  6. An error Save Failed: You have insufficient permissions for task save appears and my changes have been discarded.

I tried the following on the function taskSave in flex-objects/classes/Admin/AdminController.php, from:

if (!$object->isAuthorized('create', 'admin', $this->user)) {

to:

if (
    !$object->isAuthorized('create', 'admin', $this->user)
    && !$object->parent()->isAuthorized('create', 'admin', $this->user)
) {

which goal is to allow the creation of sub-objects if the logged-in user has the create permissions on the parent as well.

I also needed to update the theme with a function that kicks in to add the user as the author by default, so that it inherits the permissions. Otherwise, Joe does not have any access to the page:

    /**
     * Handle restrictions and initialization of the theme as early as possible.
     * […]
     */
    public function onThemeInitialized(Event $event)
    {
        […]

        $this->enable([
            'onAdminCreatePageFrontmatter' => [
                [ 'addPermissionsToPageOwned', 1 ],
            ],
        ]);
    }

    /**
     * [onAdminCreatePageFrontmatter]
     * Add the current user as author in the header when creating a page.
     * Also allows them to delete the page they created.
     * […]
     */
    public function addPermissionsToPageOwned( Event $event )
    {
        $page   = $event;
        $header = $page['header'];

        if (!isset($header['permissions']['authors'])) {
            $header['permissions']['authors'] = [
                $this->grav['admin']->user->get('username')
            ];
        }

        $header['permissions']['groups']['authors']['delete'] = true;

        $event['header'] = $header;
    }

Not sure if it’s any good way to solve this Issue and if it does not create other issues, but it works for me™ for now. :)

@mahagr mahagr self-assigned this Mar 15, 2021
@mahagr
Copy link
Contributor

mahagr commented Mar 15, 2021

I need to check this out.

But really, if the object doesn't exist, ACL should really only check the parent, if it is allowed to create the object.

PS: your fix adding parent breaks every other flex object type.

@arkhi
Copy link
Author

arkhi commented Mar 15, 2021

PS: your fix adding parent breaks every other flex object type.

That is less than ideal indeed; thanks for the heads up. :)

@mahagr mahagr added bug Something isn't working fixed Issue has already been fixed labels Mar 18, 2021
@mahagr
Copy link
Contributor

mahagr commented Mar 18, 2021

Grav fix is also needed: getgrav/grav@1c24f9f

I basically changed $object->isAuthorized('create') behavior so that it automatically detects if you are creating the current page or if you are asking if you can create a new page under the existing page.

In short:

  • If the page exists, create has the current behavior: can you create a page under the current page?
  • If the page does not exist, create has a new behavior: can you create this page under the parent page?

PS: This is a backward-incompatible change and causes no access to the new page if you rely on the old behavior.

@mahagr mahagr closed this as completed Mar 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Issue has already been fixed
Projects
None yet
Development

No branches or pull requests

2 participants