tgstation-server 6.19.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
UserGroupAuthority.cs
Go to the documentation of this file.
1using System;
3using System.Linq;
6
7using GreenDonut;
8
13
23
25{
28 {
33
38
43
54 IDatabaseContext databaseContext,
55 CancellationToken cancellationToken)
56 {
57 ArgumentNullException.ThrowIfNull(ids);
58 ArgumentNullException.ThrowIfNull(databaseContext);
59
60 return databaseContext
61 .Groups
62 .Where(group => ids.Contains(group.Id!.Value))
63 .ToDictionaryAsync(userGroup => userGroup.Id!.Value, cancellationToken);
64 }
65
88
91 => new(
92 () =>
93 {
94 if (id != claimsPrincipalAccessor.User.GetTgsUserId())
95 return Flag(AdministrationRights.ReadUsers);
96
97 return null;
98 },
99 async () =>
100 {
102 if (includeJoins)
104 .Where(x => x.Id == id)
105 .FirstOrDefaultAsync(cancellationToken);
106 else
108
109 if (userGroup == null)
110 return Gone<UserGroup>();
111
113 });
114
117 => new(
118 () => (IAuthorizationRequirement?)null,
119 async () =>
120 {
121 var userId = claimsPrincipalAccessor.User.GetTgsUserId();
123 .Users
124 .AsQueryable()
125 .Where(user => user.Id == userId)
126 .Select(user => user.Group)
127 .FirstOrDefaultAsync(cancellationToken);
128
129 if (group == null)
130 return Gone<UserGroup>();
131
133 });
134
137 => new(
138 () => Flag(AdministrationRights.ReadUsers),
139 () => ValueTask.FromResult(QueryableImpl(includeJoins)));
140
142 public RequirementsGated<AuthorityResponse<UserGroup>> Create(string name, Models.PermissionSet? permissionSet, CancellationToken cancellationToken)
143 {
144 ArgumentNullException.ThrowIfNull(name);
145 return new(
146 () => Flag(AdministrationRights.WriteUsers),
147 async () =>
148 {
150 .Groups
151 .AsQueryable()
152 .CountAsync(cancellationToken);
153 if (totalGroups >= generalConfigurationOptions.Value.UserGroupLimit)
154 return Conflict<UserGroup>(ErrorCode.UserGroupLimitReached);
155
157 {
159 InstanceManagerRights = permissionSet?.InstanceManagerRights ?? InstanceManagerRights.None,
160 };
161
162 var dbGroup = new UserGroup
163 {
164 Name = name,
166 };
167
170 Logger.LogInformation("Created new user group {groupName} ({groupId})", dbGroup.Name, dbGroup.Id);
171
173 dbGroup,
174 HttpSuccessResponse.Created);
175 });
176 }
177
179 public RequirementsGated<AuthorityResponse<UserGroup>> Update(long id, string? newName, Models.PermissionSet? newPermissionSet, CancellationToken cancellationToken)
180 => new(
181 () => Flag(AdministrationRights.WriteUsers),
182 async () =>
183 {
185 .Groups
186 .AsQueryable()
187 .Where(x => x.Id == id)
188 .Include(x => x.PermissionSet)
189 .FirstOrDefaultAsync(cancellationToken);
190
191 if (currentGroup == default)
192 return Gone<UserGroup>();
193
194 if (newPermissionSet != null)
195 {
196 currentGroup.PermissionSet!.AdministrationRights = newPermissionSet.AdministrationRights ?? currentGroup.PermissionSet.AdministrationRights;
198 }
199
201
203
205 });
206
209 => new(
210 () => Flag(AdministrationRights.WriteUsers),
211 async () =>
212 {
214 .Groups
215 .AsQueryable()
216 .Where(x => x.Id == id && x.Users!.Count == 0)
217 .ExecuteDeleteAsync(cancellationToken);
218
219 if (numDeleted > 0)
220 return new();
221
222 // find out how we failed
224 .Groups
225 .AsQueryable()
226 .Where(x => x.Id == id)
227 .AnyAsync(cancellationToken);
228
229 return new(
231 ? new ErrorMessageResponse(ErrorCode.UserGroupNotEmpty)
232 : new ErrorMessageResponse(),
235 : HttpFailureResponse.Gone);
236 });
237
244 {
246 .Groups
247 .AsQueryable();
248
249 if (includeJoins)
251 .Include(x => x.Users)
252 .Include(x => x.PermissionSet);
253
254 return queryable;
255 }
256 }
257}
Represents a set of server permissions.
AdministrationRights? AdministrationRights
The Rights.AdministrationRights for the user.
Represents an error message returned by the server.
ILogger< AuthorityBase > Logger
Gets the ILogger for the AuthorityBase.
Evaluates a set of IAuthorizationRequirements to be checked before executing a response.
readonly IClaimsPrincipalAccessor claimsPrincipalAccessor
The IClaimsPrincipalAccessor for the UserGroupAuthority.
IQueryable< UserGroup > QueryableImpl(bool includeJoins)
Get the IQueryable<T> UserGroups.
RequirementsGated< AuthorityResponse< UserGroup > > Update(long id, string? newName, Models.PermissionSet? newPermissionSet, CancellationToken cancellationToken)
Updates a UserGroup.A ValueTask<TResult> resulting in a RequirementsGated<TResult> UserGroup Authorit...
RequirementsGated< AuthorityResponse< UserGroup > > Create(string name, Models.PermissionSet? permissionSet, CancellationToken cancellationToken)
Create a UserGroup.A RequirementsGated<TResult> UserGroup AuthorityResponse<TResult>.
RequirementsGated< IQueryable< UserGroup > > Queryable(bool includeJoins)
Gets all registered UserGroups.A RequirementsGated<TResult> IQueryable<T> of UserGroups.
readonly IUserGroupsDataLoader userGroupsDataLoader
The IUserGroupsDataLoader for the UserGroupAuthority.
static Task< Dictionary< long, UserGroup > > GetUserGroups(IReadOnlyList< long > ids, IDatabaseContext databaseContext, CancellationToken cancellationToken)
Implements the userGroupsDataLoader.
readonly IOptionsSnapshot< GeneralConfiguration > generalConfigurationOptions
The IOptionsSnapshot<TOptions> of the GeneralConfiguration.
RequirementsGated< AuthorityResponse< UserGroup > > GetId(long id, bool includeJoins, CancellationToken cancellationToken)
Gets the UserGroup with a given id .A RequirementsGated<TResult> User AuthorityResponse<TResult>.
RequirementsGated< AuthorityResponse< UserGroup > > Read(CancellationToken cancellationToken)
Gets the current UserGroup.A ValueTask<TResult> resulting in a UserGroup AuthorityResponse<TResult>.
RequirementsGated< AuthorityResponse > DeleteEmpty(long id, CancellationToken cancellationToken)
Deletes an empty UserGroup.A RequirementsGated<TResult> AuthorityResponse representing the running op...
UserGroupAuthority(IDatabaseContext databaseContext, ILogger< UserGroupAuthority > logger, IUserGroupsDataLoader userGroupsDataLoader, IClaimsPrincipalAccessor claimsPrincipalAccessor, IOptionsSnapshot< GeneralConfiguration > generalConfigurationOptions)
Initializes a new instance of the UserGroupAuthority class.
Backend abstract implementation of IDatabaseContext.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext.A Task representing the running operation.
DbSet< User > Users
The Users in the DatabaseContext.
DbSet< UserGroup > Groups
The UserGroups in the DatabaseContext.
Represents a group of Users.
Definition UserGroup.cs:16
IDatabaseCollection< UserGroup > Groups
The DbSet<TEntity> for UserGroups.
Interface for accessing the current request's ClaimsPrincipal.
ClaimsPrincipal User
Get the current ClaimsPrincipal.
ErrorCode
Types of Response.ErrorMessageResponses that the API may return.
Definition ErrorCode.cs:12
@ List
User may list files if the Models.Instance allows it.
InstanceManagerRights
Rights for managing Models.Instances.
AdministrationRights
Administration rights for the server.
HttpFailureResponse
Indicates the type of HTTP status code an failing AuthorityResponse should generate.
HttpSuccessResponse
Indicates the type of HTTP status code a successful AuthorityResponse<TResult> should generate.