Parcourir la source

First Working Rpc Mobile Prototype

Frank van den Bos il y a 2 ans
Parent
commit
271d75d66d

+ 1 - 0
InABox.Client.RPC/RPCClientSession.cs

@@ -9,5 +9,6 @@ namespace InABox.Rpc
         public Platform Platform { get; set; }
         public string? Version { get; set; }
         public string UserID { get; set; }
+        public Guid UserGuid { get; set; }
     }
 }

+ 29 - 6
InABox.Core/Security/Security.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.ComponentModel;
 using System.Linq;
 using System.Reflection;
 using System.Threading.Tasks;
@@ -27,7 +28,8 @@ namespace InABox.Core
                     {
                         var tokens = CoreUtils.TypeList(
                             AppDomain.CurrentDomain.GetAssemblies(),
-                            x => !x.IsAbstract && !x.IsGenericType && x.GetInterfaces().Any(i => i == typeof(ISecurityDescriptor))
+                            x => !x.IsAbstract && !x.IsGenericType &&
+                                 x.GetInterfaces().Any(i => i == typeof(ISecurityDescriptor))
                         );
                         foreach (var _class in tokens)
                         {
@@ -49,12 +51,12 @@ namespace InABox.Core
                         });
                         var edit = Task.Run(() =>
                         {
-                            foreach (var _class in tokens.Where(x=>x.GetCustomAttribute<AutoEntity>() == null))
+                            foreach (var _class in tokens.Where(x => x.GetCustomAttribute<AutoEntity>() == null))
                                 CheckAutoToken(_class, typeof(CanEdit<>));
                         });
                         var delete = Task.Run(() =>
                         {
-                            foreach (var _class in tokens.Where(x=>x.GetCustomAttribute<AutoEntity>() == null))
+                            foreach (var _class in tokens.Where(x => x.GetCustomAttribute<AutoEntity>() == null))
                                 CheckAutoToken(_class, typeof(CanDelete<>));
                         });
                         var issues = Task.Run(() =>
@@ -94,6 +96,28 @@ namespace InABox.Core
             _descriptors = null;
         }
 
+        public static void CheckTokens()
+        {
+            var tasks = new Task[] {
+                Task.Run(() =>
+                {
+                    _usertokens ??= new Client<UserSecurityToken>().Load(
+                        new Filter<UserSecurityToken>(x => x.User.ID).IsEqualTo(ClientFactory.UserGuid)
+                    );
+                }),
+                Task.Run(() =>
+                {
+                    _grouptokens ??= new Client<SecurityToken>().Load(
+                        new Filter<SecurityToken>(x => x.Group.ID).IsEqualTo(ClientFactory.UserSecurityID));
+                }),
+                Task.Run(() =>
+                {
+                    _globaltokens ??= new Client<GlobalSecurityToken>().Load();
+                }),
+            };
+            Task.WaitAll(tasks);
+        }
+        
         private static void CheckAutoToken(Type _class, Type type)
         {
             var basetype = typeof(AutoSecurityDescriptor<,>);
@@ -113,20 +137,19 @@ namespace InABox.Core
                 if (userGuid == Guid.Empty)
                     return false;
 
+                CheckTokens();
+                
                 // First Check for a matching User Token (override)
-                _usertokens ??= new Client<UserSecurityToken>().Load(new Filter<UserSecurityToken>(x => x.User.ID).IsEqualTo(userGuid));
                 var usertoken = _usertokens.FirstOrDefault(x => x.Descriptor.Equals(descriptor.Code));
                 if (usertoken != null)
                     return usertoken.Enabled;
 
                 // If not found, fall back to the Group Token
-                _grouptokens ??= new Client<SecurityToken>().Load(new Filter<SecurityToken>(x => x.Group.ID).IsEqualTo(securityId));
                 var grouptoken = _grouptokens.FirstOrDefault(x => x.Descriptor.Equals(descriptor.Code));
                 if (grouptoken != null)
                     return grouptoken.Enabled;
 
                 // Still not found? fall back to the Global Token
-                _globaltokens ??= new Client<GlobalSecurityToken>().Load();
                 var globaltoken = _globaltokens.FirstOrDefault(x => x.Descriptor.Equals(descriptor.Code));
                 if (globaltoken != null)
                     return globaltoken.Enabled;

+ 1 - 1
InABox.Core/Serialization.cs

@@ -697,7 +697,7 @@ namespace InABox.Core
             if (!type.IsSubclassOf(typeof(TObject)))
                 throw new Exception($"{type.EntityName()} is not a subclass of {typeof(TObject).EntityName()}");
 
-            var obj = (Activator.CreateInstance(type) as BaseObject)!;
+            var obj = (Activator.CreateInstance(type) as TObject)!;
             obj.SetObserving(false);
 
             var nProps = reader.ReadInt32();

+ 5 - 4
InABox.Mobile/Shared/Bluetooth.cs

@@ -22,7 +22,7 @@ namespace InABox.Mobile
         public String[] Devices { get; private set; }
         public int[] BatteryLevels { get; private set; }
         public DateTime TimeStamp { get; private set; }
-        public List<string> KnownBlueToothMACAddresses { get; set; }
+        public string[] KnownBlueToothMACAddresses { get; set; }
         public List<string> DetectedBlueToothMACAddresses { get; set; }
         public List<string> SavedBlueToothMACAddresses { get; set; }
 
@@ -64,7 +64,7 @@ namespace InABox.Mobile
             Devices = new String[] { };
             BatteryLevels = new int[] { };
             DetectedBlueToothMACAddresses = new List<string>();
-            KnownBlueToothMACAddresses = new List<string>();
+            KnownBlueToothMACAddresses = new String[] { };
             SavedBlueToothMACAddresses = new List<string>();
         }
 
@@ -164,7 +164,7 @@ namespace InABox.Mobile
                     else
                         return;
 
-                    if (KnownBlueToothMACAddresses.Count == 0)
+                    if (KnownBlueToothMACAddresses?.Any() == false)
                         return;
 
                     string deviceID = e.Device.NativeDevice.ToString();
@@ -238,7 +238,8 @@ namespace InABox.Mobile
                     else
                         return;
                 }
-
+                
+                DetectedBlueToothMACAddresses.Clear();
                 SavedBlueToothMACAddresses.Clear();
 
                 if (!bluetooth.IsAvailable)

+ 1 - 98
InABox.Mobile/Shared/MobileUtils.cs

@@ -58,111 +58,14 @@ namespace InABox.Mobile
 
         public static IAppVersion AppVersion { get { return DependencyService.Get<IAppVersion>(); } }
         
-        public static void LoadFromLink()
-        {
-            try
-            {
-                var arr = GetArray();
-                var array = arr.Item1;
-                var s = arr.Item2;
-                if (!CheckExpiry(array))
-                {
-                    App.Data.LoadFromLinkString = "Link has expired";
-                    return;
-                }
-
-                SetDBSettings(array, s);
-                SaveSettings();
-                RemoveSessionID();
-                
-                SetupClient();
-                
-            }
-            catch (Exception e)
-            {
-                if (String.IsNullOrWhiteSpace(App.DBSettings.URLs[0]))
-                    LoadDemoSettings(App.DBSettings);
-            }
-        }
-        
-        public static IRpcClientTransport Transport { get; private set; }
         
-        public static bool SetupClient()
-        {
-            if (!App.DBSettings.URLs.Any())
-                return false;
-
-            List<Task<String>> pings = new List<Task<String?>>();
-            foreach (var url in App.DBSettings.URLs)
-                pings.Add(Task.Run<String?>(() =>
-                {
-                    int iRetry = 0;
-                    while (!new RpcClientSocketTransport(url).Ping() && (iRetry < 10))
-                    {
-                        Thread.Sleep(1000);
-                        iRetry++;
-                    }
-                    return iRetry < 10 ? url : "";
-                }));
-            var index = Task.WaitAny(pings.ToArray<Task>());
-            var result = pings[index].Result;
-            
-            if (String.IsNullOrWhiteSpace(result))
-                return false;
-            
-            Transport = new RpcClientSocketTransport(result);
-            ClientFactory.SetClientType(typeof(RpcClient<>), InABox.Core.Platform.TimeBench, MobileUtils.AppVersion.InstalledVersionNumber + App.Data.DeviceString, Transport);
-            return true;
-        }
-
-        private static Tuple<string[], string> GetArray()
-        {
-            var s = Encryption.Decrypt(App.Data.LoadFromLinkString, "logindetailslink");
-            var index = s.IndexOf("ENDURLS");
-            var substring = s.Substring(index + 8, s.Length - index - 8);
-            return new Tuple<string[], string>(substring.Split(","), s);
-        }
-
-        private static bool CheckExpiry(string[] array)
-        {
-            DateTime expiry = DateTime.Parse(array[2]);
-            if (DateTime.Now.Subtract(expiry) < new TimeSpan(0, 5, 0))
-                return true;
-            else
-                return false;
-        }
-
-        private static void SetDBSettings(string[] array, string s)
-        {
-            var urls = s.Substring(0, s.IndexOf("ENDURLS") - 1);
-            App.DBSettings.URLs = urls.Split(",");
-            App.DBSettings.UserID = array[0];
-            App.DBSettings.Password = array[1];
-        }
-
-        private static void SaveSettings()
-        {
-            ClientFactory.InvalidateUser();
-            new LocalConfiguration<DatabaseSettings>().Save(App.DBSettings);
-            App.Data.ChangeUser = true;
-        }
-
+     
         private static void RemoveSessionID()
         {
             if (App.Current.Properties.ContainsKey("SessionID"))
                 App.Current.Properties.Remove("SessionID");
         }
 
-        public static void LoadDemoSettings(DatabaseSettings settings)
-        {
-            List<string> list = new List<string>();
-            list.Add("http://demo.prsdigital.com.au:8003");
-            list.Add("http://demo2.prsdigital.com.au:8003");
-            list.Add("http://demo3.prsdigital.com.au:8003");
-            settings.URLs = list.ToArray();
-            settings.UserID = "GUEST";
-            settings.Password = "guest";
-        }
 
 
         private static async Task<bool> Retry(Func<Task<bool>> action, int interval, int retryCount = 3)