-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathPermissions.cs
More file actions
231 lines (206 loc) · 8.21 KB
/
Permissions.cs
File metadata and controls
231 lines (206 loc) · 8.21 KB
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
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using GitHub.Actions.WorkflowParser.Conversion;
using Newtonsoft.Json;
namespace GitHub.Actions.WorkflowParser
{
[DataContract]
public class Permissions
{
[JsonConstructor]
public Permissions()
{
}
public Permissions(Permissions copy)
{
Actions = copy.Actions;
ArtifactMetadata = copy.ArtifactMetadata;
Attestations = copy.Attestations;
Checks = copy.Checks;
Contents = copy.Contents;
Deployments = copy.Deployments;
Issues = copy.Issues;
Discussions = copy.Discussions;
Packages = copy.Packages;
Pages = copy.Pages;
PullRequests = copy.PullRequests;
RepositoryProjects = copy.RepositoryProjects;
Statuses = copy.Statuses;
SecurityEvents = copy.SecurityEvents;
IdToken = copy.IdToken;
Models = copy.Models;
Workflows = copy.Workflows;
}
public Permissions(
PermissionLevel permissionLevel,
bool includeIdToken,
bool includeAttestations,
bool includeModels)
{
Actions = permissionLevel;
ArtifactMetadata = permissionLevel;
Attestations = includeAttestations ? permissionLevel : PermissionLevel.NoAccess;
Checks = permissionLevel;
Contents = permissionLevel;
Deployments = permissionLevel;
Issues = permissionLevel;
Discussions = permissionLevel;
Packages = permissionLevel;
Pages = permissionLevel;
PullRequests = permissionLevel;
RepositoryProjects = permissionLevel;
Statuses = permissionLevel;
SecurityEvents = permissionLevel;
IdToken = includeIdToken ? permissionLevel : PermissionLevel.NoAccess;
// Models must not have higher permissions than Read
Models = includeModels
? (permissionLevel == PermissionLevel.Write ? PermissionLevel.Read : permissionLevel)
: PermissionLevel.NoAccess;
// Workflows is excluded from write-all / read-all; must be explicitly requested
Workflows = PermissionLevel.NoAccess;
}
private static KeyValuePair<string, (PermissionLevel, PermissionLevel)>[] ComparisonKeyMapping(Permissions left, Permissions right)
{
return new[]
{
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("actions", (left.Actions, right.Actions)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("artifact-metadata", (left.ArtifactMetadata, right.ArtifactMetadata)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("attestations", (left.Attestations, right.Attestations)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("checks", (left.Checks, right.Checks)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("contents", (left.Contents, right.Contents)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("deployments", (left.Deployments, right.Deployments)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("discussions", (left.Discussions, right.Discussions)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("issues", (left.Issues, right.Issues)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("packages", (left.Packages, right.Packages)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("pages", (left.Pages, right.Pages)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("pull-requests", (left.PullRequests, right.PullRequests)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("repository-projects", (left.RepositoryProjects, right.RepositoryProjects)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("statuses", (left.Statuses, right.Statuses)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("security-events", (left.SecurityEvents, right.SecurityEvents)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("id-token", (left.IdToken, right.IdToken)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("models", (left.Models, right.Models)),
new KeyValuePair<string, (PermissionLevel, PermissionLevel)>("workflows", (left.Workflows, right.Workflows)),
};
}
[DataMember(Name = "actions", EmitDefaultValue = false)]
public PermissionLevel Actions
{
get;
set;
}
[DataMember(Name = "artifact-metadata", EmitDefaultValue = false)]
public PermissionLevel ArtifactMetadata
{
get;
set;
}
[DataMember(Name = "attestations", EmitDefaultValue = false)]
public PermissionLevel Attestations
{
get;
set;
}
[DataMember(Name = "checks", EmitDefaultValue = false)]
public PermissionLevel Checks
{
get;
set;
}
[DataMember(Name = "contents", EmitDefaultValue = false)]
public PermissionLevel Contents
{
get;
set;
}
[DataMember(Name = "deployments", EmitDefaultValue = false)]
public PermissionLevel Deployments
{
get;
set;
}
[DataMember(Name = "discussions", EmitDefaultValue = false)]
public PermissionLevel Discussions
{
get;
set;
}
[DataMember(Name = "id-token", EmitDefaultValue = false)]
public PermissionLevel IdToken
{
get;
set;
}
[DataMember(Name = "issues", EmitDefaultValue = false)]
public PermissionLevel Issues
{
get;
set;
}
[DataMember(Name = "models", EmitDefaultValue = false)]
public PermissionLevel Models
{
get;
set;
}
[DataMember(Name = "packages", EmitDefaultValue = false)]
public PermissionLevel Packages
{
get;
set;
}
[DataMember(Name = "pages", EmitDefaultValue = false)]
public PermissionLevel Pages
{
get;
set;
}
[DataMember(Name = "pull-requests", EmitDefaultValue = false)]
public PermissionLevel PullRequests
{
get;
set;
}
[DataMember(Name = "repository-projects", EmitDefaultValue = false)]
public PermissionLevel RepositoryProjects
{
get;
set;
}
[DataMember(Name = "security-events", EmitDefaultValue = false)]
public PermissionLevel SecurityEvents
{
get;
set;
}
[DataMember(Name = "statuses", EmitDefaultValue = false)]
public PermissionLevel Statuses
{
get;
set;
}
[DataMember(Name = "workflows", EmitDefaultValue = false)]
public PermissionLevel Workflows
{
get;
set;
}
public Permissions Clone()
{
return new Permissions(this);
}
internal bool ViolatesMaxPermissions(Permissions maxPermissions, out List<PermissionLevelViolation> permissionsViolations)
{
var mapping = Permissions.ComparisonKeyMapping(this, maxPermissions);
permissionsViolations = new List<PermissionLevelViolation>();
foreach (var (key, (permissionLevel, maxPermissionLevel)) in mapping)
{
if (!permissionLevel.IsLessThanOrEqualTo(maxPermissionLevel))
{
permissionsViolations.Add(new PermissionLevelViolation(key, permissionLevel, maxPermissionLevel));
}
}
return permissionsViolations.Count > 0;
}
}
}