Skip to content

Commit

Permalink
WIP add group api
Browse files Browse the repository at this point in the history
  • Loading branch information
oiseauroch committed Oct 3, 2024
1 parent d90a5dc commit 9bd10ea
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 123 deletions.
8 changes: 4 additions & 4 deletions includes/YesWiki.php
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ public function UserIsOwner($tag = "")
* @return string the ACL associated with the group $gname
* @see UserIsInGroup to check if a user belongs to some group
*/
public function GetGroupACL($group)
public function GetGroupACL($group) //FIXME
{
if (array_key_exists($group, $this->_groupsCache)) {
return $this->_groupsCache[$group];
Expand All @@ -967,7 +967,7 @@ public function GetGroupACL($group)
* The new acl for that group
* @return boolean True if the new acl defines the group recursively
*/
public function MakesGroupRecursive($gname, $acl, $origin = null, $checked = array())
public function MakesGroupRecursive($gname, $acl, $origin = null, $checked = array()) //FIXME
{
$gname = strtolower(trim($gname));
if ($origin === null) {
Expand Down Expand Up @@ -1014,7 +1014,7 @@ public function MakesGroupRecursive($gname, $acl, $origin = null, $checked = arr
* 1001 if $gname is not named with alphanumeric chars
* @see GetGroupACL
*/
public function SetGroupACL($gname, $acl)
public function SetGroupACL($gname, $acl) //FIXME
{
if (preg_match('/[^A-Za-z0-9]/', $gname)) {
return 1001;
Expand Down Expand Up @@ -1049,7 +1049,7 @@ public function SetGroupACL($gname, $acl)
*
* @return array The list of all group names
*/
public function GetGroupsList()
public function GetGroupsList() //FIXME
{
$res = $this->GetMatchingTriples(GROUP_PREFIX . '%', WIKINI_VOC_ACLS_URI);
$prefix_len = strlen(GROUP_PREFIX);
Expand Down
107 changes: 97 additions & 10 deletions includes/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use YesWiki\Core\Controller\CsrfTokenController;
use YesWiki\Core\Controller\PageController;
use YesWiki\Core\Controller\UserController;
use YesWiki\Core\Controller\GroupController;
use YesWiki\Core\Exception\DeleteUserException;
use YesWiki\Core\Exception\ExitException;
use YesWiki\Core\Service\AclService;
Expand All @@ -30,6 +31,12 @@
use YesWiki\Core\YesWikiController;
use YesWiki\Security\Controller\SecurityController;

use YesWiki\Core\Exception\GroupNameDoesNotExistException;
use YesWiki\Core\Exception\GroupNameAlreadyUsedException;
use YesWiki\Core\Exception\UserNameDoesNotExistException;
use YesWiki\Core\Exception\InvalidGroupNameException;
use YesWiki\Core\Exception\UserEmailAlreadyUsedException;

class ApiController extends YesWikiController
{
/**
Expand Down Expand Up @@ -248,6 +255,96 @@ public function getAllUsers($userFields = ['name', 'email', 'signuptime'])
return new ApiResponse($users);
}


/**
* @Route("/api/groups",methods={"POST"},options={"acl":{"public"}})
*/
public function createGroup()
{
$this->denyAccessUnlessAdmin();
$groupController = $this->getService(GroupController::class);

if (empty($_POST['name'])) {
$code = Response::HTTP_BAD_REQUEST;
$result = [
'error' => "\$_POST['name'] should not be empty",
];
} else {
try {
$group_name = $_POST['name'];
$users = empty($_POST['users']) ? array() : $_POST['users'];
$result = $groupController->create($group_name, $users);
$code = Response::HTTP_OK;
} catch (GroupNameAlreadyUsedException $th) {
$code = Response::HTTP_UNPROCESSABLE_ENTITY;
$result = [
'notCreated' => [strval($_POST['name'])],
'error' => str_replace('{currentName}', strval($_POST('name')), _t('USERSETTINGS_NAME_ALREADY_USED'))
];
} catch (InvalidGroupNameException $th) {
$code = Response::HTTP_UNPROCESSABLE_ENTITY;
$result = [
'notCreated' => [strval($_POST['name'])],
'error' => $th->getMessage()
];
} catch (UserNameDoesNotExistException | GroupNameDoesNotExistException $th) {
$code = Response::HTTP_UNPROCESSABLE_ENTITY;
$result = [
'notCreated' => [strval($_POST['name'])],
'error' => str_replace('{currentName}', $th->getMessage(), _t('USERSETTINGS_NAME_NOT_FOUND'))
];
} catch (ExitException $th) {
throw $th;
} catch (Exception $th) {
$code = Response::HTTP_BAD_REQUEST;
$result = [
'notCreated' => [strval($_POST['name'])],
'error' => $th->getMessage()
];
} catch (Throwable $th) {
$code = Response::HTTP_INTERNAL_SERVER_ERROR;
$result = [
'notCreated' => [strval($_POST['name'])],
'error' => $th->getMessage()
];
}
}
return new ApiResponse($result, $code);
}

/**
* @Route("/api/groups",methods={"GET"},options={"acl":{"public"}})
*/
public function getAllGroups()
{
$this->denyAccessUnlessAdmin();
$groupController = $this->getService(GroupController::class);

return new ApiResponse($groupController->getAll());
}


/**
* @Route("/api/groups/{group_name}",methods={"GET"}, options={"acl":{"public"}})
*/
public function getGroup(string $group_name)
{
$this->denyAccessUnlessAdmin();
$groupController = $this->getService(GroupController::class);

try {
$result = $groupController->getMembers($group_name);
$code = Response::HTTP_OK;
} catch (GroupNameDoesNotExistException $th) {
$code = Response::HTTP_NOT_FOUND;
$result = [
'notFound' => $group_name,
'error' => $th->getMessage()
];
}
return new ApiResponse($result, $code);
}

/**
* @Route("/api/comments/{tag}",methods={"GET"}, options={"acl":{"public"}})
*/
Expand Down Expand Up @@ -298,16 +395,6 @@ public function deleteCommentViaPostMethod($tag)
return $this->deleteComment($tag);
}

/**
* @Route("/api/groups", options={"acl":{"public"}})
*/
public function getAllGroups()
{
$this->denyAccessUnlessAdmin();

return new ApiResponse($this->wiki->GetGroupsList());
}

/**
* @Route("/api/pages", options={"acl":{"public"}})
*/
Expand Down
Loading

0 comments on commit 9bd10ea

Please sign in to comment.