|
1
|
|
|
<?php namespace Anomaly\UsersModule\Role\Permission; |
|
2
|
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Addon\AddonCollection; |
|
4
|
|
|
use Anomaly\UsersModule\Role\Contract\RoleInterface; |
|
5
|
|
|
use Illuminate\Config\Repository; |
|
6
|
|
|
use Illuminate\Translation\Translator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class PermissionFormFields |
|
10
|
|
|
* |
|
11
|
|
|
* @link http://pyrocms.com/ |
|
12
|
|
|
* @author PyroCMS, Inc. <[email protected]> |
|
13
|
|
|
* @author Ryan Thompson <[email protected]> |
|
14
|
|
|
*/ |
|
15
|
|
|
class PermissionFormFields |
|
16
|
|
|
{ |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Handle the fields. |
|
20
|
|
|
* |
|
21
|
|
|
* @param PermissionFormBuilder $builder |
|
22
|
|
|
* @param AddonCollection $addons |
|
23
|
|
|
* @param Translator $translator |
|
24
|
|
|
* @param Repository $config |
|
25
|
|
|
*/ |
|
26
|
|
|
public function handle( |
|
27
|
|
|
PermissionFormBuilder $builder, |
|
28
|
|
|
AddonCollection $addons, |
|
29
|
|
|
Translator $translator, |
|
30
|
|
|
Repository $config |
|
31
|
|
|
) { |
|
32
|
|
|
/* @var RoleInterface $role */ |
|
33
|
|
|
$role = $builder->getEntry(); |
|
34
|
|
|
|
|
35
|
|
|
$fields = []; |
|
36
|
|
|
|
|
37
|
|
|
$namespaces = array_merge(['streams'], $addons->withConfig('permissions')->namespaces()); |
|
38
|
|
|
|
|
39
|
|
|
/* |
|
40
|
|
|
* gather all the addons with a |
|
41
|
|
|
* permissions configuration file. |
|
42
|
|
|
* |
|
43
|
|
|
* @var Addon $addon |
|
44
|
|
|
*/ |
|
45
|
|
|
foreach ($namespaces as $namespace) { |
|
46
|
|
|
|
|
47
|
|
|
foreach ($config->get($namespace . '::permissions', []) as $group => $permissions) { |
|
48
|
|
|
|
|
49
|
|
|
/* |
|
50
|
|
|
* Determine the general |
|
51
|
|
|
* form UI components. |
|
52
|
|
|
*/ |
|
53
|
|
|
$label = $namespace . '::permission.' . $group . '.name'; |
|
54
|
|
|
|
|
55
|
|
|
if (!$translator->has($warning = $namespace . '::permission.' . $group . '.warning')) { |
|
56
|
|
|
$warning = null; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (!$translator->has($instructions = $namespace . '::permission.' . $group . '.instructions')) { |
|
60
|
|
|
$instructions = null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/* |
|
64
|
|
|
* Gather the available |
|
65
|
|
|
* permissions for the group. |
|
66
|
|
|
*/ |
|
67
|
|
|
$available = array_combine( |
|
68
|
|
|
array_map( |
|
69
|
|
|
function ($permission) use ($namespace, $group) { |
|
70
|
|
|
return $namespace . '::' . $group . '.' . $permission; |
|
71
|
|
|
}, |
|
72
|
|
|
$permissions |
|
73
|
|
|
), |
|
74
|
|
|
array_map( |
|
75
|
|
|
function ($permission) use ($namespace, $group) { |
|
76
|
|
|
return $namespace . '::permission.' . $group . '.option.' . $permission; |
|
77
|
|
|
}, |
|
78
|
|
|
$permissions |
|
79
|
|
|
) |
|
80
|
|
|
); |
|
81
|
|
|
|
|
82
|
|
|
/* |
|
83
|
|
|
* Build the checkboxes field |
|
84
|
|
|
* type to handle the UI. |
|
85
|
|
|
*/ |
|
86
|
|
|
$fields[str_replace('.', '_', $namespace . '::' . $group)] = [ |
|
87
|
|
|
'label' => $label, |
|
88
|
|
|
'warning' => $warning, |
|
89
|
|
|
'instructions' => $instructions, |
|
90
|
|
|
'type' => 'anomaly.field_type.checkboxes', |
|
91
|
|
|
'value' => $role->getPermissions(), |
|
92
|
|
|
'config' => [ |
|
93
|
|
|
'options' => $available, |
|
94
|
|
|
], |
|
95
|
|
|
]; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$builder->setFields($fields); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|