BootstrapAdmin/Bootstrap.Admin/Controllers/Api/RolesController.cs

103 lines
3.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Bootstrap.Admin.Query;
using Bootstrap.DataAccess;
using Longbow.Web.Mvc;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class RolesController : ControllerBase
{
/// <summary>
///
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
[HttpGet]
public QueryData<object> Get([FromQuery]QueryRoleOption value)
{
return value.RetrieveData();
}
/// <summary>
/// 通过指定用户ID/部门ID/菜单ID获得所有角色集合已经授权的有checked标记
/// </summary>
/// <param name="id">用户ID/部门ID/菜单ID</param>
/// <param name="type">类型</param>
/// <returns></returns>
[HttpPost("{id}")]
public IEnumerable<object> Post(string id, [FromQuery]string type)
{
IEnumerable<Role> ret = new List<Role>();
switch (type)
{
case "user":
ret = RoleHelper.RetrievesByUserId(id);
break;
case "group":
ret = RoleHelper.RetrievesByGroupId(id);
break;
case "menu":
ret = RoleHelper.RetrievesByMenuId(id);
break;
}
return ret.Select(m => new { m.Id, m.Checked, m.RoleName, m.Description });
}
/// <summary>
/// 保存角色
/// </summary>
/// <param name="id">角色ID</param>
/// <param name="values">选中的ID集合</param>
/// <param name="type">type=menu时菜单维护页面对角色授权弹框保存按钮调用</param>
/// <returns></returns>
[HttpPut("{id}")]
[ButtonAuthorize(Url = "~/Admin/Roles", Auth = "assignUser,assignGroup,assignMenu,assignApp")]
public bool Put(string id, [FromBody]IEnumerable<string> values, [FromQuery]string type)
{
var ret = false;
switch (type)
{
case "user":
ret = UserHelper.SaveByRoleId(id, values);
break;
case "group":
ret = GroupHelper.SaveByRoleId(id, values);
break;
case "menu":
ret = MenuHelper.SaveMenusByRoleId(id, values);
break;
case "app":
ret = AppHelper.SaveByRoleId(id, values);
break;
}
return ret;
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
[HttpPost]
[ButtonAuthorize(Url = "~/Admin/Roles", Auth = "add,edit")]
public bool Post([FromBody]Role value)
{
return RoleHelper.Save(value);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
[HttpDelete]
[ButtonAuthorize(Url = "~/Admin/Roles", Auth = "del")]
public bool Delete([FromBody]IEnumerable<string> value)
{
return RoleHelper.Delete(value);
}
}
}