Skip to content

Commit 3bcb5a2

Browse files
authored
Ticket: #add Allow category manager to view all tickets in his category (#6501)
Author: @yverhenne
1 parent 0907f72 commit 3bcb5a2

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

main/inc/lib/TicketManager.php

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,39 @@ public static function getUsersInCategory($categoryId)
242242
return Database::store_result($result);
243243
}
244244

245+
/**
246+
* Returns the list of category IDs assigned to a user.
247+
*
248+
* @param int $userId
249+
* @param int $projectId
250+
*
251+
* @return int[]
252+
*/
253+
public static function getCategoryIdsByUser($userId, $projectId = 0)
254+
{
255+
$tableRel = Database::get_main_table(TABLE_TICKET_CATEGORY_REL_USER);
256+
$tableCat = Database::get_main_table(TABLE_TICKET_CATEGORY);
257+
$userId = (int) $userId;
258+
$projectId = (int) $projectId;
259+
260+
$sql = "SELECT rel.category_id
261+
FROM $tableRel rel
262+
INNER JOIN $tableCat cat ON (rel.category_id = cat.id)
263+
WHERE rel.user_id = $userId";
264+
265+
if (!empty($projectId)) {
266+
$sql .= " AND cat.project_id = $projectId";
267+
}
268+
269+
$result = Database::query($sql);
270+
$categories = [];
271+
while ($row = Database::fetch_array($result)) {
272+
$categories[] = (int) $row['category_id'];
273+
}
274+
275+
return $categories;
276+
}
277+
245278
/**
246279
* @param int $categoryId
247280
*/
@@ -902,7 +935,14 @@ public static function getTicketsByCurrentUser(
902935

903936
// Check if a role was set to the project
904937
if ($userIsAllowInProject == false) {
905-
$sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
938+
$categoryList = self::getCategoryIdsByUser($userId, $projectId);
939+
$categoryCondition = '';
940+
if (!empty($categoryList)) {
941+
$categoryIds = implode(',', array_map('intval', $categoryList));
942+
$categoryCondition = " OR ticket.category_id IN ($categoryIds)";
943+
}
944+
945+
$sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId".$categoryCondition.")";
906946
}
907947

908948
// Search simple
@@ -1094,11 +1134,25 @@ public static function getTotalTicketsCurrentUser()
10941134
// Check if a role was set to the project
10951135
if (!empty($allowRoleList) && is_array($allowRoleList)) {
10961136
if (!in_array($userInfo['status'], $allowRoleList)) {
1097-
$sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
1137+
$categoryList = self::getCategoryIdsByUser($userId, $projectId);
1138+
$categoryCondition = '';
1139+
if (!empty($categoryList)) {
1140+
$categoryIds = implode(',', array_map('intval', $categoryList));
1141+
$categoryCondition = " OR ticket.category_id IN ($categoryIds)";
1142+
}
1143+
1144+
$sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId".$categoryCondition.")";
10981145
}
10991146
} else {
11001147
if (!api_is_platform_admin()) {
1101-
$sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId )";
1148+
$categoryList = self::getCategoryIdsByUser($userId, $projectId);
1149+
$categoryCondition = '';
1150+
if (!empty($categoryList)) {
1151+
$categoryIds = implode(',', array_map('intval', $categoryList));
1152+
$categoryCondition = " OR ticket.category_id IN ($categoryIds)";
1153+
}
1154+
1155+
$sql .= " AND (ticket.assigned_last_user = $userId OR ticket.sys_insert_user_id = $userId".$categoryCondition.")";
11021156
}
11031157
}
11041158

@@ -2588,4 +2642,4 @@ public static function notifiyTicketUpdated(int $ticketId, int $categoryId, stri
25882642
}
25892643
}
25902644
}
2591-
}
2645+
}

main/ticket/ticket_details.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,14 @@ class: "controls"
134134
$allowEdition = $ticket['ticket']['assigned_last_user'] == $user_id ||
135135
$ticket['ticket']['sys_insert_user_id'] == $user_id ||
136136
$isAdmin;
137+
$allowCategory = TicketManager::userIsAssignedToCategory(
138+
$user_id,
139+
$ticket['ticket']['category_id']
140+
);
137141

138142
if (false === $userIsAllowInProject) {
139-
// make sure it's either a user assigned to this ticket, or the reporter, or and admin
140-
if (false === $allowEdition) {
143+
// make sure it's either a user assigned to this ticket, the reporter, an admin or the category manager
144+
if (false === $allowEdition && false === $allowCategory) {
141145
api_not_allowed(true);
142146
}
143147
}
@@ -539,4 +543,4 @@ function getForm($ticket)
539543
$form->addButtonSend(get_lang('Send'));
540544

541545
return $form;
542-
}
546+
}

0 commit comments

Comments
 (0)