private void SaveUsers(string domain, bool updateExisting, UserCredential userCredential, out Exception exception) { exception = null; try { var service = new DirectoryService(new BaseClientService.Initializer { HttpClientInitializer = userCredential, ApplicationName = "Uintra" }); var request = service.Users.List(); request.Domain = domain; request.OrderBy = UsersResource.ListRequest.OrderByEnum.Email; var users = request.Execute().UsersValue.ToList(); foreach (var user in users) { var email = user.Emails.First().Address; var existingUser = _intranetUserService.GetByEmail(email); if (existingUser != null) { if (updateExisting) { var updateUserDto = user.Map <UpdateUserDto>(); updateUserDto.Id = existingUser.Id; updateUserDto.NewMedia = GetMediaId(user, service); _intranetUserService.Update(updateUserDto); } } else { var createUserDto = user.Map <CreateUserDto>(); createUserDto.MediaId = GetMediaId(user, service); _intranetUserService.Create(createUserDto); } } } catch (Exception e) { exception = e; } }
public virtual IHttpActionResult Create(CreateUserDto createModel) { if (ModelState.IsValid) { var user = _intranetUserService.GetByEmail(createModel.Email); if (user != null) { return(Conflict()); } var id = _intranetUserService.Create(createModel); return(Ok(id)); } else { return(BadRequest(GetModelErrors())); } }