1
|
|
|
<?php namespace Modules\User\Permissions; |
2
|
|
|
|
3
|
|
|
use Pingpong\Modules\Module; |
4
|
|
|
|
5
|
|
|
class PermissionManager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Module |
9
|
|
|
*/ |
10
|
|
|
private $module; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
*/ |
14
|
|
|
public function __construct() |
15
|
|
|
{ |
16
|
|
|
$this->module = app('modules'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get the permissions from all the enabled modules |
21
|
|
|
* @return array |
22
|
|
|
*/ |
23
|
|
|
public function all() |
24
|
|
|
{ |
25
|
|
|
$permissions = []; |
26
|
|
|
foreach ($this->module->enabled() as $enabledModule) { |
|
|
|
|
27
|
|
|
$configuration = config(strtolower('asgard.' . $enabledModule->getName()) . '.permissions'); |
28
|
|
|
if ($configuration) { |
29
|
|
|
$permissions[$enabledModule->getName()] = $configuration; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return $permissions; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return a correctly type casted permissions array |
38
|
|
|
* @param $permissions |
39
|
|
|
* @return array |
40
|
|
|
*/ |
41
|
|
|
public function clean($permissions) |
42
|
|
|
{ |
43
|
|
|
if (!$permissions) { |
44
|
|
|
return []; |
45
|
|
|
} |
46
|
|
|
$cleanedPermissions = []; |
47
|
|
|
foreach ($permissions as $permissionName => $checkedPermission) { |
48
|
|
|
$cleanedPermissions[$permissionName] = $this->getState($checkedPermission); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $cleanedPermissions; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param $checkedPermission |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
protected function getState($checkedPermission) |
59
|
|
|
{ |
60
|
|
|
if ($checkedPermission == 'true') { |
61
|
|
|
return true; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($checkedPermission == 'false') { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return (bool)$checkedPermission; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Are all of the permissions passed of false value? |
73
|
|
|
* @param array $permissions Permissions array |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function permissionsAreAllFalse(array $permissions) |
77
|
|
|
{ |
78
|
|
|
$uniquePermissions = array_unique($permissions); |
79
|
|
|
|
80
|
|
|
if (count($uniquePermissions) > 1) { |
81
|
|
|
return false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$uniquePermission = reset($uniquePermissions); |
85
|
|
|
|
86
|
|
|
$cleanedPermission = $this->getState($uniquePermission); |
87
|
|
|
|
88
|
|
|
return $cleanedPermission === false; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|