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.
63 lines
2.3 KiB
63 lines
2.3 KiB
using System.Linq;
|
|
using Application.Models;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Application.Domain.Entities;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace UserCenter.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class RoleController : CrudController<Role, PagedListModel<EditRoleModel>, EditRoleModel, EditRoleModel>
|
|
{
|
|
private readonly AjaxController _ajax;
|
|
|
|
public RoleController(IRepository<Role> repo, AjaxController 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 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 });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |