|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xtools; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* A User is a wiki user who has the same username across all projects in an Xtools installation. |
|
7
|
|
|
*/ |
|
8
|
|
|
class User extends Model |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
/** @var int */ |
|
12
|
|
|
protected $id; |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $username; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create a new User given a username. |
|
19
|
|
|
* @param string $username |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($username) |
|
22
|
|
|
{ |
|
23
|
|
|
$this->username = ucfirst($username); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Get the username. |
|
28
|
|
|
* @return string |
|
29
|
|
|
*/ |
|
30
|
|
|
public function getUsername() |
|
31
|
|
|
{ |
|
32
|
|
|
return $this->username; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Get the user's ID on the given project. |
|
37
|
|
|
* @param Project $project |
|
38
|
|
|
* @return int |
|
39
|
|
|
*/ |
|
40
|
|
|
public function getId(Project $project) |
|
41
|
|
|
{ |
|
42
|
|
|
return $this->getRepository()->getId($project->getDatabaseName(), $this->getUsername()); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Get a list of this user's groups on the given project. |
|
47
|
|
|
* @return string[] |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getGroups(Project $project) |
|
50
|
|
|
{ |
|
51
|
|
|
$groupsData = $this->getRepository()->getGroups($project, $this->getUsername()); |
|
|
|
|
|
|
52
|
|
|
$groups = preg_grep('/\*/', $groupsData, PREG_GREP_INVERT); |
|
53
|
|
|
sort($groups); |
|
54
|
|
|
return $groups; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Get a list of this user's groups on all projects. |
|
59
|
|
|
* @param Project $project A project to query; if not provided, the default will be used. |
|
60
|
|
|
*/ |
|
61
|
|
|
public function getGlobalGroups(Project $project = null) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->getRepository()->getGlobalGroups($this->getUsername(), $project); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the full URL to Special:UserRights for this user on the given project. |
|
68
|
|
|
* @param Project $project |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
|
|
public function userRightsUrl(Project $project) |
|
72
|
|
|
{ |
|
73
|
|
|
return $project->getUrl() . $project->getScriptPath() . "?title=Special:UserRights&user=" . |
|
74
|
|
|
$this->getUsername(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Does this user exist on the given project. |
|
79
|
|
|
* @param Project $project |
|
80
|
|
|
* @return bool |
|
81
|
|
|
*/ |
|
82
|
|
|
public function existsOnProject(Project $project) |
|
83
|
|
|
{ |
|
84
|
|
|
$id = $this->getId($project); |
|
85
|
|
|
return $id > 0; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Is this user an Administrator on the given project? |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function isAdmin(Project $project) |
|
93
|
|
|
{ |
|
94
|
|
|
return (false !== array_search('sysop', $this->getGroups($project))); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: