From 047cb3514e5f497bb36747505ed667a6efd49f61 Mon Sep 17 00:00:00 2001 From: "Samuele E. Locatelli" Date: Fri, 29 Mar 2019 12:30:59 +0100 Subject: [PATCH] Completata user login... --- MConnectSDK/MConnectClient.cs | 87 +++++++++++++++++++++++++++-------- MConnectSDK/Utils.cs | 29 ++++++++++++ TestClient/MainForm.cs | 2 + 3 files changed, 99 insertions(+), 19 deletions(-) diff --git a/MConnectSDK/MConnectClient.cs b/MConnectSDK/MConnectClient.cs index 14f6992..dd29ee2 100644 --- a/MConnectSDK/MConnectClient.cs +++ b/MConnectSDK/MConnectClient.cs @@ -1039,9 +1039,8 @@ namespace MConnectSDK // inizio da watchdog... setWatchdog(); var userList = new UserPayload(); - List answ = userListMConnect(); - // salvo in cache locale (comunque...) - userListCache = answ; + updateUserListCacheFromCloud(); + List answ = userListCache; // aggiungo nella risposta... userList.UserList = answ; userList.Message = $"{answ.Count} Users Found!"; @@ -1061,7 +1060,6 @@ namespace MConnectSDK var taskCliInfo = Task.Run(() => GetClientStatusAsync()); taskCliInfo.Wait(); - // faccio la chiamata x elenco utenti... // parametri chiamata...SENZA /api... string shortBaseUrl = BaseUrl.Replace("/api", ""); @@ -1116,7 +1114,7 @@ namespace MConnectSDK // inizio da watchdog... setWatchdog(); var userList = new UserPayload(); - List answ = userListMConnect(); + List answ = userListCache; // filtro SOLO per stato (improted) var resFilt = answ.FindAll(X => X.LoginStatus == UserStatus.IMPORTED); if (resFilt.Count > 0) @@ -1151,7 +1149,7 @@ namespace MConnectSDK // inizio da watchdog... setWatchdog(); var userList = new UserPayload(); - List answ = userListMConnect(); + List answ = userListCache; // CERCO utente x settare importato... if (answ.Exists(x => x.Username == username)) { @@ -1222,9 +1220,13 @@ namespace MConnectSDK { // inizio da watchdog... setWatchdog(); + // controllo con la chiamata della clientInfo... + var taskCliInfo = Task.Run(() => GetClientStatusAsync()); + taskCliInfo.Wait(); + // parto pessimista... bool userOk = false; - // variabilia ccessorie + // variabili accessorie var userList = new UserPayload(); UserData answ = new UserData(); ErrorResult errore = new ErrorResult(); @@ -1233,8 +1235,53 @@ namespace MConnectSDK { // recupero ed aggiorno userListcache updateUserListCacheFromCloud(); - // cerco in utenti simulati che ci sia - UserData userFound = userListCache.Find(x => x.Username == username); + UserData userFound = new UserData(); + // se sono online --> login + if (reqStatus.CloudStatusOk) + { + //Uri callUri = new Uri(BaseUrl + $"/FAKE/auth/login"); + Uri callUri = new Uri(BaseUrl + $"/{organizationCode}/auth/login"); + var payload = "{\"Username\": \"" + username + "\",\"Password\": \"" + password + "\"}"; + HttpContent callCont = new StringContent(payload, Encoding.UTF8, "application/json"); + // effettuo call effettiva + var taskRes = Task.Run(() => Utils.PostURI(callUri, callCont)); + taskRes.Wait(); + var rawResult = taskRes.Result; + if (rawResult != "") + { + // deserializzo + var resp = JsonConvert.DeserializeObject(rawResult); + // controllo risultato: se 200 / status OK --> loggato! + if (resp.status == "Ok" || resp.statusCode == 200) + { + // creo obj userData... + Image userImg = null; + var item = resp.result.user; + userFound = new UserData() + { + UserId = item.Id, + IsAdmin = item.IsAdmin, + Username = item.Username, + Email = item.Email, + Cognome = item.Lastname, + Nome = item.Firstname, + UserImage = userImg + }; + } + else + { + userFound = null; + } + } + else + { } + } + // se sono offline --> cache locale! + else + { + // cerco in utenti che ci sia + userFound = userListCache.Find(x => x.Username == username); + } if (userFound != null) { // controllo che sia VALIDO e NON deleted... @@ -1357,17 +1404,19 @@ namespace MConnectSDK /// private void updateUserListCacheFromCloud() { - if (_currParam.testMode) - { - // non faccio nulla se non attesa... - Thread.Sleep(500); - } - else - { - // se NON modalità testing LEGGO davvero da cloud - // effettuo confronto e segno deleted i NON presenti (SE importati) con chaimata... - } + // se NON modalità testing LEGGO davvero da cloud la NUOVA lista + List answ = userListMConnect(); + +#if false + // salvo elenco corrente + var currUser = userListCache; + // possibile fare cancellazione "logica" per utenti improtati e NON + presenti... + +#endif + + // salvo in cache locale (comunque...) + userListCache = answ; } /// diff --git a/MConnectSDK/Utils.cs b/MConnectSDK/Utils.cs index 99a0f40..2ed0791 100644 --- a/MConnectSDK/Utils.cs +++ b/MConnectSDK/Utils.cs @@ -271,6 +271,35 @@ namespace MConnectSDK /// public List result; } + /// + /// Struttura dati risposta LOGIN UTENTE utenti + /// + public class userLoginResponse + { + /// + /// Stato della risposta + /// + public string status; + /// + /// Valore risposta + /// + public int statusCode; + /// + /// Messaggio della risposta + /// + public string message; + /// + /// Utente trovato + /// + public userLoginData result; + } + + public class userLoginData + { + public userData user { get; set; } + public string token { get; set; } + public string refreshToken { get; set; } + } public class userData { diff --git a/TestClient/MainForm.cs b/TestClient/MainForm.cs index e46e656..810dc35 100644 --- a/TestClient/MainForm.cs +++ b/TestClient/MainForm.cs @@ -397,6 +397,7 @@ namespace TestClient await Task.Run(() => { var userResult = MCC.getUserImportedListAsync(); + userResult.Wait(); List userList = ((UserPayload)userResult.Result.Payload).UserList; synchronizationContext.Post(new SendOrPostCallback(o => @@ -439,6 +440,7 @@ namespace TestClient { UserData userData = new UserData(); var userResult = MCC.TryUserLogin(user, pwd); + userResult.Wait(); var currImage = pictBox.Image; //if (userData.Username != "")