简述 本文转自http://yuangang.cnblogs.com并加以整理。 今天我们创建几个与登录用户相关的数据表的接口和实现类
索引 Asp.net MVC项目系列教程
项目开始 一、新建登录用户类Account 之前lesson5我们登录验证直接返回了管理员实体类SYS_USER,但是在实际的后台操作验证中,我们并不需要SYS_USER的许多属性,同时我们需要它的一些扩展属性,我们来新建一个管理员类,来具体的描述这个管理员,我们在Service类库下面新建一个管理类Account.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 using System.Collections.Generic;namespace Service{ public class Account { #region Attribute public int Id { get; set ; } public string Name { get; set ; } public string LogName { get; set ; } public string PassWord { get; set ; } public bool IsAdmin { get; set ; } public string Face_Img { get; set ; } public Domain.SYS_DEPARTMENT DptInfo { get; set ; } public List<Domain.SYS_DEPARTMENT> Dpt { get; set ; } public List<Domain.SYS_PERMISSION> Permissions { get; set ; } public List<Domain.SYS_ROLE> Roles { get; set ; } public List<Domain.SYS_POST_USER> PostUser { get; set ; } public List<Domain.SYS_MODULE> Modules { get; set ; } #endregion } }
二、修改我们的用户管理接口IUserManage 修改用户管理接口IService/SysManage/IUserManage.cs. 添加几个方法:根据用户ID获取本职部门名称、 删除用户、根据用户构造用户基本信息、从Cookie中获取用户信息
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 string GetUserDptName (int id) ;bool Remove (int userId) ;Account GetAccountByUser (Domain.SYS_USER user) ;Account GetAccountByCookie () ;
三、我们分别创建几个关联表的接口和实现类 创建SYS_USERINFO(用户档案)、SYS_USER_ROLE(用户角色)、SYS_USER_PERMISSION(用户权限)、SYS_POST_USER(用户岗位)、SYS_USER_DEPARTMENT(用户部门)、SYS_PERMISSION(权限表)、SYS_DEPARTMENT(部门表)、SYS_MODULE(模块表)的接口和实现类
IUserInfoManage、UserInfoManage
1 2 3 4 5 6 7 8 9 10 11 12 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Service.IService{ public interface IUserInfoManage : IRepository<Domain.SYS_USERINFO> { } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.ServiceImp{ public class UserInfoManage : RepositoryBase<Domain.SYS_USERINFO>, IService.IUserInfoManage { } }
IUserRoleManage、UserRoleManage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading.Tasks;namespace Service.IService{ public interface IUserRoleManage : IRepository<Domain.SYS_USER_ROLE> { bool SetUserRole (int userId, string roleId) ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.ServiceImp{ public class UserRoleManage : RepositoryBase<Domain.SYS_USER_ROLE>, IService.IUserRoleManage { public bool SetUserRole (int userId, string roleId) { try { this .Delete(p => p.FK_USERID == userId); if (string .IsNullOrEmpty(roleId)) return true ; foreach (var entity in roleId.Split(',' ).Select(t => new Domain.SYS_USER_ROLE() { FK_USERID = userId, FK_ROLEID = int .Parse(t) })) { this .dbSet.Add(entity); } return this .Context.SaveChanges() > 0 ; } catch (Exception e) { throw e; } } } }
IPermissionManage、PermissionManage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.IService{ public interface IPermissionManage : IRepository<Domain.SYS_PERMISSION> { List<int > GetPermissionIdBySysId(string sysId); } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 using System;using System.Collections.Generic;using System.Data.Common;using System.Data.SqlClient;using System.Linq;using System.Text;namespace Service.ServiceImp{ public class PermissionManage : RepositoryBase<Domain.SYS_PERMISSION>, IService.IPermissionManage { public List<int > GetPermissionIdBySysId(string sysId) { try { string sql = "select p.id from sys_permission p where exists(select 1 from sys_module t where t.fk_belongsystem=@sysid and t.id=p.moduleid)" ; DbParameter para = new SqlParameter("@sysid" , sysId); return this .SelectBySql<int >(sql, para); } catch (Exception e) { throw e; } } } }
IUserPermissionManage、UserPermissionManage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.IService{ public interface IUserPermissionManage : IRepository<Domain.SYS_USER_PERMISSION> { bool SetUserPermission (int userId, string newper, string sysId) ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 using System;using System.Collections.Generic;using System.Linq;using System.Text;using Service.IService;namespace Service.ServiceImp{ public class UserPermissionManage : RepositoryBase<Domain.SYS_USER_PERMISSION>, IUserPermissionManage { IPermissionManage PermissionManage { get; set ; } public bool SetUserPermission (int userId, string newper, string sysId) { try { var permissionId = this .PermissionManage.GetPermissionIdBySysId(sysId).Cast<int >().ToList(); if (this .IsExist(p => p.FK_USERID == userId && permissionId.Any(e => e == p.FK_PERMISSIONID))) { this .Delete(p => p.FK_USERID == userId && permissionId.Any(e => e == p.FK_PERMISSIONID)); } var str = newper.Trim(',' ).Split(',' ); foreach (var per in str.Select(t => new Domain.SYS_USER_PERMISSION() { FK_USERID = userId, FK_PERMISSIONID = int .Parse(t) })) { this .dbSet.Add(per); } return this .Context.SaveChanges() > 0 ; } catch (Exception e) { throw e; } } } }
IPostUserManage、PostUserManage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.IService{ public interface IPostUserManage : IRepository<Domain.SYS_POST_USER> { List<Domain.SYS_USER> GetUserListByPostId(string postId); List<Domain.SYS_POST> GetPostListByUserId(string userId); bool SavePostUser (int userId, string postId) ; dynamic GetPostNameBySysPostUser (ICollection<Domain.SYS_POST_USER> collection) ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.ServiceImp{ public class PostUserManage : RepositoryBase<Domain.SYS_POST_USER>, IService.IPostUserManage { public List<Domain.SYS_USER> GetUserListByPostId(string postId) { try { string sql = @"select * from sys_user t where exists(select u.fk_userid from sys_post_user u inner join sys_post_department p on u.fk_post_departmentid=p.id where t.id=u.fk_userid and p.fk_post_id in (" + postId + ") group by u.fk_userid)" ; return this .SelectBySql<Domain.SYS_USER>(sql); } catch (Exception e) { throw e.InnerException; } } public List<Domain.SYS_POST> GetPostListByUserId(string userId) { return this .LoadAll(p => userId.Contains(p.FK_USERID.ToString())).Select(p => p.SYS_POST_DEPARTMENT.SYS_POST).ToList(); } public bool SavePostUser (int userId, string postId) { try { if (this .IsExist(p => p.FK_USERID == userId)) { var oldCount = this .LoadAll(p => p.FK_USERID == userId).Select(p => p.FK_POST_DEPARTMENTID).ToList().Cast<int >().ToList(); var newpostId = postId.Trim(',' ).Split(new string [] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(p => int .Parse(p)).ToList(); if (oldCount.Count == newpostId.Count && oldCount.All(newpostId.Contains)) return true ; this .Delete(p => p.FK_USERID == userId); } if (!string .IsNullOrEmpty(postId)) { var list = postId.Split(',' ).Select(item => new Domain.SYS_POST_USER() { FK_USERID = userId, FK_POST_DEPARTMENTID = int .Parse(item) }).ToList(); return this .SaveList(list ) > 0 ; } return true ; } catch (Exception e) { throw e.InnerException; } } public dynamic GetPostNameBySysPostUser (ICollection<Domain.SYS_POST_USER> collection) { string post_departmentid = collection.Select(p => p.FK_POST_DEPARTMENTID).Aggregate(string .Empty, (current, t) => current + "'" + t + "'," ).TrimEnd(',' ); try { string sql = @"select d.name+'-'+p.postname as postname,s.id from sys_department d inner join sys_post_department s on d.id=s.fk_department_id inner join sys_post p on p.id=s.fk_post_id where s.id in (" + post_departmentid + ")" ; return this .ExecuteSqlQuery(sql); } catch (Exception e) { throw e.InnerException; } } } }
IUserDepartmentManage、UserDepartmentManage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.IService{ public interface IUserDepartmentManage : IRepository<Domain.SYS_USER_DEPARTMENT> { List<Domain.SYS_USER> GetUserListByDptId(List<string > dptId); List<Domain.SYS_DEPARTMENT> GetDptListByUserId(int userId); bool SaveUserDpt (int userId, string dptId) ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.ServiceImp{ public class UserDepartmentManage : RepositoryBase<Domain.SYS_USER_DEPARTMENT>, IService.IUserDepartmentManage { public List<Domain.SYS_USER> GetUserListByDptId(List<string > dptId) { return this .LoadAll(p => dptId.Contains(p.DEPARTMENT_ID)).Select(p => p.SYS_USER).ToList(); } public List<Domain.SYS_DEPARTMENT> GetDptListByUserId(int userId) { return this .LoadAll(p => p.USER_ID == userId).Select(p => p.SYS_DEPARTMENT).ToList(); } public bool SaveUserDpt (int userId, string dptId) { try { if (this .IsExist(p => p.USER_ID == userId)) { var oldCount = this .LoadAll(p => p.USER_ID == userId && dptId.Contains(p.DEPARTMENT_ID)).Select(p => p.DEPARTMENT_ID).ToList(); var newdptid = dptId.Split(',' ).OrderBy(c => c).ToList(); if (oldCount.Count == newdptid.Count && oldCount.All(newdptid.Contains)) return true ; this .Delete(p => p.USER_ID == userId); } if (!string .IsNullOrEmpty(dptId)) { var list = dptId.Split(',' ).Select(item => new Domain.SYS_USER_DEPARTMENT() { DEPARTMENT_ID = item, USER_ID = userId }).ToList(); return this .SaveList(list ) > 0 ; } return true ; } catch (Exception e) { throw e.InnerException; } } } }
IDepartmentManage、DepartmentManage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.IService{ public interface IDepartmentManage : IRepository<Domain.SYS_DEPARTMENT> { List<Domain.SYS_DEPARTMENT> RecursiveDepartment(List<Domain.SYS_DEPARTMENT> list ); List<Domain.SYS_DEPARTMENT> RecursiveDepartment(string parentId); string CreateCode (string parentCode) ; bool DepartmentIsExists (string idlist) ; string GetDepartmentName (string id) ; object GetDepartmentName (string name, decimal? level) ; System.Collections.IList GetDepartmentByDetail () ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 using Common.JsonHelper;using System;using System.Collections;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.ServiceImp{ public class DepartmentManage : RepositoryBase<Domain.SYS_DEPARTMENT>, IService.IDepartmentManage { public string CreateCode (string parentId) { string _strCode = string .Empty; #region 验证上级部门code是否为空,为空返回,第一级部门的Code if (string .IsNullOrEmpty(parentId)) { var query = this .LoadAll(p => p.PARENTID == null || p.PARENTID == "" ).OrderBy(p => p.CODE).ToList(); if (!query.Any()) { return "001" ; } for (int i = 1 ; i <= query.Count; i++) { string code = query[i - 1 ].CODE; if (string .IsNullOrEmpty(code)) { return FormatCode(i); } if (i != int .Parse(code)) { return FormatCode(i); } } return FormatCode(query.Count + 1 ); } #endregion #region 上级部门不为空,返回当前上级部门下的部门Code var parentDpt = this .Get(p => p.ID == parentId); if (parentDpt != null) { var queryable = this .LoadAll(p => p.CODE.Length == parentDpt.CODE.Length + 3 && p.PARENTID == parentId).OrderBy(p => p.CODE).ToList(); if (queryable.Any()) { for (int i = 1 ; i <= queryable.Count; i++) { string _code = queryable[i - 1 ].CODE; _code = _code.Substring(parentDpt.CODE.Length); int _intCode = 0 ; Int32.TryParse(_code, out _intCode); if (i != _intCode) { _strCode = parentDpt.CODE + FormatCode(i); return _strCode; } } _strCode = parentDpt.CODE + FormatCode(queryable.Count + 1 ); } else { _strCode = parentDpt.CODE + FormatCode(1 ); return _strCode; } } #endregion return _strCode; } public string FormatCode (int dptCode) { try { string _strCode = string .Empty; if (dptCode <= 9 && dptCode >= 1 ) { return "00" + dptCode; } else if (dptCode <= 99 && dptCode > 9 ) { return "0" + dptCode; } else if (dptCode <= 999 && dptCode > 99 ) { return _strCode; } return string .Empty; } catch (Exception ex) { throw ex; } } public bool DepartmentIsExists (string idlist) { return this .IsExist(p => idlist.Contains(p.PARENTID)); } public List<Domain.SYS_DEPARTMENT> RecursiveDepartment(List<Domain.SYS_DEPARTMENT> list ) { var result = new List<Domain.SYS_DEPARTMENT>(); if (list .Count > 0 ) { ChildDepartment(list , result, null); } return result; } public List<Domain.SYS_DEPARTMENT> RecursiveDepartment(string parentId) { var list = this .LoadAll(null); var result = new List<Domain.SYS_DEPARTMENT>(); if (list .Any()) { result.AddRange(list .Where(p => p.ID == parentId).ToList()); if (!string .IsNullOrEmpty(parentId)) ChildDepartment(list .ToList(), result, parentId); else ChildDepartment(list .ToList(), result, null); } return result; } private void ChildDepartment (List<Domain.SYS_DEPARTMENT> newlist, List<Domain.SYS_DEPARTMENT> list , string id) { var result = newlist.Where(p => p.PARENTID == id).OrderBy(p => p.CODE).ThenBy(p => p.SHOWORDER).ToList(); if (result.Any()) { for (int i = 0 ; i < result.Count(); i++) { list .Add(result[i]); ChildDepartment(newlist, list , result[i].ID); } } } public string GetDepartmentName (string id) { var query = this .LoadAll(p => p.ID == id); if (query == null || !query.Any()) return "" ; return query.First().NAME; } public object GetDepartmentName (string name, decimal? level) { if (level > 1 ) { string nbsp = " " ; for (int i = 0 ; i < level; i++) { nbsp += " " ; } name = nbsp + "|--" + name; } return name; } public IList GetDepartmentByDetail () { var list = RecursiveDepartment(this .LoadAll(null).ToList()) .Select(p => new { id = p.ID, code = p.CODE, name = GetDepartmentName(p.NAME, p.BUSINESSLEVEL) }).ToList(); return JsonConverter.JsonClass(list ); } } }
IModuleManage、ModuleManage
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.IService{ public interface IModuleManage : IRepository<Domain.SYS_MODULE> { List<Domain.SYS_MODULE> GetModule(int userId, List<Domain.SYS_PERMISSION> permission, string siteId); List<Domain.SYS_MODULE> RecursiveModule(List<Domain.SYS_MODULE> list ); bool MoreModifyModule (int moduleId, int levels) ; } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 using Common.JsonHelper;using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Service.ServiceImp{ public class ModuleManage : RepositoryBase<Domain.SYS_MODULE>, IService.IModuleManage { public List<Domain.SYS_MODULE> GetModule(int userId, List<Domain.SYS_PERMISSION> permission, string siteId) { var retmodule = new List<Domain.SYS_MODULE>(); var permodule = new List<Domain.SYS_MODULE>(); if (permission != null) { permodule.AddRange(permission.Select(p => p.SYS_MODULE)); permodule = permodule.Distinct(new ModuleDistinct()).ToList(); } permodule = permodule.Where(p => p.ISSHOW == 1 ).ToList(); var prevModule = this .LoadListAll(null); if (permodule.Count > 0 ) { foreach (var item in permodule) { RecursiveModule(prevModule, retmodule, item.PARENTID); retmodule.Add(item); } } retmodule = retmodule.Distinct(new ModuleDistinct()).ToList(); return retmodule.OrderBy(p => p.LEVELS).ThenBy(p => p.SHOWORDER).ToList(); } private void RecursiveModule (List<Domain.SYS_MODULE> PrevModule, List<Domain.SYS_MODULE> retmodule, int ? parentId) { var result = PrevModule.Where(p => p.ID == parentId); if (result != null) { foreach (var item in result) { retmodule.Add(item); RecursiveModule(PrevModule, retmodule, item.PARENTID); } } } public List<Domain.SYS_MODULE> RecursiveModule(List<Domain.SYS_MODULE> list ) { List<Domain.SYS_MODULE> result = new List<Domain.SYS_MODULE>(); if (list != null && list .Count > 0 ) { ChildModule(list , result, 0 ); } return result; } private void ChildModule (List<Domain.SYS_MODULE> list , List<Domain.SYS_MODULE> newlist, int parentId) { var result = list .Where(p => p.PARENTID == parentId).OrderBy(p => p.LEVELS).OrderBy(p => p.SHOWORDER).ToList(); if (result.Count() > 0 ) { for (int i = 0 ; i < result.Count(); i++) { newlist.Add(result[i]); ChildModule(list , newlist, result[i].ID); } } } public bool MoreModifyModule (int moduleId, int levels) { var ChildModule = this .LoadAll(p => p.PARENTID == moduleId).ToList(); if (ChildModule.Any()) { foreach (var item in ChildModule) { item.LEVELS = levels + 1 ; this .Update(item); MoreModifyModule(item.ID, item.LEVELS); } } return true ; } public dynamic LoadModuleInfo (int id) { return JsonConverter.JsonClass(this .LoadAll(p => p.PARENTID == id).OrderBy(p => p.ID).Select(p => new { p.ID, p.NAME }).ToList()); } } public class ModuleDistinct : IEqualityComparer<Domain.SYS_MODULE> { public bool Equals (Domain.SYS_MODULE x, Domain.SYS_MODULE y) { return x.ID == y.ID; } public int GetHashCode (Domain.SYS_MODULE obj) { return obj.ToString().GetHashCode(); } } }
四、修改UserManage实现类 为UserManage增加多个属性,后面通过Spring注入。最后增加权限去重class,暂时也放在UserManage类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 using Service.IService;using System.Linq;using Domain;using System;using Common.JsonHelper;using System.Collections.Generic;namespace Service.ServiceImp{ public class UserManage : RepositoryBase<SYS_USER>, IUserManage { IDepartmentManage DepartmentManage { get; set ; } IUserDepartmentManage UserDepartmentManage { get; set ; } IUserInfoManage UserInfoManage { get; set ; } IUserRoleManage UserRoleManage { get; set ; } IUserPermissionManage UserPermissionManage { get; set ; } IPostUserManage PostUserManage { get; set ; } IPermissionManage PermissionManage { get; set ; } public SYS_USER UserLogin (string userAccount, string password) { var entity = this .Get(p => p.ACCOUNT == userAccount); if (entity != null && new Common.CryptHelper.AESCrypt().Decrypt(entity.PASSWORD) == password) { return entity; } return null; } public bool IsAdmin (int userId) { SYS_USER entity = this .Get(p => p.ID == userId); if (entity == null) return false ; var roles = entity.SYS_USER_ROLE.Select(p => new SYS_ROLE { ID = p.SYS_ROLE.ID }); return roles.ToList().Any(item => item.ID == Common.Enums.ClsDic.DicRole["超级管理员" ]); } public string GetUserName (int Id) { var query = this .LoadAll(c => c.ID == Id); if (query == null || !query.Any()) { return "" ; } return query.First().NAME; } public string GetUserDptName (int id) { if (id <= 0 ) return "" ; var dptid = this .Get(p => p.ID == id).DPTID; return this .DepartmentManage.Get(p => p.ID == dptid).NAME; } public bool Remove (int userId) { try { if (this .UserInfoManage.IsExist(p => p.USERID == userId)) { this .UserInfoManage.Delete(p => p.USERID == userId); } if (this .UserRoleManage.IsExist(p => p.FK_USERID == userId)) { this .UserRoleManage.Delete(p => p.FK_USERID == userId); } if (this .UserPermissionManage.IsExist(p => p.FK_USERID == userId)) { this .UserPermissionManage.Delete(p => p.FK_USERID == userId); } if (this .PostUserManage.IsExist(p => p.FK_USERID == userId)) { this .PostUserManage.Delete(p => p.FK_USERID == userId); } if (this .UserDepartmentManage.IsExist(p => p.USER_ID == userId)) { this .UserDepartmentManage.Delete(p => p.USER_ID == userId); } if (this .IsExist(p => p.ID == userId)) { this .Delete(p => p.ID == userId); } return true ; } catch (Exception e) { throw e.InnerException; } } public Account GetAccountByUser (SYS_USER users) { if (users == null) return null; var permission = GetPermissionByUser(users); var role = users.SYS_USER_ROLE.Select(p => p.SYS_ROLE).ToList(); var dpt = users.SYS_USER_DEPARTMENT.Select(p => p.SYS_DEPARTMENT).ToList(); var post = users.SYS_POST_USER.ToList(); var dptInfo = this .DepartmentManage.Get(p => p.ID == users.DPTID); var module = permission.Select(p => p.SYS_MODULE).ToList().Distinct(new ModuleDistinct()).ToList(); Account account = new Account() { Id = users.ID, Name = users.NAME, LogName = users.ACCOUNT, PassWord = users.PASSWORD, IsAdmin = IsAdmin(users.ID), DptInfo = dptInfo, Dpt = dpt, Face_Img = users.FACE_IMG, Permissions = permission, Roles = role, PostUser = post, Modules = module }; return account; } public Account GetAccountByCookie () { var cookie = Common.CookieHelper.GetCookie("cookie_rememberme" ); if (cookie != null) { if (!string .IsNullOrEmpty(cookie.Value)) { var cookievalue = new Common.CryptHelper.AESCrypt().Decrypt(cookie.Value); if (!JsonSplit.IsJson(cookievalue)) return null; try { var jsonFormat = JsonConverter.ConvertJson(cookievalue); if (jsonFormat != null) { var users = UserLogin(jsonFormat.username, new Common.CryptHelper.AESCrypt().Decrypt(jsonFormat.password)); if (users != null) return GetAccountByUser(users); } } catch { return null; } } } return null; } private List<SYS_PERMISSION> GetPermissionByUser(SYS_USER users) { if (IsAdmin(users.ID)) return PermissionManage.LoadListAll(null); var perlist = new List<SYS_PERMISSION>(); perlist.AddRange(users.SYS_USER_PERMISSION.Select(p => p.SYS_PERMISSION).ToList()); perlist.AddRange(users.SYS_USER_ROLE.Select(p => p.SYS_ROLE.SYS_ROLE_PERMISSION.Select(c => c.SYS_PERMISSION)).SelectMany(c => c.Select(e => e)).Cast<SYS_PERMISSION>().ToList()); perlist = perlist.Distinct(new PermissionDistinct()).ToList(); return perlist; } } public class PermissionDistinct : IEqualityComparer<SYS_PERMISSION> { public bool Equals (SYS_PERMISSION x, SYS_PERMISSION y) { return x.ID == y.ID; } public int GetHashCode (SYS_PERMISSION obj) { return obj.ToString().GetHashCode(); } } }
五、修改spring配置(Service/Config/Service.xml) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 <object id ="Service.User" type ="Service.ServiceImp.UserManage,Service" singleton ="false" > <property name ="UserInfoManage" ref ="Service.UserInfo" /> <property name ="UserRoleManage" ref ="Service.UserRole" /> <property name ="UserPermissionManage" ref ="Service.UserPermission" /> <property name ="PostUserManage" ref ="Service.PostUser" /> <property name ="PermissionManage" ref ="Service.Permission" /> <property name ="DepartmentManage" ref ="Service.Department" /> <property name ="UserDepartmentManage" ref ="Service.UserDepartment" /> </object > <object id ="Service.Module" type ="Service.ServiceImp.ModuleManage,Service" singleton ="false" > </object > <object id ="Service.UserInfo" type ="Service.ServiceImp.UserInfoManage,Service" singleton ="false" > </object > <object id ="Service.UserRole" type ="Service.ServiceImp.UserRoleManage,Service" singleton ="false" > </object > <object id ="Service.PostUser" type ="Service.ServiceImp.PostUserManage,Service" singleton ="false" > </object > <object id ="Service.UserPermission" type ="Service.ServiceImp.UserPermissionManage,Service" singleton ="false" > </object > <object id ="Service.Permission" type ="Service.ServiceImp.PermissionManage,Service" singleton ="false" > </object > <object id ="Service.Department" type ="Service.ServiceImp.DepartmentManage,Service" singleton ="false" > </object > <object id ="Service.UserDepartment" type ="Service.ServiceImp.UserDepartmentManage,Service" singleton ="false" > </object >