aboutsummaryrefslogtreecommitdiff
path: root/app/controllers/massmail/permissions.php
blob: bd8200bf6429db977f289213ec1f8b08a84b7a11 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php

class Massmail_PermissionsController extends \AuthenticatedController
{

    public function before_filter(&$action, &$args)
    {
        parent::before_filter($action, $args);

        if (!\MassMail\MassMailPermission::has(User::findCurrent()->id, true)) {
            throw new AccessDeniedException();
        }

        Navigation::activateItem('/messaging/massmail/permissions');
    }

    /**
     * Lists all existing permissions.
     * @return void
     * @throws AccessDeniedException
     */
    public function index_action()
    {
        PageLayout::setTitle(_('Berechtigungen für den Nachrichtenversand an Zielgruppen'));

        $this->permissions = \MassMail\MassMailPermission::findBySQL("1");
        usort(
            $this->permissions,
            fn ($a, $b) => strnatcasecmp($a->institute_name, $b->institute_name)
        );

        $sidebar = Sidebar::Get();
        $actions = new ActionsWidget();
        $actions->addLink(
            _('Neue Berechtigung vergeben'),
            $this->url_for('massmail/permissions/edit'),
            Icon::create('add'),
        )->asDialog('size=medium');
        $sidebar->addWidget($actions);

        $this->render_vue_app(
            Studip\VueApp::create('massmail/MassMailPermissions')
        );
    }

    /**
     * Provides a form for entering or editing a massmail permission.
     * @param int $id which permission to edit, create a new one if $id is 0
     * @return void
     * @throws AccessDeniedException
     */
    public function edit_action(int $id = 0)
    {
        $permission = new \MassMail\MassMailPermission($id);

        PageLayout::setTitle(
            $permission->isNew()
                ? _('Berechtigung erstellen')
                : _('Berechtigung bearbeiten')
        );

        $institutes = [];
        foreach (Institute::getInstitutes() as $one) {
            $institutes[$one['Institut_id']] = $one['Name'];
        }

        $degrees = [];
        foreach (Degree::findBySQL("1 ORDER BY `name`") as $one) {
            $degrees[$one->id] = $one->name;
        }

        $subjects = [];
        foreach (StudyCourse::findBySQL("1 ORDER BY `name`") as $one) {
            $subjects[$one->id] = $one->name;
        }

        $form = \Studip\Forms\Form::fromSORM(
            $permission,
            [
                'fields' => [
                    'institute_id' => [
                        'type' => 'select',
                        'required' => true,
                        'label' => _('Einrichtung'),
                        'value' => $permission->institute_id,
                        'options' => $institutes
                    ],
                    'min_perm' => [
                        'type' => 'select',
                        'required' => true,
                        'label' => _('Benötigte Rechte'),
                        'value' => $permission->min_perm,
                        'options' => [
                            'admin' => 'admin',
                            'dozent' => 'dozent',
                            'tutor' => 'tutor'
                        ]
                    ],
                    'allowed_degrees' => [
                        'type' => 'checkboxCollection',
                        'collapsable' => true,
                        'label' => _('Erlaubte Abschlüsse'),
                        'value' => $permission->allowed_degrees->pluck('id'),
                        'options' => $degrees
                    ],
                    'allowed_subjects' => [
                        'type' => 'checkboxCollection',
                        'collapsable' => true,
                        'label' => _('Erlaubte Fächer'),
                        'value' => $permission->allowed_subjects->pluck('id'),
                        'options' => $subjects
                    ],
                    'allowed_institutes' => [
                        'type' => 'checkboxCollection',
                        'collapsable' => true,
                        'label' => _('Erlaubte Einrichtungen (außer den eigenen)'),
                        'value' => $permission->allowed_institutes->pluck('id'),
                        'options' => $institutes
                    ]
                ]
            ]
        )->setURL($this->url_for('massmail/permissions/store', $id));

        $this->render_form($form);
    }

    /**
     * Stores permission data by editing an existing or creating a new one.
     * @param int $id the permission to store
     * @return void
     * @throws AccessDeniedException
     */
    public function store_action(int $id = 0)
    {
        CSRFProtection::verifyUnsafeRequest();
        $permission = new \MassMail\MassMailPermission($id);
        $permission->institute_id = Request::option('institute_id');
        $permission->min_perm = Request::get('min_perm');
        $permission->allowed_degrees = Degree::findMany(Request::optionArray('allowed_degrees'));
        $permission->allowed_subjects = StudyCourse::findMany(Request::optionArray('allowed_subjects'));
        $permission->allowed_institutes = Institute::findMany(Request::optionArray('allowed_institutes'));

        if ($permission->store() !== false) {
            PageLayout::postSuccess('Die Daten wurden gespeichert.');
        } else {
            PageLayout::postError('Die Daten konnten nicht gespeichert werden.');
        }

        $this->relocate('massmail/permissions');
    }

    /**
     * Deletes the given permission entry.
     * @param int $id the permission to delete
     * @return void
     * @throws AccessDeniedException
     */
    public function delete_action(int $id)
    {
        $permission = \MassMail\MassMailPermission::find($id);
        if ($permission) {
            if ($permission->delete()) {
                PageLayout::postSuccess(_('Die Berechtigung wurde gelöscht.'));
            } else {
                PageLayout::postError(_('Die Berechtigung konnte nicht gelöscht werden.'));
            }
        } else {
            PageLayout::postError(_('Die Berechtigung wurde nicht gefunden.'));
        }

        $this->relocate('massmail/permissions');
    }

}