I managed to get it to work as I would like. I have one little area I need to look at but it's more to do with the way things are on my site.
I had to create a class to go with the plugin (got the idea from TransAlias) and I borrowed a function from DLphx for webgroups by id.
It creates a document resource using your default template and designated parent based on a user group and will assign a permissions group based on permission group name.
Plugin: OnWebSaveUser, OnWebDeleteUser
/**
* CreateUserDocument
*
* Create a child document in designated Resource
*
* @category plugin
* @version 1.0
* @internal @properties &employeeGroup=Employee Group;text;Employees &parentDocument=Document to save child to;text;0 &documentGroup=Document Group Access;text;0
* @internal @events onWebSaveUser, onWebDeleteUser
* @internal @modx_category Content
* @internal @disabled 0
**/
// Maintains child documents based on users web group and saves them to a designated resource
if(!defined('MODX_BASE_PATH')){die('What are you doing? Get out of here!');}
if (!isset ($plugin_dir) ) { $plugin_dir = 'createUserDocument'; }
if (!isset ($plugin_path) ) { $plugin_path = $modx->config['base_path'].'assets/plugins/'.$plugin_dir; }
if (!class_exists('createUserDocument')) {
require_once $plugin_path.'/createuserdocument.class.inc.php';
}
$cud = new createUserDocument($modx);
// convert employees into an array
$webgroup = explode(",", $employeeGroup);
$e = &$modx->Event;
switch ($e->name) {
case 'OnWebSaveUser':
// We need to check if it matches the desginated web group
$userid = $params['userid'];
$fullname = $params['userfullname'];
$userGroup = $cud->isMemberOfWebGroupByUserId($userid, $webgroup);
$docExists = $cud->docExists($userid, $parentDocument);
if ( $userGroup ) {
if ( $docExists < 1 ) {
// We have a matched webgroup.
// We now need to check if we already have a document for this user
$cud->createDocument($fullname, $userid, $parentDocument, $documentGroup);
}
} else {
$cud->deleteDocument($params['userid'], $parentDocument, $documentGroup);
}
break;
case 'OnWebDeleteUser':
// We should delete the create user document and access group record.
$cud->deleteDocument($params['userid'], $parentDocument, $documentGroup);
break;
default:
break;
}
Class: create in plugins\createUserDocument (requisite: must have DocLister Installed)
<?php
class createUserDocument {
public $cache = array(
'cm' => array(),
'ui' => array(),
'mo' => array()
);
public $doc;
function __construct() {
$modx = EvolutionCMS();
include_once(MODX_BASE_PATH."assets/lib/MODxAPI/modResource.php");
$this->doc = new modResource($modx);
}
function docExists ( $userid, $parent ) {
$modx = EvolutionCMS();
$tbl = $modx->getFullTableName("site_content");
$sql = "SELECT id FROM {$tbl} WHERE longtitle={$userid} AND parent={$parent} LIMIT 1;";
$ret = $modx->db->getColumn("id", $sql);
$modx->logEvent(0, 1, print_r($ret, true), 'createUserDocument');
// return the first record!
return $ret[0];
}
function createDocument ( $fullname, $userid, $parent, $documentGroup ) {
$modx = EvolutionCMS();
$this->doc->create(array(
'pagetitle' => $fullname,
'longtitle' => $userid,
'template' => $modx->getConfig('default_template'),
'parent' => $parent,
'published' => 0,
'hidemenu' => 1
));
$id = $this->doc->save(false,true);
// Do we need to assign permissions?
if ( isset($documentGroup) ) {
// Create document permission
$tbl = $modx->getFullTableName("documentgroup_names");
$sql = "SELECT * FROM {$tbl} WHERE name='{$documentGroup}' LIMIT 1;";
$ret = $modx->db->getColumn("id", $sql);
$docGrp = $ret[0];
$fields = array(
'document' => $id,
'document_group' => $docGrp
);
$insertid = $modx->db->insert($fields, $modx->getFullTableName("document_groups") );
}
}
function deleteDocument ( $userid, $parent, $documentGroup ) {
$modx = EvolutionCMS();
$id = $this->docExists($userid, $parent);
$modx->logEvent(0, 1, "deleteDocument1: {$userid} {$parent} {$id}", 'createUserDocument');
if ( $id > 0 ) {
//$this->doc->delete($docExists);
$modx->logEvent(0, 1, "delete it {$id}", 'createUserDocument');
if ($documentgroup) $modx->db->delete($modx->getFullTableName("document_groups"), "document={$id}");
$modx->db->delete($modx->getFullTableName("site_content"), "id={$id}");
}
}
// Returns true if the user id is in one the specified webgroups
/**
* @param int $userid
* @param array $groupNames
* @return bool
*/
function isMemberOfWebGroupByUserId($userid = 0, $groupNames = array()) {
$modx = evolutionCMS();
$userid = (int)$userid;
$sql = "SELECT `wgn`.`name` FROM {$tbl} `wgn` INNER JOIN {$tbl2} `wg` ON `wg`.`webgroup`=`wgn`.`id` AND `wg`.`webuser`={$userid}";
//$modx->logEvent(0, 1, $sql, 'createUserDocument');
// if $groupNames is not an array return false
if (!is_array($groupNames)) {
return false;
}
// if the user id is a negative number make it positive
if (intval($userid) < 0) {
$userid = -($userid);
}
// Creates an array with all webgroups the user id is in
if (!array_key_exists($userid, $this->cache["mo"])) {
$tbl = $modx->getFullTableName("webgroup_names");
$tbl2 = $modx->getFullTableName("web_groups");
$sql = "SELECT `wgn`.`name` FROM {$tbl} `wgn` INNER JOIN {$tbl2} `wg` ON `wg`.`webgroup`=`wgn`.`id` AND `wg`.`webuser`={$userid}";
$this->cache["mo"][$userid] = $grpNames = $modx->db->getColumn("name", $sql);
} else {
$grpNames = $this->cache["mo"][$userid];
}
// Check if a supplied group matches a webgroup from the array we just created
foreach ($groupNames as $k => $v) {
if (in_array(trim($v), $grpNames)) {
return true;
}
}
// If we get here the above logic did not find a match, so return false
return false;
}
}
?>
I've cut a few corners. I found I couldn't make full use of "fullname" as you cannot get it during the delete process as you only get the deleted user id after the record has been deleted. So I opted to put the user ID in the longtitle.