如何将新创建的用户分配到modx Programmaticaly 中的特定群组?以下是我的代码
if(isset($_POST) && count($_POST)){
$oUser = $modx->newObject('modUser');
$oUser->set('username', "test");
//$oUser->set('password', "test");
$oProfile = $modx->newObject('modUserProfile');
$oProfile->set('fullname', $_POST['fname']);
$oProfile->set('email', $_POST['email']);
$oUser->addOne($oProfile);
if($oUser->save()===false){
echo "Error";
}else
echo "Done";
}
我用谷歌搜索但我发现的是图形教程如何创建组并编辑用户然后分配角色,如果你知道任何教程那么它也没关系。
答案 0 :(得分:2)
以下是我一直在做的事情,这是一个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-&gt; joinGroup($ joingroup,$ joinrole);其中joingroup是组id~或name,joinrole是角色id~或name。这里记录了:http://api.modx.com/revolution/2.1/_model_modx_moduser.class.html#%5CmodUser::joinGroup()
答案 1 :(得分:2)
在revo&gt; 2.2中创建/编辑内容的最佳方法是使用“基于类的处理器” - https://www.markhamstra.com/modx-blog/2012/2/getting-started-with-class-based-processors-2.2/将用户添加到组,使用此处理器https://github.com/modxcms/revolution/blob/develop/core/model/modx/processors/security/user/update.class.php - http://rtfm.modx.com/display/revolution20/Using+runProcessor
$param = array(
'id' => 1, // user id
'groups' => array(
array(
"usergroup" => 1,
"name" => "Administrator",
"member" => 1,
"role" => 2,
"rolename" => "Super User",
"primary_group" => true,
"rank" => 0,
"menu" => null
),
array( .... )
)
);
$response = $modx->runProcessor('security/user/update',$param );
if ($response->isError()) {
return $response->getMessage();
}