public IHttpActionResult SetOwner(SetRoleBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = _context.Users.SingleOrDefault(u => u.Id == model.AccountId); if (user == null) { return(BadRequest("The user have account id " + model.AccountId + " does not exist.")); } var owner = _context.Owners.SingleOrDefault(o => o.AccountId == user.Id); if (owner == null) { return(BadRequest("The user have account id " + model.AccountId + " is not an Owner.")); } var listRole = _userManager.GetRoles(user.Id); if (listRole.Count > 0) { _userManager.RemoveFromRoles(user.Id, listRole.ToArray()); } _userManager.AddToRole(user.Id, RoleName.Owner); return(Ok("Approved")); }
public IHttpActionResult RejectOwner(SetRoleBindingModel model) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } var user = _context.Users.SingleOrDefault(u => u.Id == model.AccountId); if (user == null) { return(BadRequest("The user have account id " + model.AccountId + " does not exist.")); } var owner = _context.Owners.SingleOrDefault(o => o.AccountId == user.Id); if (owner == null) { return(BadRequest("The user have account id " + model.AccountId + " is not an Owner.")); } if (!_userManager.IsInRole(model.AccountId, RoleName.WaitForConfirmation)) { return(BadRequest("User has been approved.")); } _context.Owners.Remove(owner); _context.SaveChanges(); _userManager.Delete(user); _context.SaveChanges(); return(Ok("Rejected")); }
public async Task <IHttpActionResult> SetRole(SetRoleBindingModel model) { if (model.UserId == null) { model.UserId = User.Identity.GetUserId(); Validate(model); } if (!ModelState.IsValid) { return(BadRequest(ModelState)); } IdentityResult result; if (await UserManager.IsInRoleAsync(model.UserId, model.Role)) { result = await UserManager.RemoveFromRoleAsync(model.UserId, model.Role); } else { result = await UserManager.AddToRoleAsync(model.UserId, model.Role); } if (!result.Succeeded) { return(GetErrorResult(result)); } return(Ok()); }