using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; using System.Web.Http; using System.Web.Http.Results; using System.Web; using System.IO; using MongoDB.Bson; using MongoDB.Driver; using Newtonsoft.Json.Linq; using ShadowEditor.Server.Base; using ShadowEditor.Server.Helpers; using ShadowEditor.Model.System; using ShadowEditor.Server.CustomAttribute; namespace ShadowEditor.Server.Controllers.System { /// /// 操作权限管理 /// public class OperatingAuthorityController : ApiBase { /// /// 获取列表 /// /// /// [HttpGet] [Authority(OperatingAuthority.LIST_OPERATING_AUTHORITY)] public JsonResult List(string keyword = "") { var fields = typeof(OperatingAuthority).GetFields(); var rows = new JArray(); foreach (var i in fields) { rows.Add(new JObject { ["ID"] = i.Name, ["Name"] = i.GetValue(typeof(OperatingAuthority)).ToString(), }); } return Json(new { Code = 200, Msg = "Get Successfully!", Data = new { total = rows.Count, rows, }, }); } /// /// 根据角色ID获取权限 /// /// /// [HttpGet] public JsonResult Get(string roleID) { var fields = typeof(OperatingAuthority).GetFields(); var helper = new MongoHelper(); var filter = Builders.Filter.Eq("RoleID", roleID); var docs = helper.FindMany(Constant.OperatingAuthorityCollectionName, filter).ToList(); var rows = new JArray(); foreach (var i in fields) { rows.Add(new JObject { ["ID"] = i.Name, ["Name"] = i.GetValue(typeof(OperatingAuthority)).ToString(), ["Enabled"] = docs.Exists(n => n["AuthorityID"].ToString() == i.Name), }); } return Json(new { Code = 200, Msg = "Get Successfully!", Data = new { total = rows.Count, rows, }, }); } /// /// 保存 /// /// /// [HttpPost] [Authority(OperatingAuthority.SAVE_OPERATING_AUTHORITY)] public JsonResult Save(RoleEditModel model) { var objectId = ObjectId.GenerateNewId(); if (!string.IsNullOrEmpty(model.ID) && !ObjectId.TryParse(model.ID, out objectId)) { return Json(new { Code = 300, Msg = "ID is not allowed." }); } if (string.IsNullOrEmpty(model.Name)) { return Json(new { Code = 300, Msg = "Name is not allowed to be empty." }); } if (model.Name.StartsWith("_")) { return Json(new { Code = 300, Msg = "Name is not allowed to start with _." }); } var mongo = new MongoHelper(); var filter = Builders.Filter.Eq("ID", objectId); var update1 = Builders.Update.Set("Name", model.Name); var update2 = Builders.Update.Set("UpdateTime", DateTime.Now); var update = Builders.Update.Combine(update1, update2); mongo.UpdateOne(Constant.OperatingAuthorityCollectionName, filter, update); return Json(new { Code = 200, Msg = "Saved successfully!" }); } } }