forked from SimpleMachines/SimpleDesk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleDesk-Assign.php
More file actions
369 lines (318 loc) · 13.1 KB
/
SimpleDesk-Assign.php
File metadata and controls
369 lines (318 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**************************************************************
* Simple Desk Project - www.simpledesk.net *
***************************************************************
* An advanced help desk modification built on SMF *
***************************************************************
* *
* * Copyright 2025 - SimpleDesk.net *
* *
* This file and its contents are subject to the license *
* included with this distribution, license.txt, which *
* states that this software is New BSD Licensed. *
* Any questions, please contact SimpleDesk.net *
* *
***************************************************************
* SimpleDesk Version: 2.1.0 *
* File Info: SimpleDesk-Assign.php *
**************************************************************/
/**
* This file handles ticket assignments, both the user interface for the assignment page, plus
* actually assigning users to tickets. There are also helper functions here for that purpose.
*
* @package source
* @since 1.0
*/
if (!defined('SMF'))
die('Hacking attempt...');
/**
* Sets up the assignment UI and invokes the template.
*
* This validates that the user can see the ticket in question initially, before determining the level of assignment that can be
* carried out.
*
* Users who can assign any ticket cause a query to trigger to identify users who have shd_staff permission (and thus can be the
* target of assignment) and the template is loaded with a dropdown to provide this choice to the user.
*
* Users who can only assign to themselves will at this point have the ticket assigned to them if it was unassigned before, or
* unassigned from them if they are able to assign tickets to themselves in the first place; if it was assigned to someone else,
* they will not have the option.
*
* Access through action=helpdesk;sa=assign;ticket=x;sessvar=sessid before redirecting back to the ticket, or add ;home to the
* URL to have it redirect back to the home page (passed through to the form for {@link shd_assign2()}'s use too)
*
* @see shd_assign2()
* @see shd_get_possible_assignees()
* @since 1.0
*/
function shd_assign()
{
global $smcFunc, $context, $user_info, $sourcedir, $txt, $scripturl;
checkSession('get');
if (empty($context['ticket_id']))
shd_fatal_lang_error('shd_no_ticket');
$context['shd_return_to'] = isset($_REQUEST['home']) ? 'home' : 'ticket';
// Get ticket details - and kick it out if they shouldn't be able to see it.
$query = shd_db_query('', '
SELECT id_member_started, id_member_assigned, private, subject, hdt.id_dept, hdt.status, hdd.dept_name
FROM {db_prefix}helpdesk_tickets AS hdt
INNER JOIN {db_prefix}helpdesk_depts AS hdd ON (hdt.id_dept = hdd.id_dept)
WHERE {query_see_ticket} AND id_ticket = {int:ticket}',
array(
'ticket' => $context['ticket_id'],
)
);
$row = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
if (empty($row))
shd_fatal_lang_error('shd_no_ticket');
list($ticket_starter, $ticket_owner, $private, $subject, $dept, $status, $dept_name) = $row;
$log_params = array(
'subject' => $subject,
'ticket' => $context['ticket_id'],
);
if ($status == TICKET_STATUS_CLOSED || $status == TICKET_STATUS_DELETED)
shd_fatal_lang_error('shd_cannot_assign', false);
if (shd_allowed_to('shd_assign_ticket_any', $dept)) // can regularly assign? If so, load up potential candidates and throw it at the template.
{
$members = shd_get_possible_assignees($private, $ticket_starter, $dept);
if (!in_array($ticket_owner, $members)) // if for whatever reason the current assignee is not accessible staff, treat it as unassigned
$ticket_owner = 0;
if (!empty($members))
{
$query = shd_db_query('', '
SELECT id_member, real_name
FROM {db_prefix}members
WHERE id_member IN ({array_int:members})
ORDER BY real_name',
array(
'members' => $members,
)
);
$members = array();
while ($row = $smcFunc['db_fetch_assoc']($query))
$members[$row['id_member']] = $row['real_name'];
}
if (empty($members))
shd_fatal_lang_error('shd_no_staff_assign');
if ($context['shd_multi_dept'])
$context['linktree'][] = array(
'url' => $scripturl . '?' . $context['shd_home'] . ';dept=' . $dept,
'name' => $dept_name,
);
$context['linktree'][] = array(
'url' => $scripturl . '?action=helpdesk;sa=ticket;ticket=' . $context['ticket_id'],
'name' => $subject,
);
$context['linktree'][] = array(
'name' => $txt['shd_ticket_assign_ticket'],
);
$context['member_list'] = array(0 => $txt['shd_unassigned']) + $members;
$context['ticket_assigned'] = $ticket_owner;
$context['page_title'] = $txt['shd_ticket_assign_ticket'];
loadTemplate('sd_template/SimpleDesk-Assign');
$context['sub_template'] = 'assign';
}
elseif (shd_allowed_to('shd_assign_ticket_own', $dept) && shd_allowed_to('shd_staff', $dept)) // can't just randomly assign (and must be staff), so look at if it's already assigned or not.
{
if ($ticket_owner == 0) // unassigned
{
$log_params += array(
'user_id' => $user_info['id'],
'user_name' => $user_info['name'],
);
shd_log_action('assign', $log_params);
shd_commit_assignment($context['ticket_id'], $user_info['id']);
}
elseif ($ticket_owner == $user_info['id']) // assigned to self already, unassign it
{
shd_log_action('unassign', $log_params);
shd_commit_assignment($context['ticket_id'], 0);
}
else // oops, assigned to somebody else
shd_fatal_lang_error('shd_cannot_assign_other', false);
}
else
shd_fatal_lang_error('shd_cannot_assign', false);
}
/**
* Handles the actual assignment form, validates it and carries it out.
*
* Primarily this is just about receiving the form, making the same checks that {@link shd_assign()} does and then
* logging the action before passing over to {@link shd_commit_assignment()} to actually assign the ticket.
*
* @see shd_assign()
* @see shd_commit_assignment()
* @since 1.0
*/
function shd_assign2()
{
global $context, $smcFunc, $user_info, $sourcedir;
checkSession();
checkSubmitOnce('check');
if (empty($context['ticket_id']))
shd_fatal_lang_error('shd_no_ticket');
$context['shd_return_to'] = isset($_REQUEST['home']) ? 'home' : 'ticket';
$assignee = isset($_REQUEST['to_user']) ? (int) $_REQUEST['to_user'] : 0;
// Get ticket details - and kick it out if they shouldn't be able to see it.
$query = shd_db_query('', '
SELECT id_member_started, id_member_assigned, private, subject, status, id_dept
FROM {db_prefix}helpdesk_tickets AS hdt
WHERE {query_see_ticket} AND id_ticket = {int:ticket}',
array(
'ticket' => $context['ticket_id'],
)
);
$row = $smcFunc['db_fetch_row']($query);
$smcFunc['db_free_result']($query);
if (empty($row))
shd_fatal_lang_error('shd_no_ticket');
list($ticket_starter, $ticket_owner, $private, $subject, $status, $dept) = $row;
// The core details that we'll be logging
$log_params = array(
'subject' => $subject,
'ticket' => $context['ticket_id'],
);
// Just in case, are they cancelling?
if (isset($_REQUEST['cancel']))
return redirectexit('action=helpdesk;sa=ticket;ticket=' . $context['ticket_id']);
if ($status == TICKET_STATUS_CLOSED || $status == TICKET_STATUS_DELETED)
shd_fatal_lang_error('shd_cannot_assign', false);
if (shd_allowed_to('shd_assign_ticket_any', $dept)) // can regularly assign? If so, see if our requested member is staff and can see the ticket
{
if ($assignee == 0) // can always unassign a ticket
{
shd_log_action('unassign', $log_params);
shd_commit_assignment($context['ticket_id'], 0);
}
else
{
$members = shd_get_possible_assignees($private, $ticket_starter, $dept);
if (in_array($assignee, $members)) // can only assign a ticket to a member if they're on Santa's good list
{
global $user_profile;
loadMemberData($assignee, false, 'minimal');
$log_params += array(
'user_id' => $assignee,
'user_name' => $user_profile[$assignee]['real_name'],
);
shd_log_action('assign', $log_params);
shd_commit_assignment($context['ticket_id'], $assignee);
}
else
shd_fatal_lang_error('shd_assigned_not_permitted', false);
}
}
elseif (shd_allowed_to('shd_assign_ticket_own', $dept) && shd_allowed_to('shd_staff', $dept)) // can't just randomly assign (and must be staff), so look at if it's already assigned or not.
{
if ($ticket_owner == 0) // unassigned
{
$log_params += array(
'user_id' => $user_info['id'],
'user_name' => $user_info['name'],
);
shd_log_action('assign', $log_params);
shd_commit_assignment($context['ticket_id'], $user_info['id']);
}
elseif ($ticket_starter == $user_info['id']) // assigned to self already, unassign it
{
shd_log_action('unassign', $log_params);
shd_commit_assignment($context['ticket_id'], 0);
}
else // oops, assigned to somebody else
shd_fatal_lang_error('shd_cannot_assign_other', false);
}
else
shd_fatal_lang_error('shd_cannot_assign', false);
}
/**
* Actually commits a ticket assignment to the database.
*
* This function sets up the relevant options before handing over to shd_modify_ticket_post() which is the preferred way to modify
* tickets generally. No permissions check is performed here.
*
* Relies on $context['shd_return_to'] to have been set, defaults to 'ticket' to return to ticket, otherwise return to home page
* on value of 'home'.
*
* @param int $ticket Ticket id number, assumed to exist.
* @param int $assignment User id of the user to which this ticket will be assigned; can be 0 to unassign a ticket
*
* @see shd_assign()
* @see shd_assign2()
*/
function shd_commit_assignment($ticket, $assignment, $is_ajax = false)
{
global $smcFunc, $sourcedir, $context, $modSettings;
require_once($sourcedir . '/sd_source/Subs-SimpleDeskPost.php');
$msgOptions = array();
$posterOptions = array();
$ticketOptions = array(
'id' => $context['ticket_id'],
'assigned' => $assignment,
);
// Int hooks - after we've set everything up but before we actually press the button
call_integration_hook('shd_hook_assign', array(&$ticket, &$assignment));
shd_modify_ticket_post($msgOptions, $ticketOptions, $posterOptions);
// Handle notifications
require_once($sourcedir . '/sd_source/SimpleDesk-Notifications.php');
shd_notifications_notify_assign($ticket, $assignment);
// If we're doing it AJAXively, we just want this to do the job and then go back to AJAX workflow.
if ($is_ajax)
return;
if (!empty($context['shd_return_to']) && $context['shd_return_to'] == 'home')
return redirectexit($context['shd_home'] . $context['shd_dept_link']);
return redirectexit('action=helpdesk;sa=ticket;ticket=' . $ticket);
}
/**
* Return a list of possible users that can be assigned a ticket.
*
* This function centralises who a ticket can be assigned to. Currently this is:
* - user with shd_staff permission
* - can see "any" ticket (not just their own)
* - additionally, if the ticket is private, the user must also be able to see 'any' private ticket.
* - additionally, check whether user is staff + ticket starter, and then whether we should block them from being assigned
* - (since 1.1) additionally if we have set the option, which users are true admins, and if so, remove them from the list too
*
* @param bool $private Whether the ticket in question is private or not.
* @param int $ticket_owner User id of the ticket owner
* @param int $dept The department of the ticket, used for establishing permissions.
*
* @return array An indexed array of member ids that this ticket could be assigned to.
* @see shd_assign()
* @see shd_assign2()
* @since 1.0
*/
function shd_get_possible_assignees($private = false, $ticket_owner = 0, $dept = -1)
{
global $context, $smcFunc, $modSettings;
// people who can handle a ticket
$staff = shd_members_allowed_to('shd_staff', $dept);
// is it private, if so, remove that list
if ((bool) $private === true)
{
$private = shd_members_allowed_to('shd_view_ticket_private_any', $dept);
$staff = array_intersect($staff, $private);
}
// What about admins? We ignoring them? Note we're talking about *real* admins who implicitly have every permission, not any homebrew
if (!empty($modSettings['shd_admins_not_assignable']))
{
$query = $smcFunc['db_query']('', '
SELECT id_member
FROM {db_prefix}members
WHERE id_group = 1
OR FIND_IN_SET(1, additional_groups)',
array()
);
$admins = array();
while ($row = $smcFunc['db_fetch_row']($query))
$admins[] = $row[0];
$smcFunc['db_free_result']($query);
$staff = array_diff($staff, $admins);
}
// can they actually see said ticket
$visible = shd_members_allowed_to('shd_view_ticket_any', $dept);
if (empty($modSettings['shd_staff_ticket_self'])) // by default, staff members can't be assigned a ticket if they started it
$staff = array_diff($staff, array($ticket_owner));
// spit back the list of staff members who can see any ticket (+private if dealt with)
return array_intersect($staff, $visible);
}