You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
90 lines
3.5 KiB
90 lines
3.5 KiB
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using Application.Domain.Entities;
|
|
using IoT.Shared.Application.Models;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Platform.Areas.IoTCenter.Controllers;
|
|
using System.Linq;
|
|
using System.Reflection;
|
|
|
|
namespace IoT.Shared.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("Admin/[controller]/[action]")]
|
|
[Area("Admin")]
|
|
[ControllerScope(ControllerScopeType.Platform)]
|
|
public class RoleController : CrudController<Role, EditRoleModel>
|
|
{
|
|
private readonly AjaxController _ajax;
|
|
private readonly IRepository<Organ> _organRepo;
|
|
|
|
public RoleController(IRepository<Role> repo, AjaxController ajax, IRepository<Organ> organRepo) : base(repo)
|
|
{
|
|
this._ajax = ajax;
|
|
this._organRepo = organRepo;
|
|
}
|
|
|
|
public override IQueryable<Role> Include(IQueryable<Role> query)
|
|
{
|
|
return query
|
|
.Include(o => o.Organ)
|
|
.Include(o=>o.RolePermissions).ThenInclude(o=>o.Permission);
|
|
}
|
|
|
|
public override IQueryable<Role> Query(PagedListModel<EditRoleModel> model, IQueryable<Role> query)
|
|
{
|
|
return base.Query(model, query)
|
|
.WhereIf(!string.IsNullOrEmpty(model.Query.Name), o => o.Name.Contains(model.Query.Name))
|
|
.WhereIf(model.Query.OrganId.HasValue, o => o.OrganId == model.Query.OrganId.Value)
|
|
.OrderBy(o => o.Name);
|
|
}
|
|
|
|
public override void ToDisplayModel(Role entity, EditRoleModel model)
|
|
{
|
|
var name = this._organRepo.ReadOnlyTable()
|
|
.Where(o => o.Left <= entity.Organ.Left && o.Right >= entity.Organ.Right)
|
|
.ToList()
|
|
.ToTree()
|
|
.FirstOrDefault(o => o.Id == entity.OrganId)?.GetDisplayName();
|
|
ViewData.Add(model.OrganId, name);
|
|
model.Permissions = entity.RolePermissions.Select(o => o.PermissionId).ToList();
|
|
entity.RolePermissions.ForEach(o => ViewData.Add(o.PermissionId, o.Permission.Name));
|
|
}
|
|
|
|
public override void ToEditModel(Role entity, EditRoleModel model)
|
|
{
|
|
this.ViewData.SelectList(o => model.OrganId, () => this._ajax.GetOrgan(model.OrganId).SelectList());
|
|
if (entity != null)
|
|
{
|
|
model.Permissions = entity.RolePermissions.Select(o => o.PermissionId).ToList();
|
|
}
|
|
ViewData.MultiSelectList(o => model.Permissions,
|
|
() => this._ajax.GetPermission(model.Permissions, this.GetType().GetCustomAttribute<ControllerScopeAttribute>().Scope).MultiSelectList()
|
|
);
|
|
}
|
|
|
|
public override void ToEntity(EditRoleModel model, Role entity)
|
|
{
|
|
foreach (var id in entity.UserRoles.Select(o => o.OrganRoleId).ToList())
|
|
{
|
|
if (!model.Permissions.Any(o => o == id))
|
|
{
|
|
entity.RolePermissions.RemoveAll(o => !o.IsReadOnly && o.PermissionId == id);
|
|
}
|
|
}
|
|
foreach (var id in model.Permissions)
|
|
{
|
|
if (!entity.RolePermissions.Any(o => o.PermissionId == id))
|
|
{
|
|
entity.RolePermissions.Add(new RolePermission { PermissionId = id });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|