1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace midcom\datamanager\extension\type; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
use Symfony\Component\OptionsResolver\Options; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolverInterface; |
12
|
|
|
use midcom\datamanager\extension\compat; |
13
|
|
|
use Symfony\Component\Form\Extension\Core\Type\RadioType; |
14
|
|
|
use midcom; |
15
|
|
|
use midcom_core_user; |
16
|
|
|
use Symfony\Component\Form\FormView; |
17
|
|
|
use Symfony\Component\Form\FormInterface; |
18
|
|
|
use midcom\datamanager\storage\container\dbacontainer; |
19
|
|
|
/** |
20
|
|
|
* Experimental privilege type |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
class privilege extends RadioType |
24
|
|
|
{ |
25
|
|
|
public $res; |
26
|
|
|
|
27
|
|
|
public $defaultChoices = array( |
28
|
|
|
'widget privilege: allow' => MIDCOM_PRIVILEGE_ALLOW, |
29
|
|
|
'widget privilege: deny' => MIDCOM_PRIVILEGE_DENY, |
30
|
|
|
'widget privilege: inherit' => MIDCOM_PRIVILEGE_INHERIT, |
31
|
|
|
); |
32
|
|
|
/** |
33
|
|
|
* Symfony 2.6 compat |
34
|
|
|
* |
35
|
|
|
* {@inheritDoc} |
36
|
|
|
*/ |
37
|
|
|
public function setDefaultOptions(OptionsResolverInterface $resolver) |
38
|
|
|
{ |
39
|
|
|
$this->configureOptions($resolver); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
|
public function configureOptions(OptionsResolver $resolver) |
46
|
|
|
{ |
47
|
|
|
$this->res = $resolver; |
48
|
|
|
$map_privilege = function (Options $options) { |
49
|
|
|
$return_options = $this->defaultChoices; |
50
|
|
|
return $return_options; |
51
|
|
|
}; |
52
|
|
|
$resolver->setDefaults(array( |
53
|
|
|
'choices' => $map_privilege, |
54
|
|
|
'choices_as_values' => true, |
55
|
|
|
'expanded' => true, |
56
|
|
|
)); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritdoc} |
61
|
|
|
*/ |
62
|
|
|
public function buildView(FormView $view, FormInterface $form, array $options) |
63
|
|
|
{ |
64
|
|
|
parent::buildView($view, $form, $options); |
65
|
|
|
$view->vars['privilege'] = $this; |
66
|
|
|
$view->vars['type_conf'] = $options['type_config']; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function get_effective_value(array $options,$object = null) |
70
|
|
|
{ |
71
|
|
|
if (!$object) |
72
|
|
|
{ |
73
|
|
|
$defaults = midcom::get()->auth->acl->get_default_privileges(); |
74
|
|
|
return(($defaults[$options['privilege_name']] === MIDCOM_PRIVILEGE_ALLOW)); |
75
|
|
|
} |
76
|
|
|
if ($options['assignee']== 'SELF') { |
77
|
|
|
if ($object instanceof midcom_db_group) { |
78
|
|
|
//There's no sane way to query group privileges in auth right now, so we only return defaults |
79
|
|
|
$defaults = midcom::get()->auth->acl->get_default_privileges(); |
80
|
|
|
return(($defaults[$options['privilege_name']] === MIDCOM_PRIVILEGE_ALLOW)); |
81
|
|
|
} |
82
|
|
|
return(midcom::get()->auth->can_user_do($options['privilege_name'], |
83
|
|
|
new midcom_core_user($object->__object->id),$options['classname'])); |
84
|
|
|
} |
85
|
|
|
if ($principal = midcom::get()->auth->get_assignee($options['assignee'])) { |
86
|
|
|
return $object->can_do($options['privilege_name'], $principal); |
87
|
|
|
} |
88
|
|
|
return $object->can_do($options['privilege_name'], $options['assignee']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function render_choices(array $options,$object = null) |
92
|
|
|
{ |
93
|
|
|
$l10n = midcom::get()->i18n->get_l10n('midcom.datamanager'); |
94
|
|
|
$inherit = $this->get_effective_value($options,$object); |
95
|
|
|
$allow = 'widget privilege: allow'; |
96
|
|
|
$deny = 'widget privilege: deny'; |
97
|
|
|
|
98
|
|
|
if ($inherit === true) { |
99
|
|
|
$base = $l10n->get('widget privilege: inherit %s'); |
100
|
|
|
$allow = $l10n->get($allow); |
101
|
|
|
$d = sprintf($base,$allow); |
102
|
|
|
} elseif ($inherit === false) { |
103
|
|
|
$base = $l10n->get('widget privilege: inherit %s'); |
104
|
|
|
$deny = $l10n->get($deny); |
105
|
|
|
$d = sprintf($base,$deny); |
106
|
|
|
} |
107
|
|
|
return $d; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function search_for_object($object) |
111
|
|
|
{ |
112
|
|
|
$object_found = false; |
113
|
|
|
$help_obj = $object; |
114
|
|
|
|
115
|
|
|
while (!$object_found) |
116
|
|
|
{ |
117
|
|
|
if ($help_obj instanceof dbacontainer) { |
118
|
|
|
$object_found = true; |
|
|
|
|
119
|
|
|
return $help_obj->get_value(); |
120
|
|
|
} else { |
121
|
|
|
if (!empty($help_obj->parent)) { |
122
|
|
|
$help_obj = $help_obj->parent->vars['data']; |
123
|
|
|
} else { |
124
|
|
|
return null; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* {@inheritdoc} |
133
|
|
|
* |
134
|
|
|
* Symfony < 2.8 compat |
135
|
|
|
*/ |
136
|
|
|
public function getName() |
137
|
|
|
{ |
138
|
|
|
return $this->getBlockPrefix(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function getBlockPrefix() |
145
|
|
|
{ |
146
|
|
|
return 'privilege'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
public function getParent() |
153
|
|
|
{ |
154
|
|
|
return compat::get_type_name('radiocheckselect'); |
155
|
|
|
} |
156
|
|
|
} |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: