14 namespace Fossology\UI\Api\Controllers;
28 use Psr\Http\Message\ServerRequestInterface;
45 public function getGroups($request, $response, $args)
47 $userDao = $this->restHelper->getUserDao();
50 $groups = $userDao->getAdminGroupMap($this->restHelper->getUserId(),
Auth::PERM_ADMIN);
52 $groups = $userDao->getUserGroupMap($this->restHelper->getUserId());
55 foreach ($groups as $key => $value) {
56 $groupObject =
new Group($key,$value);
57 $groupList[] = $groupObject->getArray();
59 return $response->withJson($groupList, 200);
75 $queryParams = $request->getQueryParams();
76 $groupName = $queryParams[
'name'] ??
'';
78 $groupName = $request->getHeaderLine(
'name') ?:
'';
80 if (empty($groupName)) {
83 $userDao = $this->restHelper->getUserDao();
84 $groupId = $userDao->addGroup($groupName);
85 $userDao->addGroupMembership($groupId, $this->restHelper->getUserId());
86 $returnVal =
new Info(200,
"Group $groupName added.", InfoType::INFO);
87 return $response->withJson($returnVal->getArray(), $returnVal->getCode());
99 public function deleteGroup($request, $response, $args)
102 if (empty($args[
'pathParam'])) {
105 $userId = $this->restHelper->getUserId();
108 $userDao = $this->restHelper->getUserDao();
109 $groupMap = $userDao->getDeletableAdminGroupMap($userId,
112 if ($apiVerison == ApiVersion::V2) {
113 $groupName = $args[
'pathParam'];
114 $groupId = intval($userDao->getGroupIdByName($groupName));
116 $groupId = intval($args[
'pathParam']);
119 if (!$this->dbHelper->doesIdExist(
"groups",
"group_pk", $groupId)) {
120 throw new HttpNotFoundException(
"Group id not found!");
123 $userDao->deleteGroup($groupId);
124 $returnVal =
new Info(202,
"User Group will be deleted", InfoType::INFO);
125 unset($groupMap[$groupId]);
126 }
catch (\Exception $e) {
127 throw new HttpBadRequestException($e->getMessage(), $e);
129 return $response->withJson($returnVal->getArray(), $returnVal->getCode());
141 public function deleteGroupMember($request, $response, $args)
144 $dbManager = $this->dbHelper->getDbManager();
148 if ($apiVersion == ApiVersion::V2) {
149 $user_pk = intval($this->restHelper->getUserDao()->getUserByName($args[
'userPathParam'])[
'user_pk']);
150 $group_pk = intval($this->restHelper->getUserDao()->getGroupIdByName($args[
'pathParam']));
152 $user_pk = intval($args[
'userPathParam']);
153 $group_pk = intval($args[
'pathParam']);
157 $userHasGroupAccess = $this->restHelper->getUserDao()->isAdvisorOrAdmin(
158 $this->restHelper->getUserId(), $group_pk);
160 if (!$this->dbHelper->doesIdExist(
"groups",
"group_pk", $group_pk)) {
161 throw new HttpNotFoundException(
"Group id not found!");
163 if (!$this->dbHelper->doesIdExist(
"users",
"user_pk", $user_pk)) {
164 throw new HttpNotFoundException(
"User id not found!");
166 if (! $userIsAdmin && ! $userHasGroupAccess) {
167 throw new HttpForbiddenException(
"Not advisor or admin of the group. " .
168 "Can not process request.");
170 $fetchResult = $dbManager->getSingleRow(
171 "SELECT group_user_member_pk FROM group_user_member " .
172 "WHERE group_fk=$1 AND user_fk=$2", [$group_pk, $user_pk],
173 __METHOD__ .
".getByGroupAndUser");
174 if (empty($fetchResult)) {
175 throw new HttpNotFoundException(
"Not a member!");
177 $group_user_member_pk = $fetchResult[
'group_user_member_pk'];
179 $adminGroupUsers = $this->restHelper->getPlugin(
'group_manage_users');
180 $adminGroupUsers->updateGUMPermission($group_user_member_pk, -1,$dbManager);
181 $returnVal =
new Info(200,
"User will be removed from group.", InfoType::INFO);
182 return $response->withJson($returnVal->getArray(), $returnVal->getCode());
195 $userId = $this->restHelper->getUserId();
197 $userDao = $this->restHelper->getUserDao();
198 $groupMap = $userDao->getDeletableAdminGroupMap($userId,
201 $groupList = array();
202 foreach ($groupMap as $key => $value) {
203 $groupObject =
new Group($key, $value);
204 $groupList[] = $groupObject->getArray();
206 return $response->withJson($groupList, 200);
221 $userId = $this->restHelper->getUserId();
222 $userDao = $this->restHelper->getUserDao();
223 $groupMap = $userDao->getAdminGroupMap($userId, $_SESSION[
Auth::USER_LEVEL]);
225 if (empty($groupMap)) {
230 $groupId = $apiVersion == ApiVersion::V2 ? intval($this->restHelper->getUserDao()->getGroupIdByName($args[
'pathParam'])) : intval($args[
'pathParam']);
233 $dbManager = $this->dbHelper->getDbManager();
235 $stmt = __METHOD__ .
"getUsersWithGroup";
236 $dbManager->prepare($stmt,
"SELECT user_pk, group_perm
237 FROM users INNER JOIN group_user_member gum ON gum.user_fk=users.user_pk AND gum.group_fk=$1;");
239 $result = $dbManager->execute($stmt, array($groupId));
240 $usersWithGroup = $dbManager->fetchAll($result);
243 $memberList = array();
244 foreach ($usersWithGroup as $record) {
245 $user = $this->dbHelper->getUsers($record[
'user_pk']);
246 $userGroupMember =
new UserGroupMember($user[0],$record[
"group_perm"]);
249 $dbManager->freeResult($result);
251 return $response->withJson($memberList, 200);
267 $dbManager = $this->dbHelper->getDbManager();
272 if ($apiVersion == ApiVersion::V2) {
273 $newuser = intval($this->restHelper->getUserDao()->getUserByName($args[
'userPathParam'])[
'user_pk']);
274 $group_pk = intval($this->restHelper->getUserDao()->getGroupIdByName($args[
'pathParam']));
276 $group_pk = intval($args[
'pathParam']);
277 $newuser = intval($args[
'userPathParam']);
279 $newperm = intval($body[
'perm']);
282 $userHasGroupAccess = $this->restHelper->getUserDao()->isAdvisorOrAdmin(
283 $this->restHelper->getUserId(), $group_pk);
285 if (!isset($newperm)) {
288 if (!$this->dbHelper->doesIdExist(
"groups",
"group_pk", $group_pk)) {
291 if (!$this->dbHelper->doesIdExist(
"users",
"user_pk", $newuser)) {
294 if ($newperm < 0 || $newperm > 2) {
297 if (! $userIsAdmin && ! $userHasGroupAccess) {
299 "Can not process request.");
301 $stmt = __METHOD__ .
".getByGroupAndUser";
302 $sql =
"SELECT group_user_member_pk FROM group_user_member WHERE group_fk=$1 AND user_fk=$2;";
303 $fetchResult = $dbManager->getSingleRow($sql, [$group_pk, $newuser], $stmt);
306 if (!empty($fetchResult)) {
309 $dbManager->prepare($stmt = __METHOD__ .
".insertGUP",
310 "INSERT INTO group_user_member (group_fk, user_fk, group_perm) VALUES ($1,$2,$3)");
311 $dbManager->freeResult(
312 $dbManager->execute($stmt, array($group_pk, $newuser, $newperm)));
314 $returnVal =
new Info(200,
"User will be added to group.", InfoType::INFO);
315 return $response->withJson($returnVal->getArray(), $returnVal->getCode());
327 public function changeUserPermission($request, $response, $args)
331 $dbManager = $this->dbHelper->getDbManager();
335 if ($apiVersion == ApiVersion::V2) {
336 $user_pk = intval($this->restHelper->getUserDao()->getUserByName($args[
'userPathParam'])[
'user_pk']);
337 $group_pk = intval($this->restHelper->getUserDao()->getGroupIdByName($args[
'pathParam']));
339 $user_pk = intval($args[
'userPathParam']);
340 $group_pk = intval($args[
'pathParam']);
345 $userHasGroupAccess = $this->restHelper->getUserDao()->isAdvisorOrAdmin(
346 $this->restHelper->getUserId(), $group_pk);
350 if (!isset($newperm)) {
351 throw new HttpBadRequestException(
"Permission should be provided");
353 if (!$this->dbHelper->doesIdExist(
"groups",
"group_pk", $group_pk)) {
354 throw new HttpNotFoundException(
"Group id not found!");
356 if (!$this->dbHelper->doesIdExist(
"users",
"user_pk", $user_pk)) {
357 throw new HttpNotFoundException(
"User id not found!");
360 throw new HttpBadRequestException(
"Permission can not be negative");
363 throw new HttpBadRequestException(
"Permission can not be greater than 2");
365 if (! $userIsAdmin && ! $userHasGroupAccess) {
366 throw new HttpForbiddenException(
"Not advisor or admin of the group. " .
367 "Can not process request.");
372 $group_user_member_pk = $dbManager->getSingleRow(
"SELECT group_user_member_pk FROM group_user_member WHERE group_fk=$1 AND user_fk=$2",
373 [$group_pk, $user_pk],
374 __METHOD__ .
".getByGroupAndUser")[
'group_user_member_pk'];
376 if (empty($group_user_member_pk)) {
377 throw new HttpNotFoundException(
"User not part of the group");
380 $adminGroupUsers = $this->restHelper->getPlugin(
'group_manage_users');
381 $adminGroupUsers->updateGUMPermission($group_user_member_pk, $newperm,$dbManager);
382 $info =
new Info(202,
"Permission updated successfully.", InfoType::INFO);
383 return $response->withJson($info->getArray(), $info->getCode());
Contains the constants and helpers for authentication of user.
static isAdmin()
Check if user is admin.
Controller for Group model.
createGroup($request, $response, $args)
getGroups($request, $response, $args)
getDeletableGroups($request, $response, $args)
getGroupMembers($request, $response, $args)
addMember($request, $response, $args)
Base controller for REST calls.
getParsedBody(ServerRequestInterface $request)
Parse request body as JSON and return associative PHP array.
Override Slim response for withJson function.
static getVersion(ServerRequestInterface $request)
Different type of infos provided by REST.
Info model to contain general error and return values.