diff --git a/asg_form/Controllers/Dbset.cs b/asg_form/Controllers/Dbset.cs index 8a6cc70..f9f1287 100644 --- a/asg_form/Controllers/Dbset.cs +++ b/asg_form/Controllers/Dbset.cs @@ -13,6 +13,7 @@ using System.Security.Cryptography; using System.Text; using static asg_form.Controllers.Budget.BgCountController; using static asg_form.Controllers.InviteReferee; +using static asg_form.Controllers.menuAssignController; namespace asg_form.Controllers { @@ -285,6 +286,7 @@ namespace asg_form.Controllers public DbSet budgetDetails { get; set; } public DbSet T_Invitation { get; set; } + public DbSet mainMenu { get; set; } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { string connStr = @"Host=localhost;Port=2345;Database=asg;Username=postgres;Password=luolan12323;"; diff --git a/asg_form/Controllers/admin.cs b/asg_form/Controllers/admin.cs index 246b8d5..01310f5 100644 --- a/asg_form/Controllers/admin.cs +++ b/asg_form/Controllers/admin.cs @@ -29,6 +29,7 @@ using Flandre.Core.Common; using Flandre.Core.Messaging; using Mirai.Net.Data.Messages.Concretes; using Flandre.Core.Messaging.Segments; +using System.Runtime.Serialization; namespace asg_form.Controllers { @@ -308,8 +309,41 @@ namespace asg_form.Controllers } + /// + /// 设置用户权限口 + /// + [Route("api/v1/admin/setRight")] + [HttpPost] + [Authorize] + public async Task> opRight(userRights msg) + { + if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin")) + { + return BadRequest(new error_mb { code = 400, message = "无权访问" }); + } + using (var db = new TestDbContext()) + { + var user = await userManager.FindByIdAsync(msg.userId); + if (user == null) { return BadRequest(new error_mb { code = 404, message = "用户未找到" }); } + try + { + user.roleListCode = msg.roleListCode; + user.roleListName = msg.roleListName; + await userManager.UpdateAsync(user); + return Ok(new { code = 200, message = "成功存入" ,msg}); + }catch (Exception ex) + { + return Ok(new { code = 500, message = "服务器错误", ex }); + } + } + } - + public class userRights + { + public string userId { get; set; } + public string? roleListCode { get; set; } + public string? roleListName { get; set; } + } /// /// 设置管理员,需要superadmin /// diff --git a/asg_form/Controllers/menuAssignController.cs b/asg_form/Controllers/menuAssignController.cs index 5e73ca0..2a0a6dc 100644 --- a/asg_form/Controllers/menuAssignController.cs +++ b/asg_form/Controllers/menuAssignController.cs @@ -1,7 +1,122 @@ -namespace asg_form.Controllers -{ - public class menuAssignController - { +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using System.Security.Claims; +using static asg_form.Controllers.InviteReferee; +namespace asg_form.Controllers +{ + public class menuAssignController : ControllerBase + { + public class menuDB + { + public string id { get; set; } + public string path { get; set; } + public string icon_class { get; set; } + public string title { get; set; } + public string parent_id { get; set; } + public string auth { get; set; } + public string component { get; set; } + public string allow_operate { get; set; } + public string show { get; set; } + } + public class menuInput + { + public string id { get; set; } + public string path { get; set; } + public string iconClass { get; set; } + public string title { get; set; } + public string parentId { get; set; } + public string auth { get; set; } + public string component { get; set; } + public string allowOperate { get; set; } + public string show { get; set; } + } + + private readonly UserManager userManager; + + /// + /// 新增菜单 + /// + [Route("api/v1/admin/menuAdd")] + [HttpPost] + [Authorize] + public async Task> menuAdd([FromBody] menuInput msg) + { + if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin")) + { + return BadRequest(new error_mb { code = 400, message = "无权访问" }); + } + using (var db = new TestDbContext()) + { + try + { + var mA = db.mainMenu.Find(msg.id); + if (mA == null) + { + var menu = new menuDB + { + id = msg.id, + path = msg.path, + icon_class = msg.iconClass, + title = msg.title, + parent_id = msg.parentId, + auth = msg.auth, + component = msg.component, + allow_operate = msg.allowOperate, + show = msg.show, + }; + db.mainMenu.Add(menu); + db.SaveChanges(); + } + else + { + mA.id = msg.id; + mA.path = msg.path; + mA.icon_class = msg.iconClass; + mA.title = msg.title; + mA.parent_id = msg.parentId; + mA.auth = msg.auth; + mA.component = msg.component; + mA.allow_operate = msg.allowOperate; + mA.show = msg.show; + db.SaveChanges(); + } + return Ok(new error_mb { code = 200, message = "成功存入" }); + } + catch (Exception ex) + { + return Ok(new { code = 500, message = "服务器错误", ex }); + } + } + } + + /// + /// 删除菜单 + /// + [Route("api/v1/admin/menuDel")] + [HttpDelete] + [Authorize] + public async Task> menuDel([FromQuery] string uid) + { + if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin")) + { + return BadRequest(new error_mb { code = 400, message = "无权访问" }); + } + using (var db = new TestDbContext()) + { + try + { + db.Remove(db.mainMenu.FirstOrDefaultAsync(n => n.id==uid)); + await db.SaveChangesAsync(); + return Ok(new error_mb { code = 200, message = "成功删除" }); + } + catch (Exception ex) + { + return Ok(new { code = 500, message = "服务器错误", ex }); + } + } + } } }