From 3cc84dc72d12f77c756b9bff5b040a7ad3f5d668 Mon Sep 17 00:00:00 2001 From: michele Date: Fri, 19 Sep 2025 11:50:06 +0200 Subject: [PATCH] =?UTF-8?q?Aggiunti=20controlli=20univocit=C3=A0=20usernam?= =?UTF-8?q?e=20e=20pw=20in=20Registrazioni=20e=20Insert/Update=20Tecnici?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Controllers/RegistrazioniController.cs | 40 +++++++++++++++++- Controllers/TecniciController.cs | 57 +++++++++++++++++--------- 2 files changed, 76 insertions(+), 21 deletions(-) diff --git a/Controllers/RegistrazioniController.cs b/Controllers/RegistrazioniController.cs index bd0c6af..bab114d 100644 --- a/Controllers/RegistrazioniController.cs +++ b/Controllers/RegistrazioniController.cs @@ -124,6 +124,12 @@ namespace VirtualTask.Controllers bool bAziPres = false; bool privacy1=false; + Controllo cUtePres = isLoginPresente(model.username, model.passwd, helper); + if (cUtePres.isPresente) + { + ModelState.AddModelError("tcuser", "Login già utilizzata. Scegliere un nome utente differente"); + } + bAziPres = checkAziendaPresente(model.azienda); if (bAziPres) { @@ -145,7 +151,7 @@ namespace VirtualTask.Controllers privacy1 = true; } - if (!bAziPres && bEmail && !privacy1) + if (!bAziPres && bEmail && !privacy1 && !cUtePres.isPresente) { urlBase = apiUrl + "registrazioni/add"; Uri baseAddress = new Uri(urlBase); @@ -714,6 +720,38 @@ namespace VirtualTask.Controllers return txt; } + public Controllo isLoginPresente(string user, string pwd, SessionHelper helper) + { + Controllo c = new Controllo(); + c.isPresente = false; + + urlBase = apiUrl + "tecniciListAll"; + urlBase = urlBase + "?usr=" + user; + urlBase = urlBase + "&pwd=" + pwd; + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + + List modelList = new List(); + + HttpResponseMessage response = client.GetAsync(baseAddress).Result; + + if (response.IsSuccessStatusCode) + { + string data = response.Content.ReadAsStringAsync().Result; + modelList = JsonConvert.DeserializeObject>(data); + if (modelList != null && modelList.Count > 0) + c.isPresente = true; + } + else + { + errMes = response.Content.ReadAsStringAsync().Result; + helper.SetStringValue("errMsg", errMes); + c.errMsg = errMes; + } + return c; + } + private static Random random = new Random(); public static string RandomString(int length) diff --git a/Controllers/TecniciController.cs b/Controllers/TecniciController.cs index 45aa45d..ad62b54 100644 --- a/Controllers/TecniciController.cs +++ b/Controllers/TecniciController.cs @@ -270,32 +270,48 @@ namespace VirtualTask.Controllers { return RedirectToAction("Login2", "Login"); } - tecnico.tccodazi = tenant; - apiUrl = helper.GetStringValue("apiUrl"); - admin = helper.GetStringValue("admin"); - ViewBag.Admin = admin; - urlBase = apiUrl + "tecnici/mod"; - urlBase = urlBase + "?token=" + token; - Uri baseAddress = new Uri(urlBase); - client = new HttpClient(); - client.BaseAddress = baseAddress; - - string data = JsonConvert.SerializeObject(tecnico); - StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); - HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; - - if (response.IsSuccessStatusCode) + //step 1 prima controllo che le credenziali di login non siano già presenti. + //per fare questo chiamo il metodo api tecniciListAll + Controllo c = isLoginPresente(tecnico.tcuser, tecnico.tcpwd, helper); + if (c.isPresente) { - return RedirectToAction("Index"); + ModelState.AddModelError("tcuser", "Login già utilizzata. Scegliere un nome utente differente"); + } + + if (!c.isPresente) + { + tecnico.tccodazi = tenant; + + apiUrl = helper.GetStringValue("apiUrl"); + admin = helper.GetStringValue("admin"); + ViewBag.Admin = admin; + urlBase = apiUrl + "tecnici/mod"; + urlBase = urlBase + "?token=" + token; + Uri baseAddress = new Uri(urlBase); + client = new HttpClient(); + client.BaseAddress = baseAddress; + + string data = JsonConvert.SerializeObject(tecnico); + StringContent content = new StringContent(data, Encoding.UTF8, "application/json"); + HttpResponseMessage response = client.PostAsync(baseAddress, content).Result; + + if (response.IsSuccessStatusCode) + { + return RedirectToAction("Index"); + } + else + { + errMes = response.Content.ReadAsStringAsync().Result; + helper.SetStringValue("errMsg", errMes); + return RedirectToAction("Error"); + } + //return View(tecnico); } else { - errMes = response.Content.ReadAsStringAsync().Result; - helper.SetStringValue("errMsg", errMes); - return RedirectToAction("Error"); + return View(tecnico); } - //return View(tecnico); } #endregion EDIT @@ -384,6 +400,7 @@ namespace VirtualTask.Controllers string e = helper.GetStringValue("errMsg"); return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier, ErrMsg = e }); } + public Controllo isLoginPresente(string user, string pwd, SessionHelper helper) { Controllo c=new Controllo();