Think of the childrens!
That’s a good point, Miguel... I think the permissions are not inherited. I wrote a script that would copy a folder and update permissions on each child document in the folder. This is kinda heady stuff, and I simplified it a lot so I could post it here, but it should at least give you an idea of which API functions you need to research:
<?php
private function _duplicate($args)
{
// Load up the master doc (e.g. a folder) to be copied
$masterdoc = $modx->getObject('modResource', 123);
// This will actually CREATE the doc.
$new_doc = $masterdoc->duplicate();
if ( !is_object($new_doc) )
{
die('There was a problem duplicating the resource.');
}
$homepage_id = $new_doc->get('id');
// Special handling for the homepage
$new_doc->set('pagetitle', 'Some name' );
$new_doc->set('menutitle', 'Some menu title' );
$new_doc->set('alias', urlify('my-alias') );
$new_doc->set('published', 1); // 0 || 1
$new_doc->save();
// add resource group (class var used in another function)
$this->userResourceGroup = $modx->getObject('modResourceGroup', array('name'=> 'Name of my Resource Group') );
// add user group
$userUserGroup = $modx->getObject('modUserGroup', array('name'=> 'Name of my User Group' )
//add resource group access
$resourceGroupAccess = $modx->newObject('modAccessResourceGroup');
$resourceGroupAccess->set('target', $this->userResourceGroup->get('id'));
$resourceGroupAccess->set('principal_class', 'modUserGroup');
$resourceGroupAccess->set('principal', $userUserGroup->get('id'));
$resourceGroupAccess->set('authority', 9999);
$resourceGroupAccess->set('policy', 4 ); // 4 for Load, List and View
$resourceGroupAccess->set('context_key', 'web');
$resourceGroupAccess->save();
// Process the children
$this->_process_children_resources($new_doc);
}
/*------------------------------------------------------------------------------
INPUT: MODx resource object, loops in on itself
------------------------------------------------------------------------------*/
private function _process_children_resources($resource)
{
$children = $resource->getMany('Children');
foreach ($children as $child) {
$child->set('published', 1); // Children docs published by default
$child->save();
$this->_join_resource_groups($child);
// down the rabit hole
$this->_process_children_resources($child);
}
}
/*------------------------------------------------------------------------------
Removes a $doc from any resource groups it currently is in, then adds it to
the whatever group was defined/created above via $this->userResourceGroup
------------------------------------------------------------------------------*/
private function _join_resource_groups($doc)
{
// Clear all existing access permissions
$existing_resourceGroupResource = $modx->getCollection('modResourceGroupResource', array('document'=>$doc->get('id') ) );
if ($existing_resourceGroupResource)
{
foreach ($existing_resourceGroupResource as $r)
{
$r->remove();
}
}
//add resource to resource group
$resourceGroupResource = $modx->newObject('modResourceGroupResource');
$resourceGroupResource->set('document', $doc->get('id'));
$resourceGroupResource->set('document_group', $this->userResourceGroup->get('id'));
$resourceGroupResource->save();
}