XF 2.1 Is a permission rebuild required?

Lawrence

Well-known member
I'm adding the clone node option (which is nearly complete) to my admin tools add-on . When you select to clone a node, there are two check boxes that are defaulted checked, and they are: clone content moderators (for that node), and clone the node permissions. As the permissions themselves are not changed for the content moderators and the node (just the content id), is a rebuild required? I'm thinking no, as the actual permissions are not changed, however; I would like to be sure before the updated add-on is released. Any feedback is appreciated.

Thank-you,

Lawrence
 
The node entity already schedules a permission rebuild job if you are inserting or changing a node's parent, so if your tool does the duplication process all in one, then the scheduled rebuild would already pick it up. If you were to do a permission clone after the fact, for example, then you would need a permission rebuild.

Worth mentioning that node moderators and node permissions are related. If you clone the moderators, you basically need to at least clone their permissions or they won't have any extra permissions; moderator permissions are just specific user permissions on the node. Similarly, if you clone node permissions, you'd likely end up cloning moderator permissions (but not adding the content moderator records). There isn't anything inherently wrong with that but it's probably not what people expect.

So if you are going to have multiple checkboxes here, you probably need to be careful with what you're actually copying. Otherwise, "clone moderators and permissions" makes more sense to me.
 
Thank you Mike.

I extend the actionSave and check to see if a clone_id is present, if not return the parent.

PHP:
    public function actionSave(ParameterBag $params)
    {

        if (!$clonedId = $this->filter('cloned_id', 'int'))
        {
            return parent::actionSave($params);
        }
       
        $node = $this->em()->create('XF:Node');
        $node->node_type_id = $this->getNodeTypeId();

        $this->nodeSaveProcess($node)->run();

        if ($this->filter('clone_mods', 'bool'))
        {
            $moderators = \XF::finder('XF:ModeratorContent')
                ->where('content_type', 'node')
                ->where('content_id', $clonedId)
                ->fetch();
               
            foreach ($moderators AS $moderator)
            {
                $contentMod = $this->em()->create('XF:ModeratorContent');
                $contentMod->content_type = $moderator->content_type;
                $contentMod->content_id = $node->node_id;
                $contentMod->user_id = $moderator->user_id;
               
                $contentMod->save();

                $contentPerms = \XF::finder('XF:PermissionEntryContent')
                    ->where('content_type', 'node')
                    ->where('content_id', $clonedId)
                    ->where('user_id', $moderator->user_id)
                    ->fetch()
                    ->toArray();

                $inserter = $this->service('EAEAddons\AdminTools:InsertPermissions');
                $inserter->setUserId($moderator->user_id);
                $inserter->setContentType('node');
                $inserter->setContentId($node->node_id);
               
                $inserter->insertPerms($contentPerms);
            }
        }
       
        if ($this->filter('clone_perms', 'bool'))
        {
            // working on this part today
        }
        return $this->redirect($this->buildLink('nodes') . $this->buildLinkHash($node->node_id));
    }

I need to run this first $this->nodeSaveProcess($node)->run(); to get the newly cloned node id first, before I can update the source content moderators and their permissions for the new node. And this all works fine.

So, when I run the nodeSaveProcess, permissions get rebuilt then? And then when I insert the source node permissions into the new node the rebuild permissions will have to be triggered again? I would rather it be run just once when all is said and done, but I need the new node's node_id first...
 
... Similarly, if you clone node permissions, you'd likely end up cloning moderator permissions (but not adding the content moderator records)...

I just discovered that, thanks @Mike . I re-looked over my XF 1 add-on and seen that I only copied the permissions once. In the above example code it would be coping permissions for content moderators twice, so it's not needed.

So if you are going to have multiple checkboxes here, you probably need to be careful with what you're actually copying. Otherwise, "clone moderators and permissions" makes more sense to me.

It does for me too, now, I'll change it to Clone moderators and permissions. Thanks again, :)
 
Just an update. The clone nodes is done, and the rebuilding of the permissions happens once at the end, automatically.
 
I replaced the above code with this:

PHP:
    public function actionSave(ParameterBag $params)
    {
        if (!$clonedId = $this->filter('cloned_id', 'int'))
        {
            return parent::actionSave($params);
        }
        
        $node = $this->em()->create('XF:Node');
        $node->node_type_id = $this->getNodeTypeId();

        $this->nodeSaveProcess($node)->run();

        if ($this->filter('clone_extras', 'bool'))
        {
            $clonePlugin = $this->plugin('EAEAddons\AdminTools:CloneExtras');

            $clonePlugin->insertContentMods($clonedId, $node->node_id);
            $clonePlugin->insertContentPerms($clonedId, $node->node_id);
        }
        return $this->redirect($this->buildLink('nodes') . $this->buildLinkHash($node->node_id));
    }

I went with a controller plugin as each node type will require the same code, so this will help keep the add-on a little smaller in size.
 
Top Bottom