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; namespace Tango.MachineStudio.UI.Authentication { /// /// Represents the default Machine Studio Authentication provider /// /// /// public class DefaultAuthenticationProvider : ExtendedObject, IAuthenticationProvider { 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; /// /// Performs a user login by the specified email and password. /// /// The email. /// The password. /// /// Login failed for user " + email public User Login(string email, string password) { User user = ObservablesEntitiesAdapter.Instance.Users.SingleOrDefault(x => x.Email.ToLower() == email.ToLower() && x.Password == password); if (user == null) { throw new AuthenticationException("Invalid credentials for " + email); } 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."); } if (user != null) { using (ObservablesContext db = ObservablesContext.CreateDefault()) { var u = db.Users.Single(x => x.Guid == user.Guid); u.LastLogin = DateTime.UtcNow; db.SaveChanges(); } } CurrentUser = user; return user; } /// /// Logs-out the current logged-in user. /// public void Logout() { CurrentUser = null; } } }