-
Notifications
You must be signed in to change notification settings - Fork 20
/
UserInfo.cs
217 lines (189 loc) · 6.62 KB
/
UserInfo.cs
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
/*
Copyright (C) 2009 Max Semenik
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System.Collections.Generic;
using System.Xml;
namespace WikiFunctions.API
{
/// <summary>
/// Information about a user
/// </summary>
public sealed class UserInfo
{
/// <summary>
/// Username
/// </summary>
public string Name
{ get; private set; }
/// <summary>
/// Database ID
/// </summary>
public int Id
{ get; private set; }
/// <summary>
/// Whether we are logged in
/// </summary>
public bool IsLoggedIn
{ get { return Id != 0; } }
/// <summary>
/// Whether the current user is an administrator
/// </summary>
public bool IsSysop
{ get { return IsInGroup("sysop"); } }
/// <summary>
/// Whether the current user is a flagged bot
/// </summary>
public bool IsBot
{ get { return IsInGroup("bot") || HasRight("bot"); } }
/// <summary>
/// Whether the user has the apihighlimits right
/// </summary>
public bool HasApiHighLimit
{ get { return HasRight("apihighlimits"); } }
/// <summary>
/// Whether the current user is blocked from editing
/// </summary>
public bool IsBlocked
{ get; private set; }
/// <summary>
/// Whether the user has an unread user talk message
/// </summary>
public bool HasMessages
{ get; internal set; }
/// <summary>
/// The number of unread notifications for the user
/// </summary>
public int Notifications
{ get; internal set; }
/// <summary>
///
/// </summary>
/// <param name="group"></param>
/// <returns></returns>
public bool IsInGroup(string group)
{
return string.IsNullOrEmpty(group) || Groups.Contains(group);
}
/// <summary>
///
/// </summary>
/// <param name="right"></param>
/// <returns></returns>
public bool HasRight(string right)
{
return string.IsNullOrEmpty(right) || Rights.Contains(right);
}
/// <summary>
///
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public bool CanEditPage(PageInfo page)
{
return (IsInGroup(page.EditProtection) || HasRight(page.EditProtection))
&& !(page.NamespaceID == Namespace.MediaWiki && !HasRight("editinterface"));
}
/// <summary>
/// Returns whether the user can delete the page based on group/edit rights
/// Examples: sysop, eliminator groups should be able to delete pages
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public bool CanDeletePage(PageInfo page)
{
return (HasRight("delete"));
}
/// <summary>
/// Returns whether the user can create the page based on group/edit rights
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public bool CanCreatePage(PageInfo page)
{
return (IsInGroup(page.CreateProtection) || HasRight(page.CreateProtection));
}
/// <summary>
///
/// </summary>
/// <param name="page"></param>
/// <returns></returns>
public bool CanMovePage(PageInfo page)
{
return page.NamespaceID != Namespace.MediaWiki
&& (IsInGroup(page.MoveProtection) || HasRight(page.MoveProtection));
}
/// <summary>
/// Creates a UserInfo class from an meta=userinfo XML
/// </summary>
/// <param name="xml">XML document to process. Must be already checked for error status by
/// ApiEdit.CheckForErrors()</param>
internal UserInfo(XmlDocument xml)
{
var users = xml.GetElementsByTagName("userinfo");
if (users.Count == 0) throw new BrokenXmlException(null, "XML with <userinfo> element expected");
var user = users[0];
Name = user.Attributes["name"].Value;
Id = int.Parse(user.Attributes["id"].Value);
var groups = user["groups"];
if (groups != null)
{
foreach (XmlNode g in groups.GetElementsByTagName("g"))
{
Groups.Add(g.InnerText);
}
}
var rights = user["rights"];
if (rights != null)
{
foreach (XmlNode r in rights.GetElementsByTagName("r"))
{
Rights.Add(r.InnerText);
}
}
Update(xml);
}
/// <summary>
/// Updates the information about the current user
/// </summary>
/// <param name="xml">XML to process</param>
internal void Update(XmlDocument xml)
{
var users = xml.GetElementsByTagName("userinfo");
if (users.Count > 0)
{
HasMessages = users[0].Attributes["messages"] != null;
IsBlocked = users[0].Attributes["blockedby"] != null;
}
var notifications = xml.GetElementsByTagName("notifications");
if (notifications.Count > 0 &&
notifications[0].Attributes != null &&
notifications[0].Attributes["rawcount"] != null)
{
Notifications = int.Parse(notifications[0].Attributes["rawcount"].Value);
}
else
{
Notifications = 0;
}
}
/// <summary>
/// Creates a UserInfo object for an unregistered user
/// </summary>
internal UserInfo()
{
}
private readonly List<string> Groups = new List<string>();
private readonly List<string> Rights = new List<string>();
}
}