forked from wikimedia/mediawiki-extensions-InviteSignup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInviteSignupHooks.php
90 lines (78 loc) · 2.56 KB
/
InviteSignupHooks.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
<?php
class InviteSignupHooks {
public static function onBeforeInitialize( Title $title, &$unused, &$output, &$user,
WebRequest $request
) {
if ( !$title->isSpecialPage() ) {
return true;
}
list( $name ) = SpecialPageFactory::resolveAlias( $title->getDBkey() );
if ( ($name !== 'UserLogin') && ($name !== 'CreateAccount') ) {
return true;
}
$hash = $request->getVal( 'invite', $request->getCookie( 'invite' ) );
if ( $hash ) {
$store = new InviteStore( wfGetDB( DB_SLAVE ), 'invitesignup' );
$invite = $store->getInvite( $hash );
if ( $invite && $invite['used'] === null ) {
global $wgInviteSignupHash;
$wgInviteSignupHash = $hash;
$request->response()->setCookie( 'invite', $hash );
// Ensure user is allowed to register without entering email
global $wgEmailConfirmToEdit;
$wgEmailConfirmToEdit = false;
// Make sure user can access the CreateAccount page
// possibly this should append to the array instead of overwriting
global $wgWhitelistRead;
$wgWhitelistRead = array ("Special:CreateAccount");
}
}
}
public static function onUserGetRights( $user, &$rights ) {
global $wgInviteSignupHash;
if ( $wgInviteSignupHash === null ) {
return true;
}
$rights[] = 'createaccount';
}
public static function onUserCreateForm( &$template ) {
global $wgInviteSignupHash;
if ( $wgInviteSignupHash === null ) {
return true;
}
$template->data['link'] = null;
$template->data['useemail'] = false;
}
public static function onAddNewAccount( User $user ) {
global $wgInviteSignupHash;
if ( $wgInviteSignupHash === null ) {
return true;
}
$store = new InviteStore( wfGetDB( DB_MASTER ), 'invitesignup' );
$invite = $store->getInvite( $wgInviteSignupHash );
$user->setOption( 'is-inviter', $invite['inviter'] );
$user->setEmail( $invite['email'] );
$user->confirmEmail();
foreach ( $invite['groups'] as $group ) {
$user->addGroup( $group );
}
$user->saveSettings();
$store->addSignupDate( $user, $wgInviteSignupHash );
global $wgRequest;
$wgRequest->response()->setCookie( 'invite', '', time() - 86400 );
}
public static function onLoadExtensionSchemaUpdates( DatabaseUpdater $updater ) {
$dir = __DIR__ . '/sql';
$type = $updater->getDB()->getType();
switch ( $type ) {
case 'mysql':
$updater->addExtensionTable( 'invitesignup', "$dir/invitesignup.sql" );
break;
case 'postgres':
$updater->addExtensionTable( 'invitesignup', "$dir/invitesignup.pg.sql" );
break;
default:
throw new MWException( "InviteSignup does not support $type yet." );
}
}
}