Test Setup Failed
Pull Request — master (#190)
by
unknown
08:53
created

privilege::buildView()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 6
rs 9.4285
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
92
        if ($this->get_effective_value($options, $object)) {
93
            $label = $l10n->get('widget privilege: allow');
94
        } else {
95
            $label = $l10n->get('widget privilege: deny');
96
        }
97
        return sprintf($l10n->get('widget privilege: inherit %s'), $label);
98
    }
99
100
    public function search_for_object($object)
101
    {
102
        $help_obj = $object;
103
104
        while (true)
105
        {
106
            if ($help_obj instanceof dbacontainer) {
107
                return $help_obj->get_value();
108
            } else {
109
                if (!empty($help_obj->parent)) {
110
                    $help_obj = $help_obj->parent->vars['data'];
111
                } else {
112
                    return null;
113
                }
114
            }
115
        }
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     *
121
     * Symfony < 2.8 compat
122
     */
123
    public function getName()
124
    {
125
        return $this->getBlockPrefix();
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getBlockPrefix()
132
    {
133
        return 'privilege';
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getParent()
140
    {
141
        return compat::get_type_name('radiocheckselect');
142
    }
143
}