From e0b0859f62924d38c8cd7ac9975303c4bfb08624 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Thu, 20 Dec 2018 14:24:19 +0200 Subject: Added environments support for users & roles !! --- .../Controllers/MachineStudioController.cs | 2 +- .../Controllers/PPCController.cs | 37 ++++++++++++++++------ 2 files changed, 28 insertions(+), 11 deletions(-) (limited to 'Software/Visual_Studio/Web/Tango.MachineService') diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs index 245c18b9b..ed1a807be 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs @@ -100,7 +100,7 @@ namespace Tango.MachineService.Controllers var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password); - if (user != null && user.HasPermission(Permissions.PublishMachineStudioVersion)) + if (user != null && user.HasPermission(Permissions.PublishMachineStudioVersions)) { var latestVersion = db.MachineStudioVersions.ToList().OrderByDescending(x => Version.Parse(x.Version)).FirstOrDefault(); Version currentVersion = Version.Parse(request.Version); diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs index 9ce22bbb0..662883223 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs @@ -18,6 +18,7 @@ using Tango.BL; using Tango.BL.Builders; using Tango.BL.Entities; using Tango.BL.Enumerations; +using Tango.Core; using Tango.Core.DB; using Tango.Core.Helpers; using Tango.Core.IO; @@ -101,9 +102,15 @@ namespace Tango.MachineService.Controllers }); } - response.DbAddress = Config.DB_ADDRESS; - response.DbUserName = credentials.UserName; - response.DbPassword = credentials.Password; + response.DataSource = new DataSource() + { + Address = Config.DB_ADDRESS, + Catalog = Config.DB_CATALOG, + UserName = credentials.UserName, + Password = credentials.Password, + Type = DataSourceType.SQLServer, + }; + response.OSKey = machine.OsKey; response.SetupActivation = machine.SetupActivation; response.SetupRemoteAssistance = machine.SetupRemoteAssistance; @@ -160,9 +167,14 @@ namespace Tango.MachineService.Controllers }); } - response.DbAddress = Config.DB_ADDRESS; - response.DbUserName = credentials.UserName; - response.DbPassword = credentials.Password; + response.DataSource = new DataSource() + { + Address = Config.DB_ADDRESS, + Catalog = Config.DB_CATALOG, + UserName = credentials.UserName, + Password = credentials.Password, + Type = DataSourceType.SQLServer, + }; } return response; @@ -231,9 +243,14 @@ namespace Tango.MachineService.Controllers }); } - response.DbAddress = Config.DB_ADDRESS; - response.DbUserName = credentials.UserName; - response.DbPassword = credentials.Password; + response.DataSource = new DataSource() + { + Address = Config.DB_ADDRESS, + Catalog = Config.DB_CATALOG, + UserName = credentials.UserName, + Password = credentials.Password, + Type = DataSourceType.SQLServer, + }; } return response; @@ -290,7 +307,7 @@ namespace Tango.MachineService.Controllers var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password); - if (user != null && user.HasPermission(Permissions.PublishMachineStudioVersion)) + if (user != null && user.HasPermission(Permissions.PublishPPCVersions)) { var versions = db.TangoVersions.ToList().Where(x => x.MachineVersionGuid == request.MachineVersionGuid).OrderByDescending(x => Version.Parse(x.Version)).ToList(); -- cgit v1.3.1 From 5bec920df45bb79e5912a97f2d0afc1a849adbd2 Mon Sep 17 00:00:00 2001 From: Roy Ben-Shabat Date: Sun, 23 Dec 2018 09:34:03 +0200 Subject: Fixed some issues with observables generator. --- Software/DB/Tango.mdf | Bin 75497472 -> 75497472 bytes Software/DB/Tango_log.ldf | Bin 20578304 -> 20578304 bytes .../Authentication/LoginRequest.cs | 15 ++ .../Authentication/LoginResponse.cs | 15 ++ .../Tango.MachineStudio.Common.csproj | 2 + .../Visual_Studio/Tango.BL/Entities/Machine.cs | 31 ----- Software/Visual_Studio/Tango.BL/Entities/Rml.cs | 30 ---- .../Components/DataBaseDescriptionsHelper.cs | 31 +++++ .../Visual_Studio/Tango.DAL.Remote/DB/MACHINE.cs | 1 - Software/Visual_Studio/Tango.DAL.Remote/DB/RML.cs | 3 - .../Tango.DAL.Remote/DB/RemoteADO.edmx | 46 +------ .../Tango.DAL.Remote/DB/RemoteADO.edmx.diagram | 153 ++++++++++----------- .../ObservablesGenerator.cs | 38 ++++- .../Controllers/MachineStudioController.cs | 60 +++++++- .../Tango.MachineService.csproj | 6 + .../Web/Tango.MachineService/Web.config | 4 + .../Web/Tango.MachineService/packages.config | 1 + 17 files changed, 251 insertions(+), 185 deletions(-) create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginRequest.cs create mode 100644 Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginResponse.cs (limited to 'Software/Visual_Studio/Web/Tango.MachineService') diff --git a/Software/DB/Tango.mdf b/Software/DB/Tango.mdf index 952f63196..810f4b5c0 100644 Binary files a/Software/DB/Tango.mdf and b/Software/DB/Tango.mdf differ diff --git a/Software/DB/Tango_log.ldf b/Software/DB/Tango_log.ldf index 24819348d..9281ede10 100644 Binary files a/Software/DB/Tango_log.ldf and b/Software/DB/Tango_log.ldf differ diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginRequest.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginRequest.cs new file mode 100644 index 000000000..762fb5dd5 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginRequest.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Transport.Web; + +namespace Tango.MachineStudio.Common.Authentication +{ + public class LoginRequest : WebRequestMessage + { + public String Email { get; set; } + public String Password { get; set; } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginResponse.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginResponse.cs new file mode 100644 index 000000000..075a5ec10 --- /dev/null +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Authentication/LoginResponse.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Transport.Web; + +namespace Tango.MachineStudio.Common.Authentication +{ + public class LoginResponse : WebResponseMessage + { + public DataSource DataSource { get; set; } + } +} diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj index 31d28def8..ca4d95805 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj @@ -75,6 +75,8 @@ GlobalVersionInfo.cs + + diff --git a/Software/Visual_Studio/Tango.BL/Entities/Machine.cs b/Software/Visual_Studio/Tango.BL/Entities/Machine.cs index 92ed46850..38926d3e4 100644 --- a/Software/Visual_Studio/Tango.BL/Entities/Machine.cs +++ b/Software/Visual_Studio/Tango.BL/Entities/Machine.cs @@ -80,8 +80,6 @@ namespace Tango.BL.Entities public event EventHandler DefaultRmlChanged; - public event EventHandler LoadedRmlChanged; - public event EventHandler DefaultSpoolTypeChanged; protected String _serialnumber; @@ -279,7 +277,6 @@ namespace Tango.BL.Entities /// [Column("LOADED_RML_GUID")] - [ForeignKey("LoadedRml")] public String LoadedRmlGuid { @@ -960,34 +957,6 @@ namespace Tango.BL.Entities } } - protected Rml _loadedrml; - - /// - /// Gets or sets the machine rml1. - /// - - [XmlIgnore] - [JsonIgnore] - public virtual Rml LoadedRml - { - get - { - return _loadedrml; - } - - set - { - if (_loadedrml != value) - { - _loadedrml = value; - - LoadedRmlChanged?.Invoke(this, value); - - RaisePropertyChanged(nameof(LoadedRml)); - } - } - } - protected SpoolType _defaultspooltype; /// diff --git a/Software/Visual_Studio/Tango.BL/Entities/Rml.cs b/Software/Visual_Studio/Tango.BL/Entities/Rml.cs index b79736ad4..3bbdd3fab 100644 --- a/Software/Visual_Studio/Tango.BL/Entities/Rml.cs +++ b/Software/Visual_Studio/Tango.BL/Entities/Rml.cs @@ -71,8 +71,6 @@ namespace Tango.BL.Entities public event EventHandler> MachinesChanged; - public event EventHandler> Machines1Changed; - public event EventHandler MediaColorChanged; public event EventHandler MediaConditionChanged; @@ -899,32 +897,6 @@ namespace Tango.BL.Entities } } - protected SynchronizedObservableCollection _machines1; - - /// - /// Gets or sets the rml machines1. - /// - - public virtual SynchronizedObservableCollection Machines1 - { - get - { - return _machines1; - } - - set - { - if (_machines1 != value) - { - _machines1 = value; - - Machines1Changed?.Invoke(this, value); - - RaisePropertyChanged(nameof(Machines1)); - } - } - } - protected MediaColor _mediacolor; /// @@ -1079,8 +1051,6 @@ namespace Tango.BL.Entities Machines = new SynchronizedObservableCollection(); - Machines1 = new SynchronizedObservableCollection(); - ProcessParametersTablesGroups = new SynchronizedObservableCollection(); } diff --git a/Software/Visual_Studio/Tango.Core/Components/DataBaseDescriptionsHelper.cs b/Software/Visual_Studio/Tango.Core/Components/DataBaseDescriptionsHelper.cs index 0f1193c6c..620fe9dbc 100644 --- a/Software/Visual_Studio/Tango.Core/Components/DataBaseDescriptionsHelper.cs +++ b/Software/Visual_Studio/Tango.Core/Components/DataBaseDescriptionsHelper.cs @@ -18,6 +18,12 @@ namespace Tango.Core.Components public String ColumnDescription { get; set; } } + public class ForeignKeyDescription + { + public String TableName { get; set; } + public String ColumnName { get; set; } + } + public static List GetDescriptions(DbConnection connection) { List dbDescriptions = new List(); @@ -67,5 +73,30 @@ ORDER BY t.name, c.colorder"; return dbDescriptions; } + + public static List GetForeignKeys(DbConnection connection) + { + List keys = new List(); + + var command = connection.CreateCommand(); + command.CommandText = "SELECT a.TABLE_NAME, a.COLUMN_NAME FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS b JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE a ON a.CONSTRAINT_CATALOG = b.CONSTRAINT_CATALOG AND a.CONSTRAINT_NAME = b.CONSTRAINT_NAME"; + + DataTable table = new DataTable(); + table.Load(command.ExecuteReader()); + + foreach (var row in table.Rows.OfType()) + { + String table_name = row.ItemArray.GetValue(0).ToString(); + String column_name = row.ItemArray.GetValue(1).ToString(); + + keys.Add(new ForeignKeyDescription() + { + TableName = table_name, + ColumnName = column_name + }); + } + + return keys; + } } } diff --git a/Software/Visual_Studio/Tango.DAL.Remote/DB/MACHINE.cs b/Software/Visual_Studio/Tango.DAL.Remote/DB/MACHINE.cs index 54b7a6231..f49eba95a 100644 --- a/Software/Visual_Studio/Tango.DAL.Remote/DB/MACHINE.cs +++ b/Software/Visual_Studio/Tango.DAL.Remote/DB/MACHINE.cs @@ -63,7 +63,6 @@ namespace Tango.DAL.Remote.DB public virtual ICollection MACHINES_EVENTS { get; set; } public virtual ORGANIZATION ORGANIZATION { get; set; } public virtual RML RML { get; set; } - public virtual RML RML1 { get; set; } public virtual SPOOL_TYPES SPOOL_TYPES { get; set; } } } diff --git a/Software/Visual_Studio/Tango.DAL.Remote/DB/RML.cs b/Software/Visual_Studio/Tango.DAL.Remote/DB/RML.cs index a7a5b1deb..5b4ca71ff 100644 --- a/Software/Visual_Studio/Tango.DAL.Remote/DB/RML.cs +++ b/Software/Visual_Studio/Tango.DAL.Remote/DB/RML.cs @@ -22,7 +22,6 @@ namespace Tango.DAL.Remote.DB this.JOBS = new HashSet(); this.LIQUID_TYPES_RMLS = new HashSet(); this.MACHINES = new HashSet(); - this.MACHINES1 = new HashSet(); this.PROCESS_PARAMETERS_TABLES_GROUPS = new HashSet(); } @@ -65,8 +64,6 @@ namespace Tango.DAL.Remote.DB public virtual ICollection LIQUID_TYPES_RMLS { get; set; } [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] public virtual ICollection MACHINES { get; set; } - [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")] - public virtual ICollection MACHINES1 { get; set; } public virtual MEDIA_COLORS MEDIA_COLORS { get; set; } public virtual MEDIA_CONDITIONS MEDIA_CONDITIONS { get; set; } public virtual MEDIA_MATERIALS MEDIA_MATERIALS { get; set; } diff --git a/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx b/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx index ed932c954..c51caf219 100644 --- a/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx +++ b/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx @@ -1869,7 +1869,7 @@ - + @@ -1881,18 +1881,6 @@ - - - - - - - - - - - - @@ -2496,11 +2484,7 @@ - - - - - + @@ -2946,11 +2930,7 @@ - - - - - + @@ -3841,8 +3821,7 @@ - - + @@ -4041,8 +4020,7 @@ - - + @@ -5149,7 +5127,7 @@ - + @@ -5161,18 +5139,6 @@ - - - - - - - - - - - - diff --git a/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx.diagram b/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx.diagram index 886074e80..4c3b4610a 100644 --- a/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx.diagram +++ b/Software/Visual_Studio/Tango.DAL.Remote/DB/RemoteADO.edmx.diagram @@ -5,81 +5,81 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -148,8 +148,7 @@ - - + diff --git a/Software/Visual_Studio/Utilities/Tango.DBObservablesGenerator.CLI/ObservablesGenerator.cs b/Software/Visual_Studio/Utilities/Tango.DBObservablesGenerator.CLI/ObservablesGenerator.cs index e45815046..3a40dfd1f 100644 --- a/Software/Visual_Studio/Utilities/Tango.DBObservablesGenerator.CLI/ObservablesGenerator.cs +++ b/Software/Visual_Studio/Utilities/Tango.DBObservablesGenerator.CLI/ObservablesGenerator.cs @@ -16,6 +16,7 @@ using Tango.Core; using System.Data; using System.Diagnostics; using static Tango.Core.Components.DataBaseDescriptionsHelper; +using System.Threading; namespace Tango.DBObservablesGenerator.CLI { @@ -31,6 +32,7 @@ namespace Tango.DBObservablesGenerator.CLI public void GenerateCSharp(String targetPath) { List dbDescriptions = new List(); + List foreign_keys = new List(); //Get column descriptions... using (RemoteDB db = new RemoteDB(SettingsManager.Default.GetOrCreate().DataSource)) @@ -39,6 +41,7 @@ namespace Tango.DBObservablesGenerator.CLI db.Database.Connection.Open(); dbDescriptions = Core.Components.DataBaseDescriptionsHelper.GetDescriptions(db.Database.Connection); + foreign_keys = Core.Components.DataBaseDescriptionsHelper.GetForeignKeys(db.Database.Connection); } //Generate Entities... @@ -61,7 +64,38 @@ namespace Tango.DBObservablesGenerator.CLI Description = table_description, }; - List foreignKeys = table.PropertyType.GenericTypeArguments.First().GetProperties().Skip(3).Where(x => x.PropertyType == typeof(String)).Where(x => x.Name.ToLower().Contains("guid")).Select(x => DalNameToStandardName(x.Name).SingularizeMVC().Replace("Guid", "")).ToList(); + List guessed_keys = table.PropertyType.GenericTypeArguments.First().GetProperties().Skip(3).Where(x => x.PropertyType == typeof(String)).Where(x => x.Name.ToLower().Contains("guid")).Select(x => DalNameToStandardName(x.Name).SingularizeMVC().Replace("Guid", "")).OrderBy(x => x).ToList(); + + List foreignKeys = foreign_keys.Where(x => x.TableName == table.Name).Select(x => DalNameToStandardName(x.ColumnName).SingularizeMVC().Replace("Guid", "")).OrderBy(x => x).ToList(); + + String t_name = table.Name; + + if (guessed_keys.Count != foreignKeys.Count) + { + Console.ForegroundColor = ConsoleColor.Yellow; + + Console.WriteLine($"Specious entry at {table.Name}."); + for (int i = 0; i < Math.Max(guessed_keys.Count, foreignKeys.Count); i++) + { + if (i < guessed_keys.Count) + { + Console.Write(guessed_keys[i]); + } + + Console.Write(" <=> "); + + if (i < foreignKeys.Count) + { + Console.Write(foreignKeys[i]); + } + + Console.WriteLine(); + } + + Console.ForegroundColor = ConsoleColor.Gray; + + Thread.Sleep(2000); + } foreach (var field in table.PropertyType.GenericTypeArguments.First().GetProperties().Skip(3)) { @@ -117,7 +151,7 @@ namespace Tango.DBObservablesGenerator.CLI { codeField.Type = field.PropertyType.Name; - if (codeField.Name.EndsWith("Guid")) + if (foreignKeys.Contains(codeField.Name.Replace("Guid", ""))) { codeField.IsForeignKey = true; codeField.ForeignKeyName = codeField.Name.Replace("Guid", ""); diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs index ed1a807be..8225d75e1 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs @@ -1,4 +1,5 @@ -using System; +using Microsoft.IdentityModel.Clients.ActiveDirectory; +using System; using System.Collections.Generic; using System.Linq; using System.Net; @@ -6,10 +7,13 @@ using System.Net.Http; using System.Security.Authentication; using System.Web.Http; using Tango.BL; +using Tango.BL.Builders; using Tango.BL.Entities; using Tango.BL.Enumerations; +using Tango.Core.Cryptography; using Tango.MachineService.Helpers; using Tango.MachineService.Models; +using Tango.MachineStudio.Common.Authentication; using Tango.MachineStudio.Common.Update; namespace Tango.MachineService.Controllers @@ -23,6 +27,8 @@ namespace Tango.MachineService.Controllers _pendingUploads = new List(); } + #region Update + [HttpPost] public CheckForUpdatesResponse CheckForUpdates(CheckForUpdatesRequest request) { @@ -182,5 +188,57 @@ namespace Tango.MachineService.Controllers return new LatestVersionResponse() { Version = version != null ? version.Version : "0.0.0.0" }; } } + + #endregion + + public LoginResponse Login(LoginRequest request) + { + var authContext = new AuthenticationContext("https://login.microsoftonline.com/2ebd63a5-bc2f-41dc-9066-4409ed5e5dd4"); + UserCredential userCredential = new UserCredential(request.Email, request.Password); + AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", "ec612854-7abc-457b-808a-5d0c5ba80c57", userCredential); + + using (ObservablesContext db = ObservablesContextHelper.CreateContext()) + { + + db.Roles.ToList(); + db.Permissions.ToList(); + db.UsersRoles.ToList(); + db.RolesPermissions.ToList(); + + var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password); + + if (user == null) + { + //Than add the user !! + + IHashGenerator g = new BasicHashGenerator(); + + BL.Entities.User new_user = new User(); + new_user.Email = request.Email; + new_user.Password = g.Encrypt("Aa123456"); + new_user.Organization = db.Organizations.Single(x => x.Name == "Twine"); + new_user.Address = new Address() { }; + new_user.Contact = new Contact() + { + FirstName = authResult.UserInfo.GivenName, + LastName = authResult.UserInfo.FamilyName, + }; + new_user.Roles.Add(db.Roles.Single(x => (Roles)x.Code == Roles.User)); + } + } + + return new LoginResponse() + { + DataSource = new Core.DataSource() + { + Address = Config.DB_ADDRESS, + Catalog = Config.DB_CATALOG, + Type = Core.DataSourceType.Azure, + UserName = request.Email, + Password = request.Password, + } + }; + } + } } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj index c6245e18e..5b13d8afd 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj +++ b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj @@ -64,6 +64,12 @@ ..\..\packages\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.1.0.3\lib\net45\Microsoft.CodeDom.Providers.DotNetCompilerPlatform.dll + + ..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10707.1513-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.dll + + + ..\..\packages\Microsoft.IdentityModel.Clients.ActiveDirectory.2.7.10707.1513-rc\lib\net45\Microsoft.IdentityModel.Clients.ActiveDirectory.WindowsForms.dll + ..\..\packages\Microsoft.SqlServer.SqlManagementObjects.140.17283.0\lib\net40\Microsoft.SqlServer.AzureStorageEnum.dll diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Web.config b/Software/Visual_Studio/Web/Tango.MachineService/Web.config index 3590b89d8..ef2420349 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Web.config +++ b/Software/Visual_Studio/Web/Tango.MachineService/Web.config @@ -125,6 +125,10 @@ + + + + diff --git a/Software/Visual_Studio/Web/Tango.MachineService/packages.config b/Software/Visual_Studio/Web/Tango.MachineService/packages.config index ede4fa80d..080382800 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/packages.config +++ b/Software/Visual_Studio/Web/Tango.MachineService/packages.config @@ -20,6 +20,7 @@ + -- cgit v1.3.1 From 4e496467dcc8549605917a44263d78cef40f71bb Mon Sep 17 00:00:00 2001 From: Roy Ben Shabat Date: Sun, 23 Dec 2018 12:44:08 +0200 Subject: Implemented machine studio active directory secure login. --- .../Build/Shortcuts/Machine Studio.lnk | Bin 1516 -> 1581 bytes .../MachineStudioSettings.cs | 5 +--- .../Tango.MachineStudio.Common.csproj | 4 +-- .../DefaultAuthenticationProvider.cs | 19 +++++++++++-- .../ViewModels/LoadingViewVM.cs | 5 ++-- .../ViewModels/LoginViewVM.cs | 16 +++++++---- .../Controllers/MachineStudioController.cs | 31 +++++++++++++-------- .../Controllers/PPCController.cs | 3 ++ .../Helpers/AzureDirectoryHelper.cs | 19 +++++++++++++ .../Tango.MachineService.csproj | 3 +- 10 files changed, 75 insertions(+), 30 deletions(-) create mode 100644 Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs (limited to 'Software/Visual_Studio/Web/Tango.MachineService') diff --git a/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk b/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk index adc17d481..9c594a6b0 100644 Binary files a/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk and b/Software/Visual_Studio/Build/Shortcuts/Machine Studio.lnk differ diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs index cab3d2bfc..1d3660c5f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/MachineStudioSettings.cs @@ -39,10 +39,8 @@ namespace Tango.MachineStudio.Common public String LastVirtualMachineSerialNumber { get; set; } /// - /// Gets or sets the update service address. + /// Gets or sets the machine service address. /// - public String UpdateServiceAddress { get; set; } - public String MachineServiceAddress { get; set; } /// @@ -97,7 +95,6 @@ namespace Tango.MachineStudio.Common { LastBounds = new Rect(); LoggingCategories = new List(); - UpdateServiceAddress = "http://twine01/MachineStudioUpdateService/MachineStudioUpdateService.svc"; MachineServiceAddress = "http://machineservice.twine-srv.com/"; DefaultIssueReportTags = new List(); StudioModulesBounds = new List(); diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj index ca4d95805..cd99b00d0 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.Common/Tango.MachineStudio.Common.csproj @@ -127,7 +127,7 @@ - + @@ -310,7 +310,7 @@ - + \ No newline at end of file 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 fd68ed8d1..3293dab3f 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Authentication/DefaultAuthenticationProvider.cs @@ -10,6 +10,9 @@ using Tango.MachineStudio.Common.Authentication; using Tango.BL; using Tango.BL.Enumerations; using System.Data.Entity; +using Tango.Transport.Web; +using Tango.Settings; +using Tango.MachineStudio.Common; namespace Tango.MachineStudio.UI.Authentication { @@ -49,11 +52,21 @@ namespace Tango.MachineStudio.UI.Authentication /// Login failed for user " + email public User Login(string email, string password) { - using (ObservablesContext db = ObservablesContext.CreateDefault()) + var settings = SettingsManager.Default.GetOrCreate(); + + IWebTransportClient service = new WebTransportClient(); + var response = service.PostJson(settings.MachineServiceAddress + "/api/MachineStudio/Login", new LoginRequest() { - String hash = User.GetPasswordHash(password); + Email = email, + Password = password, + + }).Result; + ObservablesContext.OverrideSettingsDataSource(response.DataSource); + + using (ObservablesContext db = ObservablesContext.CreateDefault()) + { db.Roles.Load(); db.Permissions.Load(); db.RolesPermissions.Load(); @@ -62,7 +75,7 @@ namespace Tango.MachineStudio.UI.Authentication .Include(x => x.UsersRoles) .Include(x => x.Contact) .Include(x => x.Address) - .Include(x => x.Organization).SingleOrDefault(x => x.Email.ToLower() == email.ToLower() && x.Password == hash); + .Include(x => x.Organization).SingleOrDefault(x => x.Email.ToLower() == email.ToLower()); if (user == null) { diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs index 545d51ef2..6e8d0488b 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoadingViewVM.cs @@ -144,9 +144,8 @@ namespace Tango.MachineStudio.UI.ViewModels Status = "Loading, please wait..."; - ObservablesStaticCollections.Instance.Initialize(); - - _eventLogger.Log(EventTypes.ApplicationStarted, "Application Started!"); + //ObservablesStaticCollections.Instance.Initialize(); + //_eventLogger.Log(EventTypes.ApplicationStarted, "Application Started!"); Status = "Starting application..."; diff --git a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs index a37879a5e..4e659d46a 100644 --- a/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/ViewModels/LoginViewVM.cs @@ -6,6 +6,8 @@ using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; +using Tango.BL; +using Tango.BL.Enumerations; using Tango.Core.Commands; using Tango.Core.Cryptography; using Tango.MachineStudio.Common; @@ -90,7 +92,7 @@ namespace Tango.MachineStudio.UI.ViewModels _navigationManager = navigationManager; _authenticationProvider = authenticationProvider; _eventLogger = eventLogger; - LoginCommand = new RelayCommand(Login,() => !IsLogging); + LoginCommand = new RelayCommand(Login, () => !IsLogging); cryptographer = new Rfc2898Cryptographer(); Email = _settings.LastLoginEmail; @@ -121,6 +123,10 @@ namespace Tango.MachineStudio.UI.ViewModels await Task.Factory.StartNew(() => { _authenticationProvider.Login(Email, Password); + + ObservablesStaticCollections.Instance.Initialize(); + _eventLogger.Log(EventTypes.ApplicationStarted, "Application Started!"); + _navigationManager.NavigateTo(NavigationView.MainView); _settings.LastLoginEmail = Email; _settings.RememberMe = RememberMe; @@ -128,13 +134,13 @@ namespace Tango.MachineStudio.UI.ViewModels _settings.LastLoginPassword = RememberMe ? cryptographer.Encrypt(Password) : null; _settings.Save(); - _eventLogger.Log("User logged in"); + _eventLogger.Log("User logged in."); }); } - catch (Exception) + catch (Exception ex) { - - _notificationProvider.ShowError("Invalid credentials. Please try again."); + LogManager.Log(ex, "Login Error."); + _notificationProvider.ShowError($"The specified email or password was incorrect, or you don't have a permission to run this application.\nError: {ex.Message}"); } finally { diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs index 8225d75e1..8485cd67d 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/MachineStudioController.cs @@ -14,6 +14,7 @@ using Tango.Core.Cryptography; using Tango.MachineService.Helpers; using Tango.MachineService.Models; using Tango.MachineStudio.Common.Authentication; +using System.Data.Entity; using Tango.MachineStudio.Common.Update; namespace Tango.MachineService.Controllers @@ -191,40 +192,45 @@ namespace Tango.MachineService.Controllers #endregion + [HttpPost] public LoginResponse Login(LoginRequest request) { - var authContext = new AuthenticationContext("https://login.microsoftonline.com/2ebd63a5-bc2f-41dc-9066-4409ed5e5dd4"); - UserCredential userCredential = new UserCredential(request.Email, request.Password); - AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", "ec612854-7abc-457b-808a-5d0c5ba80c57", userCredential); + AuthenticationResult authResult = AzureDirectoryHelper.AuthenticateUser(request.Email, request.Password); using (ObservablesContext db = ObservablesContextHelper.CreateContext()) { - db.Roles.ToList(); db.Permissions.ToList(); db.UsersRoles.ToList(); db.RolesPermissions.ToList(); - var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower() && x.Password == request.Password); + var user = db.Users.SingleOrDefault(x => x.Email.ToLower() == request.Email.ToLower()); + + IHashGenerator g = new BasicHashGenerator(); if (user == null) { //Than add the user !! - - IHashGenerator g = new BasicHashGenerator(); - - BL.Entities.User new_user = new User(); + User new_user = new User(); new_user.Email = request.Email; - new_user.Password = g.Encrypt("Aa123456"); - new_user.Organization = db.Organizations.Single(x => x.Name == "Twine"); - new_user.Address = new Address() { }; + new_user.Password = g.Encrypt(request.Password); + new_user.Organization = db.Organizations.Include(x => x.Address).Single(x => x.Name == "Twine"); + new_user.Address = new_user.Organization.Address.Clone(); new_user.Contact = new Contact() { FirstName = authResult.UserInfo.GivenName, LastName = authResult.UserInfo.FamilyName, + FullName = authResult.UserInfo.GivenName + " " + authResult.UserInfo.FamilyName, + Email = request.Email, }; new_user.Roles.Add(db.Roles.Single(x => (Roles)x.Code == Roles.User)); } + else + { + user.Password = g.Encrypt(request.Password); + } + + db.SaveChanges(); } return new LoginResponse() @@ -234,6 +240,7 @@ namespace Tango.MachineService.Controllers Address = Config.DB_ADDRESS, Catalog = Config.DB_CATALOG, Type = Core.DataSourceType.Azure, + IntegratedSecurity = false, UserName = request.Email, Password = request.Password, } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs index 662883223..68f597fcc 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs +++ b/Software/Visual_Studio/Web/Tango.MachineService/Controllers/PPCController.cs @@ -108,6 +108,7 @@ namespace Tango.MachineService.Controllers Catalog = Config.DB_CATALOG, UserName = credentials.UserName, Password = credentials.Password, + IntegratedSecurity = false, Type = DataSourceType.SQLServer, }; @@ -173,6 +174,7 @@ namespace Tango.MachineService.Controllers Catalog = Config.DB_CATALOG, UserName = credentials.UserName, Password = credentials.Password, + IntegratedSecurity = false, Type = DataSourceType.SQLServer, }; } @@ -249,6 +251,7 @@ namespace Tango.MachineService.Controllers Catalog = Config.DB_CATALOG, UserName = credentials.UserName, Password = credentials.Password, + IntegratedSecurity = false, Type = DataSourceType.SQLServer, }; } diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs b/Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs new file mode 100644 index 000000000..fe7733323 --- /dev/null +++ b/Software/Visual_Studio/Web/Tango.MachineService/Helpers/AzureDirectoryHelper.cs @@ -0,0 +1,19 @@ +using Microsoft.IdentityModel.Clients.ActiveDirectory; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; + +namespace Tango.MachineService.Helpers +{ + public static class AzureDirectoryHelper + { + public static AuthenticationResult AuthenticateUser(String email, String password) + { + var authContext = new AuthenticationContext("https://login.microsoftonline.com/2ebd63a5-bc2f-41dc-9066-4409ed5e5dd4"); + UserCredential userCredential = new UserCredential(email, password); + AuthenticationResult authResult = authContext.AcquireToken("https://graph.windows.net/", "ec612854-7abc-457b-808a-5d0c5ba80c57", userCredential); + return authResult; + } + } +} \ No newline at end of file diff --git a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj index 5b13d8afd..c05cb5fe7 100644 --- a/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj +++ b/Software/Visual_Studio/Web/Tango.MachineService/Tango.MachineService.csproj @@ -277,6 +277,7 @@ + @@ -379,7 +380,7 @@ False - + -- cgit v1.3.1