From 51afc4f6a17383e91a72c2ce060e82604d43c3a8 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Mon, 20 Aug 2018 19:43:15 +0300 Subject: Working on new Machine Studio DB. --- .../DefaultAuthenticationProvider.cs | 44 +++++++++++++--------- 1 file changed, 26 insertions(+), 18 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs') diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs index 4e5425138..de84e43e8 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs @@ -9,6 +9,7 @@ using Tango.BL.Entities; using Tango.MachineStudio.Common.Authentication; using Tango.BL; using Tango.BL.Enumerations; +using System.Data.Entity; namespace Tango.MachineStudio.UI.Authentication { @@ -48,32 +49,39 @@ namespace Tango.MachineStudio.UI.Authentication /// Login failed for user " + email public User Login(string email, string password) { - String hash = User.GetPasswordHash(password); + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { + db.Configuration.LazyLoadingEnabled = false; - User user = ObservablesEntitiesAdapter.Instance.Users.SingleOrDefault(x => x.Email.ToLower() == email.ToLower() && x.Password == hash); + String hash = User.GetPasswordHash(password); - if (user == null) - { - throw new AuthenticationException("Invalid credentials for " + email); - } + db.Roles.Load(); + db.Permissions.Load(); + db.RolesPermissions.Load(); - if (!user.HasPermission(Permissions.RunMachineStudio)) - { - throw new AuthenticationException("It seems like you do not have sufficient privileges to run Machine Studio. Please contact your administrator."); - } + User user = db.Users + .Include(x => x.UsersRoles) + .Include(x => x.Organization).SingleOrDefault(x => x.Email.ToLower() == email.ToLower() && x.Password == hash); - if (user != null) - { - using (ObservablesContext db = ObservablesContext.CreateDefault()) + if (user == null) + { + throw new AuthenticationException("Invalid credentials for " + email); + } + + if (!user.HasPermission(Permissions.RunMachineStudio)) { - var u = db.Users.Single(x => x.Guid == user.Guid); - u.LastLogin = DateTime.UtcNow; + throw new AuthenticationException("It seems like you do not have sufficient privileges to run Machine Studio. Please contact your administrator."); + } + + if (user != null) + { + user.LastLogin = DateTime.UtcNow; db.SaveChanges(); } - } - CurrentUser = user; - return user; + CurrentUser = user; + return user; + } } /// -- cgit v1.3.1