Эх сурвалжийг харах

Авторизация и почти готовый профиль

ГаряевСА 4 өдөр өмнө
parent
commit
c0cae54970

+ 1 - 0
Users_Roles/Users_Roles.csproj

@@ -24,6 +24,7 @@
     <!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
     <PackageReference Condition="'$(Configuration)' == 'Debug'" Include="Avalonia.Diagnostics" Version="11.1.0" />
     <PackageReference Include="Avalonia.ReactiveUI" Version="11.1.0" />
+    <PackageReference Include="MessageBox.Avalonia" Version="3.1.6" />
     <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.10">
       <PrivateAssets>all</PrivateAssets>
       <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

+ 22 - 2
Users_Roles/ViewModels/AddUserVM.cs

@@ -10,11 +10,14 @@ using System.Text;
 using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using Users_Roles.Models;
+using MsBox.Avalonia.Enums;
+using MsBox.Avalonia;
 
 namespace Users_Roles.ViewModels
 {
     internal class AddUserVM : ViewModelBase
     {
+        string _messageButton;
         User? _newUser;
         public User? NewUser { get => _newUser; set => this.RaiseAndSetIfChanged(ref _newUser, value); }
 
@@ -22,12 +25,22 @@ namespace Users_Roles.ViewModels
         {
             _newUser = new User();
             _newUser.RoleUser = idRole;
+            if (idRole == 1)
+            {
+                MessageButton = "Зарегистрировать администратора";
+            }
+            else
+            {
+                MessageButton = "Зарегистрировать пользователя";
+            }
         }
 
         string _massege;
         string _newPassword;
         public string NewPassword { get => _newPassword; set => _newPassword = value; }
         public string Massege { get => _massege; set => this.RaiseAndSetIfChanged(ref _massege, value); }
+        public string MessageButton { get => _messageButton; set => _messageButton = value; }
+
         bool TruePassword()
         {
             bool flag = false;
@@ -93,11 +106,16 @@ namespace Users_Roles.ViewModels
                 NewUser.Image = buffer;
             }
         }
+        public static _43pGaruaev2Context myConnection = new _43pGaruaev2Context();
         public void AddUser()
         {
-            if (TruePassword())
+            if (myConnection.Users.Any(x => x.Login == NewUser.Login))
             {
-
+                Massege = "Пользователь с таким логином уже существует";
+            }
+            else if (TruePassword())
+            {
+                
             }
             else
             {
@@ -114,6 +132,8 @@ namespace Users_Roles.ViewModels
 
                 MainWindowViewModel.myConnection.SaveChanges();
 
+                MessageBoxManager.GetMessageBoxStandard("Добавление", "Вы зарегистрировались", ButtonEnum.Ok).ShowAsync();
+
                 MainWindowViewModel.Instance.PageContent = new PageRegistrationV();
             }
         }

+ 44 - 1
Users_Roles/ViewModels/PageRegistrationVM.cs

@@ -1,21 +1,64 @@
-using System;
+using ReactiveUI;
+using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Security.Cryptography;
 using System.Text;
 using System.Threading.Tasks;
+using Users_Roles.Models;
 
 namespace Users_Roles.ViewModels
 {
     internal class PageRegistrationVM : ViewModelBase
     {
+        string _massege;
         bool isVisUser = false;
         bool isVisAdmin = true;
+        string _login;
+        string _password ="";
+        byte[] _passwordBool;
         public bool IsVisUser { get => isVisUser; set => isVisUser = value; }
         public bool IsVisAdmin { get => isVisAdmin; set => isVisAdmin = value; }
+        public string Massege { get => _massege; set => this.RaiseAndSetIfChanged(ref _massege,value); }
+        public string Login { get => _login; set => _login = value; }
+        public string Password { get => _password; set => _password = value; }
+        public byte[] PasswordBool { get => _passwordBool; set => _passwordBool = value; }
+        public PageRegistrationVM()
+        {
+            if (MainWindowViewModel.myConnection.Users.Any(x => x.RoleUser == 1))
+            {
+                IsVisUser = true;
+                IsVisAdmin = false;
+            }
+            else 
+            {
+                IsVisUser = false;
+                IsVisAdmin = true;
+            }
+        }
 
         public void ToAddUser(int idRole)
         {
             MainWindowViewModel.Instance.PageContent = new AddUserV(idRole);
         }
+        public void ToAuth()
+        {
+            PasswordBool = MD5.HashData(Encoding.ASCII.GetBytes(Password));
+            if (MainWindowViewModel.myConnection.Users.Any(x => x.Login == Login))
+            {
+                if (MainWindowViewModel.myConnection.Users.Where(x => x.Login == Login).Where(x => x.Pasword == PasswordBool).Any())
+                {
+                    MainWindowViewModel.Instance.PageContent = new ProfileV(MainWindowViewModel.myConnection.Users.Where(x => x.Login == Login).Where(x => x.Pasword == PasswordBool).Select(x => x.IdUser).First());
+                }
+                else
+                {
+                    Massege = "Неправильный логин или пароль";
+                }
+            }
+            else 
+            {
+                Massege = "Неправильный логин или пароль";
+            }
+        }
     }
 }

+ 50 - 0
Users_Roles/ViewModels/ProfileVM.cs

@@ -0,0 +1,50 @@
+using Avalonia.Controls.ApplicationLifetimes;
+using Avalonia.Platform.Storage;
+using Microsoft.EntityFrameworkCore;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Users_Roles.Models;
+using static System.Net.Mime.MediaTypeNames;
+
+namespace Users_Roles.ViewModels
+{
+    internal class ProfileVM : ViewModelBase
+    {
+        User _user;
+        public ProfileVM(int UserID)
+        {
+            User = MainWindowViewModel.myConnection.Users.Include(x => x.RoleUserNavigation).FirstOrDefault(x => x.IdUser == UserID);
+        }
+
+        public User User { get => _user; set => _user = value; }
+
+        public void Out()
+        {
+            MainWindowViewModel.Instance.PageContent = new PageRegistrationV();
+        }
+
+        public async Task AddOnePhoto()
+        {
+            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;
+            }
+        }
+    }
+}

+ 2 - 3
Users_Roles/Views/AddUserV.axaml

@@ -20,10 +20,9 @@
 				<TextBox Text="{Binding NewUser.Login}"></TextBox>
 				<TextBlock>Пароль</TextBlock>
 				<TextBox PasswordChar="*" Text="{Binding NewPassword}"></TextBox>
+				<Button Command="{Binding AddOnePhoto}">Выбрать фото</Button>
+				<Button Command="{Binding AddUser}" Content="{Binding MessageButton}"/>
 				<TextBlock Foreground="Red" Text="{Binding Massege}"></TextBlock>
-				<Image Height="100" Width="100" Source="{Binding NewUser.Image,Converter={StaticResource myImageConverter}}"></Image>
-				<Button Command="{Binding AddOnePhoto}">Изменить фото</Button>
-				<Button Command="{Binding AddUser}">Зарегистрировать администратора</Button>
 				</StackPanel>
 			</Border>
 		</StackPanel>

+ 6 - 1
Users_Roles/Views/PageRegistrationV.axaml

@@ -12,8 +12,13 @@
 			<Border Classes ="trash">
 				<StackPanel>
 					<Button IsVisible="{Binding IsVisAdmin}" Command="{Binding ToAddUser}" CommandParameter="1">Зарегистрировать администратора</Button>
+					<TextBlock>Логин</TextBlock>
+					<TextBox Text="{Binding Login}"></TextBox>
+					<TextBlock>Пароль</TextBlock>
+					<TextBox PasswordChar="*" Text="{Binding Password}"></TextBox>
 					<Button IsVisible="{Binding IsVisUser}" Command="{Binding ToAddUser}" CommandParameter="2">Зарегистрироваться</Button>
-					<Button IsVisible="{Binding IsVisUser}" Command="">Авторизоваться</Button>
+					<Button IsVisible="{Binding IsVisUser}" Command="{Binding ToAuth}">Авторизоваться</Button>
+					<TextBlock Foreground="Red" Text="{Binding Massege}"></TextBlock>
 				</StackPanel >
 			</Border>
 		</StackPanel>

+ 28 - 0
Users_Roles/Views/ProfileV.axaml

@@ -0,0 +1,28 @@
+<UserControl xmlns="https://github.com/avaloniaui"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+                xmlns:vm="using:Users_Roles.ViewModels"
+			 mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
+                x:DataType="vm:ProfileVM"      
+				xmlns:conv="using:Users_Roles.Converters"
+			 x:Class="Users_Roles.ProfileV">
+	<UserControl.Resources>
+		<conv:ImageConverter x:Key="myImageConverter"/>
+	</UserControl.Resources>
+	<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" >
+		<StackPanel>
+			<Border Classes ="trash">
+				<StackPanel>
+					<TextBlock Text="{Binding User.Fio,StringFormat= ФИО: {0}}"></TextBlock>
+					<TextBlock Text="{Binding User.Login,StringFormat= Логин: {0}}"></TextBlock>
+					<TextBlock Text="{Binding User.RoleUserNavigation.Title,StringFormat= Роль: {0}}"></TextBlock>
+					<Image Height="100" Width="100" Source="{Binding User.Image,Converter={StaticResource myImageConverter}}"></Image>
+					<Button Command="{Binding AddOnePhoto}">Выбрать фото</Button>
+					<Button Command="{Binding Out}">Выйти</Button>
+				</StackPanel >
+			</Border>
+		</StackPanel>
+	</StackPanel>
+	
+</UserControl>

+ 15 - 0
Users_Roles/Views/ProfileV.axaml.cs

@@ -0,0 +1,15 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Markup.Xaml;
+using Users_Roles.ViewModels;
+
+namespace Users_Roles;
+
+public partial class ProfileV : UserControl
+{
+    public ProfileV(int UserID)
+    {
+        InitializeComponent();
+        DataContext = new ProfileVM(UserID);
+    }
+}