From 912b130dd226de4359e1fd2b7c58ff57fdc5c948 Mon Sep 17 00:00:00 2001 From: oiseauroch Date: Wed, 2 Oct 2024 15:40:01 +0200 Subject: [PATCH] WIP group improvement --- includes/controllers/GroupController.php.tmp | 19 ++++++++++------- .../UserNameDoesNotExistException.php | 9 ++++++++ includes/services/GroupManager.php | 21 ++++++++++++++----- includes/services/UserManager.php | 2 +- 4 files changed, 37 insertions(+), 14 deletions(-) create mode 100644 includes/exceptions/UserNameDoesNotExistException.php diff --git a/includes/controllers/GroupController.php.tmp b/includes/controllers/GroupController.php.tmp index 7ad7c69d1..93bf066c0 100644 --- a/includes/controllers/GroupController.php.tmp +++ b/includes/controllers/GroupController.php.tmp @@ -7,6 +7,7 @@ use YesWiki\Core\Exception\InvalidGroupNameException; use YesWiki\Core\Exception\GroupNameDoesNotExistException; use YesWiki\Core\Exception\GroupNameAlreadyUsedException; use YesWiki\Core\Exception\UserNameAlreadyUsedException; +use YesWiki\Core\Exception\UserNameDoesNotExistException; use YesWiki\Core\Service\GroupManager; use YesWiki\Core\Service\UserManager; use YesWiki\Core\YesWikiController; @@ -60,7 +61,9 @@ class GroupController extends YesWikiController if ($this->isNameValid($name)) { $this->groupManager->create($name, $members); - } else + } else { + throw new InvalidGroupNameException(_t('INVALID_GROUP_NAME')); // FIXME vérifier INVALID_GROUP_NAME + } } /** @@ -84,30 +87,30 @@ class GroupController extends YesWikiController */ public function addOne(string $group_name, string $name) { - if(!$this->groupManager->groupExists()) { + if(!$this->groupManager->groupExists($group_name)) { throw new GroupNameDoesNotExistException(); } if(str_starts_with($name, "@") { - if($this->groupManager->groupExists()) { - throw new GroupNameAlreadyUsedException(_t('GROUP_NAME_ALREADY_USED')); + if(!$this->groupManager->groupExists($name)) { + throw new GroupNameDoesNotExistException(_t('GROUP_NAME_DOES_NOT_EXIST')); // FIXME voir GROUP_NAME_DOES_NOT_EXIST } } else { - if($this->userManager->userExist()) { - throw new UserNameAlreadyUsedException(_t('USER_NAME_ALREADY_USED')); + if(!$this->userManager->userExist($name)) { + throw new UserNameDoesNotExistException(_t('USER_NAME_DOES_NOT_EXIST')); // FIXME voir USER_NAME_DOES_NOT_EXIST } } - $this->groupManager->add($group_name, $name); } /** * remove user or group from group + * @param string $$group_name group to remove from * @param string $name user or group to remove * @return bool * @throws UserDoesNotExistException * @throws GroupDoesNotExistException */ - public function removeOne(string $name) + public function removeOne(string $group_name, string $name) { } diff --git a/includes/exceptions/UserNameDoesNotExistException.php b/includes/exceptions/UserNameDoesNotExistException.php new file mode 100644 index 000000000..9bb1549b9 --- /dev/null +++ b/includes/exceptions/UserNameDoesNotExistException.php @@ -0,0 +1,9 @@ +tripleStore->getOne($group_name, WIKINI_VOC_ACLS, GROUP_PREFIX) == null; + return $this->tripleStore->getOne($group_name, WIKINI_VOC_ACLS, GROUP_PREFIX) != null; } /** @@ -36,7 +36,12 @@ public function create(string $group_name, array $members): void { $member_str = implode("\n", $members); $this->tripleStore->create($group_name, WIKINI_VOC_ACLS, $member_str, GROUP_PREFIX); } - + + /** + * get direct members of group. Do not list member of child groups. + * @param string group_name + * @return string[] + */ public function getMembers(string $group_name) :array { $members = $this->tripleStore->getOne($group_name, WIKINI_VOC_ACLS, GROUP_PREFIX); return explode("\n", $members); @@ -50,14 +55,20 @@ public function getMembers(string $group_name) :array { public function add(string $group_name, array $members):void { $old_members = $this->getMembers($group_name); if (!in_array($group_name, $members) ) { - $new_members = implode("\n", $members); - $new_members = $old_members . "\n" . $new_members; + $new_members = array_merge($old_members, $members); + $new_members = array_unique($$new_members); + $new_members = implode("\n", $new_members); if($this->tripleStore->delete($group_name, WIKINI_VOC_ACLS, $old_members, GROUP_PREFIX)) { $this->tripleStore->create($group_name, WIKINI_VOC_ACLS, $new_members, GROUP_PREFIX); } else { - $this->tripleStore->update($group_name, WIKINI_VOC_ACLS, $old_members, $new_members , GROUP_PREFIX) + $this->tripleStore->update($group_name, WIKINI_VOC_ACLS, $old_members, $new_members , GROUP_PREFIX); } } } + + public function remove(string $group_name, string $member): void { + $old_members = $this->getMembers($group_name); + + } } diff --git a/includes/services/UserManager.php b/includes/services/UserManager.php index f7faca5c3..d1f7e255e 100644 --- a/includes/services/UserManager.php +++ b/includes/services/UserManager.php @@ -66,7 +66,7 @@ private function arrayToUser(?array $userAsArray = null, bool $fillEmpty = false } public function userExist($name): bool { - return !empty($this->getOneByName($name)) + return !empty($this->getOneByName($name)); } public function getOneByName($name, $password = null): ?User