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.
86 lines
3.1 KiB
86 lines
3.1 KiB
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
|
|
namespace IoT.Shared.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class RoleController : CrudController<Role, EditRoleModel>
|
|
{
|
|
private readonly PermissionController _ajax;
|
|
|
|
public RoleController(IRepository<Role> repo, PermissionController ajax) : base(repo)
|
|
{
|
|
this._ajax = ajax;
|
|
}
|
|
|
|
public override IQueryable<Role> Include(IQueryable<Role> query)
|
|
{
|
|
return query.Include(o => o.RolePermissions).ThenInclude(o => o.Permission).ThenInclude(o => o.Category).ThenInclude(o => o.Parent);
|
|
}
|
|
|
|
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))
|
|
.OrderBy(o => o.Name);
|
|
}
|
|
|
|
public override void ToDisplayModel(Role entity, EditRoleModel model)
|
|
{
|
|
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)
|
|
{
|
|
if (entity != null)
|
|
{
|
|
model.Permissions = entity.RolePermissions.Select(o => o.PermissionId).ToList();
|
|
}
|
|
this.ViewData.MultiSelectList(o => model.Permissions, () => this._ajax.GetPermissionMultiSelectList(model.Permissions));
|
|
}
|
|
|
|
public override void ToEntity(EditRoleModel model, Role entity)
|
|
{
|
|
foreach (var id in entity.RolePermissions.Select(o => o.PermissionId).ToList())
|
|
{
|
|
if (!model.Permissions.Any(o => o == id))
|
|
{
|
|
entity.RolePermissions.RemoveAll(o => o.PermissionId == id);
|
|
}
|
|
}
|
|
foreach (var id in model.Permissions)
|
|
{
|
|
if (!entity.RolePermissions.Any(o => o.PermissionId == id))
|
|
{
|
|
entity.RolePermissions.Add(new RolePermission { PermissionId = id });
|
|
}
|
|
}
|
|
}
|
|
|
|
[ApiExplorerSettings(IgnoreApi = true)]
|
|
public MultiSelectList GetRoleMultiSelectList(IEnumerable<Guid> selected)
|
|
{
|
|
if (selected == null)
|
|
{
|
|
selected = new List<Guid>();
|
|
}
|
|
var list = this.Repo.ReadOnlyTable()
|
|
.Select(o => new { o.Id, o.Name })
|
|
.ToList();
|
|
return new MultiSelectList(list, "Id", "Name", selected);
|
|
}
|
|
}
|
|
} |