示例#1
0
 public static void DecodePassword(User user)
 {
     if (user != null && user.UserAuthentications != null)
     {
         var password = user.UserAuthentications.FirstOrDefault(x => x.AuthenticationType == AuthenticationType.Password);
         if (password != null)
         {
             password.AuthenticationData = SimpleEncryption.Decode(password.AuthenticationData);
         }
     }
 }
示例#2
0
        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));
            }
        }