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 From ea97f593f82a1176125901c257c8e81c45c23b67 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 23 Aug 2018 11:17:23 +0300 Subject: ObservablesContext is now LazyLoading = 'false' by default. --- .../ViewModels/MainViewVM.cs | 1 - .../ViewModels/MainViewVM.cs | 2 -- .../ViewModels/MainViewVM.cs | 3 --- .../ViewModels/EventsViewVM.cs | 1 - .../ViewModels/MainViewVM.cs | 1 - .../ViewModels/MainViewVM.cs | 3 --- .../EventLogging/DefaultEventLogger.cs | 1 - .../Authentication/DefaultAuthenticationProvider.cs | 1 - .../EventLogging/DefaultEventLogger.cs | 1 - Software/Visual_Studio/Tango.BL/ObservableEntity.cs | 21 ++++++++++++++++++++- .../Tango.BL/ObservablesContextExtension.cs | 1 + .../Tango.BL/ObservablesEntitiesAdapter.cs | 1 + .../Tango.BL/ObservablesStaticCollections.cs | 2 -- 13 files changed, 22 insertions(+), 17 deletions(-) (limited to 'Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs') diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs index 1811ea7c7..02f496b1f 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.ColorLab/ViewModels/MainViewVM.cs @@ -281,7 +281,6 @@ namespace Tango.MachineStudio.ColorLab.ViewModels Task.Factory.StartNew(() => { _dbContext = ObservablesContext.CreateDefault(); - _dbContext.Configuration.LazyLoadingEnabled = false; Machines = _dbContext.Machines.ToObservableCollection(); ColorSpaces = _dbContext.ColorSpaces.ToObservableCollection(); diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs index 6178ae30a..60c2e65f9 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Developer/ViewModels/MainViewVM.cs @@ -732,7 +732,6 @@ namespace Tango.MachineStudio.Developer.ViewModels LogManager.Log("Initializing machine Db context..."); _machineDbContext = ObservablesContext.CreateDefault(); - _machineDbContext.Configuration.LazyLoadingEnabled = false; if (_settings.LastSelectedMachineGuid != null) { @@ -1498,7 +1497,6 @@ namespace Tango.MachineStudio.Developer.ViewModels LogManager.Log("Creating active job DB context..."); _activeJobDbContext = ObservablesContext.CreateDefault(); - _activeJobDbContext.Configuration.LazyLoadingEnabled = false; LogManager.Log("Initializing available color spaces, RMLs & Winding methods..."); diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.HardwareDesigner/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.HardwareDesigner/ViewModels/MainViewVM.cs index 54b954745..6ba28cb72 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.HardwareDesigner/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.HardwareDesigner/ViewModels/MainViewVM.cs @@ -102,7 +102,6 @@ namespace Tango.MachineStudio.HardwareDesigner.ViewModels if (_db != null) _db.Dispose(); _db = ObservablesContext.CreateDefault(); - _db.Configuration.LazyLoadingEnabled = false; CurrentVersion.HardwareMotors = _db.HardwareMotorTypes.ToList().Select(x => new HardwareMotor() { HardwareMotorType = x }).ToObservableCollection(); CurrentVersion.HardwareDancers = _db.HardwareDancerTypes.ToList().Select(x => new HardwareDancer() { HardwareDancerType = x }).ToObservableCollection(); @@ -117,7 +116,6 @@ namespace Tango.MachineStudio.HardwareDesigner.ViewModels { using (var db = ObservablesContext.CreateDefault()) { - db.Configuration.LazyLoadingEnabled = false; _hardwareVersions = db.HardwareVersions.ToObservableCollection(); InvokeUI(() => { @@ -131,7 +129,6 @@ namespace Tango.MachineStudio.HardwareDesigner.ViewModels if (_db != null) _db.Dispose(); _db = ObservablesContext.CreateDefault(); - _db.Configuration.LazyLoadingEnabled = false; CurrentVersion = _db.Adapter.GetHardwareVersion(x => x.Guid == selectedVersion.Guid); diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs index 20eba4b8e..68094d91f 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Logging/ViewModels/EventsViewVM.cs @@ -138,7 +138,6 @@ namespace Tango.MachineStudio.Logging.ViewModels await Task.Factory.StartNew(() => { _db = ObservablesContext.CreateDefault(); - _db.Configuration.LazyLoadingEnabled = false; _db.EventTypes.Load(); _db.EventTypesCategories.Load(); diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs index 183cdb247..de9a8de4a 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/ViewModels/MainViewVM.cs @@ -218,7 +218,6 @@ namespace Tango.MachineStudio.MachineDesigner.ViewModels CanWork = false; _db = ObservablesContext.CreateDefault(); - _db.Configuration.LazyLoadingEnabled = false; Adapter = new ObservablesStaticCollections(); Adapter.ApplicationDisplayPanelVersions = _db.ApplicationDisplayPanelVersions.ToObservableCollection(); diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.UsersAndRoles/ViewModels/MainViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.UsersAndRoles/ViewModels/MainViewVM.cs index 0def388d3..db312b78f 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.UsersAndRoles/ViewModels/MainViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.UsersAndRoles/ViewModels/MainViewVM.cs @@ -199,7 +199,6 @@ namespace Tango.MachineStudio.UsersAndRoles.ViewModels await Task.Factory.StartNew(() => { _userContext = ObservablesContext.CreateDefault(); - _userContext.Configuration.LazyLoadingEnabled = false; Roles = _userContext.Roles.ToObservableCollection(); ManagedUser = _userContext.Adapter.GetUser(SelectedUser.Guid); @@ -256,7 +255,6 @@ namespace Tango.MachineStudio.UsersAndRoles.ViewModels await Task.Factory.StartNew(() => { _manageContext = ObservablesContext.CreateDefault(); - _manageContext.Configuration.LazyLoadingEnabled = false; ManagedOrganization = _manageContext.Adapter.GetOrganizationAndUsers(SelectedOrganization.Guid); }); @@ -270,7 +268,6 @@ namespace Tango.MachineStudio.UsersAndRoles.ViewModels Task.Factory.StartNew(() => { _organizationsContext = ObservablesContext.CreateDefault(); - _organizationsContext.Configuration.LazyLoadingEnabled = false; Organizations = _organizationsContext.Organizations .Include(x => x.Machines) diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs index c5b9c63cb..cb4611cad 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/EventLogging/DefaultEventLogger.cs @@ -81,7 +81,6 @@ namespace Tango.MachineStudio.Common.EventLogging try { _db = ObservablesContext.CreateDefault(); - _db.Configuration.LazyLoadingEnabled = false; _db.ActionTypes.ToList(); _db.EventTypesActions.ToList(); 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 de84e43e8..c16f76449 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs @@ -51,7 +51,6 @@ namespace Tango.MachineStudio.UI.Authentication { using (ObservablesContext db = ObservablesContext.CreateDefault()) { - db.Configuration.LazyLoadingEnabled = false; String hash = User.GetPasswordHash(password); diff --git a/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs b/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs index d981723be..adf99e3a3 100644 --- a/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs +++ b/Software/Visual_Studio/PPC/Tango.PPC.Common/EventLogging/DefaultEventLogger.cs @@ -80,7 +80,6 @@ namespace Tango.PPC.Common.EventLogging try { _db = ObservablesContext.CreateDefault(); - _db.Configuration.LazyLoadingEnabled = false; _db.ActionTypes.ToList(); _db.EventTypesActions.ToList(); diff --git a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs index d6cdcfb16..1b69a8bdb 100644 --- a/Software/Visual_Studio/Tango.BL/ObservableEntity.cs +++ b/Software/Visual_Studio/Tango.BL/ObservableEntity.cs @@ -157,7 +157,26 @@ namespace Tango.BL /// public virtual void Save(ObservablesContext context) { - context.SaveChanges(); + if (context == ObservablesEntitiesAdapter.Instance.Context) + { + var tableName = this.GetType().GetCustomAttribute().Name; + + String propName = tableName.FromDalNameToTitleCase(); + + DbSet set = ObservablesEntitiesAdapter.Instance.Context.GetType().GetProperty(propName, BindingFlags.Instance | BindingFlags.Public).GetValue(context) as DbSet; + ObservableCollection obs = ObservablesEntitiesAdapter.Instance.GetType().GetProperty(propName, BindingFlags.Instance | BindingFlags.Public).GetValue(ObservablesEntitiesAdapter.Instance) as ObservableCollection; + + if (!obs.Contains(this as T)) + { + set.Add(this as T); + } + + ObservablesEntitiesAdapter.Instance.SaveChanges(); + } + else + { + context.SaveChanges(); + } } /// diff --git a/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs b/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs index 49c80695f..f387bd1e1 100644 --- a/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs +++ b/Software/Visual_Studio/Tango.BL/ObservablesContextExtension.cs @@ -34,6 +34,7 @@ namespace Tango.BL public ObservablesContext(DataSource dataSource) : base(dataSource.ToConnection(), true) { Database.SetInitializer(null); + Configuration.LazyLoadingEnabled = false; _adapter = new ObservablesContextAdapter(this); } diff --git a/Software/Visual_Studio/Tango.BL/ObservablesEntitiesAdapter.cs b/Software/Visual_Studio/Tango.BL/ObservablesEntitiesAdapter.cs index fdc359da0..4fdaf9071 100644 --- a/Software/Visual_Studio/Tango.BL/ObservablesEntitiesAdapter.cs +++ b/Software/Visual_Studio/Tango.BL/ObservablesEntitiesAdapter.cs @@ -69,6 +69,7 @@ namespace Tango.BL private ObservablesEntitiesAdapter() { Context = ObservablesContext.CreateDefault(); + Context.Configuration.LazyLoadingEnabled = true; } /// diff --git a/Software/Visual_Studio/Tango.BL/ObservablesStaticCollections.cs b/Software/Visual_Studio/Tango.BL/ObservablesStaticCollections.cs index db5faef4b..4820c784b 100644 --- a/Software/Visual_Studio/Tango.BL/ObservablesStaticCollections.cs +++ b/Software/Visual_Studio/Tango.BL/ObservablesStaticCollections.cs @@ -42,8 +42,6 @@ namespace Tango.BL { db = ObservablesContext.CreateDefault(); - db.Configuration.LazyLoadingEnabled = false; - WindingMethods = db.WindingMethods.ToObservableCollection(); ColorSpaces = db.ColorSpaces.ToObservableCollection(); SpoolTypes = db.SpoolTypes.ToObservableCollection(); -- cgit v1.3.1