1
|
|
|
<?php namespace Distilleries\PermissionUtil\Helpers; |
2
|
|
|
|
3
|
|
|
use Distilleries\PermissionUtil\Contracts\PermissionUtilContract; |
4
|
|
|
use Illuminate\Contracts\Auth\Guard; |
5
|
|
|
|
6
|
|
|
class PermissionUtil implements PermissionUtilContract { |
7
|
|
|
|
8
|
|
|
protected $auth; |
9
|
|
|
protected $config; |
10
|
|
|
|
11
|
30 |
|
public function __construct(Guard $auth, array $config = []) { |
12
|
30 |
|
$this->auth = $auth; |
13
|
30 |
|
$this->config = $config; |
14
|
|
|
} |
15
|
|
|
|
16
|
28 |
|
public function hasAccess($key) |
17
|
|
|
{ |
18
|
28 |
|
if (empty($this->config['auth_restricted'])) { |
19
|
2 |
|
return true; |
20
|
|
|
} |
21
|
|
|
|
22
|
26 |
|
if ($this->auth->check()) { |
23
|
|
|
|
24
|
24 |
|
$user = $this->auth->user(); |
25
|
24 |
|
$implement = class_implements($user, true); |
26
|
|
|
|
27
|
24 |
|
if (empty($implement) || empty($implement['Distilleries\PermissionUtil\Contracts\PermissionUtilContract'])) { |
28
|
2 |
|
return true; |
29
|
|
|
} |
30
|
|
|
|
31
|
22 |
|
return (!empty($user)) ? $user->hasAccess($key) : false; |
32
|
|
|
} |
33
|
|
|
|
34
|
2 |
|
return false; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/* |
38
|
|
|
* Checks each elements of the array if the access is granted |
39
|
|
|
* @param Array $arrayKeys |
40
|
|
|
* @param Boolean $isAndRelation: if true its a AND relation check, if false a OR relation |
41
|
|
|
* @param String $child: if set, the hasAccess method will be called using element[$child] instead of the array's element itself |
42
|
|
|
* @return boolean |
43
|
|
|
*/ |
44
|
18 |
|
public function hasAccessArray($arrayKeys, $isAndRelation = false, $child = null) { |
45
|
18 |
|
$hasAccess = null; |
46
|
18 |
|
foreach ($arrayKeys as $key) { |
47
|
16 |
|
if ($hasAccess === null) { |
48
|
16 |
|
$hasAccess = $this->hasAccess(($child == null ? $key : $key[$child])); |
49
|
|
|
} else { |
50
|
12 |
|
if ($isAndRelation) { |
51
|
6 |
|
$hasAccess = $hasAccess && $this->hasAccess(($child == null ? $key : $key[$child])); |
|
|
|
|
52
|
6 |
|
if (!$hasAccess) break; |
53
|
|
|
} else { |
54
|
6 |
|
$hasAccess = $hasAccess || $this->hasAccess(($child == null ? $key : $key[$child])); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
18 |
|
return $hasAccess != null ?: false; |
59
|
|
|
} |
60
|
|
|
} |