public async Task <IActionResult> UpsertUser([FromBody] UpsertUserCmd request) { try { var result = await _service.UpsertUser(request); return(Ok(result)); } catch (BusinessLogicException ex) { return(BadRequest(new Response { Status = false, Message = ex.Message })); } catch (Exception e) { return(BadRequest(new Response { Status = false, Message = ErrorMessages.UnkownError //e.Message })); } }
public async Task <Response> UpsertUser(UpsertUserCmd request) { if (!string.IsNullOrEmpty(request.Id)) { var user = await _userManager.FindByIdAsync(request.Id); if (user == null) { throw new BusinessLogicException("خطای ناشناخته ای رخ داده است"); } user = _mapper.Map(request, user); // update user role var roles = await _userManager.GetRolesAsync(user); await _userManager.RemoveFromRolesAsync(user, roles); await _userManager.AddToRolesAsync(user, request.Roles); var result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { throw new Exception(); } } else { var user = new ApplicationUser(); var existingUser = await _userManager.FindByNameAsync(request.UserName); if (existingUser != null) { throw new BusinessLogicException("این نام کاربری قبلن استفاده شده است. نام کاربری دیگری را امتحان کنید"); } // user = await result.ProjectTo<ApplicationUser>(_mapper.ConfigurationProvider).First(); user = _mapper.Map <ApplicationUser>(request); // user = _mapper.Map(request, user); user.Id = Guid.NewGuid().ToString(); // user.UserName = request.UserName; // user.PasswordHash = request.Password; var res = await _userManager.CreateAsync(user, request.Password); if (!res.Succeeded) { throw new BusinessLogicException("خطای ناشناخته"); } await _userManager.AddToRolesAsync(user, request.Roles); } return(new Response { Status = true, Message = "success" }); }
public async Task <Response> UpsertUser(UpsertUserCmd request) { ///// var department = await _context.Departments .FirstOrDefaultAsync(x => x.Id == request.DepartmentId); if (department == null) { throw new BusinessLogicException("دپارتمان مورد تایید نمی باشد"); } ////// if (!string.IsNullOrEmpty(request.Id)) { var user = await _userManager.FindByIdAsync(request.Id); if (user == null) { throw new BusinessLogicException("کاربری برای ویرایش موجود نمی باشد"); } user = _mapper.Map(request, user); user.Department = department; // update user role var roles = await _userManager.GetRolesAsync(user); await _userManager.RemoveFromRolesAsync(user, roles); await _userManager.AddToRolesAsync(user, request.Roles); // removing all user claims var userClaims = await _userManager.GetClaimsAsync(user); var removeClaim = await _userManager.RemoveClaimsAsync(user, userClaims); if (!removeClaim.Succeeded) { throw new BusinessLogicException("خطای ناشناخته ای رخ داده است claims"); } // add claims to user var claims = ClaimStore.AllClaims.Where(x => request.Claims.Contains(x.Type)).ToList(); foreach (var claim in claims) { var resultAddition = await _userManager.AddClaimAsync(user, claim); if (!resultAddition.Succeeded) { throw new BusinessLogicException("user خطای ناشناخته ای رخ داده است"); } } var result = await _userManager.UpdateAsync(user); if (!result.Succeeded) { throw new Exception(); } } else { var user = new ApplicationUser(); var existingUser = await _userManager.FindByNameAsync(request.UserName); if (existingUser != null) { throw new BusinessLogicException("این نام کاربری استفاده شده است. نام کاربری دیگری را امتحان کنید"); } // user = await result.ProjectTo<ApplicationUser>(_mapper.ConfigurationProvider).First(); //user = _mapper.Map<ApplicationUser>(request); // user = _mapper.Map(request, user); user.FirstName = request.FirstName; user.Code = request.Code; user.LastName = request.LastName; await _userManager.AddToRolesAsync(user, request.Roles); user.Department = department; user.Id = Guid.NewGuid().ToString(); // user.UserName = request.UserName; // user.PasswordHash = request.Password; // add claims to user var claims = ClaimStore.AllClaims.Where(x => request.Claims.Contains(x.Type)).ToList(); foreach (var claim in claims) { var resultAddition = await _userManager.AddClaimAsync(user, claim); if (!resultAddition.Succeeded) { throw new BusinessLogicException("خطای ناشناخته ای رخ داده است claims"); } } var res = await _userManager.CreateAsync(user, request.Password); if (!res.Succeeded) { throw new BusinessLogicException("خطای ناشناخته"); } await _userManager.AddToRolesAsync(user, request.Roles); } return(new Response { Status = true, Message = "success" }); }