-
Notifications
You must be signed in to change notification settings - Fork 0
/
TuleapLogin.php
258 lines (234 loc) · 6.87 KB
/
TuleapLogin.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
<?php
namespace TuleapIntegration\Special;
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserGroupManager;
use MediaWiki\User\UserOptionsManager;
use Title;
use TitleFactory as TitleFactory;
use TuleapIntegration\TuleapConnection;
use TuleapIntegration\TuleapResourceOwner;
use TuleapIntegration\UserMappingProvider;
use UnexpectedValueException;
use User;
class TuleapLogin extends \SpecialPage {
/**
* @var string[]
*/
private $groupMapping = [
'is_writer' => 'editor',
'is_admin' => 'sysop',
'is_bot' => 'bot'
];
/** @var TuleapConnection */
private $tuleap;
/** @var TitleFactory */
private $titleFactory;
/** @var UserFactory */
private $userFactory;
/** @var UserOptionsManager */
private $userOptionsManager;
/** @var UserGroupManager */
private $groupManager;
/** @var UserMappingProvider */
private $userMappingProvider;
/** @var array */
private $permissionConfig = [];
/**
* @param TuleapConnection $tuleap
* @param TitleFactory $titleFactory
* @param UserFactory $userFactory
* @param UserOptionsManager $userOptionsManager
* @param UserGroupManager $groupManager
* @param UserMappingProvider $userMappingProvider
*/
public function __construct(
TuleapConnection $tuleap, TitleFactory $titleFactory, UserFactory $userFactory,
UserOptionsManager $userOptionsManager, UserGroupManager $groupManager,
UserMappingProvider $userMappingProvider
) {
parent::__construct( 'TuleapLogin', '', false );
$this->tuleap = $tuleap;
$this->titleFactory = $titleFactory;
$this->userFactory = $userFactory;
$this->userOptionsManager = $userOptionsManager;
$this->groupManager = $groupManager;
$this->userMappingProvider = $userMappingProvider;
}
/**
* @inheritDoc
*/
public function execute( $subPage ) {
$this->setHeaders();
if ( $this->getUser()->isRegistered() ) {
$this->redirectToReturnTo();
return true;
}
if ( $subPage === 'callback' ) {
return $this->callback();
}
$url = $this->tuleap->getAuthorizationUrl(
$this->getRequest()->getVal( 'returnto' ),
$this->getRequest()->getBool( 'prompt' )
);
$this->getOutput()->redirect( $url );
return true;
}
/**
* Retrieve access token and log user in
*
* @return bool
* @throws \MWException
*/
public function callback() {
$loginRequired = $this->getRequest()->getText( 'error' ) === 'login_required';
if ( $loginRequired ) {
if ( $this->canAnonsRead() ) {
// If anons can read, allow them
$this->getRequest()->getSession()->set( 'tuleap-anon-auth-done', true );
$this->redirectToMainPage();
return true;
} elseif ( !$this->askedForLogin() ) {
// Otherwise, if not already, ask user to login
$this->askForLogin();
return true;
}
}
// User logged, in set the user
try {
$this->tuleap->obtainAccessToken( $this->getRequest() );
$resourceOwner = $this->tuleap->getResourceOwner();
$this->setUser( $resourceOwner );
$this->redirectToReturnTo();
} catch ( IdentityProviderException | UnexpectedValueException | \Exception $e ) {
$isDebug = $this->getRequest()->getBool( 'debug' );
$message = $isDebug ? new \RawMessage( $e->getMessage() ) : 'tuleap-login-error-desc';
$this->getOutput()->showErrorPage( 'tuleap-login-error', $message );
return true;
}
return true;
}
/**
* Set session
*
* @param TuleapResourceOwner $owner
* @return bool|User
* @throws \MWException
*/
private function setUser( TuleapResourceOwner $owner ) {
$legacyUser = $this->userMappingProvider->provideUserForId( $owner->getId() );
if ( !( $legacyUser instanceof User ) ) {
$user = $this->userFactory->newFromName( $owner->getId() );
}
$user->setRealName( $owner->getRealName() );
$user->setEmail( $owner->getEmail() );
$user->load();
if ( !$user->isRegistered() ) {
$user->addToDatabase();
}
if ( $owner->isEmailVerified() ) {
$user->confirmEmail();
}
if ( $owner->getLocale() ) {
$this->userOptionsManager->setOption( $user, 'language', $owner->getLocale() );
$this->userOptionsManager->saveOptions( $user );
}
$this->setUserGroups( $user );
$user->setToken();
$this->getRequest()->getSession()->persist();
$user->setCookies();
$this->getContext()->setUser( $user );
$user->saveSettings();
$GLOBALS['wgUser'] = $user;
$sessionUser = User::newFromSession( $this->getRequest() );
$sessionUser->load();
// Retrieve required data and store to session
$this->tuleap->getIntegrationData(
$this->getConfig()->get( 'TuleapProjectId' ), null, null, true
);
return $user;
}
/**
* Assign users to appropriate groups
*
* @param User $user
* @throws IdentityProviderException
*/
private function setUserGroups( User $user ) {
// Load permissions again, as now user context is set
$this->loadPermissions();
if ( !$this->permissionConfig ) {
return;
}
foreach ( $this->permissionConfig as $key => $value ) {
if ( !isset( $this->groupMapping[$key] ) ) {
continue;
}
$group = $this->groupMapping[$key];
if ( $value ) {
$this->groupManager->addUserToGroup( $user, $group );
} else {
$this->groupManager->removeUserFromGroup( $user, $group );
}
}
}
/**
* Load permission config
*
* @return void
* @throws IdentityProviderException
*/
private function loadPermissions() {
$this->permissionConfig = [];
$projectId = $this->getConfig()->get( 'TuleapProjectId' );
$data = $this->tuleap->getPermissionConfig( $projectId );
if ( isset( $data['permissions'] ) ) {
$this->permissionConfig = $data['permissions'];
}
$this->getRequest()->getSession()->set(
'tuleap-permissions', $this->permissionConfig
);
}
/**
* After login, return to whatever user wanted to see
*/
private function redirectToReturnTo() {
if ( $this->getRequest()->getSession()->exists( 'returnto' ) ) {
$title = $this->titleFactory->newFromText(
$this->getRequest()->getSession()->get( 'returnto' )
);
if ( !( $title instanceof Title ) ) {
$this->redirectToMainPage();
return;
}
$this->getRequest()->getSession()->remove( 'returnto' );
$this->getRequest()->getSession()->save();
$this->getOutput()->redirect( $title->getFullURL() );
}
}
private function redirectToMainPage() {
$this->getOutput()->redirect( $this->titleFactory->newMainPage()->getFullURL() );
}
/**
* @return bool
*/
private function canAnonsRead(): bool {
$this->loadPermissions();
if ( !$this->permissionConfig ) {
return false;
}
return $this->permissionConfig['is_reader'];
}
private function askForLogin() {
$this->getRequest()->getSession()->set( 'tuleap-ask-for-login', true );
header( 'Location:' . $this->getPageTitle()->getLocalURL( [ 'prompt' => 1 ] ) );
exit;
}
/**
* Check if we already asked for login
* @return bool
*/
private function askedForLogin(): bool {
return (bool)$this->getRequest()->getSession()->get( 'tuleap-ask-for-login' );
}
}