public void AddUser(User user, DeviceController device) { if (user == null || device == null) { return; } if (user.UserAuthentications == null || user.UserAuthentications.Count == 0) { return; } if (user.GetUserAccessableDeviceIds().Contains(device.DeviceID) == false) { return; } var deviceID = device.DeviceID; var deviceCode = device.Code.ToInt32(); Log.Info("Getting user authentication infos..."); var userAuthenticationsOfDevice = user.UserAuthentications.Where(a => a.DeviceID == deviceID); var authenticationsOfDevice = userAuthenticationsOfDevice as IList <UserAuthentication> ?? userAuthenticationsOfDevice.ToList(); Log.Info("Getting user permission infos..."); var deviceRoles = _deviceRole.Query(new Hashtable { { "Status", (int)GeneralStatus.Enabled } }).ToList(); var userDevicePermission = user.GetUserDeviceRoleAuthorizedPermissionByDeviceId(deviceID, deviceRoles); Log.Info("Building device user..."); var deviceUser = new UserInfo(); deviceUser.UserId = user.UserCode.ToInt32(); deviceUser.ExternalUserCode = user.UserID.ToString(); // user info deviceUser.UserName = user.Name; deviceUser.UserStatus = user.Status == GeneralStatus.Enabled; deviceUser.DepartmentId = user.DepartmentID; deviceUser.Comment = user.Remark; // user role deviceUser.Role = (Rld.DeviceSystem.Contract.Model.UserRole)userDevicePermission.PermissionAction.GetHashCode(); deviceUser.AccessTimeZoneId = userDevicePermission.AllowedAccessTimeZoneID; //user authentication foreach (var userAuthentication in authenticationsOfDevice) { switch (userAuthentication.AuthenticationType) { case AuthenticationType.FingerPrint1: case AuthenticationType.FingerPrint2: case AuthenticationType.FingerPrint3: case AuthenticationType.FingerPrint4: case AuthenticationType.FingerPrint5: case AuthenticationType.FingerPrint6: case AuthenticationType.FingerPrint7: case AuthenticationType.FingerPrint8: case AuthenticationType.FingerPrint9: case AuthenticationType.FingerPrint10: { var service = new FingerPrintService() { Index = (int)userAuthentication.AuthenticationType, Enabled = true }; service.FingerPrintData = userAuthentication.AuthenticationData; service.UseForDuress = userAuthentication.IsDuress; deviceUser.CredentialServices.Add(service); } break; case AuthenticationType.Password: { var service = new PasswordService() { Enabled = true }; service.Password = SimpleEncryption.Decode(userAuthentication.AuthenticationData); service.UseForDuress = userAuthentication.IsDuress; deviceUser.CredentialServices.Add(service); } break; case AuthenticationType.IcCard: { var service = new CredentialCardService() { Enabled = true }; service.CardNumber = userAuthentication.AuthenticationData; service.UseForDuress = userAuthentication.IsDuress; deviceUser.CredentialServices.Add(service); } break; default: break; } } Log.Info("Invoke WebSocketOperation..."); var operation = new WebSocketOperation(deviceCode); var createUserInfoRequest = new CreateUserInfoRequest() { Token = operation.Token, UserInfo = deviceUser }; string rawRequest = DataContractSerializationHelper.Serialize(createUserInfoRequest); var rawResponse = operation.Execute(rawRequest); if (string.IsNullOrWhiteSpace(rawResponse)) { throw new Exception(string.Format("Create user id:[{0}], device user id:[{1}] to device id:[{2}] fails. Response is empty, maybe the device is not register to device system.", user.UserID, deviceUser.UserId, deviceID)); } var response = DataContractSerializationHelper.Deserialize <CreateUserInfoResponse>(rawResponse); Log.InfoFormat("Create user id:[{0}], device user id:[{1}] to device id:[{2}], result:[{3}]", user.UserID, deviceUser.UserId, deviceID, response.ResultType); if (response.ResultType != ResultType.OK) { throw new Exception(string.Format("Create user id:[{0}], device user id:[{1}] to device id:[{2}] fails.", user.UserID, deviceUser.UserId, deviceID)); } }
private void AddUserMS(User user, DeviceController deviceInfo, DeviceRole role) { var deviceId = deviceInfo.DeviceID; var tryGetAuthentication = user.UserAuthentications.FirstOrDefault(x => x.DeviceID != deviceId); if (tryGetAuthentication != null) { var otherDeviceId = tryGetAuthentication.DeviceID; var otherdeviceAuthentications = user.UserAuthentications.FindAll(x => x.DeviceID == otherDeviceId); var devicePermission = role.DeviceRolePermissions.FirstOrDefault(x => x.DeviceID == deviceId); Log.Info("Building device user..."); var deviceUser = new UserInfo(); deviceUser.UserId = tryGetAuthentication.DeviceUserID; // don't know how to determine new the device user id deviceUser.ExternalUserCode = user.UserID.ToString(); deviceUser.UserName = user.Name; deviceUser.UserStatus = user.Status == GeneralStatus.Enabled; deviceUser.DepartmentId = user.DepartmentID; deviceUser.Comment = user.Remark; deviceUser.Role = (Rld.DeviceSystem.Contract.Model.UserRole)devicePermission.PermissionAction.GetHashCode(); deviceUser.AccessTimeZoneId = devicePermission.AllowedAccessTimeZoneID; foreach (AuthenticationType type in Enum.GetValues(typeof(AuthenticationType))) { switch (type) { case AuthenticationType.FingerPrint1: case AuthenticationType.FingerPrint2: case AuthenticationType.FingerPrint3: case AuthenticationType.FingerPrint4: case AuthenticationType.FingerPrint5: case AuthenticationType.FingerPrint6: case AuthenticationType.FingerPrint7: case AuthenticationType.FingerPrint8: case AuthenticationType.FingerPrint9: case AuthenticationType.FingerPrint10: { var service = new FingerPrintService() { Index = (int)type, Enabled = false }; var userAuthentication = otherdeviceAuthentications.FirstOrDefault(a => a.AuthenticationType == type); if (userAuthentication != null) { service.Enabled = true; service.FingerPrintData = userAuthentication.AuthenticationData; service.UseForDuress = userAuthentication.IsDuress; } deviceUser.CredentialServices.Add(service); } break; case AuthenticationType.Password: { var service = new PasswordService() { Enabled = false }; var userAuthentication = otherdeviceAuthentications.FirstOrDefault(a => a.AuthenticationType == type); if (userAuthentication != null) { service.Enabled = true; service.Password = userAuthentication.AuthenticationData; service.UseForDuress = userAuthentication.IsDuress; } deviceUser.CredentialServices.Add(service); } break; case AuthenticationType.IcCard: { var service = new CredentialCardService() { Enabled = false }; var userAuthentication = otherdeviceAuthentications.FirstOrDefault(a => a.AuthenticationType == type); if (userAuthentication != null) { service.Enabled = true; service.CardNumber = userAuthentication.AuthenticationData; service.UseForDuress = userAuthentication.IsDuress; } deviceUser.CredentialServices.Add(service); } break; //case AuthenticationType.FacePrint: // break; default: break; } } Log.Info("Invoke WebSocketOperation..."); var operation = new WebSocketOperation(deviceId); var createUserInfoRequest = new CreateUserInfoRequest() { Token = operation.Token, UserInfo = deviceUser }; string rawRequest = DataContractSerializationHelper.Serialize(createUserInfoRequest); Log.DebugFormat("Request: {0}", rawRequest); var rawResponse = operation.Execute(rawRequest); Log.DebugFormat("Response: {0}", rawResponse); var response = DataContractSerializationHelper.Deserialize <UpdateUserInfoResponse>(rawResponse); Log.InfoFormat("Update user id:[{0}], device user id:[{1}] to device id:[{2}], result:[{3}]", user.UserID, deviceUser.UserId, deviceId, response.ResultType); if (response.ResultType != ResultType.OK) { throw new Exception(string.Format("Update user id:[{0}], device user id:[{1}] to device id:[{2}] fails]", user.UserID, deviceUser.UserId, deviceId)); } Log.Info("Adding UserAuthentications from database..."); foreach (var au in otherdeviceAuthentications) { var a = au.WiseClone(); a.DeviceID = deviceId; a.CreateDate = DateTime.Now; a.CreateUserID = SyncUserID; _userAuthenticationRepo.Insert(a); } } }