From 24f70d737276b3eb9b721bc694eb03944114daa6 Mon Sep 17 00:00:00 2001 From: Daniel Gyulai Date: Thu, 19 Nov 2020 11:36:14 +0100 Subject: [PATCH] Improved Auth, store registration Phone number --- CoviDok/Api/Objects/PublicAssistant.cs | 1 - CoviDok/Api/Objects/PublicDoctor.cs | 1 - CoviDok/Api/Objects/PublicParent.cs | 1 - CoviDok/BLL/User/Auth.cs | 10 +++++++--- CoviDok/Controllers/AuthController.cs | 16 ++++++++++++---- CoviDok/Data/Model/Assistant.cs | 2 -- CoviDok/Data/Model/Child.cs | 1 + CoviDok/Data/Model/Doctor.cs | 2 -- CoviDok/Data/Model/Parent.cs | 2 -- CoviDok/Data/Model/RoleUser.cs | 3 ++- CoviDok/Data/Model/User.cs | 2 -- 11 files changed, 22 insertions(+), 19 deletions(-) diff --git a/CoviDok/Api/Objects/PublicAssistant.cs b/CoviDok/Api/Objects/PublicAssistant.cs index 763731c..fc145ff 100644 --- a/CoviDok/Api/Objects/PublicAssistant.cs +++ b/CoviDok/Api/Objects/PublicAssistant.cs @@ -15,7 +15,6 @@ namespace CoviDok.Api.Objects public string Email { get; set; } public int ID { get; set; } public string PictureID { get; set; } - public Gender Gender { get; set; } public Role Role { get; set; } } } diff --git a/CoviDok/Api/Objects/PublicDoctor.cs b/CoviDok/Api/Objects/PublicDoctor.cs index ea9c5f8..4b6c083 100644 --- a/CoviDok/Api/Objects/PublicDoctor.cs +++ b/CoviDok/Api/Objects/PublicDoctor.cs @@ -14,7 +14,6 @@ namespace CoviDok.Api.Objects public string LastName { get; set; } public string PictureID { get; set; } public string Email { get; set; } - public Gender Gender { get; set; } public Role Role { get; set; } } diff --git a/CoviDok/Api/Objects/PublicParent.cs b/CoviDok/Api/Objects/PublicParent.cs index b57924c..f02009e 100644 --- a/CoviDok/Api/Objects/PublicParent.cs +++ b/CoviDok/Api/Objects/PublicParent.cs @@ -15,7 +15,6 @@ namespace CoviDok.Api.Objects public string Email { get; set; } public ICollection Children { get; set; } = new List(); public string PictureID { get; set; } - public Gender Gender { get; set; } public Role Role { get; set; } } } diff --git a/CoviDok/BLL/User/Auth.cs b/CoviDok/BLL/User/Auth.cs index fb064e2..ef797c7 100644 --- a/CoviDok/BLL/User/Auth.cs +++ b/CoviDok/BLL/User/Auth.cs @@ -81,7 +81,8 @@ namespace CoviDok.BLL.User LastName = registration.LastName, Email = registration.Email, Password = RoleUser.GetHashString(registration.Password), - Role = Role.Ast + Role = Role.Ast, + Phone = registration.Phone }; context.Assistants.Add(ast); await context.SaveChangesAsync(); @@ -93,7 +94,9 @@ namespace CoviDok.BLL.User LastName = registration.LastName, Email = registration.Email, Password = RoleUser.GetHashString(registration.Password), - Role = Role.Doc }; + Role = Role.Doc, + Phone = registration.Phone + }; context.Doctors.Add(doc); await context.SaveChangesAsync(); response.Body["id"] = doc.Id.ToString(); @@ -105,7 +108,8 @@ namespace CoviDok.BLL.User LastName = registration.LastName, Email = registration.Email, Password = RoleUser.GetHashString(registration.Password), - Role = Role.Par + Role = Role.Par, + Phone = registration.Phone }; context.Parents.Add(par); await context.SaveChangesAsync(); diff --git a/CoviDok/Controllers/AuthController.cs b/CoviDok/Controllers/AuthController.cs index 183649f..e24c372 100644 --- a/CoviDok/Controllers/AuthController.cs +++ b/CoviDok/Controllers/AuthController.cs @@ -6,6 +6,7 @@ using CoviDok.BLL.Sessions; using CoviDok.BLL.User; using CoviDok.Data.SessionProviders; using Microsoft.AspNetCore.Mvc; +using System.Collections.Generic; using System.Text.RegularExpressions; using System.Threading.Tasks; @@ -24,10 +25,17 @@ namespace CoviDok.Controllers [HttpPost("login")] public async Task> PostLogin(AuthLogin authLogin) { - AuthIdentity authIdentity = await Auth.AuthenticateUser(authLogin.Email, authLogin.Password); - if (authIdentity == null) return Unauthorized(); - authIdentity.SessionID = await Handler.CreateSession(authIdentity.Role, authIdentity.UserId); - return authIdentity; + try + { + AuthIdentity authIdentity = await Auth.AuthenticateUser(authLogin.Email, authLogin.Password); + if (authIdentity == null) return Unauthorized(); + authIdentity.SessionID = await Handler.CreateSession(authIdentity.Role, authIdentity.UserId); + return authIdentity; + } + catch (KeyNotFoundException) + { + return NotFound(); + } } // POST: /api/Auth/register diff --git a/CoviDok/Data/Model/Assistant.cs b/CoviDok/Data/Model/Assistant.cs index dd9f194..a171d66 100644 --- a/CoviDok/Data/Model/Assistant.cs +++ b/CoviDok/Data/Model/Assistant.cs @@ -17,7 +17,6 @@ namespace CoviDok.Data.Model Email = assistant.Email; PictureId = assistant.PictureID; DoctorId = assistant.DoctorId; - Gender = assistant.Gender; } public PublicAssistant ToPublic() { @@ -29,7 +28,6 @@ namespace CoviDok.Data.Model PictureID = PictureId, ID = Id, DoctorId = DoctorId, - Gender = Gender, Role = Api.Role.Ast }; } diff --git a/CoviDok/Data/Model/Child.cs b/CoviDok/Data/Model/Child.cs index 3674bfa..dd6130e 100644 --- a/CoviDok/Data/Model/Child.cs +++ b/CoviDok/Data/Model/Child.cs @@ -12,6 +12,7 @@ namespace CoviDok.Data.Model public int ParentId { get; set; } //public ICollection MedicalHistory { get; set; } = new List(); public string SSN { get; set; } + public Gender Gender { get; set; } public PublicChild ToPublic() { diff --git a/CoviDok/Data/Model/Doctor.cs b/CoviDok/Data/Model/Doctor.cs index dfa46ee..d6d7757 100644 --- a/CoviDok/Data/Model/Doctor.cs +++ b/CoviDok/Data/Model/Doctor.cs @@ -17,7 +17,6 @@ namespace CoviDok.Data.Model LastName = doctor.LastName; Email = doctor.Email; PictureId = doctor.PictureID; - Gender = doctor.Gender; } public PublicDoctor ToPublic() { @@ -28,7 +27,6 @@ namespace CoviDok.Data.Model Email = Email, PictureID = PictureId, ID = Id, - Gender = Gender, Role = Api.Role.Doc }; } diff --git a/CoviDok/Data/Model/Parent.cs b/CoviDok/Data/Model/Parent.cs index 45520b6..5a8c737 100644 --- a/CoviDok/Data/Model/Parent.cs +++ b/CoviDok/Data/Model/Parent.cs @@ -16,7 +16,6 @@ namespace CoviDok.Data.Model LastName = parent.LastName; Email = parent.Email; PictureId = parent.PictureID; - Gender = parent.Gender; } public PublicParent ToPublic() { @@ -26,7 +25,6 @@ namespace CoviDok.Data.Model Email = Email, PictureID = PictureId, ID = Id, - Gender = Gender, Role = Api.Role.Par }; foreach (Child child in Children) diff --git a/CoviDok/Data/Model/RoleUser.cs b/CoviDok/Data/Model/RoleUser.cs index 78411d9..2663ffe 100644 --- a/CoviDok/Data/Model/RoleUser.cs +++ b/CoviDok/Data/Model/RoleUser.cs @@ -13,7 +13,8 @@ namespace CoviDok.Data.Model public Role Role { get; set; } public string Email { get; set; } - + + public string Phone { get; set; } public string Password { get; set; } private static byte[] GetHash(string inputString) { diff --git a/CoviDok/Data/Model/User.cs b/CoviDok/Data/Model/User.cs index 37f0984..a6339ad 100644 --- a/CoviDok/Data/Model/User.cs +++ b/CoviDok/Data/Model/User.cs @@ -15,7 +15,5 @@ namespace CoviDok.Data.Model public DateTime RegistrationDate { get; set; } public string PictureId { get; set; } - - public Gender Gender { get; set; } } }