public static void EncodePassword(User user) { if (user != null && user.UserAuthentications != null) { var passwords = user.UserAuthentications.FindAll(x => x.AuthenticationType == AuthenticationType.Password); if (passwords.Any()) { passwords.ForEach(x => x.AuthenticationData = SimpleEncryption.Encode(x.AuthenticationData)); } } }
public void UpdateUser(User systemUserInfo, UserInfo deviceUserInfo, DeviceController device) { if (systemUserInfo == null || device == null) { return; } systemUserInfo.Name = GetDeviceUserName(deviceUserInfo); systemUserInfo.Remark = deviceUserInfo.Comment; UpdateUserDepartment(systemUserInfo, deviceUserInfo.DepartmentId); Log.Info("Getting user authentication infos..."); var deviceID = device.DeviceID; var userAuthenticationsOfDevice = systemUserInfo.UserAuthentications.Where(a => a.DeviceID == deviceID); var authenticationsOfDevice = userAuthenticationsOfDevice as IList <UserAuthentication> ?? userAuthenticationsOfDevice.ToList(); var deviceUserId = systemUserInfo.UserCode.ToInt32(); var userAuthenticationFromDevice = new List <UserAuthentication>(); foreach (var service in deviceUserInfo.CredentialServices) { var userAuthentication = new UserAuthentication() { UserID = systemUserInfo.UserID, DeviceID = deviceID, DeviceUserID = deviceUserId, Status = GeneralStatus.Enabled, CreateDate = DateTime.Now, CreateUserID = GlobalSetting.DeviceSystemId, }; if (service is PasswordService) { var passwordService = service as PasswordService; var originalUserAuthentication = authenticationsOfDevice.FirstOrDefault(a => a.AuthenticationType == AuthenticationType.Password); userAuthentication.UserAuthenticationID = originalUserAuthentication != null ? originalUserAuthentication.UserAuthenticationID : 0; userAuthentication.AuthenticationType = AuthenticationType.Password; userAuthentication.AuthenticationData = SimpleEncryption.Encode(passwordService.Password); userAuthentication.IsDuress = passwordService.UseForDuress; } else if (service is CredentialCardService) { var credentialCardService = service as CredentialCardService; var originalUserAuthentication = authenticationsOfDevice.FirstOrDefault(a => a.AuthenticationType == AuthenticationType.IcCard); userAuthentication.UserAuthenticationID = originalUserAuthentication != null ? originalUserAuthentication.UserAuthenticationID : 0; userAuthentication.AuthenticationType = AuthenticationType.IcCard; userAuthentication.AuthenticationData = credentialCardService.CardNumber; userAuthentication.IsDuress = credentialCardService.UseForDuress; } else if (service is FingerPrintService) { var fpService = service as FingerPrintService; var originalUserAuthentication = authenticationsOfDevice.FirstOrDefault(a => a.AuthenticationType.GetHashCode() == fpService.Index); userAuthentication.UserAuthenticationID = originalUserAuthentication != null ? originalUserAuthentication.UserAuthenticationID : 0; userAuthentication.AuthenticationType = (AuthenticationType)fpService.Index; userAuthentication.AuthenticationData = fpService.FingerPrintData; userAuthentication.IsDuress = fpService.UseForDuress; } userAuthenticationFromDevice.Add(userAuthentication); } Log.Info("Sync user to database..."); var addedAuthentications = new List <UserAuthentication>(); var deletedAuthenticationIds = new List <int>(); if (userAuthenticationFromDevice.Any()) { var originalUserAuthenticationIDs = systemUserInfo.UserAuthentications.Select(d => d.UserAuthenticationID); var UserAuthenticationIDs = userAuthenticationFromDevice.Select(d => d.UserAuthenticationID); deletedAuthenticationIds = originalUserAuthenticationIDs.Except(UserAuthenticationIDs).ToList(); addedAuthentications = userAuthenticationFromDevice.FindAll(d => d.UserAuthenticationID == 0); } else { deletedAuthenticationIds = systemUserInfo.UserAuthentications.Select(d => d.UserAuthenticationID).ToList(); } userAuthenticationFromDevice.FindAll(d => d.UserAuthenticationID != 0).ForEach(d => { var auth = systemUserInfo.UserAuthentications.First(x => x.UserAuthenticationID == d.UserAuthenticationID); auth.AuthenticationData = d.AuthenticationData; _userAuthenticationRepo.Update(auth); Log.InfoFormat("User authentication id={0} updated", d.UserAuthenticationID); }); deletedAuthenticationIds.ForEach(d => { _userAuthenticationRepo.Delete(d); Log.InfoFormat("User authentication id={0} deleted", d); }); addedAuthentications.ForEach(d => { var auth = _userAuthenticationRepo.Insert(d); Log.InfoFormat("User authentication id={0} inserted", auth.UserAuthenticationID); }); _userRepo.Update(systemUserInfo); _userEventRepo.Insert(new UserEvent() { EventType = UserEventType.Modify, UserID = systemUserInfo.UserID, CreateDate = DateTime.Now, CreateUserID = GlobalSetting.DeviceSystemId, IsFinished = true, EventData = "Sync system user operation", }); }
public void AddUser(User systemUserInfo, UserInfo deviceUserInfo, DeviceController device) { var deviceID = device.DeviceID; var deviceUserId = systemUserInfo.UserCode.ToInt32(); systemUserInfo.Photo = "avator.jpg"; systemUserInfo.Type = UserType.Employee; systemUserInfo.UserCode = systemUserInfo.UserCode; systemUserInfo.Gender = GenderType.Male; systemUserInfo.Phone = ""; systemUserInfo.Status = GeneralStatus.Enabled; systemUserInfo.StartDate = DateTime.Now; systemUserInfo.EndDate = null; systemUserInfo.UserPropertyInfo.LastName = null; systemUserInfo.UserPropertyInfo.FirstName = null; systemUserInfo.UserPropertyInfo.Nationality = 0; systemUserInfo.UserPropertyInfo.NativePlace = null; systemUserInfo.UserPropertyInfo.Birthday = new DateTime(2000, 1, 1); systemUserInfo.UserPropertyInfo.Marriage = Marriage.Single; systemUserInfo.UserPropertyInfo.PoliticalStatus = null; systemUserInfo.UserPropertyInfo.Degree = null; systemUserInfo.UserPropertyInfo.HomeNumber = ""; systemUserInfo.UserPropertyInfo.EnglishName = ""; systemUserInfo.UserPropertyInfo.Company = ""; systemUserInfo.UserPropertyInfo.TechnicalTitle = ""; systemUserInfo.UserPropertyInfo.TechnicalLevel = ""; systemUserInfo.UserPropertyInfo.IDType = (int)IDType.ID; systemUserInfo.UserPropertyInfo.IDNumber = ""; systemUserInfo.UserPropertyInfo.SocialNumber = ""; systemUserInfo.UserPropertyInfo.Email = ""; systemUserInfo.UserPropertyInfo.Address = ""; systemUserInfo.UserPropertyInfo.Postcode = ""; systemUserInfo.UserPropertyInfo.Remark = ""; systemUserInfo.Name = GetDeviceUserName(deviceUserInfo); systemUserInfo.Remark = deviceUserInfo.Comment; UpdateUserDepartment(systemUserInfo, deviceUserInfo.DepartmentId); foreach (var service in deviceUserInfo.CredentialServices) { var userAuthentication = new UserAuthentication() { UserID = systemUserInfo.UserID, DeviceID = deviceID, DeviceUserID = deviceUserId, Status = GeneralStatus.Enabled, CreateDate = DateTime.Now, CreateUserID = GlobalSetting.DeviceSystemId, }; if (service is PasswordService) { var passwordService = service as PasswordService; userAuthentication.AuthenticationType = AuthenticationType.Password; userAuthentication.AuthenticationData = SimpleEncryption.Encode(passwordService.Password); userAuthentication.IsDuress = passwordService.UseForDuress; } else if (service is CredentialCardService) { var credentialCardService = service as CredentialCardService; userAuthentication.AuthenticationType = AuthenticationType.IcCard; userAuthentication.AuthenticationData = credentialCardService.CardNumber; userAuthentication.IsDuress = credentialCardService.UseForDuress; } else if (service is FingerPrintService) { var fpService = service as FingerPrintService; userAuthentication.AuthenticationType = (AuthenticationType)fpService.Index; userAuthentication.AuthenticationData = fpService.FingerPrintData; userAuthentication.IsDuress = fpService.UseForDuress; } systemUserInfo.UserAuthentications.Add(userAuthentication); } var userAuthenticationRepo = RepositoryManager.GetRepository <IUserAuthenticationRepository>(); var userPropertyRepo = RepositoryManager.GetRepository <IUserPropertyRepository>(); var userRepo = RepositoryManager.GetRepository <IUserRepository>(); var userEventRepo = RepositoryManager.GetRepository <IUserEventRepository>(); Log.Info("Sync user to database..."); userPropertyRepo.Insert(systemUserInfo.UserPropertyInfo); userRepo.Insert(systemUserInfo); EncodePassword(systemUserInfo); systemUserInfo.UserAuthentications.ForEach(a => a.UserID = systemUserInfo.UserID); systemUserInfo.UserAuthentications.ForEach(a => userAuthenticationRepo.Insert(a)); userEventRepo.Insert(new UserEvent() { EventType = UserEventType.Add, UserID = systemUserInfo.UserID, CreateDate = DateTime.Now, CreateUserID = GlobalSetting.DeviceSystemId, IsFinished = true, EventData = "Add user by sync system user operation", }); }