aboutsummaryrefslogtreecommitdiff
path: root/app/routes/ResourceProperties.php
blob: 2ddbbaf74ea5f3082444020b5ebacca521ee938e (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
<?php
namespace RESTAPI\Routes;

/**
 * This file contains API routes related to ResourceProperty objects.
 *
 * @author      Moritz Strohm <strohm@data-quest.de>
 * @copyright   2017-2019
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
 * @since       4.5
 * @deprecated  Since Stud.IP 5.0. Will be removed in Stud.IP 6.0.
 */
class ResourceProperties extends \RESTAPI\RouteMap
{
    /**
     * Validate access to each route.
     */
    public function before()
    {
        if (!\ResourceManager::userHasGlobalPermission(\User::findCurrent(), 'admin')) {
            throw new \AccessDeniedException();
        }
    }

    /**
     * Returns all resource property definitions.
     *
     * @get /resources/properties
     */
    public function getAllResourcePropertyDefinitions()
    {
        $properties = \ResourcePropertyDefinition::findBySql('TRUE ORDER BY name ASC');

        $result = [];

        if ($properties) {
            foreach ($properties as $p) {
                $result[] = $p->toRawArray();
            }
        }

        return $result;
    }


    /**
     * Creates a new resource property definition.
     *
     * @post /resources/add_property
     */
    public function addResourcePropertyDefinition()
    {
        $name = \Request::get('name');
        $description = \Request::i18n('description');
        $type = \Request::get('type');
        $write_permission_level = \Request::get('write_permission_level');
        $options = \Request::get('options', '');
        $range_search = \Request::bool('range_search');

        if (!$name) {
            $this->halt(
                400,
                'The field \'name\' must not be empty!'
            );
        }
        if (!in_array($type, \ResourcePropertyDefinition::getDefinedTypes())) {
            $this->halt(
                400,
                'Invalid property type specified!'
            );
        }
        if (!in_array($write_permission_level, ['user', 'autor', 'tutor', 'admin'])) {
            $this->halt(
                400,
                'Invalid permission level in field \'write_permission_level\'!'
            );
        }

        $property = new \ResourcePropertyDefinition();
        $property->name = $name;
        $property->description = $description;
        $property->type = $type;
        $property->options = $options ?: '';
        $property->range_search = $range_search;
        $property->write_permission_level = $write_permission_level;

        if (!$property->store()) {
            $this->halt(
                500,
                'Error while saving the property!'
            );
        }
        return $property->toRawArray();
    }


    /**
     * Get a resource property definition object.
     *
     * @get /resources/property/:property_id
     */
    public function getResourcePropertyDefinition($property_id)
    {
        $property = \ResourcePropertyDefinition::find($property_id);
        if (!$property) {
            $this->notFound('ResourcePropertyDefinition object not found!');
        }

        return $property->toRawArray();
    }


    /**
     * Modifies a resource property definition.
     *
     * @put /resources/property/:property_id
     */
    public function editResourcePropertyDefinition($property_id)
    {
        $property = \ResourcePropertyDefinition::find($property_id);
        if (!$property) {
            $this->notFound('ResourcePropertyDefinition object not found!');
        }

        if ($property->system) {
            $this->halt(
                403,
                'System properties must not be edited!'
            );
        }

        $name = $this->data['name'];
        $description = $this->data['description'];
        $type = $this->data['type'];
        $write_permission_level = $this->data['write_permission_level'];
        $options = $this->data['options'];
        $range_search = $this->data['range_search'];

        if ($name) {
            $property->name = $name;
        }

        if ($description) {
            $property->description = $description;
        }

        if ($type) {
            if (!in_array($type, \ResourcePropertyDefinition::getDefinedTypes())) {
                $this->halt(
                    400,
                    'Invalid property type specified!'
                );
            }
            $property->type = $type;
        }

        if ($write_permission_level) {
            if (!in_array($write_permission_level, ['user', 'autor', 'tutor', 'admin'])) {
                $this->halt(
                    400,
                    'Invalid permission level in field \'write_permission_level\'!'
                );
            }
            $property->write_permission_level = $write_permission_level;
        }

        if ($options) {
            $property->options = $options;
        }

        if ($range_search) {
            $property->range_search = $range_search;
        }

        if ($property->isDirty()) {
            if ($property->store()) {
                return $property->toRawArray();
            } else {
                $this->halt(
                    500,
                    'Error while saving the property!'
                );
            }
        }

        return $property->toRawArray();
    }


    /**
     * Deletes a resource property definition object.
     *
     * @delete /resources/property/:property_id
     */
    public function deleteResourcePropertyDefinition($property_id)
    {
        $property = \ResourcePropertyDefinition::find($property_id);
        if (!$property) {
            $this->notFound('ResourcePropertyDefinition object not found!');
        }

        if (!\ResourceManager::userHasGlobalPermission(\User::findCurrent(), 'admin')) {
            $this->halt(403);
        }

        //Check if the property is in use:

        if ($property->isInUse()) {
            $this->halt(
                403,
                'The property is in use and can therefore not be deleted!'
            );
        }

        if ($property->delete()) {
            return "OK";
        } else {
            $this->halt(
                500,
                'Error while deleting resource property definition!'
            );
        }
    }
}