tgstation-server 6.12.0
The /tg/station 13 server suite
Loading...
Searching...
No Matches
InstancePermissionSetController.cs
Go to the documentation of this file.
1using System;
2using System.Linq;
5
9
22
24{
30 {
35
61
70 [HttpPut]
74#pragma warning disable CA1506
76 {
77 ArgumentNullException.ThrowIfNull(model);
78
81 .AsQueryable()
82 .Where(x => x.Id == model.PermissionSetId)
83 .Select(x => new Models.PermissionSet
84 {
85 Id = x.Id,
86 UserId = x.UserId,
87 })
88 .FirstOrDefaultAsync(cancellationToken);
89
90 if (existingPermissionSet == default)
91 return this.Gone();
92
93 if (existingPermissionSet.UserId.HasValue)
94 {
96 .Users
97 .AsQueryable()
98 .Where(x => x.Id == existingPermissionSet.UserId.Value)
99 .Select(x => x.CanonicalName)
100 .FirstAsync(cancellationToken);
101
102 if (userCanonicalName == Models.User.CanonicalizeName(Models.User.TgsSystemUserName))
103 return Forbid();
104 }
105
107 {
108 EngineRights = RightsHelper.Clamp(model.EngineRights ?? EngineRights.None),
109 ChatBotRights = RightsHelper.Clamp(model.ChatBotRights ?? ChatBotRights.None),
110 ConfigurationRights = RightsHelper.Clamp(model.ConfigurationRights ?? ConfigurationRights.None),
111 DreamDaemonRights = RightsHelper.Clamp(model.DreamDaemonRights ?? DreamDaemonRights.None),
112 DreamMakerRights = RightsHelper.Clamp(model.DreamMakerRights ?? DreamMakerRights.None),
113 RepositoryRights = RightsHelper.Clamp(model.RepositoryRights ?? RepositoryRights.None),
114 InstancePermissionSetRights = RightsHelper.Clamp(model.InstancePermissionSetRights ?? InstancePermissionSetRights.None),
115 PermissionSetId = model.PermissionSetId,
116 InstanceId = Instance.Require(x => x.Id),
117 };
118
120
122
123 // needs to be set for next call
126 return this.Created(dbUser.ToApi());
127 }
128#pragma warning restore CA1506
129
138 [HttpPost]
142#pragma warning disable CA1506 // TODO: Decomplexify
144 {
145 ArgumentNullException.ThrowIfNull(model);
146
148 .Instances
149 .AsQueryable()
150 .Where(x => x.Id == Instance.Id)
151 .SelectMany(x => x.InstancePermissionSets)
152 .Where(x => x.PermissionSetId == model.PermissionSetId)
153 .FirstOrDefaultAsync(cancellationToken);
154 if (originalPermissionSet == null)
155 return this.Gone();
156
157 originalPermissionSet.EngineRights = RightsHelper.Clamp(model.EngineRights ?? originalPermissionSet.EngineRights!.Value);
158 originalPermissionSet.RepositoryRights = RightsHelper.Clamp(model.RepositoryRights ?? originalPermissionSet.RepositoryRights!.Value);
159 originalPermissionSet.InstancePermissionSetRights = RightsHelper.Clamp(model.InstancePermissionSetRights ?? originalPermissionSet.InstancePermissionSetRights!.Value);
160 originalPermissionSet.ChatBotRights = RightsHelper.Clamp(model.ChatBotRights ?? originalPermissionSet.ChatBotRights!.Value);
161 originalPermissionSet.ConfigurationRights = RightsHelper.Clamp(model.ConfigurationRights ?? originalPermissionSet.ConfigurationRights!.Value);
162 originalPermissionSet.DreamDaemonRights = RightsHelper.Clamp(model.DreamDaemonRights ?? originalPermissionSet.DreamDaemonRights!.Value);
163 originalPermissionSet.DreamMakerRights = RightsHelper.Clamp(model.DreamMakerRights ?? originalPermissionSet.DreamMakerRights!.Value);
164
167 || (AuthenticationContext.GetRight(RightsType.InstancePermissionSet) & (ulong)InstancePermissionSetRights.Read) != 0;
168 return Json(
170 ? originalPermissionSet.ToApi()
172 {
173 PermissionSetId = originalPermissionSet.PermissionSetId,
174 });
175 }
176#pragma warning restore CA1506
182 [HttpGet]
186
200 () => ValueTask.FromResult(
203 .Instances
204 .AsQueryable()
205 .Where(x => x.Id == Instance.Id)
206 .SelectMany(x => x.InstancePermissionSets)
207 .OrderBy(x => x.PermissionSetId))),
208 null,
209 page,
210 pageSize,
212
221 [HttpGet("{id}")]
225 public async ValueTask<IActionResult> GetId(long id, CancellationToken cancellationToken)
226 {
227 // this functions as userId
228 var permissionSet = await DatabaseContext
229 .Instances
230 .AsQueryable()
231 .Where(x => x.Id == Instance.Id)
232 .SelectMany(x => x.InstancePermissionSets)
233 .Where(x => x.PermissionSetId == id)
234 .FirstOrDefaultAsync(cancellationToken);
235 if (permissionSet == default)
236 return this.Gone();
237 return Json(permissionSet.ToApi());
238 }
239
248 [HttpDelete("{id}")]
252 public async ValueTask<IActionResult> Delete(long id, CancellationToken cancellationToken)
253 {
255 .Instances
256 .AsQueryable()
257 .Where(x => x.Id == Instance.Id)
258 .SelectMany(x => x.InstancePermissionSets)
259 .Where(x => x.PermissionSetId == id)
260 .ExecuteDeleteAsync(cancellationToken);
261
262 return numDeleted > 0 ? NoContent() : this.Gone();
263 }
264 }
265}
virtual ? long Id
The ID of the entity.
Definition EntityId.cs:13
Metadata about a server instance.
Definition Instance.cs:9
long PermissionSetId
The EntityId.Id of the PermissionSet the InstancePermissionSet belongs to.
Represents an error message returned by the server.
Routes to a server actions.
Definition Routes.cs:9
const string List
The postfix for list operations.
Definition Routes.cs:113
const string InstancePermissionSet
The instance permission set controller.
Definition Routes.cs:88
readonly IInstanceManager instanceManager
The IInstanceManager for the ComponentInterfacingController.
async ValueTask< IActionResult > GetId(long id, CancellationToken cancellationToken)
Gets a specific Api.Models.Internal.InstancePermissionSet.
ValueTask< IActionResult > List([FromQuery] int? page, [FromQuery] int? pageSize, CancellationToken cancellationToken)
Lists InstancePermissionSets for the instance.
IActionResult Read()
Read the active InstancePermissionSet.
readonly IPermissionsUpdateNotifyee permissionsUpdateNotifyee
The IPermissionsUpdateNotifyee for the InstancePermissionSetController.
async ValueTask< IActionResult > Update([FromBody] InstancePermissionSetRequest model, CancellationToken cancellationToken)
Update the permissions for an InstancePermissionSet.
InstancePermissionSetController(IDatabaseContext databaseContext, IAuthenticationContext authenticationContext, ILogger< InstancePermissionSetController > logger, IInstanceManager instanceManager, IPermissionsUpdateNotifyee permissionsUpdateNotifyee, IApiHeadersProvider apiHeaders)
Initializes a new instance of the InstancePermissionSetController class.
async ValueTask< IActionResult > Create([FromBody] InstancePermissionSetRequest model, CancellationToken cancellationToken)
Create an InstancePermissionSet.
async ValueTask< IActionResult > Delete(long id, CancellationToken cancellationToken)
Delete an InstancePermissionSet.
ComponentInterfacingController for operations that require an instance.
Backend abstract implementation of IDatabaseContext.
DbSet< Instance > Instances
The Instances in the DatabaseContext.
DbSet< InstancePermissionSet > InstancePermissionSets
The InstancePermissionSets in the DatabaseContext.
DbSet< PermissionSet > PermissionSets
The PermissionSets in the DatabaseContext.
Task Save(CancellationToken cancellationToken)
Saves changes made to the IDatabaseContext.A Task representing the running operation.
DbSet< User > Users
The Users in the DatabaseContext.
PermissionSet PermissionSet
The User's effective PermissionSet.
ulong GetRight(RightsType rightsType)
Get the value of a given rightsType .The value of rightsType . Note that if InstancePermissionSet is ...
For creating and accessing authentication contexts.
Receives notifications about permissions updates.
ValueTask InstancePermissionSetCreated(InstancePermissionSet instancePermissionSet, CancellationToken cancellationToken)
Called when a given instancePermissionSet is successfully created.
ChatBotRights
Rights for chat bots.
ConfigurationRights
Rights for Models.IConfigurationFiles.
@ List
User may list files if the Models.Instance allows it.
DreamMakerRights
Rights for deployment.
RightsType
The type of rights a model uses.
Definition RightsType.cs:7
EngineRights
Rights for engine version management.
RepositoryRights
Rights for the git repository.
InstancePermissionSetRights
Rights for an Models.Instance.
DreamDaemonRights
Rights for managing DreamDaemon.