|
1
|
|
|
<?php |
|
2
|
|
|
/****************************************************************************** |
|
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
|
4
|
|
|
* * |
|
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
|
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
|
7
|
|
|
******************************************************************************/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Waca\Security\CredentialProviders; |
|
10
|
|
|
|
|
11
|
|
|
use Waca\DataObjects\Credential; |
|
12
|
|
|
use Waca\DataObjects\User; |
|
13
|
|
|
use Waca\PdoDatabase; |
|
14
|
|
|
use Waca\SiteConfiguration; |
|
15
|
|
|
|
|
16
|
|
|
abstract class CredentialProviderBase implements ICredentialProvider |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var PdoDatabase |
|
20
|
|
|
*/ |
|
21
|
|
|
private $database; |
|
22
|
|
|
/** |
|
23
|
|
|
* @var SiteConfiguration |
|
24
|
|
|
*/ |
|
25
|
|
|
private $configuration; |
|
26
|
|
|
/** @var string */ |
|
27
|
|
|
private $type; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* CredentialProviderBase constructor. |
|
31
|
|
|
* |
|
32
|
|
|
* @param PdoDatabase $database |
|
33
|
|
|
* @param SiteConfiguration $configuration |
|
34
|
|
|
* @param string $type |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(PdoDatabase $database, SiteConfiguration $configuration, $type) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->database = $database; |
|
39
|
|
|
$this->configuration = $configuration; |
|
40
|
|
|
$this->type = $type; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param int $userId |
|
45
|
|
|
* |
|
46
|
|
|
* @param bool $disabled |
|
47
|
|
|
* |
|
48
|
|
|
* @return Credential |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getCredentialData($userId, $disabled = false) |
|
51
|
|
|
{ |
|
52
|
|
|
$sql = 'SELECT * FROM credential WHERE type = :t AND user = :u'; |
|
53
|
|
|
$parameters = array( |
|
54
|
|
|
':u' => $userId, |
|
55
|
|
|
':t' => $this->type |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
if ($disabled !== null) { |
|
|
|
|
|
|
59
|
|
|
$sql .= ' AND disabled = :d'; |
|
60
|
|
|
$parameters[':d'] = $disabled ? 1 : 0; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$statement = $this->database->prepare($sql); |
|
64
|
|
|
$statement->execute($parameters); |
|
65
|
|
|
|
|
66
|
|
|
/** @var Credential $obj */ |
|
67
|
|
|
$obj = $statement->fetchObject(Credential::class); |
|
68
|
|
|
|
|
69
|
|
|
if ($obj === false) { |
|
|
|
|
|
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$obj->setDatabase($this->database); |
|
74
|
|
|
|
|
75
|
|
|
$statement->closeCursor(); |
|
76
|
|
|
|
|
77
|
|
|
return $obj; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @return PdoDatabase |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getDatabase() |
|
84
|
|
|
{ |
|
85
|
|
|
return $this->database; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return SiteConfiguration |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getConfiguration() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->configuration; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function deleteCredential(User $user) |
|
97
|
|
|
{ |
|
98
|
|
|
// get this factor |
|
99
|
|
|
$statement = $this->database->prepare('SELECT * FROM credential WHERE user = :user AND type = :type'); |
|
100
|
|
|
$statement->execute(array(':user' => $user->getId(), ':type' => $this->type)); |
|
101
|
|
|
/** @var Credential $credential */ |
|
102
|
|
|
$credential = $statement->fetchObject(Credential::class); |
|
103
|
|
|
$credential->setDatabase($this->database); |
|
104
|
|
|
$statement->closeCursor(); |
|
105
|
|
|
|
|
106
|
|
|
$stage = $credential->getFactor(); |
|
107
|
|
|
|
|
108
|
|
|
$statement = $this->database->prepare('SELECT COUNT(*) FROM credential WHERE user = :user AND factor = :factor'); |
|
109
|
|
|
$statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
110
|
|
|
$alternates = $statement->fetchColumn(); |
|
111
|
|
|
$statement->closeCursor(); |
|
112
|
|
|
|
|
113
|
|
|
if ($alternates <= 1) { |
|
114
|
|
|
// decrement the factor for every stage above this |
|
115
|
|
|
$sql = 'UPDATE credential SET factor = factor - 1 WHERE user = :user AND factor > :factor'; |
|
116
|
|
|
$statement = $this->database->prepare($sql); |
|
117
|
|
|
$statement->execute(array(':user' => $user->getId(), ':factor' => $stage)); |
|
118
|
|
|
} |
|
119
|
|
|
else { |
|
120
|
|
|
// There are other auth factors at this point. Don't renumber the factors just yet. |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
// delete this credential. |
|
124
|
|
|
$credential->delete(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param User $user |
|
129
|
|
|
* |
|
130
|
|
|
* @return Credential |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function createNewCredential(User $user) |
|
133
|
|
|
{ |
|
134
|
|
|
$credential = new Credential(); |
|
135
|
|
|
$credential->setDatabase($this->getDatabase()); |
|
136
|
|
|
$credential->setUserId($user->getId()); |
|
137
|
|
|
$credential->setType($this->type); |
|
138
|
|
|
|
|
139
|
|
|
return $credential; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @param int $userId |
|
144
|
|
|
* |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
|
|
public function userIsEnrolled($userId) |
|
148
|
|
|
{ |
|
149
|
|
|
$cred = $this->getCredentialData($userId); |
|
150
|
|
|
|
|
151
|
|
|
return $cred !== null; |
|
152
|
|
|
} |
|
153
|
|
|
} |