61 lines
1.8 KiB
C#
61 lines
1.8 KiB
C#
using SecurityContract.BindingModels;
|
||
using SecurityContract.Logics.IGenericEntityLogic;
|
||
using SecurityContract.Services.IGenericEntityService;
|
||
using SecurityContract.ViewModels;
|
||
using System;
|
||
using System.Linq;
|
||
using ToolsModule.ManagmentEntity;
|
||
using ToolsModule.ManagmentSecurity;
|
||
|
||
namespace SecurityBusinessLogic.BusinessLogics.GenericBusinessLogic
|
||
{
|
||
/// <summary>
|
||
/// Логика работы с пользователями
|
||
/// </summary>
|
||
public class UserBusinessLogic : GenericBusinessLogic<UserGetBindingModel, UserSetBindingModel, UserListViewModel, UserViewModel>, IUserLogic
|
||
{
|
||
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;
|
||
}
|
||
}
|
||
} |