系列目录

距离上次发布22讲已经有少许日子了,真是太抱歉,最近年关项目比较急,时间太紧,没有时间发布.请大家见谅

接下来我们的目标是

  1. 角色组管理
  2. 角色组权限设置
  3. 用户管理
  4. 把角色组授权给用户
  5. 给用户分配角色组

所以最少我们还要讲多5讲才能结束这个管理系统,经过之前的样例程序,我们很熟悉这个套路了,如果你很喜欢这个系列,你可以为这种重复性的动作写一个简单的代码生成器,或者一套强大的T4模版,其实这2个我都有,而且也是刚写的,以后系统完善了,给发布出来。

是不是还可以呢,哈哈,T4我也写了一套,但毕竟还是没有winfrom来得方便。

接下来我们在之前做好的模块管理,在22讲中,添加角色组管理的记录和操作码,如图

目前我是需要添加这么多个操作码。回头就机械性的创建DAL层,BLL层,Model层,还有注入

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Models; namespace App.IDAL
{
public interface ISysRoleRepository
{ IQueryable<SysRole> GetList(DBContainer db);
int Create(SysRole entity);
int Delete(string id);
int Edit(SysRole entity);
SysRole GetById(string id);
bool IsExist(string id);
} }

ISysRoleRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Models;
using App.IDAL;
using System.Data; namespace App.DAL
{
public class SysRoleRepository : IDisposable,ISysRoleRepository
{ public IQueryable<SysRole> GetList(DBContainer db)
{
IQueryable<SysRole> list = db.SysRole.AsQueryable();
return list;
} public int Create(SysRole entity)
{
using (DBContainer db = new DBContainer())
{
db.SysRole.AddObject(entity);
return db.SaveChanges();
}
} public int Delete(string id)
{
using (DBContainer db = new DBContainer())
{
SysRole entity = db.SysRole.SingleOrDefault(a => a.Id == id);
if (entity != null)
{ db.SysRole.DeleteObject(entity);
}
return db.SaveChanges();
}
} public int Edit(SysRole entity)
{
using (DBContainer db = new DBContainer())
{
db.SysRole.Attach(entity);
db.ObjectStateManager.ChangeObjectState(entity, EntityState.Modified);
return db.SaveChanges();
}
} public SysRole GetById(string id)
{
using (DBContainer db = new DBContainer())
{
return db.SysRole.SingleOrDefault(a => a.Id == id);
}
} public bool IsExist(string id)
{
using (DBContainer db = new DBContainer())
{
SysRole entity = GetById(id);
if (entity != null)
return true;
return false;
}
} public void Dispose()
{ }
}
}

SysRoleRepository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App.Models;
using App.Common;
using App.Models.Sys; namespace App.IBLL
{
public interface ISysRoleBLL
{
List<SysRoleModel> GetList(ref GridPager pager, string queryStr);
bool Create(ref ValidationErrors errors, SysRoleModel model);
bool Delete(ref ValidationErrors errors, string id);
bool Edit(ref ValidationErrors errors, SysRoleModel model);
SysRoleModel GetById(string id);
bool IsExist(string id);
}
}

ISysRoleBLL

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Practices.Unity;
using App.Models;
using App.Common;
using System.Transactions;
using App.Models.Sys;
using App.IBLL;
using App.IDAL;
using App.BLL.Core; namespace App.BLL
{
public class SysRoleBLL : BaseBLL, ISysRoleBLL
{
[Dependency]
public ISysRoleRepository m_Rep { get; set; }
public List<SysRoleModel> GetList(ref GridPager pager, string queryStr)
{ IQueryable<SysRole> queryData = null;
if (!string.IsNullOrWhiteSpace(queryStr))
{
queryData = m_Rep.GetList(db).Where(a => a.Name.Contains(queryStr));
}
else
{
queryData = m_Rep.GetList(db);
}
pager.totalRows = queryData.Count();
queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
return CreateModelList(ref queryData);
}
private List<SysRoleModel> CreateModelList(ref IQueryable<SysRole> queryData)
{
List<SysRoleModel> modelList = new List<SysRoleModel>();
foreach (var r in queryData)
{
modelList.Add(new SysRoleModel()
{
Id = r.Id,
Name = r.Name,
Description = r.Description,
CreateTime = r.CreateTime,
CreatePerson = r.CreatePerson,
UserName = ""
});
}
return modelList;
} public bool Create(ref ValidationErrors errors, SysRoleModel model)
{
try
{
SysRole entity = m_Rep.GetById(model.Id);
if (entity != null)
{
errors.Add(Suggestion.PrimaryRepeat);
return false;
}
entity = new SysRole();
entity.Id = model.Id;
entity.Name = model.Name;
entity.Description = model.Description;
entity.CreateTime = model.CreateTime;
entity.CreatePerson = model.CreatePerson;
if (m_Rep.Create(entity) == )
{
//分配给角色
db.P_Sys_InsertSysRight();
//清理无用的项
db.P_Sys_ClearUnusedRightOperate();
return true;
}
else
{
errors.Add(Suggestion.InsertFail);
return false;
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
ExceptionHander.WriteException(ex);
return false;
}
} public bool Delete(ref ValidationErrors errors, string id)
{
try
{
if (m_Rep.Delete(id) == )
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
ExceptionHander.WriteException(ex);
return false;
}
} public bool Edit(ref ValidationErrors errors, SysRoleModel model)
{
try
{
SysRole entity = m_Rep.GetById(model.Id);
if (entity == null)
{
errors.Add(Suggestion.Disable);
return false;
}
entity.Id = model.Id;
entity.Name = model.Name;
entity.Description = model.Description;
entity.CreateTime = model.CreateTime;
entity.CreatePerson = model.CreatePerson; if (m_Rep.Edit(entity) == )
{
return true;
}
else
{
errors.Add(Suggestion.EditFail);
return false;
}
}
catch (Exception ex)
{
errors.Add(ex.Message);
ExceptionHander.WriteException(ex);
return false;
}
} public bool IsExists(string id)
{
if (db.SysRole.SingleOrDefault(a => a.Id == id) != null)
{
return true;
}
return false;
} public SysRoleModel GetById(string id)
{
if (IsExist(id))
{
SysRole entity = m_Rep.GetById(id);
SysRoleModel model = new SysRoleModel();
model.Id = entity.Id;
model.Name = entity.Name;
model.Description = entity.Description;
model.CreateTime = entity.CreateTime;
model.CreatePerson = entity.CreatePerson;
return model;
}
else
{
return null;
}
} public bool IsExist(string id)
{
return m_Rep.IsExist(id);
} }
}

SysRoleBLL

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations; namespace App.Models.Sys
{
public class SysRoleModel
{ public string Id { get; set; } [Display(Name = "角色名称")]
public string Name { get; set; } [Display(Name = "说明")]
public string Description { get; set; }
[Display(Name = "创建时间")]
public DateTime CreateTime { get; set; }
[Display(Name = "创建人")]
public string CreatePerson { get; set; }
[Display(Name = "拥有的用户")]
public string UserName { get; set; }//拥有的用户 public string Flag { get; set; }//用户分配角色
}
}

SysRoleModel

using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using App.Common;
using App.Models;
using Microsoft.Practices.Unity;
using App.IBLL; using App.Models.Sys;
namespace App.Admin.Controllers
{
public class SysRoleController : BaseController
{
//
// GET: /SysRole/
[Dependency]
public ISysRoleBLL m_BLL { get; set; }
ValidationErrors errors = new ValidationErrors(); [SupportFilter]
public ActionResult Index()
{
ViewBag.Perm = GetPermission();
return View();
}
[SupportFilter(ActionName="Index")]
public JsonResult GetList(GridPager pager,string queryStr)
{
List<SysRoleModel> list = m_BLL.GetList(ref pager, queryStr);
var json = new
{
total = pager.totalRows,
rows = (from r in list
select new SysRoleModel()
{ Id = r.Id,
Name = r.Name,
Description = r.Description,
CreateTime = r.CreateTime,
CreatePerson = r.CreatePerson,
UserName = r.UserName }).ToArray() }; return Json(json);
} #region 创建
[SupportFilter]
public ActionResult Create()
{
ViewBag.Perm = GetPermission();
return View();
} [HttpPost]
[SupportFilter]
public JsonResult Create(SysRoleModel model)
{
model.Id = ResultHelper.NewId;
model.CreateTime = ResultHelper.NowTime;
if (model != null && ModelState.IsValid)
{ if (m_BLL.Create(ref errors, model))
{
LogHandler.WriteServiceLog(GetUserId(), "Id" + model.Id + ",Name" + model.Name, "成功", "创建", "SysRole");
return Json(JsonHandler.CreateMessage(, Suggestion.InsertSucceed));
}
else
{
string ErrorCol = errors.Error;
LogHandler.WriteServiceLog(GetUserId(), "Id" + model.Id + ",Name" + model.Name + "," + ErrorCol, "失败", "创建", "SysRole");
return Json(JsonHandler.CreateMessage(, Suggestion.InsertFail + ErrorCol));
}
}
else
{
return Json(JsonHandler.CreateMessage(, Suggestion.InsertFail));
}
}
#endregion #region 修改
[SupportFilter]
public ActionResult Edit(string id)
{
ViewBag.Perm = GetPermission();
SysRoleModel entity = m_BLL.GetById(id);
return View(entity);
} [HttpPost]
[SupportFilter]
public JsonResult Edit(SysRoleModel model)
{
if (model != null && ModelState.IsValid)
{ if (m_BLL.Edit(ref errors, model))
{
LogHandler.WriteServiceLog(GetUserId(), "Id" + model.Id + ",Name" + model.Name, "成功", "修改", "SysRole");
return Json(JsonHandler.CreateMessage(, Suggestion.EditSucceed));
}
else
{
string ErrorCol = errors.Error;
LogHandler.WriteServiceLog(GetUserId(), "Id" + model.Id + ",Name" + model.Name + "," + ErrorCol, "失败", "修改", "SysRole");
return Json(JsonHandler.CreateMessage(, Suggestion.EditFail + ErrorCol));
}
}
else
{
return Json(JsonHandler.CreateMessage(, Suggestion.EditFail));
}
}
#endregion #region 详细
[SupportFilter]
public ActionResult Details(string id)
{
ViewBag.Perm = GetPermission();
SysRoleModel entity = m_BLL.GetById(id);
return View(entity);
} #endregion #region 删除
[HttpPost]
[SupportFilter]
public JsonResult Delete(string id)
{
if (!string.IsNullOrWhiteSpace(id))
{
if (m_BLL.Delete(ref errors, id))
{
LogHandler.WriteServiceLog(GetUserId(), "Id:" + id, "成功", "删除", "SysRole");
return Json(JsonHandler.CreateMessage(, Suggestion.DeleteSucceed));
}
else
{
string ErrorCol = errors.Error;
LogHandler.WriteServiceLog(GetUserId(), "Id" + id + "," + ErrorCol, "失败", "删除", "SysRole");
return Json(JsonHandler.CreateMessage(, Suggestion.DeleteFail + ErrorCol));
}
}
else
{
return Json(JsonHandler.CreateMessage(, Suggestion.DeleteFail));
} }
#endregion }
}

SysRoleController

@using App.Admin;
@using App.Common;
@using App.Models.Sys; @{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Index_Layout.cshtml"; List<permModel> perm = (List<permModel>)ViewBag.Perm;
if (perm == null)
{
perm = new List<permModel>();
}
}
<div class="mvctool">
<input id="txtQuery" type="text" class="searchText"/>
@Html.ToolButton("btnQuery", "icon-search", "查询", perm, "Query", true)
@Html.ToolButton("btnCreate", "icon-add", "新增", perm, "Create", true)
@Html.ToolButton("btnEdit", "icon-edit", "编辑", perm, "Edit", true)
@Html.ToolButton("btnDetails", "icon-details", "详细", perm, "Details", true)
@Html.ToolButton("btnDelete", "icon-remove", "删除", perm, "Delete", true)
@Html.ToolButton("btnAllot", "icon-share", "分配用户", perm, "Allot", true) </div>
<div id="modalwindow" class="easyui-window" data-options="modal:true,closed:true,minimizable:false,shadow:false"></div>
<table id="List"></table>
<script type="text/javascript">
$(function () {
$('#List').datagrid({
url: '@Url.Action("GetList")',
width: $(window).width() - ,
methord: 'post',
height: $(window).height() - ,
fitColumns: true,
sortName: 'CreateTime',
sortOrder: 'desc',
idField: 'Id',
pageSize: ,
pageList: [, , , , ],
pagination: true,
striped: true, //奇偶行是否区分
singleSelect: true,//单选模式
//rownumbers: true,//行号
columns: [[
{ field: 'Id', title: 'ID', width: , hidden: true },
{ field: 'Name', title: '角色名称', width: , sortable: true },
{ field: 'Description', title: '说明', width: , sortable: true },
{ field: 'CreateTime', title: '创建时间', width: , sortable: true },
{ field: 'CreatePerson', title: '创建人', width: , sortable: true },
{ field: 'UserName', title: '属下管理员', width: , sortable: true }
]]
});
});
//ifram 返回
function frameReturnByClose() {
$("#modalwindow").window('close');
}
function frameReturnByReload(flag) {
if (flag)
$("#List").datagrid('load');
else
$("#List").datagrid('reload');
}
function frameReturnByMes(mes) {
$.messageBox5s('提示', mes);
}
$(function () {
$("#btnExport").click(function () {
$("#modalwindow").html("<iframe width='100%' height='98%' scrolling='no' frameborder='0'' src='/SysHelper/ReportControl'></iframe>");
$("#modalwindow").window({ title: '导出', width: , height: , iconCls: 'icon-add' }).window('open');
});
$("#btnCreate").click(function () {
$("#modalwindow").html("<iframe width='100%' height='98%' scrolling='no' frameborder='0'' src='/SysRole/Create'></iframe>");
$("#modalwindow").window({ title: '新增', width: , height: , iconCls: 'icon-add' }).window('open');
});
$("#btnEdit").click(function () {
var row = $('#List').datagrid('getSelected');
if (row != null) {
$("#modalwindow").html("<iframe width='100%' height='99%' frameborder='0' src='/SysRole/Edit?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
$("#modalwindow").window({ title: '编辑', width: , height: , iconCls: 'icon-edit' }).window('open');
} else { $.messageBox5s('提示', '@Suggestion.PlaseChooseToOperatingRecords'); }
});
$("#btnDetails").click(function () {
var row = $('#List').datagrid('getSelected');
if (row != null) {
$("#modalwindow").html("<iframe width='100%' height='98%' scrolling='no' frameborder='0' src='/SysRole/Details?id=" + row.Id + "&Ieguid=" + GetGuid() + "'></iframe>");
$("#modalwindow").window({ title: '详细', width: , height: , iconCls: 'icon-details' }).window('open');
} else { $.messageBox5s('提示', '@Suggestion.PlaseChooseToOperatingRecords'); }
});
$("#btnQuery").click(function () {
var queryStr = $("#txtQuery").val();
if (queryStr == null) {
queryStr = "%";
}
$('#List').datagrid({
url: '@Url.Action("GetList")?queryStr=' + encodeURI(queryStr)
}); });
$("#btnDelete").click(function () {
var row = $('#List').datagrid('getSelected');
if (row != null) {
$.messager.confirm('提示', '@Suggestion.YouWantToDeleteTheSelectedRecords', function (r) {
if (r) {
$.post("@Url.Action("Delete")?id=" + row.Id, function (data) {
if (data.type == )
$("#List").datagrid('load');
$.messageBox5s('提示', data.message);
}, "json"); }
});
} else { $.messageBox5s('提示', '@Suggestion.PlaseChooseToOperatingRecords'); }
});
$("#btnAllot").click(function () { });
});
</script>
@Html.Partial("~/Views/Shared/_Partial_AutoGrid.cshtml")

Index

<!--自动GRID 从第一次加载与重置窗体大小时候发生的事件:分部视图,当只有一个Grid时且ID为List-->
<script type="text/javascript">
$(function () {
$(window).resize(function () {
$('#List').datagrid('resize', {
width: $(window).width() - 10,
height: SetGridHeightSub(39)
}).datagrid('resize', {
width: $(window).width() - 10,
height: SetGridHeightSub(39)
});
}); });
</script>

_Partial_AutoGrid.cshtml

我们注重的是效果,看下

由于我们的用户管理还没做,分配还不能做,所以就先给个空的值吧。

我们能够这么机械性全靠我们的架构,才能这么清晰的分析问题。

做了这么久不知道大家有没有发现,层层扣层层,异常捕获,异常记录,日志记录,反转控制,系统的可维护性非常的高,一目了然,这也是发布文章这么久,没人质疑这个架构的所在之处(我们是不是应该自豪一下

上面的图已经好了,关于记录,那必须是要有新增修改功能了,这个留给大家自己动手做做,因为这节是没有好讲的,这是为了下一节的,权限设置做铺垫而已。

谢谢大家

构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(23)-权限管理系统-角色组模块的更多相关文章

  1. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)

    转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...

  2. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...

  3. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充 系列目录 补充一下,有人要表单的代码,这个用代码生成器生成表Flow_Form表 ...

  4. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支 系列目录 步骤设置完毕之后,就要设置好流转了,比如财务申请大于50000元( ...

  5. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤 系列目录 步骤设计很重要,特别是规则的选择. 我这里分为几个规则 1.按自行 ...

  6. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...

  7. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...

  8. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01 工作流在实际应用中还是比较广泛,网络中存在很多工作流的图形化插件,可以做到拉拽的工 ...

  9. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-【过滤器+Cache】

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-[过滤器+Cache] 系列目录 上次的探讨没有任何结果,我浏览了大量的文章 ...

  10. 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构

    原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...

随机推荐

  1. 微信小程序开发心得

    微信小程序也已出来有一段时间了,最近写了几款微信小程序项目,今天来说说感受. 首先开发一款微信小程序,最主要的就是针对于公司来运营的,因为,在申请appid(微信小程序ID号)时候,需要填写相关的公司 ...

  2. ImageView缩放选项

    ImageView.ScaleType 将图片边界缩放到所在view边界时的缩放选项. Options for scaling the bounds of an image to the bounds ...

  3. shell变量

    定义变量 定义变量时,变量名不加美元符号($),如: variableName="value" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程语言都不一样.同时,变量名 ...

  4. 第一个shell脚本

    打开文本编辑器,新建一个文件,扩展名为sh(sh代表shell),扩展名并不影响脚本执行,见名知意就好. #!/bin/bash echo "Hello World !" &quo ...

  5. ASP.NET MVC5+EF6+EasyUI 后台管理系统(64)-补充WebApi与Unity注入-配置文件

    系列目录 上一篇演示了WebApi利用Unity注入 很多人问我如何用配置文件来配置注入,本节演示如何利用配置文件来注入,道理是一样的,跳转到上一节下载源码一起来动手! 1.打开源码定位到文件Depe ...

  6. Angular企业级开发(3)-Angular MVC实现

    1.MVC介绍 Model-View-Controller 在20世纪80年代为程序语言Smalltalk发明的一种软件架构.MVC模式的目的是实现一种动态的程序设计,使后续对程序的修改和扩展简化,并 ...

  7. 【原创分享·微信支付】 C# MVC 微信支付教程系列之公众号支付

    微信支付教程系列之公众号支付         今天,我们接着讲微信支付的系列教程,前面,我们讲了这个微信红包和扫码支付.现在,我们讲讲这个公众号支付.公众号支付的应用环境常见的用户通过公众号,然后再通 ...

  8. 就这么漂来漂去---一个毕业三个月的java程序员的裸辞风波

    注:这并不是一篇技术文章,而是记录了我这几个月经历的入职,裸辞,找工作的心路历程,简单介绍一个博主的情况,我是16年毕业生,校招进了一家北京的公司,java开发,和很多年轻人一样,干了一段时间,我发现 ...

  9. Hibernate中事务声明

    Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...

  10. Android Studio开发RecyclerView遇到的各种问题以及解决(一)

    以前一直在用ListView,,,最近才看RecyclerView发现好强大.RecyclerView前提是Android版本在5.0以上,本人以前用的是eclipse只支持到4.4.索性就安装一个A ...