#pragma warning disable CA1506 public async Task <IActionResult> Create([FromBody] Api.Models.InstancePermissionSet model, CancellationToken cancellationToken) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var existingPermissionSet = await DatabaseContext .PermissionSets .AsQueryable() .Where(x => x.Id == model.PermissionSetId) .Select(x => new Models.PermissionSet { UserId = x.UserId }) .FirstOrDefaultAsync(cancellationToken) .ConfigureAwait(false); if (existingPermissionSet == default) { return(Gone()); } if (existingPermissionSet.UserId.HasValue) { var userCanonicalName = await DatabaseContext .Users .AsQueryable() .Where(x => x.Id == existingPermissionSet.UserId.Value) .Select(x => x.CanonicalName) .FirstAsync(cancellationToken) .ConfigureAwait(false); if (userCanonicalName == Models.User.CanonicalizeName(Models.User.TgsSystemUserName)) { return(Forbid()); } } var dbUser = new Models.InstancePermissionSet { ByondRights = RightsHelper.Clamp(model.ByondRights ?? ByondRights.None), ChatBotRights = RightsHelper.Clamp(model.ChatBotRights ?? ChatBotRights.None), ConfigurationRights = RightsHelper.Clamp(model.ConfigurationRights ?? ConfigurationRights.None), DreamDaemonRights = RightsHelper.Clamp(model.DreamDaemonRights ?? DreamDaemonRights.None), DreamMakerRights = RightsHelper.Clamp(model.DreamMakerRights ?? DreamMakerRights.None), RepositoryRights = RightsHelper.Clamp(model.RepositoryRights ?? RepositoryRights.None), InstancePermissionSetRights = RightsHelper.Clamp(model.InstancePermissionSetRights ?? InstancePermissionSetRights.None), PermissionSetId = model.PermissionSetId, InstanceId = Instance.Id }; DatabaseContext.InstancePermissionSets.Add(dbUser); await DatabaseContext.Save(cancellationToken).ConfigureAwait(false); return(Created(dbUser.ToApi())); }
Models.InstancePermissionSet InstanceAdminPermissionSet(Models.InstancePermissionSet permissionSetToModify) { if (permissionSetToModify == null) { permissionSetToModify = new Models.InstancePermissionSet() { PermissionSetId = AuthenticationContext.PermissionSet.Id.Value } } ; permissionSetToModify.ByondRights = RightsHelper.AllRights <ByondRights>(); permissionSetToModify.ChatBotRights = RightsHelper.AllRights <ChatBotRights>(); permissionSetToModify.ConfigurationRights = RightsHelper.AllRights <ConfigurationRights>(); permissionSetToModify.DreamDaemonRights = RightsHelper.AllRights <DreamDaemonRights>(); permissionSetToModify.DreamMakerRights = RightsHelper.AllRights <DreamMakerRights>(); permissionSetToModify.RepositoryRights = RightsHelper.AllRights <RepositoryRights>(); permissionSetToModify.InstancePermissionSetRights = RightsHelper.AllRights <InstancePermissionSetRights>(); return(permissionSetToModify); }