123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Runtime.CompilerServices;
- using System.Security.Cryptography;
- using System.Text;
- using ReactiveUI;
- using RegAuth.Models;
- namespace RegAuth.ViewModels
- {
- internal class AdminViewModel : ViewModelBase, INotifyPropertyChanged
- {
- private readonly validViewModel _otherViewModel;
- public AdminViewModel()
- {
- _otherViewModel = new validViewModel(); // Create an instance of the other ViewModel
- }
- public void ToAdmin()
- {
- MainWindowViewModel.Instance.Uc = new CreateAdmin();
- }
- User _newAdmin = new User();
-
- public User NewAdmin { get => _newAdmin; set => this.RaiseAndSetIfChanged(ref _newAdmin, value); }
- string _password;
- private string _passwordError;
- public string Password
- {
- get => _password;
- set
- {
- if (this.RaiseAndSetIfChanged(ref _password, value))
- {
- // Ïðîâåðÿåì ïàðîëü è óñòàíàâëèâàåì ñîîáùåíèå îá îøèáêå
- PasswordError = _otherViewModel.ValidatePassword(value);
- }
- }
- }
- public string PasswordError
- {
- get => _passwordError;
- private set
- {
- this.RaiseAndSetIfChanged(ref _passwordError, value); // Óâåäîìëÿåì îá èçìåíåíèè
- }
- }
- public event PropertyChangedEventHandler PropertyChanged;
- public void addAdmin()//ôóíêöèÿ äëÿ äîáàâëåíèÿ àäìèíà â áàçó
- {
- Console.WriteLine("Àäìèíà â áàçå íåò");
- if (!string.IsNullOrEmpty(Password) && string.IsNullOrEmpty(PasswordError))//åñëè ó íàñ ïàðîëü íå ïóñò è îøèáîê íå íàáëþäàåòñÿ, òîãäà ìåòîä ðàáîòàåò äàëüøå è ðåàëèçóåò äîáàâëåíèå â áä
- {
- NewAdmin.Password = MD5.HashData(Encoding.ASCII.GetBytes(Password));
- NewAdmin.Roleid = 1;
- MainWindowViewModel.myConnection.Users.Add(NewAdmin);
- MainWindowViewModel.myConnection.SaveChanges();
- MainWindowViewModel.Instance.Uc = new Main();
- }
-
- }
- protected bool RaiseAndSetIfChanged<T>(ref T field, T value, [CallerMemberName] string propertyName = null)//ýòî íàì íàäî, ÷òîáû êîððåêòíî îáðàáîòàòü ðåçóëüòàò â RaiseAndSetIfChanged
- {
- if (EqualityComparer<T>.Default.Equals(field, value)) return false; // Åñëè çíà÷åíèå íå èçìåíèëîñü, âîçâðàùàåì false
- field = value; // Îáíîâëÿåì ïîëå
- OnPropertyChanged(propertyName); // Âûçûâàåì ñîáûòèå èçìåíåíèÿ ñâîéñòâà
- return true; // Âîçâðàùàåì true, åñëè çíà÷åíèå èçìåíèëîñü
- }
- public void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- }
- }
|