-
Notifications
You must be signed in to change notification settings - Fork 3
/
API.php
218 lines (196 loc) · 6.21 KB
/
API.php
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
<?php
namespace Piwik\Plugins\Signup;
use Exception;
use Piwik\Auth;
use Piwik\Nonce;
use Piwik\Access;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugins\API\API as PluginApi;
use Piwik\Plugins\UsersManager\API as UsersManagerApi;
use Piwik\Plugins\SitesManager\API as SitesManagerApi;
/**
* API for plugin Signup
*
* @method static \Piwik\Plugins\Signup\API getInstance()
*/
class API extends PluginApi
{
/**
* Public API, returns non-sensitive settings about Signup system settings.
* Can be accessed by requesting:
*
* ?module=API&method=Signup.getSignupPublicSettings&format=json
*
* @return array Non-sensitive settings of Signup plugin.
*/
public function getSignupPublicSettings()
{
$settings = new SystemSettings();
return [
'signupPlugin' => true,
'allowNewUsersToSignup' => $settings->allowNewUsersToSignup->getValue(),
'allowedEmailDomains' => $settings->allowedEmailDomains->getValue(),
];
}
/**
* Endpoint to signup a new user.
*
* @param string $login
* @param string $password
* @param string $email
* @param string $nonce Generated from FormSignup.
*
* @return array In case of success, returns token_auth from created user, needed for signupSite().
*
* @throws Exception When signup are disabled in settings.
*/
public function signupUser($login, $password, $email, $nonce)
{
$this->mustUsersSignupAllowed();
$this->checkEmailDomainAllowed($email);
$form = new FormSignup();
if (!$form->validate()) {
return [
'result' => 'error',
'reason' => 'form_invalid',
'form_errors' => $form->getFormData()['errors'],
];
}
if (!Nonce::verifyNonce('Signup.signup', $nonce)) {
return [
'result' => 'error',
'reason' => 'nonce_invalid',
];
}
try {
Signup::$disablePasswordConfirmationOnce = true;
Access::getInstance()->doAsSuperUser(function () use ($login, $password, $email) {
UsersManagerApi::getInstance()->addUser($login, $password, $email);
});
} finally {
Signup::$disablePasswordConfirmationOnce = false;
}
return [
'result' => 'success',
'token_auth' => UsersManagerApi::getInstance()
->createAppSpecificTokenAuth($login, $password, 'SignupPlugin')
,
];
}
/**
* Create a site and give the access rights
* to the user behind $token_auth.
*
* @param string $token_auth
* @param string $siteName
*
* @return array In case of success: {"result":"success","message":"ok"}
*
* @throws Exception When signup are disabled in settings.
*/
public function signupSite(
$token_auth,
$siteName,
$urls = null,
$ecommerce = null,
$siteSearch = null,
$searchKeywordParameters = null,
$searchCategoryParameters = null,
$excludedIps = null,
$excludedQueryParameters = null,
$timezone = null,
$currency = null,
$group = null,
$startDate = null,
$excludedUserAgents = null,
$keepURLFragments = null,
$type = null,
$settingValues = null,
$excludeUnknownUrls = null
) {
$this->mustUsersSignupAllowed();
$auth = StaticContainer::get(Auth::class);
$auth->setTokenAuth($token_auth);
$authResult = $auth->authenticate();
return Access::getInstance()->doAsSuperUser(function () use (
$authResult,
$siteName,
$urls,
$ecommerce,
$siteSearch,
$searchKeywordParameters,
$searchCategoryParameters,
$excludedIps,
$excludedQueryParameters,
$timezone,
$currency,
$group,
$startDate,
$excludedUserAgents,
$keepURLFragments,
$type,
$settingValues,
$excludeUnknownUrls
) {
$idSite = SitesManagerApi::getInstance()->addSite(
$siteName,
$urls,
$ecommerce,
$siteSearch,
$searchKeywordParameters,
$searchCategoryParameters,
$excludedIps,
$excludedQueryParameters,
$timezone,
$currency,
$group,
$startDate,
$excludedUserAgents,
$keepURLFragments,
$type,
$settingValues,
$excludeUnknownUrls
);
return UsersManagerApi::getInstance()->setUserAccess($authResult->getIdentity(), 'admin', $idSite);
});
}
/**
* Check if superuser has allowed new users to signup in system settings.
*
* @throws Exception When signup are disabled in settings.
*/
private function mustUsersSignupAllowed()
{
$settings = new SystemSettings();
$allowedToSignup = $settings->allowNewUsersToSignup->getValue();
if (!$allowedToSignup) {
throw new Exception('Signup are disabled on this instance.');
}
}
/**
* Check if email domain is allowed.
*
* @throws Exception If domain is not allowed
*/
private function checkEmailDomainAllowed($email)
{
$settings = new SystemSettings();
$allowedEmailDomains = $settings->allowedEmailDomains->getValue();
if (0 === count($allowedEmailDomains)) {
return;
}
foreach ($allowedEmailDomains as $allowedEmailDomain) {
$regex = $allowedEmailDomain;
$regex = str_replace('.', '\\.', $regex);
$regex = str_replace('*', '.*', $regex);
if (preg_match("/@$regex$/i", $email)) {
return;
}
}
throw new Exception(Piwik::translate('Signup_DisallowedEmailDomain', [
array_pop(explode('@', $email)),
join(', ', $allowedEmailDomains),
]));
}
}