1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace FFCMS\Controllers\User; |
4
|
|
|
|
5
|
|
|
use FFMVC\Helpers; |
6
|
|
|
use FFCMS\{Controllers, Models, Mappers, Traits}; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Use Apps Controller Class. |
10
|
|
|
* |
11
|
|
|
* OAuth2 WWW Handler |
12
|
|
|
* |
13
|
|
|
* @author Vijay Mahrra <[email protected]> |
14
|
|
|
* @copyright (c) Copyright 2016 Vijay Mahrra |
15
|
|
|
* @license GPLv3 (http://www.gnu.org/licenses/gpl-3.0.html) |
16
|
|
|
*/ |
17
|
|
|
class Apps extends Controllers\User\Base |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* User's own API applications |
21
|
|
|
* |
22
|
|
|
* @param \Base $f3 |
23
|
|
|
* @return void |
24
|
|
|
*/ |
25
|
|
|
public function apps(\Base $f3) |
26
|
|
|
{ |
27
|
|
|
$this->redirectLoggedOutUser(); |
28
|
|
|
$this->csrf(); |
29
|
|
|
|
30
|
|
|
$oAuth2Model = Models\OAuth2::instance(); |
31
|
|
|
|
32
|
|
|
// fetch the user's apps |
33
|
|
|
$f3->set('apps', $oAuth2Model->getUserApps($f3->get('uuid'))); |
34
|
|
|
|
35
|
|
|
$f3->set('breadcrumbs', [ |
36
|
|
|
_('My Account') => 'user', |
37
|
|
|
_('Apps') => 'api_apps', |
38
|
|
|
]); |
39
|
|
|
|
40
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
41
|
|
|
echo \View::instance()->render('apps/apps.phtml'); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* register app |
47
|
|
|
* |
48
|
|
|
* @param \Base $f3 |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function appPost(\Base $f3) |
52
|
|
|
{ |
53
|
|
|
$this->csrf('@api_apps'); |
54
|
|
|
$this->redirectLoggedOutUser(); |
55
|
|
|
|
56
|
|
|
$view = 'apps/apps.phtml'; |
57
|
|
|
$oAuth2Model = Models\OAuth2::instance(); |
58
|
|
|
$appsMapper = $oAuth2Model->getAppsMapper(); |
59
|
|
|
|
60
|
|
|
// filter input vars of request, set back into REQUEST |
61
|
|
|
$appsMapper->copyfrom($f3->get('REQUEST')); |
62
|
|
|
$data = $appsMapper->filter(); |
63
|
|
|
$request = $f3->get('REQUEST'); |
64
|
|
|
foreach ($data as $k => $v) { |
65
|
|
|
if (array_key_exists($k, $request)) { |
66
|
|
|
$f3->set('REQUEST.' . $k, $v); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
// check app name exists |
71
|
|
|
$db = \Registry::get('db'); |
72
|
|
|
$m = clone $appsMapper; |
73
|
|
|
if ($m->load(['LOWER('.$db->quotekey('name').') = LOWER(?)', $m->name]) && null !== $m->client_id) { |
74
|
|
|
$this->notify(_('An app with that name is already in use!'), 'warning'); |
75
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
76
|
|
|
echo \View::instance()->render($view); |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// check required fields |
81
|
|
|
$appsMapper->validationRequired([ |
82
|
|
|
'name', |
83
|
|
|
'description', |
84
|
|
|
'callback_uri', |
85
|
|
|
]); |
86
|
|
|
|
87
|
|
|
// admin group auto-approved |
88
|
|
|
$scopes = $f3->get('userScopes'); |
89
|
|
|
|
90
|
|
|
// set defaults |
91
|
|
|
$appsMapper->client_id = $appsMapper->setUUID('client_id'); |
92
|
|
|
$appsMapper->client_secret = $appsMapper->setUUID('client_secret'); |
93
|
|
|
$appsMapper->users_uuid = $f3->get('uuid'); |
94
|
|
|
$appsMapper->scope = join(',', $scopes); |
95
|
|
|
$appsMapper->status = in_array('admin', $scopes) ? 'approved' : 'registered'; |
96
|
|
|
$appsMapper->created = Helpers\Time::database(); |
97
|
|
|
|
98
|
|
|
// at this point the app can be validated |
99
|
|
|
if (true !== $appsMapper->validate()) { |
100
|
|
|
$this->notify(['info' => $appsMapper->validationErrors($appsMapper->validate(false))]); |
101
|
|
|
$f3->set('form', $f3->get('REQUEST')); |
102
|
|
|
echo \View::instance()->render($view); |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
View Code Duplication |
if ($appsMapper->save()) { |
107
|
|
|
$this->notify(_('Your new app has been registered!'), 'success'); |
108
|
|
|
} else { |
109
|
|
|
$this->notify(_('App registration failed!'), 'error'); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$f3->reroute('@api_apps'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* register app |
118
|
|
|
* |
119
|
|
|
* @param \Base $f3 |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
public function updateAppPost(\Base $f3) |
123
|
|
|
{ |
124
|
|
|
$this->csrf('@api_apps'); |
125
|
|
|
$this->redirectLoggedOutUser(); |
126
|
|
|
|
127
|
|
|
$oAuth2Model = Models\OAuth2::instance(); |
128
|
|
|
$appsMapper = $oAuth2Model->getAppsMapper(); |
129
|
|
|
|
130
|
|
|
// filter input vars of request, set back into REQUEST |
131
|
|
|
$appsMapper->copyfrom($f3->get('REQUEST')); |
132
|
|
|
$data = $appsMapper->filter(); |
133
|
|
|
$request = $f3->get('REQUEST'); |
134
|
|
|
foreach ($data as $k => $v) { |
135
|
|
|
if (array_key_exists($k, $request)) { |
136
|
|
|
$f3->set('REQUEST.' . $k, $v); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// check app name exists |
141
|
|
View Code Duplication |
if (!$appsMapper->load(['LOWER(client_id) = LOWER(?) AND users_uuid = ?', |
142
|
|
|
$request['client_id'], $f3->get('uuid')])) { |
143
|
|
|
$this->notify(_('The app does not exist!'), 'warning'); |
144
|
|
|
$f3->reroute('@api_apps'); |
145
|
|
|
return; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
// check required fields |
149
|
|
|
$appsMapper->copyfrom($f3->get('REQUEST')); |
150
|
|
|
$appsMapper->validationRequired([ |
151
|
|
|
'name', |
152
|
|
|
'description', |
153
|
|
|
'callback_uri', |
154
|
|
|
]); |
155
|
|
|
|
156
|
|
|
// at this point the app can be validated |
157
|
|
View Code Duplication |
if (true !== $appsMapper->validate()) { |
158
|
|
|
$this->notify(['info' => $appsMapper->validationErrors($appsMapper->validate(false))]); |
159
|
|
|
$f3->reroute('@api_apps'); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
View Code Duplication |
if ($appsMapper->save()) { |
163
|
|
|
$this->notify(_('Your app has been updated!'), 'success'); |
164
|
|
|
} else { |
165
|
|
|
$this->notify(_('App update failed!'), 'error'); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$f3->reroute('@api_apps'); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|