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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Project_Unite.Models;
using Microsoft.AspNet.Identity.Owin;
using System.Web.Mvc;
using System.Diagnostics;
using System.Web.Mvc.Html;
using System.Data.Entity;
using System.Text;
namespace Project_Unite
{
public static class ACL
{
public static IHtmlString NotificationCount(this HtmlHelper hpr, string uid)
{
var db = new ApplicationDbContext();
var usr = db.Users.FirstOrDefault(x => x.Id == uid||x.UserName==uid);
if (usr == null)
return hpr.Raw("N/A");
return hpr.Raw(usr.UnreadNotifications.ToString());
}
public static IHtmlString NewestUser(this HtmlHelper hpr)
{
var db = new ApplicationDbContext();
var user = db.Users.OrderByDescending(x => x.JoinedAt).FirstOrDefault();
if (user == null)
return hpr.Raw(@"<a href=""#"">No new users</a>");
else
return hpr.Raw("<a href=\"/Profiles/ViewProfile/" + user.DisplayName + "\"><span class=\"glyphicon glyphicon-star\"></span> Our newest user, <strong>" + user.DisplayName + "</strong></a>");
}
public static IHtmlString GetLatestUnread(this HtmlHelper hpr, string userName)
{
var db = new ApplicationDbContext();
var user = db.Users.FirstOrDefault(x => x.UserName == userName);
if (user == null)
return hpr.Raw("");
var notesOrdered = user.Notifications.Where(x => x.IsRead == false).OrderByDescending(x => x.Timestamp).ToArray();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < notesOrdered.Length && i < 5; i++)
{
builder.AppendLine("<li><a href=\"" + notesOrdered[i].ActionUrl + "\">");
//Avatar holder start:
builder.AppendLine("<div style=\"width:64px;height:64px;display:inline-block;\">");
//Avatar
builder.AppendLine("<img src=\"" + notesOrdered[i].AvatarUrl + "\" width=\"64\" height=\"64\"/>");
//Avatar holder end:
builder.AppendLine("</div>");
//Notification title.
builder.AppendLine("<p><strong>" + notesOrdered[i].Title + "</strong><br/><br/>");
//Contents.
builder.AppendLine(notesOrdered[i].Description + "</p>");
builder.AppendLine("</a></li>");
}
return hpr.Raw(builder.ToString());
}
public static IHtmlString Markdown(this HtmlHelper hpr, string md)
{
return hpr.Raw(CommonMark.CommonMarkConverter.Convert(hpr.Encode(md)));
}
public static bool IsFollowed(string you, string fId)
{
var db = new ApplicationDbContext();
var uid = db.Users.FirstOrDefault(x => x.UserName == you).Id;
return db.Follows.FirstOrDefault(x => x.Follower == uid && x.Followed == fId) != null;
}
public static IHtmlString UserLink(this HtmlHelper hpr, string userId)
{
using(var db = new ApplicationDbContext())
{
var usr = db.Users.Include(x=>x.Roles).FirstOrDefault(x => x.Id == userId);
var userRoles = new List<Role>();
foreach (var usrRole in usr.Roles)
{
userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role);
}
var userRole = userRoles.OrderByDescending(m => m.Priority).FirstOrDefault();
return hpr.ActionLink(usr.DisplayName, "ViewProfile", "Profiles", new { id = usr.DisplayName }, new { style = userRole == null ? "color:white;" : @"color: " + userRole.ColorHex });
}
}
public static IHtmlString UserName(this HtmlHelper hpr, string userId)
{
using (var db = new ApplicationDbContext())
{
var usr = db.Users.Include(x => x.Roles).FirstOrDefault(x => x.Id == userId);
var userRoles = new List<Role>();
foreach (var usrRole in usr.Roles)
{
userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role);
}
var userRole = userRoles.OrderByDescending(m => m.Priority).FirstOrDefault();
if (userRole == null)
{
return hpr.Raw($@"<strong>{hpr.Encode(usr.DisplayName)}</strong>");
}
else
{
return hpr.Raw($@"<strong style=""color:{userRole.ColorHex}"">{hpr.Encode(usr.DisplayName)}</strong>");
}
}
}
public static string UserNameRaw(string userId)
{
using (var db = new ApplicationDbContext())
{
var usr = db.Users.Include(x => x.Roles).FirstOrDefault(x => x.Id == userId);
return usr.DisplayName;
}
}
public static string UserNameFromEmailRaw(string userId)
{
using (var db = new ApplicationDbContext())
{
var usr = db.Users.Include(x => x.Roles).FirstOrDefault(x => x.UserName == userId);
return usr.DisplayName;
}
}
public static bool CanSee(string userName, string fId)
{
if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId))
return false;
if (!Granted(userName, "CanPostTopics"))
return false; //obviously if this role has a global restraint for this ACL def we shouldn't let them post in ANY forum.
var db = new ApplicationDbContext();
var usr = db.Users.Include(x => x.Roles).FirstOrDefault(u => u.UserName == userName);
var userRoles = new List<Role>();
foreach (var usrRole in usr.Roles)
{
userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role);
}
db.Dispose();
var userRole = userRoles.OrderByDescending(m => m.Priority).First();
db = new ApplicationDbContext();
var forums = db.ForumCategories;
var forum = forums.First(x => x.Id == fId);
var perms = forum.Permissions.FirstOrDefault(x => x.RoleId == userRole.Id);
if (perms == null)
{
UpdateACLDefinitions(fId);
return true;
}
return (int)perms.Permissions >= (int)PermissionPreset.CanRead;
}
public static bool UserEmailConfirmed(string username)
{
if (string.IsNullOrWhiteSpace(username))
return true;
return new ApplicationDbContext().Users.FirstOrDefault(x => x.UserName == username).EmailConfirmed;
}
public static Role LowestPriorityRole()
{
var db = new ApplicationDbContext();
var roles = db.Roles;
List<Role> actualRoles = new List<Role>();
foreach (Role r in roles)
{
actualRoles.Add(r);
}
return actualRoles.OrderBy(x => x.Priority).First();
}
public static bool CanReply(string userName, string fId)
{
if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId))
return false;
if (!Granted(userName, "CanPostTopics"))
return false; //obviously if this role has a global restraint for this ACL def we shouldn't let them post in ANY forum.
var db = new ApplicationDbContext();
var usr = db.Users.Include(x => x.Roles).FirstOrDefault(u => u.UserName == userName);
var userRoles = new List<Role>();
foreach (var usrRole in usr.Roles)
{
userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role);
}
db.Dispose();
var userRole = userRoles.OrderByDescending(m => m.Priority).First();
db = new ApplicationDbContext();
var forums = db.ForumCategories;
var forum = forums.First(x => x.Id == fId);
var perms = forum.Permissions.FirstOrDefault(x => x.RoleId == userRole.Id);
if (perms == null)
{
UpdateACLDefinitions(fId);
return true;
}
return perms.Permissions >= PermissionPreset.CanReply;
}
public static ApplicationUser GetUserInfo(string id)
{
return new ApplicationDbContext().Users.FirstOrDefault(x => x.Id == id);
}
public static bool CanPost(string userName, string fId)
{
if (string.IsNullOrWhiteSpace(userName) || string.IsNullOrWhiteSpace(fId))
return false;
if (!Granted(userName, "CanPostTopics"))
return false; //obviously if this role has a global restraint for this ACL def we shouldn't let them post in ANY forum.
var db = new ApplicationDbContext();
var usr = db.Users.Include(x => x.Roles).FirstOrDefault(u => u.UserName == userName);
var userRoles = new List<Role>();
foreach (var usrRole in usr.Roles)
{
userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role);
}
db.Dispose();
var userRole = userRoles.OrderByDescending(m => m.Priority).First();
db = new ApplicationDbContext();
var forums = db.ForumCategories;
var forum = forums.First(x => x.Id == fId);
var perms = forum.Permissions.FirstOrDefault(x=>x.RoleId==userRole.Id);
if (perms == null)
{
UpdateACLDefinitions(fId);
return true;
}
return perms.Permissions >= PermissionPreset.CanPost;
}
public static void UpdateACLDefinitions(string fid)
{
var db = new ApplicationDbContext();
var forum = db.ForumCategories.FirstOrDefault(x => x.Id == fid);
if (forum == null)
return;
int recordsAdded = 0;
if (forum.Permissions.Length < db.Roles.Count())
{
var roles = db.Roles.ToArray();
foreach(var role in roles)
{
if (db.ForumPermissions.FirstOrDefault(x => x.CategoryId == fid && x.RoleId == role.Id) == null)
{
var perm = new ForumPermission();
perm.Id = Guid.NewGuid().ToString();
perm.CategoryId = forum.Id;
perm.RoleId = role.Id;
perm.Permissions = PermissionPreset.CanPost;
db.ForumPermissions.Add(perm);
recordsAdded++;
}
}
db.AuditLogs.Add(new AuditLog("system", AuditLogLevel.Admin, $"Automatic forum ACL update occurred - Forum: {forum.Name}, records added: {recordsAdded}."));
db.SaveChanges();
}
}
public static bool CanManageRole(string userId, string roleId)
{
try
{
if (!Granted(userId, "CanEditRoles"))
return false;
var db = new ApplicationDbContext();
var usr = db.Users.FirstOrDefault(u => u.UserName == userId);
var userRoles = new List<Role>();
foreach (var usrRole in usr.Roles)
{
userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role);
}
var manageRole = (Role)db.Roles.FirstOrDefault(x => x.Id == roleId);
db.Dispose();
var userRole = (Role)userRoles.OrderByDescending(m => m.Priority).First();
if (manageRole.Priority > userRole.Priority)
return false;
return true;
}
catch
{
return false;
}
}
public static ForumCategory GetForumById(string id)
{
var db = new ApplicationDbContext();
return db.ForumCategories.FirstOrDefault(x => x.Id == id);
}
public static bool Granted(string userName, string prop)
{
if (string.IsNullOrWhiteSpace(prop))
return true;
try
{
var db = new ApplicationDbContext();
var usr = db.Users.FirstOrDefault(u => u.UserName == userName);
var userRoles = new List<Role>();
foreach (var usrRole in usr.Roles)
{
userRoles.Add(db.Roles.FirstOrDefault(r => r.Id == usrRole.RoleId) as Role);
}
db.Dispose();
var userRole = userRoles.OrderByDescending(m => m.Priority).First();
var t = userRole.GetType();
foreach (var propInf in t.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
if (propInf.Name == prop && propInf.PropertyType == typeof(bool))
return (bool)propInf.GetValue(userRole);
}
return false;
}
catch (Exception ex)
{
Debug.Print(ex.ToString());
return false;
}
}
}
}
|