简体   繁体   English

如何用php更改Joomla用户组

[英]How to change a Joomla User Group with php

In a Joomla 2.5 site I want to assign a registered user to another group after they complete a form. 在Joomla 2.5站点中,我想在完成表单后将注册用户分配给另一个组。 The user would be logged in when they complete the form which will be created in Fabrik so that I can add a PHP script to run on save. 用户在完成将在Fabrik中创建的表单时将登录,以便我可以添加PHP脚本以在保存时运行。 My php skills are rudimentary. 我的PHP技能很简陋。 What would the lines in php look like that adds the current user to a group? php中的行看起来会将当前用户添加到组中?

You can use JUserHelper::setUserGroups to set the group(s) of a user. 您可以使用JUserHelper :: setUserGroups来设置用户的组。

To retrieve the current user you can use JFactory::getUser . 要检索当前用户,您可以使用JFactory :: getUser

When you have the user, you can (I haven't confirmed this) probably get the id right away. 当你有这个用户时,你可以(我还没有证实这一点)可能马上获得你的身份 Some non-tested code which will hopefully get you on the way: 一些未经测试的代码,希望能帮助您:

 $user = JFactory::getUser();
 $userId = $user->id;
 JUserHelper::setUserGroups($userId, 3);

Or in short: 或者简而言之:

 JUserHelper::setUserGroups(JFactory::getUser()->id, 3); // 3 Is the group number

You can find the group ID's by going into the table-prefix_usergroups table and retrieve the primary key. 您可以通过进入table-prefix_usergroups表并检索主键来查找组ID。 I haven't found a way to retrieve a list of groups without going into the database, but this may help. 我还没有找到一个方法来检索组的列表没有进入数据库,但是可能会有帮助。

A screenshot of my (non-edited) database with usergroups: 我的(未编辑的)数据库与用户组的屏幕截图:

用户组joomla 2.5

Here I used a different approach as JUserHelper::setUserGroups method always failed with my Joomla 3.3.1 installation. 在这里,我使用了一种不同的方法,因为JUserHelper :: setUserGroups方法总是因我的Joomla 3.3.1安装而失败。

$user = JFactory::getUser();//get current user
$g = array('2'=>2,'3'=>3);//2 for registered user, 3 for author
$user->groups = $g;
$user->save();

You may also want to look at the addUserToGroup method (since Joomla Platform 11.1) 您可能还想查看addUserToGroup方法(从Joomla Platform 11.1开始)

JUserHelper::addUserToGroup (JFactory::getUser()->id, 3); // 3 Is the group number

So for example if your user was previously in the 'registered' group (id=2), he/she will also be assigned to group 3 after this. 因此,例如,如果您的用户之前处于“已注册”组(id = 2),则此后他/她也将被分配到第3组。

Similarly there is also the removeUserFromGroup method which does the opposite. 类似地,还有removeUserFromGroup方法,它执行相反的操作。

As per the documentation, it may be useful to note that the setUserGroups method supports an array for the groups, whereas the other 2 methods I mention here support an integer. 根据文档,注意setUserGroups方法支持组的数组可能是有用的,而我在这里提到的其他2个方法支持整数。

setUserGroups(integer $userId, array $groups) : boolean setUserGroups(integer $ userId,array $ groups):boolean

addUserToGroup(integer $userId, integer $groupId) : boolean addUserToGroup(integer $ userId,integer $ groupId):boolean

removeUserFromGroup(integer $userId, integer $groupId) : boolean removeUserFromGroup(integer $ userId,integer $ groupId):boolean

https://api.joomla.org/cms-3/classes/JUserHelper.html https://api.joomla.org/cms-3/classes/JUserHelper.html

I'm using Joomla 3.7.5, and this method worked for me: 我正在使用Joomla 3.7.5,这种方法对我有用:

JUserHelper::setUserGroups($joomlaId, array(10));

Where, $joomlaId is the id of the user I want to change. 其中,$ joomlaId是我想要更改的用户的id。 This sets the user to only be a member of group 10, and saves the changes to the user DB immediately. 这将用户设置为仅作为组10的成员,并立即将更改保存到用户DB。

I couldn't find any useful way to do this in joomla 3, so here's a quickie for people who just need this to work: 我在joomla 3中找不到任何有用的方法来做到这一点,所以对于那些只需要这个工作的人来说,这是一个快速的方法:

$db = JFactory::getDbo();
$db->setQuery("replace into xo6uf_user_usergroup_map values ($user_id, $group_id)");
$db->query();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM