DepartmentProject/DepartmentPortal/Security/SecurityBusinessLogic/BusinessLogics/UserBusinessLogic.cs

60 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ToolsModule.BusinessLogics;
using ToolsModule.Enums;
using SecurityBusinessLogic.BindingModels;
using SecurityBusinessLogic.Interfaces;
using SecurityBusinessLogic.ViewModels;
using System;
using System.Linq;
namespace SecurityBusinessLogic.BusinessLogics
{
/// <summary>
/// Логика работы с пользователями
/// </summary>
public class UserBusinessLogic : GenericBusinessLogic<UserGetBindingModel, UserSetBindingModel, UserListViewModel, UserViewModel>
{
public UserBusinessLogic(IUserService service) : base(service, "Пользователи", AccessOperation.Пользователи) { }
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
{
UserName = model.Login,
PasswordHash = password
});
}
if (!result.IsSucceeded)
{
Errors.AddRange(result.Errors);
return null;
}
return result.Result as UserViewModel;
}
catch (Exception ex)
{
Errors.Add(("Ошибка получения", ex.Message));
}
return null;
}
}
}