-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathGroup.cs
315 lines (250 loc) · 8.78 KB
/
Group.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using NGitLab.Models;
namespace NGitLab.Mock;
public sealed class Group : GitLabObject
{
private string _path;
private string _name;
public Group()
: this(Guid.NewGuid().ToString("N"))
{
}
public Group(string name)
{
Groups = new GroupCollection(this);
Projects = new ProjectCollection(this);
RegisteredRunners = new RunnerCollection(this);
Permissions = new PermissionCollection(this);
Badges = new BadgeCollection(this);
Labels = new LabelsCollection(this);
Milestones = new MilestoneCollection(this);
Hooks = new GroupHookCollection(this);
Name = name;
}
public Group(User user)
: this(user.Name)
{
Path = user.UserName;
Permissions.Add(new Permission(user, AccessLevel.Owner));
IsUserNamespace = true;
}
public long Id { get; set; }
public string Name
{
get => _name;
set
{
if (IsUserNamespace)
throw new InvalidOperationException("Cannot change the name of a user namespace");
_name = value;
}
}
public string Description { get; set; }
public VisibilityLevel Visibility { get; set; }
public bool IsUserNamespace { get; }
public TimeSpan ExtraSharedRunnersLimit { get; set; }
public TimeSpan SharedRunnersLimit { get; set; }
public string RunnersToken { get; internal set; }
public bool LfsEnabled { get; set; }
public bool RequestAccessEnabled { get; set; }
public new Group Parent => base.Parent as Group;
public GroupCollection Groups { get; }
public ProjectCollection Projects { get; }
public RunnerCollection RegisteredRunners { get; }
public PermissionCollection Permissions { get; }
public BadgeCollection Badges { get; }
public LabelsCollection Labels { get; }
public MilestoneCollection Milestones { get; }
public GroupHookCollection Hooks { get; }
public IEnumerable<MergeRequest> MergeRequests => AllProjects.SelectMany(project => project.MergeRequests);
public string Path
{
get
{
_path ??= Slug.Create(Name);
return _path;
}
set
{
if (IsUserNamespace)
throw new InvalidOperationException("Cannot change the name of a user namespace");
_path = value;
}
}
public string PathWithNameSpace => Parent == null ? Path : (Parent.PathWithNameSpace + "/" + Path);
public string FullName => Parent == null ? Name : (Parent.FullName + "/" + Name);
public IEnumerable<Group> DescendantGroups
{
get
{
foreach (var group in Groups)
{
yield return group;
foreach (var subGroup in group.DescendantGroups)
{
yield return subGroup;
}
}
}
}
public DateTime CreatedAt { get; set; } = DateTime.UtcNow;
public IEnumerable<Project> AllProjects => Projects.Concat(DescendantGroups.SelectMany(group => group.Projects));
public EffectivePermissions GetEffectivePermissions() => GetEffectivePermissions(includeInheritedPermissions: true);
public EffectivePermissions GetEffectivePermissions(bool includeInheritedPermissions)
{
var result = new Dictionary<User, AccessLevel>();
if (Parent != null && includeInheritedPermissions)
{
foreach (var effectivePermission in Parent.GetEffectivePermissions(includeInheritedPermissions).Permissions)
{
Add(effectivePermission.User, effectivePermission.AccessLevel);
}
}
foreach (var permission in Permissions)
{
if (permission.User != null)
{
Add(permission.User, permission.AccessLevel);
}
else
{
foreach (var effectivePermission in permission.Group.GetEffectivePermissions(includeInheritedPermissions).Permissions)
{
Add(effectivePermission.User, effectivePermission.AccessLevel);
}
}
}
return new EffectivePermissions(result.Select(kvp => new EffectiveUserPermission(kvp.Key, kvp.Value)).ToList());
void Add(User user, AccessLevel accessLevel)
{
if (result.TryGetValue(user, out var existingAccessLevel))
{
if (accessLevel > existingAccessLevel)
{
result[user] = accessLevel;
}
}
else
{
result[user] = accessLevel;
}
}
}
public bool IsUserOwner(User user)
{
var accessLevel = GetEffectivePermissions().GetAccessLevel(user);
return accessLevel >= AccessLevel.Owner;
}
public bool CanUserViewGroup(User user)
{
if (Visibility == VisibilityLevel.Public)
return true;
if (Visibility == VisibilityLevel.Internal && user != null)
return true;
if (user == null)
return false;
if (user.IsAdmin)
return true;
var accessLevel = GetEffectivePermissions().GetAccessLevel(user);
if (accessLevel.HasValue)
return true;
return false;
}
public bool CanUserDeleteGroup(User user)
{
if (user == null)
return false;
if (user.IsAdmin)
return true;
var accessLevel = GetEffectivePermissions().GetAccessLevel(user);
return accessLevel.HasValue && accessLevel.Value == AccessLevel.Owner;
}
public bool CanUserEditGroup(User user)
{
if (user == null)
return false;
if (user.IsAdmin)
return true;
var accessLevel = GetEffectivePermissions().GetAccessLevel(user);
return accessLevel.HasValue && accessLevel.Value >= AccessLevel.Maintainer;
}
public bool CanUserAddGroup(User user)
{
if (user == null)
return false;
if (user.IsAdmin)
return true;
var accessLevel = GetEffectivePermissions().GetAccessLevel(user);
return accessLevel.HasValue && accessLevel.Value >= AccessLevel.Developer;
}
public bool CanUserAddProject(User user)
{
if (user == null)
return false;
if (user.IsAdmin)
return true;
var accessLevel = GetEffectivePermissions().GetAccessLevel(user);
return accessLevel.HasValue && accessLevel.Value >= AccessLevel.Developer;
}
public Runner AddRunner(string description, long id = default, string name = "gitlab-runner", bool paused = false, bool locked = true, bool isShared = false, bool runUntagged = false, string[] tagList = null)
{
var runner = new Runner
{
Name = name,
Description = description,
Paused = paused,
Locked = locked,
IsShared = isShared,
IpAddress = "0.0.0.0",
Online = null,
RunUntagged = runUntagged,
Id = id,
TagList = tagList,
};
RegisteredRunners.Add(runner);
return runner;
}
public bool RemoveRunner(long runnerId)
{
return RegisteredRunners.Remove(runnerId);
}
public Models.Group ToClientGroup(User currentUser)
{
return new Models.Group
{
Id = Id,
Name = Name,
Visibility = Visibility,
ParentId = Parent?.Id,
Projects = Projects.Select(p => p.ToClientProject(currentUser)).ToArray(),
FullName = FullName,
FullPath = PathWithNameSpace,
Path = Path,
Description = Description,
RequestAccessEnabled = RequestAccessEnabled,
LfsEnabled = LfsEnabled,
ExtraSharedRunnersMinutesLimit = (int)ExtraSharedRunnersLimit.TotalMinutes,
SharedRunnersMinutesLimit = (int)SharedRunnersLimit.TotalMinutes,
CreatedAt = CreatedAt,
RunnersToken = RunnersToken,
};
}
/// <summary>
/// https://docs.gitlab.com/ee/user/group/settings/group_access_tokens.html#bot-users-for-groups
/// </summary>
/// <param name="accessLevel">AccessLevel to give to the bot user</param>
/// <returns>Bot user that have been added to the group</returns>
public User CreateBotUser(AccessLevel accessLevel)
{
var botUsername = $"group_{Id}_bot_{Guid.NewGuid():D}";
var bot = new User(botUsername)
{
Email = $"{botUsername}@noreply.example.com",
};
Permissions.Add(new Permission(bot, accessLevel));
Server.Users.Add(bot);
return bot;
}
}