Passed
Push — dev-master ( 289114...1a42ae )
by Vijay
34:35
created

UsersData::patch()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
dl 0
loc 19
rs 8.8571
c 0
b 0
f 0
eloc 12
nc 3
nop 2
1
<?php
2
3
namespace FFCMS\Controllers\API;
4
5
use FFMVC\Helpers;
6
use FFCMS\{Traits, Models, Mappers};
7
8
/**
9
 * Api UsersData 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 UsersData extends APIMapper
16
{
17
    protected $adminOnly = false;
18
19
20
    /**
21
     * Perform a create/update of the an item, used by POST, PUT, PATCH
22
     *
23
     * @param \Base $f3
24
     * @param array $prohibitedFields
25
     * @return void
26
     */
27
    private function save(\Base $f3, array $prohibitedFields = [])
28
    {
29
        // do not allow request to define these fields:
30
        $data = $f3->get('REQUEST');
31
        foreach ($prohibitedFields as $field) {
32
            if (array_key_exists($field, $data)) {
33
                unset($data[$field]);
34
            }
35
        }
36
37
        // load pre-existing value
38
        $db = \Registry::get('db');
39
        $m = $this->getMapper();
40
        if ($f3->get('VERB') == 'PUT') {
41
            $m->load(['uuid = ?', $data['uuid']]);
42
        } else {
43
            $m->load(['users_uuid = ? AND ' . $db->quotekey('key') . ' = ?', $data['users_uuid'], $data['key']]);
44
        }
45
46
        // copy data and validate
47
        $oldMapper = clone($m);
48
        $m->copyfrom($data);
49
        $m->validationRequired([
50
            'users_uuid', 'key', 'value'
51
        ]);
52
        $errors = $m->validate(false);
53
        if (true !== $errors) {
54
            foreach ($errors as $error) {
55
                $this->setOAuthError('invalid_request');
56
                $this->failure($error['field'], $error['rule']);
57
            }
58
        } else {
59
            // load in original data and then replace for save
60
            if (!$m->validateSave()) {
61
                $this->setOAuthError('invalid_request');
62
                $this->failure('error', 'Unable to update object.');
63
                return;
64
            }
65
66
            $this->audit([
67
                'users_uuid' => $m->users_uuid,
68
                'actor' => $f3->get('uuid'),
69
                'event' => 'Users Data Updated via API',
70
                'old' => $oldMapper->cast(),
71
                'new' => $m->cast()
72
            ]);
73
74
            // return raw data for object?
75
            $adminView = $f3->get('is_admin') && 'admin' == $f3->get('REQUEST.view');
76
            $this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields'));
77
        }
78
    }
79
80
81
    /**
82
     * Update data
83
     *
84
     * @param \Base $f3
85
     * @param array $params
86
     * @return void
87
     */
88
    public function patch(\Base $f3, array $params)
89
    {
90
        $isAdmin = $f3->get('is_admin');
91
        $m = $this->getIdObjectIfUser($f3, $params, 'uuid', $params['id']);
92
        if (!is_object($m) || null == $m->uuid) {
93
            return;
94
        } elseif (!$isAdmin && $m->users_uuid !== $f3->get('uuid')) {
95
            $this->failure('authentication_error', "User does not have permission.", 401);
96
            return $this->setOAuthError('access_denied');
97
        }
98
99
        $f3->set('REQUEST.users_uuid', $m->users_uuid);
100
        $f3->set('REQUEST.key', $m->key);
101
102
        // these fields can't be modified
103
        return $this->save($f3, [
104
            'id', 'uuid'
105
        ]);
106
    }
107
108
109
    /**
110
     * Replace data
111
     *
112
     * @param \Base $f3
113
     * @param array $params
114
     * @return void
115
     */
116
    public function put(\Base $f3, array $params)
117
    {
118
        $isAdmin = $f3->get('is_admin');
119
        $m = $this->getIdObjectIfUser($f3, $params, 'uuid', $params['id']);
120
        if (!is_object($m) || null == $m->uuid) {
121
            return;
122
        } elseif (!$isAdmin && $m->users_uuid !== $f3->get('uuid')) {
123
            $this->failure('authentication_error', "User does not have permission.", 401);
124
            return $this->setOAuthError('access_denied');
125
        }
126
127
        $f3->set('REQUEST.uuid', $m->uuid);
128
        $f3->set('REQUEST.users_uuid', $m->users_uuid);
129
130
        // these fields can't be modified
131
        return $this->save($f3, [
132
            'id'
133
        ]);
134
    }
135
136
137
    /**
138
     * Create new data
139
     *
140
     * @param \Base $f3
141
     * @param array $params
142
     * @return void
143
     */
144
    public function post(\Base $f3, array $params)
145
    {
146
        $isAdmin = $f3->get('is_admin');
147
        if ($isAdmin && !empty($params) && array_key_exists('id', $params)) {
148
            $users_uuid = $params['id'];
149
        } elseif (!$isAdmin) {
150
            $users_uuid = $f3->get('uuid');
151
        } else {
152
            $users_uuid = $f3->get('REQUEST.users_uuid');
153
        }
154
        $f3->set('REQUEST.users_uuid', $users_uuid);
155
156
        // this fields can't be modified
157
        $prohibitedFields = [
158
            'id', 'uuid'
159
        ];
160
161
        return $this->save($f3, $prohibitedFields);
162
    }
163
164
165
}
166