aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.FileSystem/Network/GetFileSystemItemRequest.cs
blob: 3cf91e8694fec1cc58916b0ea6f3a6924fb6b26b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Environment;

namespace Tango.FileSystem.Network
{
    public class GetFileSystemItemRequest
    {
        public String Path { get; set; }
        public SpecialFolder? SpecialFolder { get; set; }
        public bool FoldersOnly { get; set; }
        public String Filter { get; set; } = "*.*";
    }
}
Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
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;
using System.Windows.Threading;

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 MachineStudioWebClient _client;
        private DispatcherTimer _refreshTokenTimer;

        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>
        /// Initializes a new instance of the <see cref="DefaultAuthenticationProvider"/> class.
        /// </summary>
        /// <param name="machineStudioWebClient">The machine studio web client.</param>
        public DefaultAuthenticationProvider(MachineStudioWebClient machineStudioWebClient)
        {
            _client = machineStudioWebClient;

            _refreshTokenTimer = new DispatcherTimer();
            _refreshTokenTimer.Interval = TimeSpan.FromMinutes(30);
            _refreshTokenTimer.Tick += _refreshTokenTimer_Tick;
            _refreshTokenTimer.Stop();
        }

        private async void _refreshTokenTimer_Tick(object sender, EventArgs e)
        {
            if (ObservablesContext.GetActualDataSource().Type == DataSourceType.AccessToken)
            {
                try
                {
                    LogManager.Log("Refreshing database access token...");
                    var response = await _client.RefreshToken(new RefreshTokenRequest());
                    ObservablesContext.UpdateAccessToken(response.AccessToken, response.Expiration);
                }
                catch (Exception ex)
                {
                    LogManager.Log(ex, "Error occurred while trying to refresh the database access token.");
                }
            }
        }

        /// <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 AuthenticationLoginResult Login(string email, string password, LoginMethod method, bool bypassVersionCheck = false, Action<String> logAction = null)
        {
            _refreshTokenTimer.Stop();

            var settings = SettingsManager.Default.GetOrCreate<MachineStudioSettings>();

            if (!App.StartupArgs.Contains("-webDebug"))
            {
                _client.Environment = settings.DeploymentSlot;
            }

            var appVersion = AssemblyHelper.GetCurrentAssemblyVersion().ToString();

            if (settings.ForceVersionUpdate)
            {
                appVersion = "1.0.0.0";
            }

            LoginResponse response = null;

            try
            {
                logAction?.Invoke("Logging in to machine service...");

                response = _client.Login(new LoginRequest()
                {

                    Email = email,
                    Password = password,
                    Version = appVersion,
                    Method = method,

                }).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
                };
            }

            if (response.PasswordChangeRequired)
            {
                return new AuthenticationLoginResult()
                {
                    Response = response
                };
            }

            try
            {
                ObservablesStaticCollections.Instance.Initialize((x) => 
                {
                    logAction.Invoke(x);
                });
            }
            catch (Exception ex)
            {
                throw new AggregateException(new MetadataException("Error initializing database connection."), ex);
            }

            using (ObservablesContext db = ObservablesContext.CreateDefault())
            {
                logAction.Invoke("Loading user permissions...");
                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;

                _refreshTokenTimer.Start();

                logAction.Invoke("Starting application...");

                return new AuthenticationLoginResult()
                {
                    User = user,
                    Response = response
                };
            }
        }

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