on_user_add hook
Permalink
Hi,
I've created a hook for on_add_user event, it creates group, adds user to that group, creates a page for that user and sets permissions allowing user to edit that page. All works fine until I delete this user, after that when I try to delete page created for that user I get following error:
Unable to load sitemap data. Response received:
Fatal error: Call to a member function getUserName() on a non-object in C:\wamp\www\chev-retailers\public_html\concrete\models\collection_version.php on line 97
code:
<?php
public function addRetailer($ui)
{
// user name is also a page name
$pageName = $ui->uName;
Loader::model('collection_types');
Loader::model('file_set');
// create retailer unique group
$g = Group::add($ui->uName, $ui->uName . " - retailer group only");
// add retailer to this group
//$ui->updateGroups(array($g->gID));
// nope - event fires too early, c5 updates user groups after this is fired
// it checks post data to add user to selected groups
$_POST['gID'] = array($g->gID , 4); // 4 - dealers
// set all arguments required to Update permissions
$args['cInheritPermissionsFrom'] = 'OVERRIDE';
$args['collectionRead'] = array('gID:' . $g->gID , 'gID:1' , 'gID:2'); // 1 - guests, 2 - regstered users
$args['collectionReadVersions'][] = 'gID:' . $g->gID;
$args['collectionWrite'][] = 'gID:' . $g->gID;
// As far as I can tell you can't get the home page using Page::getByPath
//$addPage = Page::getByPath('home');
$addPage = Page::getByID(1);
$data = array();
$data['cName'] = $pageName;
$data['cDescription'] = $pageName . " - landing page";
$ct = CollectionType::getByHandle('retailerPage');
// try to find this page
$pageCheck = Page::getByPath("/$pageName");
if ($pageCheck->error) {
$c = $addPage->add($ct, $data);
$c->updatePermissions($args);
} else {
// page already exists, just update permissions
$pageCheck->updatePermissions($args);
}
Is there anything obvoius I'm missing?
I'm just starting my adventure with C5 so please forve me my ignorance. :-)
I've created a hook for on_add_user event, it creates group, adds user to that group, creates a page for that user and sets permissions allowing user to edit that page. All works fine until I delete this user, after that when I try to delete page created for that user I get following error:
Unable to load sitemap data. Response received:
Fatal error: Call to a member function getUserName() on a non-object in C:\wamp\www\chev-retailers\public_html\concrete\models\collection_version.php on line 97
code:
<?php
public function addRetailer($ui)
{
// user name is also a page name
$pageName = $ui->uName;
Loader::model('collection_types');
Loader::model('file_set');
// create retailer unique group
$g = Group::add($ui->uName, $ui->uName . " - retailer group only");
// add retailer to this group
//$ui->updateGroups(array($g->gID));
// nope - event fires too early, c5 updates user groups after this is fired
// it checks post data to add user to selected groups
$_POST['gID'] = array($g->gID , 4); // 4 - dealers
// set all arguments required to Update permissions
$args['cInheritPermissionsFrom'] = 'OVERRIDE';
$args['collectionRead'] = array('gID:' . $g->gID , 'gID:1' , 'gID:2'); // 1 - guests, 2 - regstered users
$args['collectionReadVersions'][] = 'gID:' . $g->gID;
$args['collectionWrite'][] = 'gID:' . $g->gID;
// As far as I can tell you can't get the home page using Page::getByPath
//$addPage = Page::getByPath('home');
$addPage = Page::getByID(1);
$data = array();
$data['cName'] = $pageName;
$data['cDescription'] = $pageName . " - landing page";
$ct = CollectionType::getByHandle('retailerPage');
// try to find this page
$pageCheck = Page::getByPath("/$pageName");
if ($pageCheck->error) {
$c = $addPage->add($ct, $data);
$c->updatePermissions($args);
} else {
// page already exists, just update permissions
$pageCheck->updatePermissions($args);
}
Is there anything obvoius I'm missing?
I'm just starting my adventure with C5 so please forve me my ignorance. :-)
looks like in CollectionVersion::get(), it should be testing that the UserInfo objects load first before trying to access its methods, like:
thx for your reply, with your patch applied it works fine now.(sorry for not enclosing code in [code] tags :-)) Unfortunatelly it's a hack in a c5 core, do you know it this will be fixed in next release? otherwise I'll have to remember to patch every time I update c5 :-(
yes, this change has also now been added to the core. thx for catching this.