public async Task <IActionResult> Edit(IdentityResourceInputModel input, string button) { if (button == ControllerConstants.CANCEL) { return(RedirectToAction(nameof(Index))); } try { if (ModelState.IsValid) { await IdentityResourceService.UpsertIdentityResource(input); return(RedirectToAction(nameof(Index))); } } catch (AlreadyExistsException ex) { ModelState.Merge(ex.ModelStateDictionary); } catch (Exception ex) { ModelState.AddModelError(ControllerConstants.ERROR, ex.Message); } return(View(input)); }
public static IdentityResourceInputModel ToInputModel(this IdentityResourceModel model) { var inputModel = new IdentityResourceInputModel() { Id = model.Id, Name = model.Name, DisplayName = model.DisplayName, Description = model.Description, UserClaims = model.UserClaims } ?? new IdentityResourceInputModel(); return(inputModel); }
public async Task <IActionResult> Edit(string id) { var inputModel = new IdentityResourceInputModel(); if (!string.IsNullOrWhiteSpace(id)) { var model = await IdentityResourceService.GetIdentityResourceById(id); inputModel = model.ToInputModel(); } return(View(inputModel)); }
public void GivenItHasnotIdentityResource_WhenItCallsUpsert_AndAlreadyExistsName_ThenShouldThrowAlreadyExistsException() { // Given var identityResourceInputModel = new IdentityResourceInputModel(); IdentityResourceDataAccessMock .Setup(dataAccess => dataAccess.InsertAsync(It.IsAny <IdentityResourceData>())) .Throws(MongoWriteException); // When Assert.Throws(typeof(AlreadyExistsException), () => IdentityResourceService.UpsertIdentityResource(identityResourceInputModel).GetAwaiter().GetResult()); // Then IdentityResourceDataAccessMock.Verify(dataAccess => dataAccess.InsertAsync(It.IsAny <IdentityResourceData>()), Times.Once); }
public void GivenItHasntIdentityResource_WhenItCallsUpsert_ThenShouldInsert() { // Given var identityResourceInputModel = new IdentityResourceInputModel(); IdentityResourceDataAccessMock .Setup(dataAccess => dataAccess.InsertAsync(It.IsAny <IdentityResourceData>())) .Returns(Task.CompletedTask); // When IdentityResourceService.UpsertIdentityResource(identityResourceInputModel).GetAwaiter().GetResult(); // Then IdentityResourceDataAccessMock.Verify(dataAccess => dataAccess.InsertAsync(It.IsAny <IdentityResourceData>()), Times.Once); }
public IActionResult Edit(int id, IdentityResourceInputModel input) { if (!ModelState.IsValid) { return(View(input)); } var identityResource = _identityResourceService.GetIdentityResource(id); _mapper.Map(input, identityResource); identityResource.Updated = DateTime.Now; _identityResourceService.SaveChanges(); _logger.LogInformation("{user} edited identity resource {identityResource}", User.Identity.Name, id); return(RedirectToAction("Index")); }
public void GivenItHasIdentityResource_WhenItCallsUpsert_ThenShouldReplace() { // Given var identityResourceInputModel = new IdentityResourceInputModel() { Id = "1" }; IdentityResourceDataAccessMock .Setup(dataAccess => dataAccess.ReplaceAsync(It.IsAny <IdentityResourceData>(), It.IsAny <Expression <Func <IdentityResourceData, bool> > >())) .ReturnsAsync(true); // When IdentityResourceService.UpsertIdentityResource(identityResourceInputModel).GetAwaiter().GetResult(); // Then IdentityResourceDataAccessMock.Verify(dataAccess => dataAccess.ReplaceAsync(It.IsAny <IdentityResourceData>(), It.IsAny <Expression <Func <IdentityResourceData, bool> > >()), Times.Once); }
public IActionResult Add(IdentityResourceInputModel input) { if (!ModelState.IsValid) { return(View(input)); } var identityResource = _mapper.Map <IdentityResource>(input); identityResource.Created = DateTime.Now; _identityResourceService.AddIdentityResource(identityResource); _identityResourceService.SaveChanges(); _logger.LogInformation("{user} created identity resource {identityResource}", User.Identity.Name, identityResource); return(RedirectToAction("Index")); }
public async Task UpsertIdentityResource(IdentityResourceInputModel model) { try { var data = new IdentityResourceData(model.Id, model.Name, model.DisplayName, model.Description, model.UserClaims); if (string.IsNullOrWhiteSpace(model.Id)) { await IdentityResourceDataAccess.InsertAsync(data); } else { await IdentityResourceDataAccess.ReplaceAsync(data, updateData => updateData.Id == data.Id); } } catch (MongoWriteException ex) { ex.ThrowIfDuplicateKey(nameof(model.Name), $"O nome '{model.Name}' já existe."); throw ex; } }
public async Task <ActionResult <ResponseModel> > Edit(IdentityResourceInputModel inputModel) { ResponseModel responseModel = new ResponseModel(); try { // TODO: 效验 #region 效验 if (inputModel == null || inputModel.Id == 0) { responseModel.code = -1; responseModel.message = "更新失败: 不存在此身份资源"; return(await Task.FromResult(responseModel)); } #endregion // 覆盖更新: 先从数据库查出原有数据 var dbModel = await _configurationDbContext.IdentityResources .Include(d => d.UserClaims) .Include(d => d.Properties) .FirstOrDefaultAsync(m => m.Id == inputModel.Id); if (dbModel == null) { responseModel.code = -1; responseModel.message = "更新失败: 不存在此身份资源"; return(await Task.FromResult(responseModel)); } // InputModel => dbModel #region 普通属性赋值 dbModel.Name = inputModel.Name; dbModel.DisplayName = inputModel.DisplayName; dbModel.Description = inputModel.Description; dbModel.Required = inputModel.Required; dbModel.ShowInDiscoveryDocument = inputModel.ShowInDiscoveryDocument; dbModel.Updated = DateTime.Now; #endregion // 关联属性赋值 #region 关联属性赋值 dbModel.UserClaims = new List <IdentityResourceClaim>(); if (!string.IsNullOrEmpty(inputModel.UserClaims)) { inputModel.UserClaims.Split(",").Where(m => m != "" && m != null).ToList().ForEach(q => { dbModel.UserClaims.Add(new IdentityServer4.EntityFramework.Entities.IdentityResourceClaim() { IdentityResourceId = dbModel.Id, Type = q }); }); } #endregion // 保存到数据库 _configurationDbContext.IdentityResources.Update(dbModel); await _configurationDbContext.SaveChangesAsync(); responseModel.code = 1; responseModel.message = "更新成功 "; } catch (Exception ex) { responseModel.code = -1; responseModel.message = "更新失败: " + ex.Message; } return(await Task.FromResult(responseModel)); }