Exemplo n.º 1
0
        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",
            });
        }
Exemplo n.º 2
0
        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);
                }
            }
        }