public async Task <IActionResult> RegisterNewRole([FromBody] NewRoleModel dataModel) { AppRole newRole = new AppRole() { Name = dataModel.Name, DisplayName = dataModel.DisplayName, OrganizationID = dataModel.OrgID, Editable = true }; _roleManager.CreateAsync(newRole).Wait(); AppRole role = _context.Roles.Where(o => o.Name == dataModel.Name).FirstOrDefault(); if (role == null) { return(BadRequest("Something went wrong in the server side while registering a new role.")); } List <CustomRolePermissionLevelc> customRolePermissions = dataModel.RolePermissionLevels.Select(o => new CustomRolePermissionLevelc { Allowed = o.Allowed, FK_CustomPermisson = o.CustomPermissonID, FK_RoleID = role.Id, IsActive = true, RegistedDate = DateTime.Now }).ToList(); _context.customRolePermissionLevels.AddRange(customRolePermissions); _context.SaveChanges(true); return(Ok()); }
public async Task <IActionResult> CreateRole([FromBody] NewRoleModel role) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var roleCreated = await _roleService.CreateRoleAsync(role.Name, role.Description); return(CreatedAtAction("GetRole", new { id = roleCreated.RoleId }, role)); }
public async Task <IActionResult> AddRole(NewRoleModel model) { if (ModelState.IsValid) { var role = new ApplicationRole { Name = model.Name, Description = model.Description }; await _roleManager.CreateAsync(role); return(RedirectToAction(nameof(Index))); } return(View(model)); }
public async Task <IActionResult> UpdateRoleInformation([FromBody] NewRoleModel dataModel) { AppRole role = _context.Roles.Where(o => o.Name == dataModel.Name).FirstOrDefault(); role.DisplayName = dataModel.DisplayName; await _roleManager.UpdateAsync(role); List <CustomRolePermissionLevelc> exisingPermisson = _context.customRolePermissionLevels.Where(o => o.FK_RoleID == role.Id).ToList(); if (exisingPermisson.Count == 0) { exisingPermisson = dataModel.RolePermissionLevels.Select(o => new CustomRolePermissionLevelc { Allowed = o.Allowed, FK_CustomPermisson = o.CustomPermissonID, FK_RoleID = role.Id, IsActive = true, RegistedDate = DateTime.Now }).ToList(); } else { for (int i = 0; i < dataModel.RolePermissionLevels.Count; i++) { var match = exisingPermisson.Where(o => o.FK_CustomPermisson == dataModel.RolePermissionLevels[i].CustomPermissonID).FirstOrDefault(); if (match != null) { exisingPermisson.Where(o => o.FK_CustomPermisson == dataModel.RolePermissionLevels[i].CustomPermissonID).Select(c => { c.Allowed = dataModel.RolePermissionLevels[i].Allowed; return(c); }).ToList(); } } } _context.customRolePermissionLevels.UpdateRange(exisingPermisson); _context.SaveChanges(); return(Ok()); }
public async Task <IActionResult> GetRoleInformation([FromBody] RoleInfoReqData roleInfoReq) { NewRoleModel outputModel = new NewRoleModel(); AppRole role = _context.Roles.Where(o => o.Id == roleInfoReq.RoleID).FirstOrDefault(); if (role == null) { return(BadRequest("RoleID id not valid !")); } List <CustomRolePermissionLevelc> customRolePermissions = _context.customRolePermissionLevels.Where(o => o.FK_RoleID == roleInfoReq.RoleID).ToList(); outputModel.DisplayName = role.DisplayName; outputModel.Name = role.Name; outputModel.OrgID = role.OrganizationID; outputModel.RolePermissionLevels = customRolePermissions.Select(o => new RolePermissionLevel { CustomPermissonID = o.FK_CustomPermisson, Allowed = o.Allowed }).ToList(); return(Ok(outputModel)); }