|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file contains only the User class. |
|
4
|
|
|
*/ |
|
5
|
|
|
|
|
6
|
|
|
namespace Xtools; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* A User is a wiki user who has the same username across all projects in an XTools installation. |
|
10
|
|
|
*/ |
|
11
|
|
|
class User extends Model |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** @var int The user's ID. */ |
|
15
|
|
|
protected $id; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string The user's username. */ |
|
18
|
|
|
protected $username; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Create a new User given a username. |
|
22
|
|
|
* @param string $username |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __construct($username) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->username = ucfirst(trim($username)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Get the username. |
|
31
|
|
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getUsername() |
|
34
|
|
|
{ |
|
35
|
|
|
return $this->username; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Get the user's ID on the given project. |
|
40
|
|
|
* @param Project $project |
|
41
|
|
|
* @return int |
|
42
|
|
|
*/ |
|
43
|
|
|
public function getId(Project $project) |
|
44
|
|
|
{ |
|
45
|
|
|
return $this->getRepository()->getId($project->getDatabaseName(), $this->getUsername()); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Get a list of this user's groups on the given project. |
|
50
|
|
|
* @param Project $project The project. |
|
51
|
|
|
* @return string[] |
|
52
|
|
|
*/ |
|
53
|
|
|
public function getGroups(Project $project) |
|
54
|
|
|
{ |
|
55
|
|
|
$groupsData = $this->getRepository()->getGroups($project, $this->getUsername()); |
|
|
|
|
|
|
56
|
|
|
$groups = preg_grep('/\*/', $groupsData, PREG_GREP_INVERT); |
|
57
|
|
|
sort($groups); |
|
58
|
|
|
return $groups; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Get a list of this user's groups on all projects. |
|
63
|
|
|
* @param Project $project A project to query; if not provided, the default will be used. |
|
64
|
|
|
*/ |
|
65
|
|
|
public function getGlobalGroups(Project $project = null) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->getRepository()->getGlobalGroups($this->getUsername(), $project); |
|
|
|
|
|
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Does this user exist on the given project. |
|
72
|
|
|
* @param Project $project |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
|
|
public function existsOnProject(Project $project) |
|
76
|
|
|
{ |
|
77
|
|
|
$id = $this->getId($project); |
|
78
|
|
|
return $id > 0; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Is this user an Administrator on the given project? |
|
83
|
|
|
* @param Project $project The project. |
|
84
|
|
|
* @return bool |
|
85
|
|
|
*/ |
|
86
|
|
|
public function isAdmin(Project $project) |
|
87
|
|
|
{ |
|
88
|
|
|
return (false !== array_search('sysop', $this->getGroups($project))); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Is this user an anonymous user (IP)? |
|
93
|
|
|
* @return bool |
|
94
|
|
|
*/ |
|
95
|
|
|
public function isAnon() |
|
96
|
|
|
{ |
|
97
|
|
|
return filter_var($this->username, FILTER_VALIDATE_IP); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Is this user the same as the current XTools user? |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function isCurrentlyLoggedIn() |
|
105
|
|
|
{ |
|
106
|
|
|
$ident = $this->getRepository()->getXtoolsUserInfo(); |
|
|
|
|
|
|
107
|
|
|
return isset($ident->username) && $ident->username === $this->getUsername(); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
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: