2021-03-26 20:09:52 +04:00
|
|
|
|
using DatabaseCore.Models.Security;
|
|
|
|
|
using ModelTools.Enums;
|
|
|
|
|
using ModelTools.Interfaces;
|
2021-03-27 23:50:29 +04:00
|
|
|
|
using ModelTools.Models;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
using SecurityBusinessLogic.BusinessLogics;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
|
|
|
|
namespace DatabaseCore
|
|
|
|
|
{
|
|
|
|
|
public class SecurityManager : ISecurityManager
|
|
|
|
|
{
|
|
|
|
|
public string ErrorMessage { get; set; }
|
|
|
|
|
|
2021-03-27 23:50:29 +04:00
|
|
|
|
public bool CheckAccess(SecurityManagerCheckAccessModel model)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
using var context = DatabaseManager.GetContext;
|
|
|
|
|
Access access;
|
|
|
|
|
if (model != null)
|
|
|
|
|
{
|
|
|
|
|
// простой просмотр возможен
|
2021-03-27 23:50:29 +04:00
|
|
|
|
if (model.Model.SkipCheck && model.Type == AccessType.SimpleView)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
// если не указан идентификатор пользователя, то смотрим, может он авторизован
|
2021-03-27 23:50:29 +04:00
|
|
|
|
if (!model.Model.UserId.HasValue && UserManager.GetInstance.User != null)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
2021-03-27 23:50:29 +04:00
|
|
|
|
model.Model.UserId = UserManager.GetInstance.UserId;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
|
2021-03-27 23:50:29 +04:00
|
|
|
|
var roles = context.UserRoles.Where(x => x.UserId == model.Model.UserId).Select(x => x.Role).OrderByDescending(x => x.RolePriority).ToList();
|
2021-03-26 20:09:52 +04:00
|
|
|
|
if (roles == null)
|
|
|
|
|
{
|
|
|
|
|
ErrorMessage = $"Не верный пользователь";
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2021-03-27 23:50:29 +04:00
|
|
|
|
access = context.Accesses.FirstOrDefault(a => a.AccessOperation == model.Operation && roles.Contains(a.Role));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2021-03-27 23:50:29 +04:00
|
|
|
|
access = context.Accesses.FirstOrDefault(a => a.AccessOperation == model.Operation && UserManager.GetInstance.Roles.Contains(a.RoleId));
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
|
|
|
|
if (access != null)
|
|
|
|
|
{
|
2021-03-27 23:50:29 +04:00
|
|
|
|
if (access.AccessType >= model.Type) return true;
|
2021-03-26 20:09:52 +04:00
|
|
|
|
}
|
2021-03-27 23:50:29 +04:00
|
|
|
|
switch (model.Type)
|
2021-03-26 20:09:52 +04:00
|
|
|
|
{
|
|
|
|
|
case AccessType.FullView:
|
2021-03-27 23:50:29 +04:00
|
|
|
|
ErrorMessage = $"Нет доступа на чтение данных по сущности '{model.Entity}'";
|
2021-03-26 20:09:52 +04:00
|
|
|
|
return false;
|
|
|
|
|
case AccessType.Change:
|
2021-03-27 23:50:29 +04:00
|
|
|
|
ErrorMessage = $"Нет доступа на изменение данных по сущности '{model.Entity}'";
|
2021-03-26 20:09:52 +04:00
|
|
|
|
return false;
|
|
|
|
|
case AccessType.Delete:
|
2021-03-27 23:50:29 +04:00
|
|
|
|
ErrorMessage = $"Нет доступа на удаление данных по сущности '{model.Entity}'";
|
2021-03-26 20:09:52 +04:00
|
|
|
|
return false;
|
|
|
|
|
default:
|
2021-03-27 23:50:29 +04:00
|
|
|
|
ErrorMessage = $"Нет доступа по сущности '{model.Entity}'";
|
2021-03-26 20:09:52 +04:00
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|