aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.MachineDesigner/Views/MachineView.xaml.cs
Commit message (Expand)AuthorAgeFilesLines
* Started working on Developer module.Roy Ben-Shabat2018-01-211-0/+28
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
{
    /// <summary>
    /// Represents the default Machine Studio <see cref="IAuthenticationProvider">Authentication provider</see>
    /// </summary>
    /// <seealso cref="Tango.Core.ExtendedObject" />
    /// <seealso cref="Tango.MachineStudio.Common.Authentication.IAuthenticationProvider" />
    public class DefaultAuthenticationProvider : ExtendedObject, IAuthenticationProvider
    {
        private User _currentUser;
        /// <summary>
        /// Gets the current logged-in user.
        /// </summary>
        public User CurrentUser
        {
            get { return _currentUser; }
            set
            {
                _currentUser = value;
                CurrentUserChanged?.Invoke(this, _currentUser);
                RaisePropertyChangedAuto();
            }
        }

        /// <summary>
        /// Occurs when the current logged-in user has changed.
        /// </summary>
        public event EventHandler<User> CurrentUserChanged;

        /// <summary>
        /// Performs a user login by the specified email and password.
        /// </summary>
        /// <param name="email">The email.</param>
        /// <param name="password">The password.</param>
        /// <returns></returns>
        /// <exception cref="AuthenticationException">Login failed for user " + email</exception>
        public User Login(string email, string password)
        {
            String hash = User.GetPasswordHash(password);

            User user = ObservablesEntitiesAdapter.Instance.Users.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)
            {
                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;
        }

        /// <summary>
        /// Logs-out the current logged-in user.
        /// </summary>
        public void Logout()
        {
            CurrentUser = null;
        }
    }
}