tgstation-server 6.12.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
12
21
23{
26 {
31
36
47 IDatabaseContext databaseContext,
48 CancellationToken cancellationToken)
49 {
50 ArgumentNullException.ThrowIfNull(ids);
51 ArgumentNullException.ThrowIfNull(databaseContext);
52
53 return databaseContext
54 .Groups
55 .Where(group => ids.Contains(group.Id!.Value))
56 .ToDictionaryAsync(userGroup => userGroup.Id!.Value, cancellationToken);
57 }
58
81
84 {
86 return Forbid<UserGroup>();
87
89 if (includeJoins)
91 .Where(x => x.Id == id)
92 .FirstOrDefaultAsync(cancellationToken);
93 else
95
96 if (userGroup == null)
97 return Gone<UserGroup>();
98
100 }
101
104 {
106 if (group == null)
107 return ValueTask.FromResult(Gone<UserGroup>());
108
109 return ValueTask.FromResult(new AuthorityResponse<UserGroup>(group));
110 }
111
114 {
116 .Groups
117 .AsQueryable();
118
119 if (includeJoins)
121 .Include(x => x.Users)
122 .Include(x => x.PermissionSet);
123
124 return queryable;
125 }
126
128 public async ValueTask<AuthorityResponse<UserGroup>> Create(string name, Models.PermissionSet? permissionSet, CancellationToken cancellationToken)
129 {
130 ArgumentNullException.ThrowIfNull(name);
131
133 .Groups
134 .AsQueryable()
135 .CountAsync(cancellationToken);
136 if (totalGroups >= generalConfigurationOptions.Value.UserGroupLimit)
137 return Conflict<UserGroup>(ErrorCode.UserGroupLimitReached);
138
140 {
142 InstanceManagerRights = permissionSet?.InstanceManagerRights ?? InstanceManagerRights.None,
143 };
144
145 var dbGroup = new UserGroup
146 {
147 Name = name,
149 };
150
153 Logger.LogInformation("Created new user group {groupName} ({groupId})", dbGroup.Name, dbGroup.Id);
154
156 dbGroup,
157 HttpSuccessResponse.Created);
158 }
159
161 public async ValueTask<AuthorityResponse<UserGroup>> Update(long id, string? newName, Models.PermissionSet? newPermissionSet, CancellationToken cancellationToken)
162 {
164 .Groups
165 .AsQueryable()
166 .Where(x => x.Id == id)
167 .Include(x => x.PermissionSet)
168 .FirstOrDefaultAsync(cancellationToken);
169
170 if (currentGroup == default)
171 return Gone<UserGroup>();
172
173 if (newPermissionSet != null)
174 {
175 currentGroup.PermissionSet!.AdministrationRights = newPermissionSet.AdministrationRights ?? currentGroup.PermissionSet.AdministrationRights;
177 }
178
180
182
184 }
185
188 {
190 .Groups
191 .AsQueryable()
192 .Where(x => x.Id == id && x.Users!.Count == 0)
193 .ExecuteDeleteAsync(cancellationToken);
194
195 if (numDeleted > 0)
196 return new();
197
198 // find out how we failed
200 .Groups
201 .AsQueryable()
202 .Where(x => x.Id == id)
203 .AnyAsync(cancellationToken);
204
205 return new(
207 ? new ErrorMessageResponse(ErrorCode.UserGroupNotEmpty)
208 : new ErrorMessageResponse(),
211 : HttpFailureResponse.Gone);
212 }
213 }
214}
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.
IQueryable< UserGroup > Queryable(bool includeJoins)
Gets all registered UserGroups.A IQueryable<T> of UserGroups.
ValueTask< AuthorityResponse< UserGroup > > Read()
Gets the current UserGroup.A ValueTask<TResult> resulting in a UserGroup AuthorityResponse<TResult>.
UserGroupAuthority(IAuthenticationContext authenticationContext, IDatabaseContext databaseContext, ILogger< UserGroupAuthority > logger, IUserGroupsDataLoader userGroupsDataLoader, IOptionsSnapshot< GeneralConfiguration > generalConfigurationOptions)
Initializes a new instance of the UserGroupAuthority class.
async ValueTask< AuthorityResponse< UserGroup > > GetId(long id, bool includeJoins, CancellationToken cancellationToken)
Gets the UserGroup with a given id .A ValueTask<TResult> resulting in a User AuthorityResponse<TResul...
readonly IUserGroupsDataLoader userGroupsDataLoader
The IUserGroupsDataLoader for the UserGroupAuthority.
async ValueTask< AuthorityResponse > DeleteEmpty(long id, CancellationToken cancellationToken)
Deletes an empty UserGroup.A ValueTask representing the running operation.
static Task< Dictionary< long, UserGroup > > GetUserGroups(IReadOnlyList< long > ids, IDatabaseContext databaseContext, CancellationToken cancellationToken)
Implements the userGroupsDataLoader.
async ValueTask< AuthorityResponse< UserGroup > > Update(long id, string? newName, Models.PermissionSet? newPermissionSet, CancellationToken cancellationToken)
Updates a UserGroup.A ValueTask<TResult> resulting in a UserGroup AuthorityResponse<TResult>.
async ValueTask< AuthorityResponse< UserGroup > > Create(string name, Models.PermissionSet? permissionSet, CancellationToken cancellationToken)
Create a UserGroup.A ValueTask<TResult> resulting in a UserGroup AuthorityResponse<TResult>.
readonly IOptionsSnapshot< GeneralConfiguration > generalConfigurationOptions
The IOptionsSnapshot<TOptions> of the GeneralConfiguration.
Backend abstract implementation of IDatabaseContext.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext.A Task representing the running operation.
DbSet< UserGroup > Groups
The UserGroups in the DatabaseContext.
Represents a group of Users.
Definition UserGroup.cs:16
long? GroupId
The EntityId.Id of the User's Group.
Definition User.cs:46
UserGroup? Group
The UserGroup the User belongs to, if any.
Definition User.cs:41
ulong GetRight(RightsType rightsType)
Get the value of a given rightsType .The value of rightsType . Note that if InstancePermissionSet is ...
IDatabaseCollection< UserGroup > Groups
The DbSet<TEntity> for UserGroups.
For creating and accessing authentication contexts.
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.
RightsType
The type of rights a model uses.
Definition RightsType.cs:7
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.