Skip to content

Adjust modUserGroup #15968

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/lexicon/en/user.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@
$_lang['user_group_new'] = 'Create User Group';
$_lang['user_group_parent'] = 'Parent Group';
$_lang['user_group_remove'] = 'Delete User Group';
$_lang['user_group_remove_confirm'] = 'Are you sure you want to delete the User Group: "[[+user_group]]"?';
$_lang['user_group_remove_confirm'] = 'Are you sure you want to delete the User Group: "[[+user_group]]" and all it\'s children?';
$_lang['user_group_settings'] = 'User Group Settings';
$_lang['user_group_settings_desc'] = 'Manage Settings for the User Group';
$_lang['user_group_untitled'] = 'Untitled User Group';
Expand Down
2 changes: 1 addition & 1 deletion core/model/schema/modx.mysql.schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1256,8 +1256,8 @@
</index>

<aggregate alias="Parent" class="MODX\Revolution\modUserGroup" local="parent" foreign="id" cardinality="one" owner="foreign" />
<aggregate alias="Children" class="MODX\Revolution\modUserGroup" local="id" foreign="parent" cardinality="many" owner="local" />
<aggregate alias="Dashboard" class="MODX\Revolution\modDashboard" local="dashboard" foreign="id" cardinality="one" owner="foreign" />
<composite alias="Children" class="MODX\Revolution\modUserGroup" local="id" foreign="parent" cardinality="many" owner="local" />
<composite alias="UserGroupMembers" class="MODX\Revolution\modUserGroupMember" local="id" foreign="user_group" cardinality="many" owner="local" />
<composite alias="FormCustomizationProfiles" class="MODX\Revolution\modFormCustomizationProfileUserGroup" local="id" foreign="usergroup" cardinality="many" owner="local" />
</object>
Expand Down
15 changes: 12 additions & 3 deletions core/src/Revolution/Processors/Security/Group/GetNodes.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand Down Expand Up @@ -122,6 +123,8 @@ public function addAnonymous(array $list)
$list[] = [
'text' => '(' . $this->modx->lexicon('anonymous') . ')',
'id' => 'n_ug_0',
'expanded' => true,
'allowDrop' => false,
'leaf' => true,
'type' => 'usergroup',
'cls' => $cls,
Expand All @@ -147,15 +150,21 @@ public function prepareGroup(modUserGroup $group)
$c->where(['parent' => $group->get('id')]);
$c->limit(1);
$count = $this->modx->getCount(modUserGroup::class, $c);
return [
$itemArray = [
'text' => htmlentities($group->get('name'), ENT_QUOTES, 'UTF-8') . ' (' . $group->get('id') . ')',
'id' => 'n_ug_' . $group->get('id'),
'leaf' => $count <= 0,
'hasChildren' => $count > 0,
'type' => 'usergroup',
'qtip' => $group->get('description'),
'cls' => $cls,
'allowDrop' => true,
'iconCls' => 'icon icon-group',
];
}

if (!$itemArray['hasChildren']) {
$itemArray['expanded'] = true;
}

return $itemArray;
}
}
3 changes: 2 additions & 1 deletion core/src/Revolution/Processors/Security/Group/Remove.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand Down Expand Up @@ -45,6 +46,6 @@ public function beforeRemove()
*/
public function isAdminGroup()
{
return $this->object->get('id') === 1 || $this->object->get('name') === $this->modx->lexicon('administrator');
return $this->object->get('id') === 1 || $this->object->get('name') === 'Administrator';
}
}
123 changes: 20 additions & 103 deletions core/src/Revolution/Processors/Security/Group/Sort.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand All @@ -11,9 +12,7 @@
namespace MODX\Revolution\Processors\Security\Group;

use MODX\Revolution\Processors\Processor;
use MODX\Revolution\modUser;
use MODX\Revolution\modUserGroup;
use MODX\Revolution\modUserGroupMember;
use MODX\Revolution\modX;

/**
Expand Down Expand Up @@ -56,9 +55,6 @@ public function process()
}

$this->sortGroups($data);
if ($this->modx->hasPermission('usergroup_user_edit')) {
$this->sortUsers($data);
}

return $this->success();
}
Expand All @@ -75,93 +71,40 @@ public function sortGroups(array $data)

/* readjust groups */
foreach ($groups as $groupArray) {
if (!empty($groupArray['id'])) {
/** @var modUserGroup $userGroup */
$userGroup = $this->modx->getObject(modUserGroup::class, $groupArray['id']);
if ($userGroup === null) {
$this->modx->log(modX::LOG_LEVEL_ERROR,
'Could not sort group ' . $groupArray['id'] . ' because it could not be found.');
continue;
}
$oldParentId = $userGroup->get('parent');
} else {
$userGroup = $this->modx->newObject(modUserGroup::class);
$oldParentId = 0;
if (empty($groupArray['id'])) {
continue;
}

if ($groupArray['id'] === 1) {
continue;
}

/** @var modUserGroup $userGroup */
$userGroup = $this->modx->getObject(modUserGroup::class, $groupArray['id']);
if ($userGroup === null) {
$this->modx->log(modX::LOG_LEVEL_ERROR, 'Could not sort group ' . $groupArray['id'] . ' because it could not be found.');
continue;
}
$oldParentId = $userGroup->get('parent');


if ($groupArray['parent'] === $userGroup->get('id')) {
continue;
}

if ($groupArray['parent'] === 0 || $oldParentId !== $groupArray['parent']) {
/* get new parent, if invalid, skip, unless is root */
if ($oldParentId !== $groupArray['parent']) {
if ($groupArray['parent'] !== 0) {
/** @var modUserGroup $parentUserGroup */
$parentUserGroup = $this->modx->getObject(modUserGroup::class, $groupArray['parent']);
if ($parentUserGroup === null) {
continue;
}
$depth = $parentUserGroup->get('depth') + 1;
} else {
$depth = 0;
}

/* save new parent and depth */
$userGroup->set('parent', $groupArray['parent']);
$userGroup->set('depth', $depth);
}
if ($groupArray['id'] !== 0) {
$userGroup->save();
}
}
}

/**
* Sort and rearrange any users in the data
* @param array $data
* @return void
*/
public function sortUsers(array $data)
{
$users = [];
$this->getUsersFormatted($users, $data);
/* readjust users */
foreach ($users as $userArray) {
if (empty($userArray['id'])) {
continue;
}
/** @var modUser $user */
$user = $this->modx->getObject(modUser::class, $userArray['id']);
if ($user === null) {
continue;
}

/* get new parent, if invalid, skip, unless is root */
if ($userArray['new_group'] !== 0 && $userArray['new_group'] !== $userArray['old_group']) {
/** @var modUserGroup $membership */
$membership = $this->modx->getObject(modUserGroupMember::class, [
'user_group' => $userArray['new_group'],
'member' => $user->get('id'),
]);
if ($membership === null) {
$membership = $this->modx->newObject(modUserGroupMember::class);
$membership->set('user_group', $userArray['new_group']);
}
$membership->set('member', $user->get('id'));
if ($membership->save()) {
/* remove user from old group */
if (!empty($userArray['old_group'])) {
/** @var modUserGroup $oldMembership */
$oldMembership = $this->modx->getObject(modUserGroupMember::class, [
'user_group' => $userArray['old_group'],
'member' => $user->get('id'),
]);
if ($oldMembership) {
$oldMembership->remove();
}
}
}
}
$userGroup->save();
}
}

Expand All @@ -177,39 +120,13 @@ protected function getGroupsFormatted(&$ar_nodes, $cur_level, $parent = 0)
$id = substr($id, 2); /* get rid of CSS id n_ prefix */
if (substr($id, 0, 2) === 'ug') {
$ar_nodes[] = [
'id' => substr($id, 3),
'parent' => substr($parent, 3),
'id' => intval(substr($id, 3)),
'parent' => intval(substr($parent, 3)),
'order' => $order,
];
$order++;
}
$this->getGroupsFormatted($ar_nodes, $children, $id);
}
}

/**
* @param $ar_nodes
* @param $cur_level
* @param int $parent
*/
protected function getUsersFormatted(&$ar_nodes, $cur_level, $parent = 0)
{
$order = 0;
foreach ($cur_level as $id => $children) {
$id = substr($id, 2); /* get rid of CSS id n_ prefix */
if (substr($id, 0, 4) === 'user') {
$userMap = substr($id, 5);
$userMap = explode('_', $userMap);
$ar_nodes[] = [
'id' => $userMap[0],
'old_group' => $userMap[1],
'new_group' => substr($parent, 3),
'order' => $order,
];
$order++;
}
$this->getUsersFormatted($ar_nodes, $children, $id);
}
}

}
10 changes: 10 additions & 0 deletions core/src/Revolution/Processors/Security/Group/Update.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/*
* This file is part of MODX Revolution.
*
Expand Down Expand Up @@ -81,6 +82,10 @@ public function beforeSave()
return $this->modx->lexicon('user_group_err_already_exists');
}

if ($this->isAdminGroup()) {
$this->object->set('parent', 0);
}

return parent::beforeSave();
}

Expand Down Expand Up @@ -180,4 +185,9 @@ public function addUsers()

return $memberships;
}

public function isAdminGroup()
{
return $this->object->get('id') === 1 || $this->object->get('name') === 'Administrator';
}
}
16 changes: 8 additions & 8 deletions core/src/Revolution/mysql/modUserGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,14 @@ class modUserGroup extends \MODX\Revolution\modUserGroup
),
'composites' =>
array (
'Children' =>
array (
'class' => 'MODX\\Revolution\\modUserGroup',
'local' => 'id',
'foreign' => 'parent',
'cardinality' => 'many',
'owner' => 'local',
),
'UserGroupMembers' =>
array (
'class' => 'MODX\\Revolution\\modUserGroupMember',
Expand Down Expand Up @@ -166,14 +174,6 @@ class modUserGroup extends \MODX\Revolution\modUserGroup
'cardinality' => 'one',
'owner' => 'foreign',
),
'Children' =>
array (
'class' => 'MODX\\Revolution\\modUserGroup',
'local' => 'id',
'foreign' => 'parent',
'cardinality' => 'many',
'owner' => 'local',
),
'Dashboard' =>
array (
'class' => 'MODX\\Revolution\\modDashboard',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ MODx.panel.UserGroup = function(config) {
,fieldLabel: _('user_group_parent')
,editable: false
,anchor: '100%'
,disabled: config.record.id === 0
,disabled: config.record.id === 0 || config.record.name === 'Administrator'
,baseParams: {
action: 'Security/Group/GetList'
,addNone: true
Expand Down
21 changes: 8 additions & 13 deletions manager/assets/modext/widgets/security/modx.tree.user.group.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ MODx.tree.UserGroup = function(config) {
,rootIconCls: 'icon-group'
,root_id: 'n_ug_0'
,root_name: _('user_groups')
,enableDrag: true
,enableDrop: true
,enableDD: true
,ddGroup: 'modx-usergroup-dd'
,rootVisible: true
,ddAppendOnly: true
,useDefaultToolbar: true
Expand Down Expand Up @@ -181,18 +181,13 @@ Ext.extend(MODx.tree.UserGroup,MODx.tree.Tree,{
}

,_handleDrop: function(e) {
s = false;
switch (e.dropNode.attributes.type) {
case 'user':
s = !(e.point == 'above' || e.point == 'below');
s = s && e.target.attributes.type == 'usergroup' && e.point == 'append';
break;
case 'usergroup':
s = true;
break;
}
return s;
var id = e.data.node.attributes.id.substr(2).split('_');
id = parseInt(id[1]);

if (id === 0) return false; // block Anonymous from moving
if (id === 1) return false; // block Administrator from moving

return true;
}
});
Ext.reg('modx-tree-usergroup',MODx.tree.UserGroup);
Expand Down