2022-03-19 19:58:10 +04:00
|
|
|
|
using SecurityContract.BindingModels;
|
|
|
|
|
using SecurityContract.Interfaces.IGenericEntityService;
|
|
|
|
|
using SecurityContract.Logics.IGenericEntityLogic;
|
|
|
|
|
using SecurityContract.ViewModels;
|
2022-03-18 10:22:34 +04:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
2022-03-19 19:58:10 +04:00
|
|
|
|
using ToolsModule.BusinessLogics;
|
|
|
|
|
using ToolsModule.Enums;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
|
2022-03-19 19:58:10 +04:00
|
|
|
|
namespace SecurityBusinessLogic.BusinessLogics.GenericBusinessLogic
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2022-03-19 19:58:10 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логика работы с пользователями
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class UserBusinessLogic : GenericBusinessLogic<UserGetBindingModel, UserSetBindingModel, UserListViewModel, UserViewModel>, IUserLogic
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
public UserBusinessLogic(IUserService service) : base(service, "Пользователи", AccessOperation.Пользователи) { }
|
2022-03-18 10:22:34 +04:00
|
|
|
|
|
|
|
|
|
public UserViewModel GetOrCreateUser(UserGetBindingModel model, string password)
|
|
|
|
|
{
|
|
|
|
|
Errors.Clear();
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
model.HaveRight = !NoAccess(model, AccessType.View);
|
|
|
|
|
if (model.HaveRight && !_allowSimpleView)
|
|
|
|
|
{
|
|
|
|
|
throw new MethodAccessException("Нет прав на получение списка");
|
|
|
|
|
}
|
|
|
|
|
var result = Service.Read(model);
|
|
|
|
|
if (result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
return result.Result as UserViewModel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (result.Errors.First().Message == "Элемент удален")
|
|
|
|
|
{
|
|
|
|
|
result = Service.Restore(model);
|
|
|
|
|
}
|
|
|
|
|
else if (result.Errors.First().Message == "Элемент не найден")
|
|
|
|
|
{
|
|
|
|
|
result = Service.Create(new UserSetBindingModel
|
|
|
|
|
{
|
2022-03-18 12:47:34 +04:00
|
|
|
|
UserName = model.Login,
|
|
|
|
|
PasswordHash = password
|
2022-03-18 10:22:34 +04:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!result.IsSucceeded)
|
|
|
|
|
{
|
|
|
|
|
Errors.AddRange(result.Errors);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
return result.Result as UserViewModel;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Errors.Add(("Ошибка получения", ex.Message));
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
}
|