public async Task <IActionResult> Post([FromBody] IroncladResource model) { if (string.IsNullOrEmpty(model.Name)) { return(this.BadRequest(new { Message = $"Cannot create an identity resource without a name" })); } if (model.UserClaims?.Any() == false) { return(this.BadRequest(new { Message = $"Cannot create an identity resource without any claims" })); } var resource = new IdentityServerResource(model.Name, model.DisplayName, model.UserClaims); // optional properties resource.Enabled = model.Enabled ?? resource.Enabled; using (var session = this.store.LightweightSession()) { if (session.Query <PostgresResource>().Any(item => item.Name == model.Name)) { return(this.StatusCode((int)HttpStatusCode.Conflict, new { Message = "Identity resource already exists" })); } session.Insert(resource.ToEntity()); await session.SaveChangesAsync(); } return(this.Created(new Uri(this.HttpContext.GetIdentityServerRelativeUrl("~/api/identityresources/" + model.Name)), null)); }
public async Task <IActionResult> Put(string resourceName, [FromBody] IroncladResource model) { if (model.UserClaims?.Any() == false) { return(this.BadRequest(new { Message = $"Cannot update an identity resource without any claims" })); } using (var session = this.store.LightweightSession()) { var document = await session.Query <PostgresResource>().SingleOrDefaultAsync(item => item.Name == resourceName); if (document == null) { return(this.NotFound(new { Message = $"Identity resource '{resourceName}' not found" })); } // NOTE (Cameron): Because of the mapping/conversion unknowns we rely upon the Postgres integration to perform that operation which is why we do this... var resource = new IdentityServerResource { UserClaims = model.UserClaims, }; var entity = resource.ToEntity(); // update properties (everything supported is an optional update eg. if null is passed we will not update) document.DisplayName = model.DisplayName ?? document.DisplayName; document.UserClaims = entity.UserClaims ?? document.UserClaims; document.Enabled = model.Enabled ?? document.Enabled; session.Update(document); await session.SaveChangesAsync(); } return(this.Ok()); }