using System; using System.Collections.Generic; using System.Linq; using System.Security.Authentication; using System.Text; using System.Threading.Tasks; using Tango.Core; using Tango.BL.Entities; 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; using Tango.MachineStudio.Common.StudioApplication; using Tango.Core.Helpers; using Tango.MachineStudio.Common.Web; using Tango.BL.Builders; using System.Data.Entity.Core; namespace Tango.MachineStudio.UI.Authentication { /// /// Represents the default Machine Studio Authentication provider /// /// /// public class DefaultAuthenticationProvider : ExtendedObject, IAuthenticationProvider { private MachineStudioWebClient _client; private User _currentUser; /// /// Gets the current logged-in user. /// public User CurrentUser { get { return _currentUser; } set { _currentUser = value; CurrentUserChanged?.Invoke(this, _currentUser); RaisePropertyChangedAuto(); } } /// /// Occurs when the current logged-in user has changed. /// public event EventHandler CurrentUserChanged; /// /// Initializes a new instance of the class. /// /// The machine studio web client. public DefaultAuthenticationProvider(MachineStudioWebClient machineStudioWebClient) { _client = machineStudioWebClient; } /// /// Performs a user login by the specified email and password. /// /// The email. /// The password. /// /// Login failed for user " + email public AuthenticationLoginResult Login(string email, string password, bool bypassVersionCheck = false) { var settings = SettingsManager.Default.GetOrCreate(); _client.Environment = settings.DeploymentSlot; var appVersion = AssemblyHelper.GetCurrentAssemblyVersion().ToString(); if (settings.ForceVersionUpdate) { appVersion = "1.0.0.0"; } LoginResponse response = null; try { response = _client.Login(new LoginRequest() { Email = email, Password = password, Version = appVersion, }).Result; } catch (Exception ex) { throw new AggregateException(new AuthenticationException("Error logging in to machine service."), ex); } if (bypassVersionCheck) { response.VersionChangeRequired = false; } if (settings.Environment == MachineStudioSettings.WorkingEnvironment.Remote) { ObservablesContext.OverrideSettingsDataSource(response.DataSource); } if (response.VersionChangeRequired && !bypassVersionCheck) { return new AuthenticationLoginResult() { Response = response }; } try { ObservablesStaticCollections.Instance.Initialize(); } catch (Exception ex) { throw new AggregateException(new MetadataException("Error initializing database connection."), ex); } using (ObservablesContext db = ObservablesContext.CreateDefault()) { User user = new UserBuilder(db).Set(x => x.Email.ToLower() == email.ToLower()).WithRolesAndPermissions().WithOrganization().Build(); if (user == null) { throw new AuthenticationException("Invalid credentials for " + email); } CurrentUser = user; return new AuthenticationLoginResult() { User = user, Response = response }; } } /// /// Logs-out the current logged-in user. /// public void Logout() { CurrentUser = null; } } }