Skip to content

Commit

Permalink
Merge branch 'doryphore-dev' into feat/entry-with-extra-data
Browse files Browse the repository at this point in the history
  • Loading branch information
mrflos committed Dec 10, 2024
2 parents 71001a5 + 86d93f3 commit 094bb85
Show file tree
Hide file tree
Showing 173 changed files with 78,042 additions and 53,140 deletions.
3 changes: 1 addition & 2 deletions .phpactor.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"$schema": "/phpactor.schema.json",
"language_server_php_cs_fixer.enabled": false
}
"language_server_php_cs_fixer.enabled": true
177 changes: 59 additions & 118 deletions actions/EditGroupsAction.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
<?php

use Symfony\Component\Security\Csrf\Exception\TokenNotFoundException;
use YesWiki\Core\Controller\CsrfTokenController;
use YesWiki\Core\Service\DbService;
use YesWiki\Core\Service\TripleStore;
use YesWiki\Core\Controller\GroupController;
use YesWiki\Core\Service\UserManager;
use YesWiki\Core\YesWikiAction;

class EditGroupsAction extends YesWikiAction
{
public function run()
{
$groupController = $this->getService(GroupController::class);
$userManager = $this->getService(UserManager::class);

if (!$this->wiki->UserIsAdmin()) {
return $this->render('@templates/alert-message.twig', [
'type' => 'danger',
Expand All @@ -22,135 +24,74 @@ public function run()
$currentGroupAcl = '';
$selectedGroupName = '';
$action = '';
if (!empty($_POST['groupname'])) {
if (!is_string($_POST['groupname'])) {
$message = 'Action not possible because \'groupname\' should be a string !';
} elseif (preg_match('/[^A-Za-z0-9]/', $_POST['groupname'])) {
$message = _t('ONLY_ALPHANUM_FOR_GROUP_NAME');
$error_message = '';
error_log(implode("\n", $_POST));

if (empty($_POST)) {
error_log('$_POST empty');
} else {
if (empty($_POST['groupname'])) {
$type = 'danger';
$message = _t('NO_VAR_GROUP');
} elseif (!is_string($_POST['groupname'])) {
$type = 'danger';
$message = 'Invalid ' . _t('GROUP_NOT_STRING');
} else {
$selectedGroupName = strval($_POST['groupname']);
$action = !empty($_POST['action-save'])
? 'save'
: (
!empty($_POST['action-delete'])
? 'delete'
: ''
);
try {
if ($action === 'save') {
list('message' => $message, 'type' => $type) = $this->saveAcl($selectedGroupName);
} elseif ($action === 'delete') {
list('message' => $message, 'type' => $type) = $this->deleteGroup($selectedGroupName);
$this->confirmToken();
if (!empty($_POST['action-view'])) {
$currentGroupAcl = $groupController->getMembers($selectedGroupName);
} elseif (!empty($_POST['action-create'])) {
$groupController->create($selectedGroupName, []);
$type = 'success';
$message = str_replace('{group}', $selectedGroupName, _t('GROUP_CREATED'));
} elseif (!empty($_POST['action-update'])) {
$members = [];
foreach ($_POST as $key => $value) {
if ($value == '1') {
$members[] = $key;
}
}
$groupController->update($selectedGroupName, $members);
$message = str_replace('{group}', $selectedGroupName, _t('GROUP_SAVED'));
$type = 'success';
} elseif (!empty($_POST['action-delete'])) {
$groupController->delete($selectedGroupName);
$message = str_replace('{group}', $selectedGroupName, _t('GROUP_DELETED'));
$type = 'success';
$selectedGroupName = '';
}
} catch (TokenNotFoundException $th) {
$message = _t('ERROR_WHILE_SAVING_GROUP') . ':<br/>' . $th->getMessage();
} catch (Throwable $th) {
$type = 'danger';
$message = _t('ERROR_WHILE_EDITING_GROUP') . ':<br/>' . $th->getMessage();
}
}
}

// retrieves an array of group names from table 'triples' (content of 'resource' starts with 'ThisWikiGroup' and content of 'property' equals 'http://www.wikini.net/_vocabulary/acls')
$list = $this->wiki->GetGroupsList();
sort($list);

if (!empty($selectedGroupName) && in_array($selectedGroupName, $list)) {
$currentGroupAcl = $this->wiki->GetGroupACL($selectedGroupName);
if ($groupController->groupExists($selectedGroupName)) {
$currentGroupAcl = $groupController->getMembers($selectedGroupName);
}

return $this->render(
'@core/actions/edit-group-action.twig',
compact(['list', 'message', 'type', 'currentGroupAcl', 'selectedGroupName', 'action'])
);
}

protected function saveAcl(string $selectedGroupName): array
{
$this->confirmToken();

$message = '';
$type = 'danger';

if (!isset($_POST['acl']) || !is_string($_POST['acl'])) {
$message = '$_POST[\'acl\'] must be a string';
} else {
$newacl = strval($_POST['acl']);
if (strtolower($selectedGroupName) == ADMIN_GROUP && !$this->wiki->CheckACL($newacl)) {
$message = _t('YOU_CANNOT_REMOVE_YOURSELF');
} else {
$result = $this->wiki->SetGroupACL($selectedGroupName, $newacl);

if ($result) {
if ($result == 1000) {
$message = _t('ERROR_RECURSIVE_GROUP') . ' !';
} else {
$message = _t('ERROR_WHILE_SAVING_GROUP') . ' ' . ucfirst($selectedGroupName) . ' (' . _t('ERROR_CODE') . ' ' . $result . ')';
}
} else {
$this->wiki->LogAdministrativeAction($this->wiki->GetUserName(), _t('NEW_ACL_FOR_GROUP') . ' ' . ucfirst($selectedGroupName) . ' : ' . $newacl . "\n");
$message = _t('NEW_ACL_SUCCESSFULLY_SAVED_FOR_THE_GROUP') . ' ' . ucfirst($selectedGroupName);
$type = 'success';
}
}
if (!empty($message)) {
$error_message = ['type' => $type, 'message' => $message];
}

return compact(['message', 'type']);
}

protected function deleteGroup(string &$selectedGroupName): array
{
$message = '';
$type = 'danger';

$this->confirmToken();

if ($this->wiki->GetGroupACL($selectedGroupName) != '') { // The group is not empty
$message = _t('ONLY_EMPTY_GROUP_FOR_DELETION');
} else {
// Check if acls table contains at least one line (page)
// for which this group is the only one to have some privilege
$dbService = $this->getService(DbService::class);
$vocAcsl = WIKINI_VOC_ACLS;
$sql = "SELECT page_tag FROM {$dbService->prefixTable($vocAcsl)} WHERE list = \"@{$dbService->escape($selectedGroupName)}\"";
$ownedPages = $dbService->loadAll($sql); // if group owns no pages, then empty
if (!empty($ownedPages)) {
// Array is not empty because the query returns at least one page
$message = _t('ONLY_NO_PAGES_GROUP_FOR_DELETION') . '<br/>';
$message .= implode('<br/>', array_map(function ($acl) {
return "<a href=\"{$this->wiki->Href('', $acl['page_tag'])}\">{$acl['page_tag']}</a>";
}, $ownedPages));
} else {
// Group is empty AND is not alone having privileges on any page
$sql = <<<SQL
UPDATE {$dbService->prefixTable($vocAcsl)}
SET `list` = REPLACE(REPLACE (`list`, '@{$dbService->escape($selectedGroupName)}\\n', ''),'\\n@{$dbService->escape($selectedGroupName)}','')
WHERE `list` LIKE '%@{$dbService->escape($selectedGroupName)}%'
SQL;

$dbService->query($sql);

$tripleStore = $this->getService(TripleStore::class);
$previous = $tripleStore->getMatching(GROUP_PREFIX . $selectedGroupName, WIKINI_VOC_PREFIX . WIKINI_VOC_ACLS, '', '=');
$deletionOk = false;
if (!empty($previous)) {
$deletionOk = true;
foreach ($previous as $triple) {
if (!$tripleStore->delete($selectedGroupName, WIKINI_VOC_ACLS, $triple['value'], GROUP_PREFIX)) {
$deletionOk = false;
}
}
}
$list = $groupController->getAll();
sort($list);
$users = array_map(function ($user) { return $user['name']; }, $userManager->getAll());
sort($users);
$merged_list = array_merge(array_map(function ($el) { return '@' . $el; }, $list), $users);
unset($merged_list[array_search('@' . $selectedGroupName, $merged_list)]);
error_log('selected_group_name ' . $selectedGroupName);

if ($deletionOk) {
$message = "groupe $selectedGroupName supprimé";
$type = 'success';
$selectedGroupName = '';
} else {
$message = "Une erreur s'est poduite lors de la suppression du groupe $selectedGroupName (triple non supprimé)";
$type = 'warning';
}
}
}
$field = ['name' => '', 'propertyName' => '', 'required' => false, 'label' => $selectedGroupName];
error_log('render');

return compact(['message', 'type']);
return $this->render(
'@core/actions/edit-group-action.twig',
['error_message' => $error_message, 'list' => $list, 'selectedGroupName' => $selectedGroupName, 'field' => $field, 'options' => $merged_list, 'selectedOptionsId' => $currentGroupAcl, 'formName' => _t('USERS_GROUPS_LIST'), 'name' => _t('GROUP_SELECTION')]
);
}

protected function confirmToken()
Expand Down
12 changes: 2 additions & 10 deletions actions/UsersTableAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,8 @@ public function run()

private function addGroups(array $users): array
{
$groups = $this->wiki->GetGroupsList();

return array_map(function ($user) use ($groups) {
$userGroups = [];
foreach ($groups as $group) {
if (!empty($user['name']) && $this->userManager->isInGroup($group, $user['name'], false)) { // false to not display admins in other groups
$userGroups[] = $group;
}
}

return array_map(function ($user) {
$userGroups = $this->userManager->groupsWhereIsMember($user);
return array_merge($user->getArrayCopy(), ['groups' => $userGroups]);
}, $users);
}
Expand Down
Loading

0 comments on commit 094bb85

Please sign in to comment.