private void SyncUserManager(IActiveDirectoryContext adContext, Guid syncBatchGUID, User user, ManagerUser managerUser) { var managerUsers = _managerUserService.GetManagerUser(managerUser); if (managerUsers.Count() > 0) { var syncStatus = new SyncStatus { ObjectGUID = user.UserGuid, ObjectName = user.Username, ObjectType = "ManagerUser", SyncBatchGUID = syncBatchGUID, SyncStatusText = "NOACTION", ErrorMessage = "Manager already exists" }; _syncStatusService.Insert(syncStatus); } else { _managerUserService.Insert(managerUser); var syncStatus = new SyncStatus { ObjectGUID = user.UserGuid, ObjectName = user.Username, ObjectType = "ManagerUser", SyncBatchGUID = syncBatchGUID, SyncStatusText = "INSERT" }; _syncStatusService.Insert(syncStatus); } //TODO: If user has manager other than specified, may need to be deleted. }
public void SyncUsersFromActiveDirectory(IActiveDirectoryContext adContext, Guid syncBatchGUID) { var listOfUsers = adContext.UserRepository.GetUsers(); var listOfUsernames = listOfUsers.Select(x => x.Username).ToList(); var usersToUpdate = _userRepository.GetUsersByUsername(listOfUsernames); foreach (var user in listOfUsers) { if (usersToUpdate.FirstOrDefault(x => String.Equals(x.Username, user.Username, StringComparison.OrdinalIgnoreCase)) != null) { var userToUpdate = usersToUpdate.FirstOrDefault(x => String.Equals(x.Username, user.Username, StringComparison.OrdinalIgnoreCase)); UpdateUser(user, userToUpdate, syncBatchGUID); } else { InsertUser(user, syncBatchGUID); } if (user.ManagerUser != null) { var u = _userRepository.GetUsersByUsername(new List <string> { user.Username }); var m = _userRepository.GetUsersByUsername(new List <string> { user.ManagerUser.Username }); if (u.Count() > 0 && m.Count() > 0) { if (!_managerUserService.IsManagerExists(u.SingleOrDefault().UserGuid, m.SingleOrDefault().UserGuid)) { try { _managerUserService.Insert(new ManagerUser { ManagerGUID = m.SingleOrDefault().UserGuid, UserGUID = u.SingleOrDefault().UserGuid }); var syncStatus = new SyncStatus { ObjectGUID = u.SingleOrDefault().UserGuid, ObjectName = u.SingleOrDefault().Username, ObjectType = "ManagerUser", SyncBatchGUID = syncBatchGUID, SyncStatusText = "INSERT" }; _syncStatusService.Insert(syncStatus); } catch (Exception ex) { var syncStatus = new SyncStatus { ObjectGUID = user.UserGuid, ObjectName = user.Username, ObjectType = "User", SyncBatchGUID = syncBatchGUID, SyncStatusText = "ERROR", ErrorMessage = ex.StackTrace }; _syncStatusService.Insert(syncStatus); } } else { var syncStatus = new SyncStatus { ObjectGUID = user.UserGuid, ObjectName = user.Username, ObjectType = "User", SyncBatchGUID = syncBatchGUID, SyncStatusText = "INVALID", ErrorMessage = "Unable to find manager to update" }; _syncStatusService.Insert(syncStatus); } } } else { var syncStatus = new SyncStatus { ObjectGUID = user.UserGuid, ObjectName = user.Username, ObjectType = "User", SyncBatchGUID = syncBatchGUID, SyncStatusText = "INVALID", ErrorMessage = "No Manager found for this user" }; _syncStatusService.Insert(syncStatus); } } }
public IActionResult Add([FromBody] UserViewModel model) { if (ModelState.IsValid) { try { if (!string.IsNullOrEmpty(model.MobilePhone) && !model.MobilePhone.isValidPhoneNumber()) { throw new Exception("Please enter valid mobile number."); } if (!string.IsNullOrEmpty(model.WorkPhone) && !model.WorkPhone.isValidPhoneNumber()) { throw new Exception("Please enter valid work phone number."); } if (!string.IsNullOrEmpty(model.HomePhone) && !model.HomePhone.isValidPhoneNumber()) { throw new Exception("Please enter valid home phone number."); } if (!string.IsNullOrEmpty(model.PersonalEmail) && !model.PersonalEmail.isValidPhoneNumber()) { throw new Exception("Please enter valid personal email address."); } var existingUsers = _userService.GetUsersByUsername(new string[] { model.Username }.ToList()); if (existingUsers.Count() > 0) { throw new Exception("Username is already in use. Please choose unique."); } var user = Mapper <UserViewModel, User> .Map(model); user.UserGuid = Guid.NewGuid(); var userId = _userService.Insert(user); if (model.Group != "" && Guid.Parse(model.Group) != Guid.Empty) { _groupUserService.Insert(new GroupUser { GroupGUID = Guid.Parse(model.Group), UserGUID = userId }); } if (model.Manager != "" && Guid.Parse(model.Manager) != Guid.Empty) { _managerUserService.Insert(new ManagerUser { ManagerGUID = Guid.Parse(model.Manager), UserGUID = userId }); } user = _userService.GetUserByUserGuid(userId); return(Ok(user)); } catch (Exception ex) { return(BadRequestFormatter.BadRequest(this, ex)); } } else { return(BadRequestFormatter.BadRequest(this, "Bad Request")); } }