mirror of
https://github.com/tengge1/ShadowEditor.git
synced 2026-01-25 15:08:11 +00:00
154 lines
4.3 KiB
C#
154 lines
4.3 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 操作权限管理
|
|
/// </summary>
|
|
public class OperatingAuthorityController : ApiBase
|
|
{
|
|
/// <summary>
|
|
/// 获取列表
|
|
/// </summary>
|
|
/// <param name="keyword"></param>
|
|
/// <returns></returns>
|
|
[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,
|
|
},
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据角色ID获取权限
|
|
/// </summary>
|
|
/// <param name="roleID"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public JsonResult Get(string roleID)
|
|
{
|
|
var fields = typeof(OperatingAuthority).GetFields();
|
|
|
|
var helper = new MongoHelper();
|
|
|
|
var filter = Builders<BsonDocument>.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,
|
|
},
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存
|
|
/// </summary>
|
|
/// <param name="model"></param>
|
|
/// <returns></returns>
|
|
[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<BsonDocument>.Filter.Eq("ID", objectId);
|
|
var update1 = Builders<BsonDocument>.Update.Set("Name", model.Name);
|
|
var update2 = Builders<BsonDocument>.Update.Set("UpdateTime", DateTime.Now);
|
|
|
|
var update = Builders<BsonDocument>.Update.Combine(update1, update2);
|
|
|
|
mongo.UpdateOne(Constant.OperatingAuthorityCollectionName, filter, update);
|
|
|
|
return Json(new
|
|
{
|
|
Code = 200,
|
|
Msg = "Saved successfully!"
|
|
});
|
|
}
|
|
}
|
|
}
|