这是我一直在做的事情,这是一个在用户注册后触发的 posthook 片段[并且用户被创建]
<?php
$specialty = $hook->getValue('specialty');
$country = strtolower($hook->getValue('excountry'));
$username = $hook->getValue('username');
$staff = $hook->getValue('staff-or-resident'); //Staff | Resident
$joingroup = '';
$joinrole = '';
$blockuser = 'false';
switch ($specialty){
case 'Other' :
$joingroup = 15; // Other
$joinrole = 1; //member
$blockuser = 'true';
break;
// there are about 15 different groups and roles here...
default :
$joingroup = '0'; // none
$joinrole = '0'; // none
break;
}
if($joingroup > 0 && $joinrole > 0){
$user = $modx->getObject('modUser', array('username'=>$username));
$internalkey = $user->get('id');
$profile = $user->getOne('Profile',array('internalKey'=>$internalkey));
$user->joinGroup($joingroup, $joinrole);
if($blockuser == 'true'){ //block user if they belong to the "other" group
$profile->set('blocked',1);
}
if(!$user->save()){
return false;
};
}
return true;
关键是:$user->joinGroup($joingroup, $joinrole); 其中 joingroup 是组 ID ~ 或名称,而 joinrole 是角色 ID ~ 或名称。它记录在这里:http ://api.modx.com/revolution/2.1/_model_modx_moduser.class.html#%5CmodUser::joinGroup ()