Users   A
last analyzed

Complexity

Total Complexity 33

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 199
rs 9.3999
c 0
b 0
f 0
wmc 33
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
B get() 0 14 7
D save() 0 38 9
B patch() 0 24 6
B put() 0 22 5
A post() 0 20 2
A delete() 0 18 4
1
<?php
2
3
namespace FFCMS\Controllers\API;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Models, Mappers};
7
8
/**
9
 * Api Users REST Controller Class.
10
 *
11
 * @author Vijay Mahrra <[email protected]>
12
 * @copyright Vijay Mahrra
13
 * @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html)
14
 */
15
class Users extends Mapper
16
{
17
    protected $adminOnly = false;
18
19
    /**
20
     * @var \FFCMS\Mappers\Users mapper for class
21
     */
22
    protected $mapper;
23
24
    /**
25
     * Display the authorised user or
26
     * if an admin, the user specified in the url /@id or  param ?id=
27
     *
28
     * @param \Base $f3
29
     * @param array $params
30
     * @return null|array|boolean
31
     */
32
    public function get(\Base $f3, array $params)
33
    {
34
        $isAdmin = $f3->get('isAdmin');
35
        $m = $this->getIdObjectIfUser($f3, $params, 'uuid', $f3->get('uuid'));
36
        if (!is_object($m) || null == $m->uuid) {
37
            return;
38
        } elseif (!$isAdmin && $m->uuid !== $f3->get('uuid')) {
39
            $this->failure('authentication_error', "User does not have permission.", 401);
40
            return $this->setOAuthError('access_denied');
41
        }
42
        // return raw data for object?
43
        $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
44
        $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
45
    }
46
47
48
    /**
49
     * Perform a create/update of the an item, used by POST, PUT, PATCH
50
     *
51
     * @param \Base $f3
52
     * @param array $prohibitedFields
53
     * @return void
54
     */
55
    private function save(\Base $f3, array $prohibitedFields = [])
56
    {
57
        // do not allow request to define these fields:
58
        $data = $f3->get('REQUEST');
59
        foreach ($prohibitedFields as $field) {
60
            if (array_key_exists($field, $data)) {
61
                unset($data[$field]);
62
            }
63
        }
64
65
        if (!empty($data['password'])) {
66
            $data['password'] = Helpers\Str::password($data['password']);
67
        }
68
        $f3->set('REQUEST', $data); // update REQUEST with prohibited fields removed
69
70
        // set validation check
71
        $m = $this->getMapper();
72
        $m->copyfrom($data);
73
        $m->validationRequired();
74
        $errors = $m->validate(false);
75
        if (true !== $errors) {
76
            foreach ($errors as $error) {
77
                $this->setOAuthError('invalid_request');
78
                $this->failure($error['field'], $error['rule']);
79
            }
80
        } else {
81
            // load in original data and then replace for save
82
            if (!$m->save()) {
83
                $this->setOAuthError('invalid_request');
84
                $this->failure('error', 'Unable to update object.');
85
                return;
86
            }
87
88
            // return raw data for object?
89
            $adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view');
90
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
91
        }
92
    }
93
94
95
    /**
96
     * Update user details - normal user can
97
     *
98
     * @param \Base $f3
99
     * @param array $params
100
     * @return null|array|boolean
101
     */
102
    public function patch(\Base $f3, array $params)
103
    {
104
        $isAdmin = $f3->get('isAdmin');
105
        // should return a pre-existing object
106
        $m = $this->getIdObjectIfUser($f3, $params, 'uuid', $f3->get('uuid'));
107
        if (!is_object($m) || null == $m->uuid) {
108
            return;
109
        } elseif (!$isAdmin && $m->uuid !== $f3->get('uuid')) {
110
            $this->failure('authentication_error', "User does not have permission.", 401);
111
            return $this->setOAuthError('access_denied');
112
        }
113
114
        // these fields can't be modified
115
        $fields = [
116
            'id', 'uuid', 'created', 'login_last', 'login_count'
117
        ];
118
119
        if (!$isAdmin) {
120
            $fields[] = 'status';
121
            $fields[] = 'scopes';
122
        }
123
124
        return $this->save($f3, $fields);
125
    }
126
127
128
    /**
129
     * Replace user details - admin only
130
     *
131
     * @param \Base $f3
132
     * @param array $params
133
     * @return null|array|boolean
134
     */
135
    public function put(\Base $f3, array $params)
136
    {
137
        // should return a pre-existing object
138
        $m = $this->getIdObjectIfAdmin($f3, $params, 'uuid', $f3->get('uuid'));
139
        if (!is_object($m) || null == $m->uuid) {
140
            return;
141
        }
142
143
        // these fields can't be modified
144
        $prohibitedFields = [
145
            'id', 'uuid', 'created'
146
        ];
147
148
        // clear all object fields except the above
149
        foreach ($m->fields() as $field) {
150
            if (!in_array($field, $prohibitedFields)) {
151
                $m->$field = null;
152
            }
153
        }
154
155
        return $this->save($f3, $prohibitedFields);
156
    }
157
158
159
    /**
160
     * Create a new user - admin only
161
     *
162
     * @param \Base $f3
163
     * @return null|array|boolean
164
     */
165
    public function post(\Base $f3)
166
    {
167
        // must be an admin
168
        $isAdmin = $f3->get('isAdmin');
169
        if (!$isAdmin) {
170
            $this->failure('authentication_error', "User does not have permission.", 401);
171
            return $this->setOAuthError('access_denied');
172
        }
173
174
        // populate mapper with acceptable data for creating a new user
175
        $usersModel = Models\Users::instance();
176
        $this->mapper = $usersModel->newUserTemplate();
177
178
        // this fields can't be modified
179
        $prohibitedFields = [
180
            'id'
181
        ];
182
183
        return $this->save($f3, $prohibitedFields);
184
    }
185
186
187
    /**
188
     * Mark a user as status=closed
189
     *
190
     * @param \Base $f3
191
     * @param array $params
192
     * @return null|array|boolean
193
     */
194
    public function delete(\Base $f3, array $params)
195
    {
196
        $m = $this->getIdObjectIfAdmin($f3, $params, 'uuid', $f3->get('REQUEST.id'));
197
        if (!is_object($m) || null == $m->uuid) {
198
            return;
199
        }
200
201
        if ($f3->get('uuid') == $m->uuid) {
202
            $this->failure('client_error', "User cannot delete themself!", 401);
203
            return $this->setOAuthError('access_denied');
204
205
        }
206
207
        $m->status = 'closed';
208
        $this->data = [
209
            'deleted' => $m->save()
210
        ];
211
    }
212
213
}
214