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.
75 lines
2.6 KiB
75 lines
2.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Application.Domain.Entities;
|
|
using Application.Models;
|
|
using Infrastructure.Application;
|
|
using Infrastructure.Data;
|
|
using Infrastructure.Extensions;
|
|
using Infrastructure.Web.Mvc;
|
|
using IoTShared.Controllers;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.Rendering;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace IoTCenter.Areas.Admin.Controllers
|
|
{
|
|
[Authorize]
|
|
[Area(nameof(Admin))]
|
|
public class RoleController : CrudController<Role, PagedListModel<EditRoleModel>, EditRoleModel, EditRoleModel>
|
|
{
|
|
private readonly IRepository<Role> _roleRepo;
|
|
private readonly PermissionController _ajax;
|
|
|
|
public RoleController(IRepository<Role> roleRepo, PermissionController ajax) : base(roleRepo)
|
|
{
|
|
this._roleRepo = roleRepo;
|
|
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 ToModel(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 });
|
|
}
|
|
}
|
|
}
|
|
|
|
public MultiSelectList GetRoleMultiSelectList(IEnumerable<Guid> selected)
|
|
{
|
|
if (selected == null)
|
|
{
|
|
selected = new List<Guid>();
|
|
}
|
|
var list = this._roleRepo.ReadOnlyTable()
|
|
.Select(o => new { o.Id, o.Name })
|
|
.ToList();
|
|
return new MultiSelectList(list, "Id", "Name", selected);
|
|
}
|
|
}
|
|
} |