|
@@ -0,0 +1,114 @@
|
|
|
+using AvaloniaApplication2.Models;
|
|
|
+using ReactiveUI;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Security.Cryptography;
|
|
|
+using System.Text.RegularExpressions;
|
|
|
+using Avalonia.Controls.ApplicationLifetimes;
|
|
|
+using Avalonia.Platform.Storage;
|
|
|
+using Avalonia;
|
|
|
+
|
|
|
+namespace AvaloniaApplication2.ViewModels
|
|
|
+{
|
|
|
+ internal class AdminRegVM : ViewModelBase
|
|
|
+ {
|
|
|
+ public void Reg()
|
|
|
+ {
|
|
|
+ if (_visible)
|
|
|
+ {
|
|
|
+ bool valid = IsPasswordValid(_pass);
|
|
|
+ if (valid)
|
|
|
+ {
|
|
|
+ if (_pass != string.Empty)
|
|
|
+ {
|
|
|
+ NewUser.Password = MD5.HashData(Encoding.ASCII.GetBytes(_pass));
|
|
|
+ }
|
|
|
+ NewUser.Role = 2;
|
|
|
+ MainWindowViewModel.myconnection.Users.Add(NewUser);
|
|
|
+ MainWindowViewModel.myconnection.SaveChanges();
|
|
|
+ MainWindowViewModel.Instance.Page = new AuthAndReg();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Visible = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public async Task AddPhoto()
|
|
|
+ {
|
|
|
+ if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desctop || desctop.MainWindow?.StorageProvider is not { } provider) throw new NullReferenceException("провайдер отсутствует");
|
|
|
+
|
|
|
+ var file = await provider.OpenFilePickerAsync(
|
|
|
+ new FilePickerOpenOptions()
|
|
|
+ {
|
|
|
+ Title = "Выберите изображение",
|
|
|
+ AllowMultiple = false,
|
|
|
+ FileTypeFilter = [FilePickerFileTypes.All, FilePickerFileTypes.ImageAll]
|
|
|
+ }
|
|
|
+ );
|
|
|
+ if (file != null)
|
|
|
+ {
|
|
|
+ await using var readStream = await file[0].OpenReadAsync();
|
|
|
+ byte[] buffer = new byte[readStream.Length];
|
|
|
+ readStream.ReadAtLeast(buffer, 1);
|
|
|
+ NewUser.Image = buffer;
|
|
|
+ Addimage = "Изображение выбрано";
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private bool _visible = false;
|
|
|
+
|
|
|
+ public bool Visible { get => _visible; set => this.RaiseAndSetIfChanged(ref _visible, value); }
|
|
|
+ public User NewUser { get => _NewUser; set => this.RaiseAndSetIfChanged(ref _NewUser, value); }
|
|
|
+
|
|
|
+ User _NewUser = new User();
|
|
|
+
|
|
|
+ string _pass;
|
|
|
+ string _errorMessage;
|
|
|
+ string _addimage;
|
|
|
+ public string Pass { get => _pass; set => _pass = value; }
|
|
|
+ public string ErrorMessage { get => _errorMessage; set => this.RaiseAndSetIfChanged(ref _errorMessage,value); }
|
|
|
+ public string Addimage { get => _addimage; set => this.RaiseAndSetIfChanged(ref _addimage, value); }
|
|
|
+
|
|
|
+ private bool IsPasswordValid(string password)
|
|
|
+ {
|
|
|
+ string pattern = @"^(?=.*[A-Z].*[A-Z])(?=.*[a-z].*[a-z].*[a-z])(?=.*\d.*\d)(?!\d)[A-Za-z\d!""#$%&'()*+,-./:;<=>?@[\\\]^_`{|}~]{8,}$";
|
|
|
+ Regex regex = new Regex(pattern);
|
|
|
+
|
|
|
+ if (string.IsNullOrEmpty(password))
|
|
|
+ {
|
|
|
+ ErrorMessage = "Пароль не может быть пустым.";
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!regex.IsMatch(password))
|
|
|
+ {
|
|
|
+ if (password.Length < 8)
|
|
|
+ {
|
|
|
+ ErrorMessage = "Пароль должен содержать не менее 8 символов.";
|
|
|
+ }
|
|
|
+ else if (password[0] >= '0' && password[0] <= '9')
|
|
|
+ {
|
|
|
+ ErrorMessage = "Пароль не должен начинаться с цифры.";
|
|
|
+ }
|
|
|
+ else if (Regex.Matches(password, @"[A-Z]").Count < 2)
|
|
|
+ {
|
|
|
+ ErrorMessage = "Пароль должен содержать не менее 2 заглавных латинских символов.";
|
|
|
+ }
|
|
|
+ else if (Regex.Matches(password, @"[a-z]").Count < 3)
|
|
|
+ {
|
|
|
+ ErrorMessage = "Пароль должен содержать не менее 3 строчных латинских символов.";
|
|
|
+ }
|
|
|
+ else if (Regex.Matches(password, @"\d").Count < 2)
|
|
|
+ {
|
|
|
+ ErrorMessage = "Пароль должен содержать не менее 2 цифр.";
|
|
|
+ }
|
|
|
+
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|