1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FFCMS\Controllers\API; |
4
|
|
|
|
5
|
|
|
use FFMVC\Helpers; |
6
|
|
|
use FFCMS\{Traits, Models, Mappers}; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Api OAuth2Tokens 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 OAuth2Tokens extends Mapper |
16
|
|
|
{ |
17
|
|
|
protected $table = 'oauth2_tokens'; |
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
|
|
|
// set audit user if not set |
30
|
|
|
$data = $f3->get('REQUEST'); |
31
|
|
|
$user = $f3->get('user'); |
32
|
|
|
|
33
|
|
|
if (!array_key_exists('users_uuid', $data)) { |
34
|
|
|
$data['users_uuid'] = $user['uuid']; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
if (!array_key_exists('client_id', $data)) { |
38
|
|
|
$data['client_id'] = Helpers\Str::uuid(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (!array_key_exists('token', $data)) { |
42
|
|
|
$data['token'] = Helpers\Str::uuid(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!array_key_exists('type', $data)) { |
46
|
|
|
$data['type'] = 'access_token'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (!array_key_exists('scope', $data)) { |
50
|
|
|
$data['scope'] = 'read'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
// do not allow request to define these fields: |
54
|
|
|
foreach ($prohibitedFields as $field) { |
55
|
|
|
if (array_key_exists($field, $data)) { |
56
|
|
|
unset($data[$field]); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
// load pre-existing value |
61
|
|
|
$m = $this->getMapper(); |
62
|
|
|
|
63
|
|
|
// copy data and validate |
64
|
|
|
$oldMapper = clone($m); |
65
|
|
|
$m->copyfrom($data); |
66
|
|
|
$m->validationRequired([ |
67
|
|
|
'users_uuid' |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$errors = $m->validate(false); |
71
|
|
View Code Duplication |
if (true !== $errors) { |
72
|
|
|
foreach ($errors as $error) { |
73
|
|
|
$this->setOAuthError('invalid_request'); |
74
|
|
|
$this->failure($error['field'], $error['rule']); |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
// load original record, ovewrite |
78
|
|
|
if (!empty($data['uuid'])) { |
79
|
|
|
$m->load(['uuid = ?', $data['uuid']]); |
80
|
|
|
} |
81
|
|
|
$m->copyfrom($data); |
82
|
|
|
|
83
|
|
|
// load in original data and then replace for save |
84
|
|
|
if (!$m->validateSave()) { |
85
|
|
|
$this->setOAuthError('invalid_request'); |
86
|
|
|
$this->failure('error', 'Unable to update object.'); |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this->audit([ |
91
|
|
|
'users_uuid' => $m->users_uuid, |
92
|
|
|
'actor' => $m->client_id, |
93
|
|
|
'event' => ' App Updated via API', |
94
|
|
|
'old' => $oldMapper->cast(), |
95
|
|
|
'new' => $m->cast() |
96
|
|
|
]); |
97
|
|
|
|
98
|
|
|
// return raw data for object? |
99
|
|
|
$adminView = $f3->get('isAdmin') && 'admin' == $f3->get('REQUEST.view'); |
100
|
|
|
$this->data = $adminView ? $m->castFields($f3->get('REQUEST.fields')) : $m->exportArray($f3->get('REQUEST.fields')); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Update data |
107
|
|
|
* |
108
|
|
|
* @param \Base $f3 |
109
|
|
|
* @param array $params |
110
|
|
|
* @return null|array|boolean |
111
|
|
|
*/ |
112
|
|
|
public function patch(\Base $f3, array $params) |
113
|
|
|
{ |
114
|
|
|
$m = $this->getIdObjectIfAdmin($f3, $params, 'uuid', $params['id']); |
115
|
|
|
if (!is_object($m) || null == $m->uuid) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$f3->set('REQUEST.uuid', $m->uuid); |
120
|
|
|
|
121
|
|
|
$client_id = $f3->get('REQUEST.client_id'); |
122
|
|
|
if (empty($client_id)) { |
123
|
|
|
$f3->set('REQUEST.client_id', $m->client_id); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// these fields can't be modified |
127
|
|
|
return $this->save($f3, [ |
128
|
|
|
'id' |
129
|
|
|
]); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|