131 lines
4.8 KiB
C#
131 lines
4.8 KiB
C#
using Labaratory.Services;
|
|
using System;
|
|
using System.Windows;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Input;
|
|
using Wpf.Ui.Input;
|
|
using Labaratory.ViewModels;
|
|
using Labaratory.Views;
|
|
|
|
namespace Labaratory
|
|
{
|
|
public class LoginViewModel : BaseViewModel
|
|
{
|
|
private string _login;
|
|
private string _password;
|
|
private string _captchaText;
|
|
private string _captchaInput;
|
|
private bool _isCaptchaValid;
|
|
private bool _isLoginEnabled = true;
|
|
private int _failedAttempts = 0;
|
|
private Models.LaboratoryDBEntities db = new Models.LaboratoryDBEntities();
|
|
private Visibility _CapchaVisibility = Visibility.Hidden;
|
|
|
|
// Свойства для привязки (Binding)
|
|
public string Login { get => _login; set { _login = value; OnPropertyChanged(); } }
|
|
public string Password
|
|
{
|
|
get => _password;
|
|
set
|
|
{
|
|
if (_password == value) return; // Обязательно!
|
|
_password = value;
|
|
OnPropertyChanged();
|
|
}
|
|
}
|
|
public string CaptchaText { get => _captchaText; set { _captchaText = value; OnPropertyChanged(); } }
|
|
public string CaptchaInput { get => _captchaInput; set { _captchaInput = value; OnPropertyChanged(); } }
|
|
public bool IsCaptchaValid { get => _isCaptchaValid; set { _isCaptchaValid = value; OnPropertyChanged(); } }
|
|
public bool IsLoginEnabled { get => _isLoginEnabled; set { _isLoginEnabled = value; OnPropertyChanged(); } }
|
|
public Visibility CapchaVisibility { get => _CapchaVisibility; set { _CapchaVisibility = value; OnPropertyChanged(); } }
|
|
|
|
// Команды
|
|
public ICommand LoginCommand { get; }
|
|
public ICommand RefreshCaptchaCommand { get; }
|
|
|
|
public LoginViewModel()
|
|
{
|
|
LoginCommand = new RelayCommand<object>(
|
|
execute => ExecuteLogin(execute),
|
|
canExecute => IsLoginEnabled);
|
|
RefreshCaptchaCommand = new RelayCommand<object>(execute => GenerateNewCaptcha());
|
|
}
|
|
|
|
private void GenerateNewCaptcha()
|
|
{
|
|
CaptchaText = Valid.GenerateCaptchaText();
|
|
CaptchaInput = string.Empty;
|
|
}
|
|
|
|
private async void ExecuteLogin(object parameter)
|
|
{
|
|
if (CapchaVisibility == Visibility.Visible && !Valid.ValidateCaptcha(CaptchaInput, CaptchaText))
|
|
{
|
|
MessageBox.Show("Неверная капча!");
|
|
GenerateNewCaptcha();
|
|
await LockSystem(10); //Блокировка на 10 сек
|
|
return;
|
|
}
|
|
|
|
var user = db.Users.FirstOrDefault(u => u.Login == Login && u.Password == Password);
|
|
|
|
if (user != null)
|
|
{
|
|
MessageBox.Show($"Успешный вход! Добро пожаловать, {user.Login}");
|
|
if (parameter is Window window)
|
|
{
|
|
if (user.Role.Value == 1)
|
|
{
|
|
var nextWindow = new AdminWindow();
|
|
|
|
var mainVM = new AdminModel(user);
|
|
nextWindow.DataContext = mainVM;
|
|
nextWindow.Show();
|
|
}
|
|
if (user.Role.Value == 2)
|
|
{
|
|
var nextWindow = new ByhalterWindow();
|
|
|
|
var mainVM = new ByhalterModel(user);
|
|
nextWindow.DataContext = mainVM;
|
|
nextWindow.Show();
|
|
}
|
|
if (user.Role.Value == 3)
|
|
{
|
|
var nextWindow = new Views.Laborant();
|
|
|
|
var mainVM = new ViewModels.Laborant(user);
|
|
nextWindow.DataContext = mainVM;
|
|
nextWindow.Show();
|
|
}
|
|
if (user.Role.Value == 4)
|
|
{
|
|
var nextWindow = new Views.LaborantExplorer();
|
|
|
|
var mainVM = new ViewModels.LaborantExplorer(user);
|
|
nextWindow.DataContext = mainVM;
|
|
nextWindow.Show();
|
|
}
|
|
window.Close();
|
|
}
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show("Неверный логин или пароль");
|
|
CapchaVisibility = Visibility.Visible;
|
|
GenerateNewCaptcha();
|
|
}
|
|
}
|
|
|
|
private async Task LockSystem(int seconds)
|
|
{
|
|
IsLoginEnabled = false;
|
|
await Task.Delay(TimeSpan.FromSeconds(seconds));
|
|
IsLoginEnabled = true;
|
|
}
|
|
}
|
|
}
|