2022-03-19 22:48:13 +04:00
|
|
|
|
using DepartmentContract.BindingModels;
|
|
|
|
|
using DepartmentContract.Logics.IGenericEntityLogic;
|
|
|
|
|
using DepartmentContract.ViewModels;
|
|
|
|
|
using SecurityContract.BindingModels;
|
|
|
|
|
using SecurityContract.Logics.IGenericEntityLogic;
|
|
|
|
|
using SecurityContract.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-03-18 22:48:14 +04:00
|
|
|
|
using ToolsDesktop.BaseControls;
|
|
|
|
|
using ToolsDesktop.Controls;
|
|
|
|
|
using ToolsDesktop.Helpers;
|
|
|
|
|
using ToolsDesktop.Interfaces;
|
|
|
|
|
using ToolsDesktop.Models;
|
2022-03-20 10:10:44 +04:00
|
|
|
|
using ToolsModule.ManagmentDependency;
|
|
|
|
|
using ToolsModule.ManagmentExtension;
|
|
|
|
|
using ToolsModule.ManagmentMapping;
|
2021-04-12 16:57:29 +04:00
|
|
|
|
|
|
|
|
|
namespace DepartmentWindowsDesktop.EntityControls
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Реализация контрола для студента
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class ControlStudentElement :
|
2022-03-19 22:48:13 +04:00
|
|
|
|
GenericControlEntityElement<StudentGetBindingModel, StudentSetBindingModel, StudentListViewModel, StudentViewModel, IStudentLogic>,
|
2021-04-12 16:57:29 +04:00
|
|
|
|
IGenericControlEntityElement
|
|
|
|
|
{
|
|
|
|
|
public ControlStudentElement()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
Title = "Студент";
|
|
|
|
|
ControlId = new Guid("cbddf92c-8c00-4ad2-93e8-33c0d998a899");
|
|
|
|
|
_genericControlViewEntityElement = this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public IControl GetInstanceGenericControl() => new ControlStudentElement() { ControlId = Guid.NewGuid() };
|
|
|
|
|
|
|
|
|
|
public ControlViewEntityElementConfiguration GetConfigControl() => new()
|
|
|
|
|
{
|
|
|
|
|
ControlOnMoveElem = new Dictionary<string, (string Title, EventHandler Event)>
|
|
|
|
|
{
|
2022-03-15 22:02:13 +04:00
|
|
|
|
{ "ToolStripMenuItemAddUser", ("Добавить пользователя", (object sender, EventArgs e) => { AddUser(); }) },
|
|
|
|
|
{ "ToolStripMenuItemPasswordReset", ("Сброс пароля пользователя", (object sender, EventArgs e) => { PasswordReset(); }) }
|
2021-04-12 16:57:29 +04:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Поиск пользователя под учетку, либо добавление нового, если не найдено
|
|
|
|
|
/// </summary>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void AddUser()
|
2021-04-12 16:57:29 +04:00
|
|
|
|
{
|
|
|
|
|
var model = new StudentSetBindingModel();
|
|
|
|
|
if (FillModel(model))
|
|
|
|
|
{
|
2022-03-19 22:48:13 +04:00
|
|
|
|
var logic = DependencyManager.Instance.Resolve<IUserLogic>();
|
2021-04-12 16:57:29 +04:00
|
|
|
|
var userName = $"{model.LastName}{(model.FirstName.IsNotEmpty() ? $" {model.FirstName[0]}." : string.Empty)}{(model.Patronymic.IsNotEmpty() ? $"{model.Patronymic[0]}." : string.Empty)}";
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var user = logic.GetElement(new UserGetBindingModel { Login = model.NumberOfBook });
|
2021-08-20 18:28:34 +04:00
|
|
|
|
if (user == null)
|
2021-04-12 16:57:29 +04:00
|
|
|
|
{
|
2021-08-20 18:28:34 +04:00
|
|
|
|
if (logic.Errors.Count > 0)
|
2021-04-12 16:57:29 +04:00
|
|
|
|
{
|
2021-08-20 18:28:34 +04:00
|
|
|
|
DialogHelper.MessageException(logic.Errors, "Ошибка при создании пользователя");
|
2021-04-12 16:57:29 +04:00
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-15 22:02:13 +04:00
|
|
|
|
user = logic.Create(new UserSetBindingModel
|
2021-04-12 16:57:29 +04:00
|
|
|
|
{
|
2022-03-18 12:47:34 +04:00
|
|
|
|
UserName = model.NumberOfBook,
|
|
|
|
|
PasswordHash = userName,
|
2021-08-20 18:28:34 +04:00
|
|
|
|
Avatar = model.Photo
|
|
|
|
|
});
|
|
|
|
|
if (user == null)
|
2021-04-12 16:57:29 +04:00
|
|
|
|
{
|
2021-08-20 18:28:34 +04:00
|
|
|
|
DialogHelper.MessageException(logic.Errors, "Ошибка при создании пользователя");
|
|
|
|
|
return;
|
2021-04-12 16:57:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
2021-08-20 18:28:34 +04:00
|
|
|
|
model.UserId = user.Id;
|
|
|
|
|
var controls = tabPageMain.Controls.Find($"ControlUserId", true);
|
|
|
|
|
if (controls != null)
|
|
|
|
|
{
|
|
|
|
|
(controls[0] as AbstractBaseControl).SetValue(model);
|
|
|
|
|
}
|
2021-04-12 16:57:29 +04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Сброс пароля пользователя
|
|
|
|
|
/// </summary>
|
2022-03-15 22:02:13 +04:00
|
|
|
|
private void PasswordReset()
|
2021-04-12 16:57:29 +04:00
|
|
|
|
{
|
|
|
|
|
if (_element == null)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
var model = new StudentSetBindingModel();
|
|
|
|
|
if (FillModel(model))
|
|
|
|
|
{
|
2022-03-19 22:48:13 +04:00
|
|
|
|
var logic = DependencyManager.Instance.Resolve<IUserLogic>();
|
2022-03-15 22:02:13 +04:00
|
|
|
|
var user = logic.GetElement(new UserGetBindingModel { Id = _element.UserId });
|
2021-04-12 16:57:29 +04:00
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2022-03-18 12:47:34 +04:00
|
|
|
|
user.PasswordHash = model.NumberOfBook;
|
2022-03-15 22:02:13 +04:00
|
|
|
|
user = logic.Update(Mapper.MapToClass<UserViewModel, UserSetBindingModel>(user, true));
|
2021-04-12 16:57:29 +04:00
|
|
|
|
if (user == null)
|
|
|
|
|
{
|
|
|
|
|
DialogHelper.MessageException(logic.Errors, "Ошибка при получении пользователя");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
DialogHelper.MessageInformation("Пароль сброшен", "Успех");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|