Test Setup Failed
Pull Request — master (#190)
by
unknown
17:12
created

privilege::setDefaultOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright CONTENT CONTROL GmbH, http://www.contentcontrol-berlin.de
4
 */
5
6
namespace midcom\datamanager\extension\type;
7
8
use Symfony\Component\OptionsResolver\Options;
9
use Symfony\Component\OptionsResolver\OptionsResolver;
10
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
11
use midcom\datamanager\extension\compat;
12
use Symfony\Component\Form\Extension\Core\Type\RadioType;
13
use midcom;
14
use midcom_core_user;
15
use Symfony\Component\Form\FormView;
16
use Symfony\Component\Form\FormInterface;
17
use midcom\datamanager\storage\container\dbacontainer;
18
19
/**
20
 * Experimental privilege type
21
 */
22
class privilege extends RadioType
23
{
24
    private $defaultChoices = array(
25
        'widget privilege: allow' => MIDCOM_PRIVILEGE_ALLOW,
26
        'widget privilege: deny' => MIDCOM_PRIVILEGE_DENY,
27
        'widget privilege: inherit' => MIDCOM_PRIVILEGE_INHERIT,
28
    );
29
30
    /**
31
     *  Symfony 2.6 compat
32
     *
33
     * {@inheritDoc}
34
     */
35
    public function setDefaultOptions(OptionsResolverInterface $resolver)
36
    {
37
        $this->configureOptions($resolver);
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function configureOptions(OptionsResolver $resolver)
44
    {
45
        $map_privilege = function (Options $options) {
46
            $return_options = $this->defaultChoices;
47
            return $return_options;
48
        };
49
        $resolver->setDefaults(array(
50
            'choices' => $map_privilege,
51
            'choices_as_values' => true,
52
            'expanded' => true,
53
        ));
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function buildView(FormView $view, FormInterface $form, array $options)
60
    {
61
        parent::buildView($view, $form, $options);
62
        $view->vars['type'] = $this;
63
        $view->vars['type_conf'] = $options['type_config'];
64
    }
65
66
    public function get_effective_value(array $options, $object = null)
67
    {
68
        if (!$object)
69
        {
70
            $defaults = midcom::get()->auth->acl->get_default_privileges();
71
            return $defaults[$options['privilege_name']] === MIDCOM_PRIVILEGE_ALLOW;
72
        }
73
        if ($options['assignee'] == 'SELF') {
74
            if ($object instanceof midcom_db_group) {
75
                //There's no sane way to query group privileges in auth right now, so we only return defaults
76
                $defaults = midcom::get()->auth->acl->get_default_privileges();
77
                return (($defaults[$options['privilege_name']] === MIDCOM_PRIVILEGE_ALLOW));
78
            }
79
            return midcom::get()->auth->can_user_do($options['privilege_name'],
80
                    new midcom_core_user($object->__object->id), $options['classname']);
81
        }
82
        if ($principal = midcom::get()->auth->get_assignee($options['assignee'])) {
83
            return $object->can_do($options['privilege_name'], $principal);
84
        }
85
        return $object->can_do($options['privilege_name'], $options['assignee']);
86
    }
87
88
    public function render_choices(array $options, $object = null)
89
    {
90
        $l10n = midcom::get()->i18n->get_l10n('midcom.datamanager');
91
        $inherit = $this->get_effective_value($options,$object);
92
        $allow = 'widget privilege: allow';
93
        $deny = 'widget privilege: deny';
94
        $base = $l10n->get('widget privilege: inherit %s');
95
96
        if ($inherit === true) {
97
            $allow = $l10n->get($allow);
98
            return sprintf($base, $allow);
99
        } elseif ($inherit === false) {
100
            $deny = $l10n->get($deny);
101
            return sprintf($base, $deny);
102
        }
103
        return 'widget privilege: inherit';
104
    }
105
106
    public function search_for_object($object)
107
    {
108
        $help_obj = $object;
109
110
        while (true)
111
        {
112
            if ($help_obj instanceof dbacontainer) {
113
                return $help_obj->get_value();
114
            } else {
115
                if (!empty($help_obj->parent)) {
116
                    $help_obj = $help_obj->parent->vars['data'];
117
                } else {
118
                    return null;
119
                }
120
            }
121
        }
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     *
127
     * Symfony < 2.8 compat
128
     */
129
    public function getName()
130
    {
131
        return $this->getBlockPrefix();
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    public function getBlockPrefix()
138
    {
139
        return 'privilege';
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145
    public function getParent()
146
    {
147
        return compat::get_type_name('radiocheckselect');
148
    }
149
}