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; 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) { using (ObservablesContext db = ObservablesContext.CreateDefault()) { String hash = User.GetPasswordHash(password); db.Roles.Load(); db.Permissions.Load(); db.RolesPermissions.Load(); User user = db.Users .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); 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) { user.LastLogin = DateTime.UtcNow; db.SaveChanges(); } CurrentUser = user; return user; } } /// /// Logs-out the current logged-in user. /// public void Logout() { CurrentUser = null; } } }