public ActionResult _Add(UserRoleEditModel model) { switch (model.RoleId) { case 4: // Industry Director case 12: model.RegionId = null; model.CountryId = null; break; case 5: // Region Director case 13: model.IndustryId = null; model.CountryId = null; break; case 6: // Country Director case 14: model.IndustryId = null; model.RegionId = null; break; default: model.IndustryId = null; model.RegionId = null; model.CountryId = null; break; } if (!ModelState.IsValid) { return(Json(new { success = false, message = ModelState.GetErrors() })); } var userRole = Mapper.Map <UserRoleEditModel, UserRoleEntity>(model); var userId = model.UserEmail.Split('|')[0]; var userEmail = model.UserEmail.Split('|')[1]; userRole.UserId = Convert.ToInt32(userId); userRole.UserEmail = userEmail; var added = UserRoleServices.CreateUserRole(userRole); switch (added) { case -1: return(Json(new { success = false, title = "Error!", message = "User role could not added!" })); case 0: return(Json(new { success = false, title = "Already exists!", message = "User role could not added. It already exists." })); default: // 1: return(Json(new { success = true, message = "User role added!" })); } }
public void DeleteRoleFromUser(UserRoleEditModel userRole) { if (ModelState.IsValid) { Roles.RemoveUserFromRole(userRole.UserName, userRole.RoleName); } else { string errors = ModelState.Values.SelectMany(modelState => modelState.Errors).Aggregate(string.Empty, (current, error) => current + (error.ErrorMessage + Environment.NewLine)); throw new Exception(errors); } }
public ActionResult _Add() { var model = new UserRoleEditModel { Applications = Applications, Roles = Roles, Regions = Regions, Countries = Countries, Industries = Industries }; return(PartialView(model)); }
public async Task <IActionResult> Put([FromBody] UserRoleEditModel model) { var mapping = new Func <UserRole, Task <UserRole> >(async(entity) => { var organId = await _GetCurrentUserOrganId(); var organType = await _Repository._DbContext.Organizations.Where(x => x.Id == organId).Select(x => x.OrganizationTypeId).FirstOrDefaultAsync(); entity.Name = model.Name; entity.Description = model.Description; entity.Icon = model.IconAssetId; entity.ApplyOrgans = organType; entity.OrganizationId = organId; return(await Task.FromResult(entity)); }); return(await _PutRequest(model.Id, mapping)); }
public void AddRoleToUser(UserRoleEditModel userRole) { if (ModelState.IsValid) { try { //I tried using the Roles.AddUserToRole but I was getting a foreign key error. So I decided this way. if (Roles.IsUserInRole(userRole.UserName, userRole.RoleName)) { throw new Exception("User is already assigned that role"); } var repo = new Durandal2UserMaintContext(); var user = (from v in repo.UserProfiles select v).FirstOrDefault(x => x.UserName == userRole.UserName); if (user == null) { throw new Exception("User to update not found"); } else { var role = (from v in repo.webpages_Roles select v).FirstOrDefault(x => x.RoleName == userRole.RoleName); if (role == null) { throw new Exception("Role to use not found"); } else { user.webpages_Roles.Add(role); repo.SaveChanges(); } } } catch (Exception) { throw; } } else { string errors = ModelState.Values.SelectMany(modelState => modelState.Errors).Aggregate(string.Empty, (current, error) => current + (error.ErrorMessage + Environment.NewLine)); throw new Exception(errors); } }