OpenAuth.Net/OpenAuth.Mvc/Controllers/ModuleManagerController.cs

179 lines
5.5 KiB
C#
Raw Normal View History

using Infrastructure;
2015-11-21 23:56:39 +08:00
using OpenAuth.App;
2016-07-19 11:44:48 +08:00
using OpenAuth.App.SSO;
using OpenAuth.App.ViewModel;
2016-10-18 17:34:41 +08:00
using OpenAuth.Domain;
using OpenAuth.Mvc.Models;
2016-10-18 17:34:41 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
2015-10-26 21:58:12 +08:00
namespace OpenAuth.Mvc.Controllers
{
public class ModuleManagerController : BaseController
{
2015-11-21 23:56:39 +08:00
private ModuleManagerApp _app;
public ModuleManagerController()
{
_app = AutofacExt.GetFromFac<ModuleManagerApp>();
2015-11-21 23:56:39 +08:00
}
// GET: /ModuleManager/
[Authenticate]
2015-10-26 21:58:12 +08:00
public ActionResult Index()
{
return View();
}
2015-11-21 23:56:39 +08:00
public ActionResult Assign(Guid firstId, string key)
2015-11-25 23:29:00 +08:00
{
2016-04-20 16:33:27 +08:00
ViewBag.FirstId = firstId;
ViewBag.ModuleType = key;
2016-10-18 17:34:41 +08:00
var moduleWithChildren = AuthUtil.GetCurrentUser().ModuleWithChildren;
var modules = key == "UserModule" ? _app.LoadForUser(firstId) : _app.LoadForRole(firstId);
CheckModule(moduleWithChildren, modules);
ViewBag.Modules = BuilderModules(moduleWithChildren);
return View();
}
2016-10-18 17:34:41 +08:00
private void CheckModule(IEnumerable<TreeItem<ModuleView>> moduleWithChildren, List<Module> modules)
{
foreach (var module in moduleWithChildren)
{
if (module.Children.Any())
{
CheckModule(module.Children, modules);
}
2016-10-19 10:03:24 +08:00
if (modules.Select(u => u.Id).Contains(module.Item.Id))
2016-10-18 17:34:41 +08:00
{
2016-10-19 10:03:24 +08:00
module.Item.Checked = true;
2016-10-18 17:34:41 +08:00
}
2016-10-19 10:03:24 +08:00
2016-10-18 17:34:41 +08:00
}
}
public string BuilderModules(IEnumerable<TreeItem<ModuleView>> modules)
{
StringBuilder sb = new StringBuilder();
foreach (var moduleView in modules)
{
if (moduleView.Children.Any())
{
2016-10-19 10:03:24 +08:00
sb.Append("<fieldset class=\"layui-elem-field\">\r\n");
sb.Append("<legend>");
BuildCheckbox(sb, moduleView);
sb.Append("</legend>\r\n");
sb.Append("<div class=\"layui-field-box\">\r\n");
2016-10-18 17:34:41 +08:00
sb.Append(BuilderModules(moduleView.Children));
sb.Append("</div>\r\n");
2016-10-19 10:03:24 +08:00
sb.Append("</fieldset>\r\n");
//sb.Append("<div class=\"layui-form-item\">\r\n");
//BuildCheckbox(sb, moduleView);
//sb.Append("<div class=\"layui-input-block\">\r\n");
//sb.Append(BuilderModules(moduleView.Children));
//sb.Append("</div>\r\n");
//sb.Append("</div>\r\n");
2016-10-18 17:34:41 +08:00
}
else
{
2016-10-19 10:03:24 +08:00
BuildCheckbox(sb, moduleView);
2016-10-18 17:34:41 +08:00
}
}
return sb.ToString();
}
2016-10-19 10:03:24 +08:00
private void BuildCheckbox(StringBuilder sb, TreeItem<ModuleView> moduleView)
{
sb.Append("<input type=\"checkbox\" value=\"" + moduleView.Item.Id + "\" title=\"" + moduleView.Item.Name + "\"");
if (moduleView.Item.Checked)
{
sb.Append(" checked");
}
sb.Append(">\r\n");
}
2015-11-21 23:56:39 +08:00
/// <summary>
2015-11-22 15:55:26 +08:00
/// 加载模块下面的所有模块
2015-11-21 23:56:39 +08:00
/// </summary>
2016-10-17 11:43:56 +08:00
public string Load(Guid orgId, int page = 1, int rows = 30)
2015-11-21 23:56:39 +08:00
{
2016-10-17 11:43:56 +08:00
return JsonHelper.Instance.Serialize(_app.Load(orgId, page, rows));
2015-11-21 23:56:39 +08:00
}
2016-10-20 16:47:35 +08:00
/// <summary>
/// 加载用户模块
/// </summary>
/// <param name="firstId">The user identifier.</param>
/// <returns>System.String.</returns>
public string LoadForUser(Guid firstId)
{
var orgs = _app.LoadForUser(firstId);
return JsonHelper.Instance.Serialize(orgs);
}
/// <summary>
/// 加载角色模块
/// </summary>
/// <param name="firstId">The role identifier.</param>
/// <returns>System.String.</returns>
public string LoadForRole(Guid firstId)
{
var orgs = _app.LoadForRole(firstId);
return JsonHelper.Instance.Serialize(orgs);
}
2016-10-29 22:27:39 +08:00
public string LoadModule()
{
2016-07-19 11:44:48 +08:00
var orgs = AuthUtil.GetCurrentUser().Modules.MapToList<ModuleView>();
2015-11-27 18:10:11 +08:00
return JsonHelper.Instance.Serialize(orgs);
}
2015-12-02 16:28:01 +08:00
#region
2016-10-18 17:34:41 +08:00
2015-11-22 21:32:38 +08:00
//添加或修改模块
[HttpPost]
public string Add(Module model)
2015-11-21 23:56:39 +08:00
{
2015-11-22 21:32:38 +08:00
try
{
_app.AddOrUpdate(model);
}
catch (Exception ex)
{
2016-10-18 17:34:41 +08:00
Result.Status = false;
2016-10-14 11:22:16 +08:00
Result.Message = ex.Message;
2015-11-22 21:32:38 +08:00
}
2016-10-14 11:22:16 +08:00
return JsonHelper.Instance.Serialize(Result);
2015-11-21 23:56:39 +08:00
}
2016-10-17 00:20:55 +08:00
[HttpPost]
public string Delete(Guid[] ids)
2015-11-21 23:56:39 +08:00
{
try
{
2016-10-17 00:20:55 +08:00
foreach (var obj in ids)
2015-11-21 23:56:39 +08:00
{
2016-10-17 00:20:55 +08:00
_app.Delete(obj);
2015-11-21 23:56:39 +08:00
}
}
catch (Exception e)
{
2016-10-18 17:34:41 +08:00
Result.Status = false;
2016-10-14 11:22:16 +08:00
Result.Message = e.Message;
2015-11-21 23:56:39 +08:00
}
2016-10-14 11:22:16 +08:00
return JsonHelper.Instance.Serialize(Result);
2015-11-21 23:56:39 +08:00
}
2015-12-02 16:28:01 +08:00
2016-10-18 17:34:41 +08:00
#endregion
2015-11-21 23:56:39 +08:00
}
2015-10-26 21:58:12 +08:00
}