1

如何将新创建的用户分配给 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";
}

我用谷歌搜索,但我发现的只是图形教程如何创建组和编辑用户然后分配角色,如果你知道任何教程,那么它也很好。

4

2 回答 2

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->joinGroup($joingroup, $joinrole); 其中 joingroup 是组 ID ~ 或名称,而 joinrole 是角色 ID ~ 或名称。它记录在这里:http ://api.modx.com/revolution/2.1/_model_modx_moduser.class.html#%5CmodUser::joinGroup ()

于 2012-12-12T15:00:28.493 回答
2

在 revo > 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();
}
于 2012-12-12T15:47:52.847 回答