This repository has been archived by the owner on Apr 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PermissionUserCommand.cs
243 lines (207 loc) · 9.17 KB
/
PermissionUserCommand.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
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
using Microsoft.Data.Entity;
using SOCVR.Chatbot.Configuration;
using SOCVR.Chatbot.Database;
using System;
using System.Linq;
using TCL.Extensions;
namespace SOCVR.Chatbot.ChatbotActions.Commands.Permission
{
internal abstract class PermissionUserCommand : UserCommand
{
protected PermissionGroup? MatchInputToPermissionGroup(string userInput)
{
//take the input, remove spaces, lower case it
userInput = userInput
.Replace(" ", "")
.ToLowerInvariant();
if (userInput.EndsWith("s"))
{
userInput = userInput.Remove(userInput.Length - 2);
}
var allPermissionGroups = Enum.GetValues(typeof(PermissionGroup))
.OfType<PermissionGroup>()
.Select(x => new
{
EnumVal = x,
MatchVal = x.ToString().ToLowerInvariant()
});
var matchingPermissionGroup = allPermissionGroups
.SingleOrDefault(x => x.MatchVal == userInput);
return matchingPermissionGroup?.EnumVal;
}
protected PermissionGroupJoinabilityStatus CanTargetUserJoinPermissionGroup(PermissionGroup permissionGroup, int targetUserId, ChatExchangeDotNet.Room chatRoom)
{
using (var db = new DatabaseContext())
{
//lookup the target user
var targetUser = db.Users
.Include(x => x.Permissions)
.Include(x => x.PermissionsRequested)
.SingleOrDefault(x => x.ProfileId == targetUserId);
//if the user has never said a message in chat, the user will not exist in the database
//add this user
if (targetUser == null)
{
targetUser = new User()
{
ProfileId = targetUserId
};
db.Users.Add(targetUser);
db.SaveChanges();
}
//does the target user already belong to the requested group?
if (permissionGroup.In(targetUser.Permissions.Select(x => x.PermissionGroup)))
{
return PermissionGroupJoinabilityStatus.AlreadyInGroup;
}
//now that we have checked the general rules, switch on the specific group
//and check any specific restrictions
switch (permissionGroup)
{
case PermissionGroup.Reviewer:
return CanUserJoinReviewersGroup(targetUser, chatRoom);
case PermissionGroup.BotOwner:
return CanUserJoinBotOwnersGroup(targetUser);
default:
throw new Exception("Unknow permission group, unable to determine specific joining restrictions.");
}
}
}
protected PermissionGroupLeavabilityStatus CanTargetUserLeavePermissionGroup(PermissionGroup permissionGroup, int targetUserId)
{
using (var db = new DatabaseContext())
{
var targetUser = db.Users
.Include(x => x.Permissions)
.Where(x => x.ProfileId == targetUserId)
.Single();
var userIsInGroup = targetUser.Permissions
.Where(x => x.PermissionGroup == permissionGroup)
.Any();
if (!userIsInGroup)
{
return PermissionGroupLeavabilityStatus.WasNotInGroup;
}
//passed all checks, user can leave group
return PermissionGroupLeavabilityStatus.CanLeaveGroup;
}
}
/// <summary>
/// Tells if a user is allowed to add or remove other users from the given permission group.
/// </summary>
/// <param name="permissionGroup"></param>
/// <param name="processingUserId"></param>
/// <returns></returns>
protected PermissionGroupModifiableStatus CanUserModifyMembershipForGroup(PermissionGroup permissionGroup, int processingUserId)
{
using (var db = new DatabaseContext())
{
var processingUser = db.Users
.Include(x => x.Permissions)
.Single(x => x.ProfileId == processingUserId);
//if you are a Bot Owner, you are always allowed to do it
if (PermissionGroup.BotOwner.In(processingUser.Permissions.Select(x => x.PermissionGroup)))
{
return PermissionGroupModifiableStatus.CanModifyGroupMembership;
}
//the only general restriction is that you are in the group you are trying to modify
if (!permissionGroup.In(processingUser.Permissions.Select(x => x.PermissionGroup)))
{
return PermissionGroupModifiableStatus.NotInGroup;
}
//now check rules specific to each group
switch (permissionGroup)
{
case PermissionGroup.Reviewer:
return CanUserModifyReviewerGroup(permissionGroup, processingUser);
case PermissionGroup.BotOwner:
return CanUserModifyBotOwnersGroup(permissionGroup, processingUser);
default:
throw new Exception("Unknow permission group, unable to determine specific modification restrictions.");
}
}
}
private PermissionGroupModifiableStatus CanUserModifyReviewerGroup(PermissionGroup permissionGroup, User processingUser)
{
//user must be in the group for at least X days
var joinedGroupAt = processingUser.Permissions
.Single(x => x.PermissionGroup == permissionGroup)
.JoinedOn;
var minDaysNeededInGroup = ConfigurationAccessor.DaysInReviewersGroupBeforeProcessingRequests;
var deltaDays = (DateTimeOffset.UtcNow - joinedGroupAt).TotalDays;
if (deltaDays < minDaysNeededInGroup)
{
return PermissionGroupModifiableStatus.Reviewer_NotInGroupLongEnough;
}
//clear to modify group
return PermissionGroupModifiableStatus.CanModifyGroupMembership;
}
private PermissionGroupModifiableStatus CanUserModifyBotOwnersGroup(PermissionGroup permissionGroup, User processingUser)
{
//there are no specific requirements for this group
return PermissionGroupModifiableStatus.CanModifyGroupMembership;
}
private PermissionGroupJoinabilityStatus CanUserJoinReviewersGroup(User targetUser, ChatExchangeDotNet.Room chatRoom)
{
//check user rep
var repRequirement = ConfigurationAccessor.RepRequirementToJoinReviewers;
var targetUserRep = chatRoom.GetUser(targetUser.ProfileId).Reputation;
if (targetUserRep < repRequirement)
{
return PermissionGroupJoinabilityStatus.Reviewer_NotEnoughRep;
}
return PermissionGroupJoinabilityStatus.CanJoinGroup;
}
private PermissionGroupJoinabilityStatus CanUserJoinBotOwnersGroup(User targetUser)
{
//if the target user is not in the Reviewer group, reject
if (!PermissionGroup.Reviewer.In(targetUser.Permissions.Select(x => x.PermissionGroup)))
{
return PermissionGroupJoinabilityStatus.BotOwner_NotInReviewerGroup;
}
return PermissionGroupJoinabilityStatus.CanJoinGroup;
}
protected enum PermissionGroupJoinabilityStatus
{
/// <summary>
/// User is free to join the group
/// </summary>
CanJoinGroup,
/// <summary>
/// The user is already group
/// </summary>
AlreadyInGroup,
/// <summary>
/// The user does not have enough rep to join the Reviewer group
/// </summary>
Reviewer_NotEnoughRep,
/// <summary>
/// The user can't join the Bot Owner group because they are not in the Reviewer group
/// </summary>
BotOwner_NotInReviewerGroup,
}
protected enum PermissionGroupLeavabilityStatus
{
/// <summary>
/// The user is free to leave the group
/// </summary>
CanLeaveGroup,
/// <summary>
/// The user is not in the group
/// </summary>
WasNotInGroup,
}
protected enum PermissionGroupModifiableStatus
{
/// <summary>
/// User is free to modify the membership of the group
/// </summary>
CanModifyGroupMembership,
/// <summary>
/// User cannot modify membership of group because they are not in the group,
/// </summary>
NotInGroup,
Reviewer_NotInGroupLongEnough,
}
}
}