aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Tango.MachineStudio.UI/Windows/ExceptionResolutions.cs
blob: e27a84c3b3ab49c315f0e0d6a2ba1a6b3ce7ce9f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tango.MachineStudio.UI.Windows
{
    public enum ExceptionResolutions
    {
        Shutdown,
        Restart,
        Ignore,
        Report
    }
}
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.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL.Entities;
using Tango.MachineStudio.Common.Notifications;
using SimpleValidator.Extensions;

namespace Tango.MachineStudio.DB.ViewModels
{
    public class UsersViewVM : DbTableViewModel<User>
    {
        public UsersViewVM(INotificationProvider notification) : base(notification)
        {
            SelectedRoles = new ObservableCollection<MultiComboVM<Role>>();
        }

        private ObservableCollection<MultiComboVM<Role>> _selectedRoles;
        public ObservableCollection<MultiComboVM<Role>> SelectedRoles
        {
            get { return _selectedRoles; }
            set { _selectedRoles = value; RaisePropertyChangedAuto(); }
        }

        protected override void OnEdit()
        {
            SelectedRoles = Adapter.Roles.Select(x => new MultiComboVM<Role>(x, () => RaisePropertyChanged(nameof(SelectedRoles)))).ToObservableCollection();

            foreach (var role in SelectedRoles)
            {
                if (SelectedEntity.UsersRoles.ToList().Exists(x => x.Role == role.Entity))
                {
                    role.IsSelected = true;
                }
            }

            base.OnEdit();
        }

        protected override void OnAdd()
        {
            SelectedRoles = Adapter.Roles.Select(x => new MultiComboVM<Role>(x, () => RaisePropertyChanged(nameof(SelectedRoles)))).ToObservableCollection();

            base.OnAdd();
        }

        protected override void OnBeforeEntitySave(DialogOpenMode mode, User user)
        {
            base.OnBeforeEntitySave(mode, user);

            Adapter.Context.UsersRoles.RemoveRange(user.UsersRoles);

            foreach (var role in SelectedRoles)
            {
                if (role.IsSelected)
                {
                    user.UsersRoles.Add(new UsersRole()
                    {
                        Role = role.Entity,
                        User = user,
                        RoleGuid = role.Entity.Guid,
                        UserGuid = user.Guid
                    });
                }
            }
        }

        protected override void OnValidating()
        {
            base.OnValidating();

            if (EditEntity.Email != null)
            {
                if (Adapter.Users.ToList().Exists(x => x.Guid != EditEntity.Guid && x.Email.ToLower() == EditEntity.Email.ToLower()))
                {
                    ValidationErrors.Add("Email already exist");
                }
            }

            if (!EditEntity.Email.IsEmail())
            {
                ValidationErrors.Add("Email address is invalid");
            }

            if (!EditEntity.Password.IsMinLength(4))
            {
                ValidationErrors.Add("Password must have at least 4 characters");
            }
        }
    }
}