Skip to content

Commit 22f0dfb

Browse files
committed
Bug: Member without department got exception when seeing notices
1 parent 4151f6b commit 22f0dfb

File tree

1 file changed

+48
-41
lines changed

1 file changed

+48
-41
lines changed

src/Models/Member.php

Lines changed: 48 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
class Member extends User
1010
{
1111
protected $table = 'users';
12-
12+
1313
/**
1414
* list of all agents and returning collection.
1515
*
@@ -148,7 +148,7 @@ public static function isTicketOwner($id)
148148
}
149149
}
150150
}
151-
151+
152152
/**
153153
* Get general maximum permission level for current user.
154154
*
@@ -167,7 +167,7 @@ public static function maxLevel()
167167
}else
168168
return 1;
169169
}
170-
170+
171171
/**
172172
* Get current permission level for user.
173173
*
@@ -190,7 +190,7 @@ public static function currentLevel()
190190
}else
191191
return 1;
192192
}
193-
193+
194194
/**
195195
* Get permission level for specified category.
196196
*
@@ -215,8 +215,8 @@ public function levelInCategory($id = false)
215215
}
216216
}
217217
}
218-
}
219-
218+
}
219+
220220
/**
221221
* Check if user has close permissions on a ticket.
222222
*
@@ -228,7 +228,7 @@ public static function canCloseTicket($id)
228228
{
229229
if (!auth()->check()) return false;
230230
$member = \PanicHDMember::find(auth()->user()->id);
231-
231+
232232
$a_perm = Setting::grab('close_ticket_perm');
233233

234234
if ($member->isAdmin() && $a_perm['admin'] == 'yes') {
@@ -239,14 +239,14 @@ public static function canCloseTicket($id)
239239
return true;
240240
}
241241
}
242-
242+
243243
if ($member->isTicketOwner($id) && $a_perm['owner'] == 'yes') {
244244
return true;
245245
}
246-
246+
247247
return false;
248248
}
249-
249+
250250
/**
251251
* Check if user can make a comment on a ticket.
252252
*
@@ -257,14 +257,14 @@ public static function canCloseTicket($id)
257257
public static function canCommentTicket($id)
258258
{
259259
if (!auth()->check()) return false;
260-
260+
261261
if (\PanicHDMember::canManageTicket($id)){
262262
return true;
263263
}else{
264264
return \PanicHDMember::isTicketOwner($id);
265265
}
266266
}
267-
267+
268268
/**
269269
* Check if user has manage permissions on a ticket.
270270
*
@@ -276,18 +276,18 @@ public static function canManageTicket($id)
276276
{
277277
if (!auth()->check()) return false;
278278
$member = \PanicHDMember::find(auth()->user()->id);
279-
279+
280280
if ($member->isAdmin()){
281281
return true;
282282
}elseif ($ticket = Ticket::find($id) ){
283283
if ($member->id == $ticket->agent_id or (Setting::grab('agent_restrict') == 0 and $member->categories()->where('id',$ticket->category_id)->count() == 1)) {
284284
return true;
285285
}
286286
}
287-
287+
288288
return false;
289289
}
290-
290+
291291
/**
292292
* Check if user can view new tickets button.
293293
*
@@ -299,12 +299,12 @@ public static function canViewNewTickets()
299299
{
300300
if (!auth()->check()) return false;
301301
$member = \PanicHDMember::find(auth()->user()->id);
302-
302+
303303
if ($member->isAdmin()){
304304
return true;
305-
}elseif($member->isAgent() and $member->currentLevel() == 2){
305+
}elseif($member->isAgent() and $member->currentLevel() == 2){
306306
if(Setting::grab('agent_restrict')==1){
307-
return $member->categories()->wherePivot('autoassign','1')->count()==0 ? false : true;
307+
return $member->categories()->wherePivot('autoassign','1')->count()==0 ? false : true;
308308
}else{
309309
return true;
310310
}
@@ -322,7 +322,7 @@ public function userDepartment()
322322
{
323323
return $this->belongsTo('PanicHD\PanicHD\Models\Department', 'ticketit_department', 'id');
324324
}
325-
325+
326326
/**
327327
* Get associated department list through person_id
328328
*
@@ -331,8 +331,8 @@ public function userDepartment()
331331
public function department()
332332
{
333333
return $this->belongsTo('PanicHD\PanicHD\Models\Department');
334-
}
335-
334+
}
335+
336336
/**
337337
* Get related categories.
338338
*
@@ -342,7 +342,7 @@ public function categories()
342342
{
343343
return $this->belongsToMany('PanicHD\PanicHD\Models\Category', 'panichd_categories_users', 'user_id', 'category_id')->withPivot('autoassign')->orderBy('name');
344344
}
345-
345+
346346
/**
347347
* Get categories where user has permission to create new tickets
348348
*
@@ -351,25 +351,25 @@ public function categories()
351351
public function getNewTicketCategories()
352352
{
353353
if ($this->isAdmin()){
354-
$categories = Category::orderBy('name');
354+
$categories = Category::orderBy('name');
355355
}else{
356356
$categories = Category::where('create_level', '1')->orderBy('name')->get();
357-
357+
358358
if ($this->isAgent() and $this->currentLevel() == 2){
359359
$create_level_2 = $this->categories()->where('create_level', '2')->get();
360-
360+
361361
$categories = $categories->merge($create_level_2)->sortBy('name');
362362

363363
}
364-
}
365-
364+
}
365+
366366
if (LaravelVersion::min('5.3.0')) {
367367
return $categories->pluck('name', 'id');
368368
} else {
369369
return $categories->lists('name', 'id');
370370
}
371371
}
372-
372+
373373
/**
374374
* Get categories where user has permission to edit tickets
375375
*
@@ -382,9 +382,9 @@ public function getEditTicketCategories()
382382
}elseif($this->isAgent() and $this->currentLevel() == 2){
383383
$categories = $this->categories()->where('create_level', '<=', '2')->orderBy('name');
384384
}else{
385-
return [];
385+
return [];
386386
}
387-
387+
388388
if (LaravelVersion::min('5.3.0')) {
389389
return $categories->pluck('name', 'id');
390390
} else {
@@ -417,7 +417,7 @@ public function userTickets($complete = false)
417417
return $this->hasMany('PanicHD\PanicHD\Models\Ticket', 'user_id')->whereNull('completed_at');
418418
}
419419
}
420-
420+
421421
/**
422422
* Get ALL member tickets
423423
*
@@ -524,7 +524,7 @@ public function scopeVisible($query)
524524
public function scopeVisibleForAgent($query, $id)
525525
{
526526
$member = \PanicHDMember::findOrFail($id);
527-
527+
528528
if ($member->currentLevel() == 2) {
529529
// Depends on agent_restrict
530530
if (Setting::grab('agent_restrict') == 0) {
@@ -540,7 +540,7 @@ public function scopeVisibleForAgent($query, $id)
540540
return false;
541541
}
542542
}
543-
543+
544544
/**
545545
* Get array with all user id's from the departments where current user belongs and users that have ticketit_department = 0
546546
*
@@ -550,7 +550,12 @@ public function getMyNoticesUsers()
550550
{
551551
// Get my related departments
552552
$related_departments = [];
553-
foreach ($this->getRelatedDepartments() as $rel){
553+
554+
$c_related = $this->getRelatedDepartments();
555+
556+
if (!$c_related) return [];
557+
558+
foreach ($c_related as $rel){
554559
$related_departments [] = $rel->id;
555560
}
556561

@@ -563,22 +568,22 @@ public function getMyNoticesUsers()
563568
* - agent person in related_departments
564569
*/
565570
$related_users = \PanicHDMember::where('id','!=',$this->id)
566-
->whereIn('ticketit_department', $related_departments);
567-
571+
->whereIn('ticketit_department', $related_departments);
572+
568573
// Get users that are visible by all departments
569574
$all_dept_users = \PanicHDMember::where('ticketit_department','0');
570-
575+
571576
if (version_compare(app()->version(), '5.3.0', '>=')) {
572577
$related_users = $related_users->pluck('id')->toArray();
573578
$related_users = array_unique(array_merge($related_users, $all_dept_users->pluck('id')->toArray()));
574579
}else{
575580
$related_users = $related_users->lists('id')->toArray();
576581
$related_users = array_unique(array_merge($related_users, $all_dept_users->lists('id')->toArray()));
577582
}
578-
583+
579584
return $related_users;
580585
}
581-
586+
582587
/*
583588
* Get member related departments in Department hierarchy
584589
*
@@ -589,8 +594,10 @@ public function getMyNoticesUsers()
589594
*/
590595
public function getRelatedDepartments()
591596
{
592-
$member_department = $this->department()->get();
593-
597+
if (is_null($this->department)) return [];
598+
599+
$member_department = $this->department()->get();
600+
594601
if ($this->department()->first()->is_main()){
595602
return $member_department->merge($this->department()->first()->descendants()->get());
596603
}else{

0 commit comments

Comments
 (0)