From fa7fae95754481967851520c575b33f862bca1af Mon Sep 17 00:00:00 2001 From: zhangpeihang <948869991@qq.com> Date: Thu, 27 Jan 2022 17:33:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20=E5=AD=97=E5=85=B8?= =?UTF-8?q?=20=E8=8F=9C=E5=8D=95=20=E8=A7=92=E8=89=B2=20=E7=94=A8=E6=88=B7?= =?UTF-8?q?=20=E6=8E=A7=E5=88=B6=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/DictsController.cs | 58 ++++++++++++ .../Controllers/MenusController.cs | 92 +++++++++++++++++++ .../Controllers/RolesController.cs | 91 ++++++++++++++++++ .../Controllers/UsersController.cs | 49 ++++++++++ 4 files changed, 290 insertions(+) create mode 100644 src/vue/api/BootstrapAdmin.Api/Controllers/DictsController.cs create mode 100644 src/vue/api/BootstrapAdmin.Api/Controllers/MenusController.cs create mode 100644 src/vue/api/BootstrapAdmin.Api/Controllers/RolesController.cs create mode 100644 src/vue/api/BootstrapAdmin.Api/Controllers/UsersController.cs diff --git a/src/vue/api/BootstrapAdmin.Api/Controllers/DictsController.cs b/src/vue/api/BootstrapAdmin.Api/Controllers/DictsController.cs new file mode 100644 index 00000000..24f94022 --- /dev/null +++ b/src/vue/api/BootstrapAdmin.Api/Controllers/DictsController.cs @@ -0,0 +1,58 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. +// Website: https://admin.blazor.zone + +using BootstrapAdmin.DataAccess.Models; +using BootstrapAdmin.Web.Core; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace BootstrapAdmin.Api.Controllers +{ + /// + /// 字典表维护控制器 + /// + [Route("api/[controller]")] + [Authorize] + [ApiController] + public class DictsController : ControllerBase + { + private IDict DictService { get; } + + /// + /// + /// + /// + public DictsController(IDict dictService) => DictService = dictService; + + /// + /// 获取所有字典表数据方法 + /// + /// + [HttpGet] + public ActionResult> Get() + { + return DictService.GetAll(); + } + /// + /// 保存字典方法 + /// + /// + [HttpPost] + public bool Post([FromBody] Dict value) + { + return true; + } + + /// + /// 删除字典项方法 + /// + /// + [HttpDelete] + [Authorize(Roles = "Administrators")] + public bool Delete([FromBody] IEnumerable value) + { + return true; + } + } +} diff --git a/src/vue/api/BootstrapAdmin.Api/Controllers/MenusController.cs b/src/vue/api/BootstrapAdmin.Api/Controllers/MenusController.cs new file mode 100644 index 00000000..bb96555c --- /dev/null +++ b/src/vue/api/BootstrapAdmin.Api/Controllers/MenusController.cs @@ -0,0 +1,92 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. +// Website: https://admin.blazor.zone + +using Bootstrap.Admin.Query; +using Bootstrap.DataAccess; +using Bootstrap.Security; +using Longbow.Web.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using System.Collections.Generic; + +namespace Bootstrap.Admin.Controllers.Api +{ + /// + /// + /// + [Route("api/[controller]")] + [Authorize] + [ApiController] + public class MenusController : ControllerBase + { + /// + /// 获得所有菜单列表调用 + /// + /// + /// + [HttpGet] + public QueryData Get([FromQuery]QueryMenuOption value) + { + return value.RetrieveData(User.Identity!.Name); + } + + /// + /// 保存菜单调用 + /// + /// + [HttpPost] + [ButtonAuthorize(Url = "~/Admin/Menus", Auth = "add,edit")] + public bool Post([FromBody]BootstrapMenu value) + { + return MenuHelper.Save(value); + } + + /// + /// 删除菜单调用 + /// + /// + [HttpDelete] + [ButtonAuthorize(Url = "~/Admin/Menus", Auth = "del")] + public bool Delete([FromBody]IEnumerable value) + { + return MenuHelper.Delete(value); + } + + /// + /// 角色管理菜单授权按钮调用 + /// + /// 角色ID + /// type=role时,角色管理菜单授权调用;type=user时,菜单管理编辑页面父类菜单按钮调用 + /// + [HttpPost("{id}")] + public IEnumerable Post(string id, [FromQuery]string type) + { + IEnumerable ret = new List(); + switch (type) + { + case "role": + ret = MenuHelper.RetrieveMenusByRoleId(id); + break; + case "user": + ret = MenuHelper.RetrieveMenus(User.Identity!.Name); + break; + } + return ret; + } + + /// + /// 角色管理菜单授权保存按钮调用 + /// + /// 菜单ID + /// 角色ID集合 + /// + [HttpPut("{id}")] + [ButtonAuthorize(Url = "~/Admin/Menus", Auth = "assignRole")] + public bool Put(string id, [FromBody]IEnumerable roleIds) + { + return RoleHelper.SavaByMenuId(id, roleIds); + } + } +} diff --git a/src/vue/api/BootstrapAdmin.Api/Controllers/RolesController.cs b/src/vue/api/BootstrapAdmin.Api/Controllers/RolesController.cs new file mode 100644 index 00000000..71b1f64b --- /dev/null +++ b/src/vue/api/BootstrapAdmin.Api/Controllers/RolesController.cs @@ -0,0 +1,91 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. +// Website: https://admin.blazor.zone + +using Bootstrap.Admin.Query; +using Bootstrap.DataAccess; +using Longbow.Web.Mvc; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.Filters; +using System.Collections.Generic; +using System.Linq; + + +namespace Bootstrap.Admin.Controllers.Api +{ + /// + /// 角色维护控制器 + /// + [Route("api/[controller]")] + [Authorize] + [ApiController] + public class RolesController : ControllerBase + { + /// + /// 获取所有角色数据 + /// + /// + /// + [HttpGet] + public QueryData Get([FromQuery]QueryRoleOption value) + { + return value.RetrieveData(); + } + /// + /// 通过指定用户ID/部门ID/菜单ID获得所有角色集合,已经授权的有checked标记 + /// + /// 用户ID/部门ID/菜单ID + /// 类型 + /// + [HttpPost("{id}")] + public IEnumerable Post(string id, [FromQuery]string type) + { + var ret = type switch + { + "user" => RoleHelper.RetrievesByUserId(id), + "group" => RoleHelper.RetrievesByGroupId(id), + "menu" => RoleHelper.RetrievesByMenuId(id), + _ => new Role[0] + }; + return ret.Select(m => new { m.Id, m.Checked, m.RoleName, m.Description }); + } + /// + /// 保存角色授权方法 + /// + /// 角色ID + /// 选中的ID集合 + /// type=menu时,菜单维护页面对角色授权弹框保存按钮调用 + /// + [HttpPut("{id}")] + [ButtonAuthorize(Url = "~/Admin/Roles", Auth = "assignUser,assignGroup,assignMenu,assignApp")] + public bool Put(string id, [FromBody]IEnumerable values, [FromQuery]string type) => type switch + { + "user" => UserHelper.SaveByRoleId(id, values), + "group" => GroupHelper.SaveByRoleId(id, values), + "menu" => MenuHelper.SaveMenusByRoleId(id, values), + "app" => AppHelper.SaveByRoleId(id, values), + _ => false + }; + /// + /// 保存角色方法 + /// + /// + [HttpPost] + [ButtonAuthorize(Url = "~/Admin/Roles", Auth = "add,edit")] + public bool Post([FromBody]Role value) + { + return RoleHelper.Save(value); + } + /// + /// 删除角色方法 + /// + /// + [HttpDelete] + [ButtonAuthorize(Url = "~/Admin/Roles", Auth = "del")] + public bool Delete([FromBody]IEnumerable value) + { + return RoleHelper.Delete(value); + } + } +} diff --git a/src/vue/api/BootstrapAdmin.Api/Controllers/UsersController.cs b/src/vue/api/BootstrapAdmin.Api/Controllers/UsersController.cs new file mode 100644 index 00000000..c62ccb05 --- /dev/null +++ b/src/vue/api/BootstrapAdmin.Api/Controllers/UsersController.cs @@ -0,0 +1,49 @@ +// Copyright (c) Argo Zhang (argo@163.com). All rights reserved. +// Licensed under the LGPL License, Version 3.0. See License.txt in the project root for license information. +// Website: https://admin.blazor.zone + +using BootstrapAdmin.DataAccess.Models; +using BootstrapAdmin.Web.Core; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; + +namespace BootstrapAdmin.Api.Controllers +{ + /// + /// 用户控制器 + /// + [Route("api/[controller]")] + [Authorize] + [ApiController] + public class UsersController : ControllerBase + { + private IUser UserService { get; } + + /// + /// + /// + /// + public UsersController(IUser userService) => UserService = userService; + + /// + /// 调用获取所有用户信息 用户管理查询按钮 + /// + /// + [HttpGet] + public ActionResult> Get() + { + return UserService.GetAll(); + } + + /// + /// api 握手协议 + /// + /// + [AllowAnonymous] + [HttpOptions] + public string? Options() + { + return null; + } + } +}