/Xoohoo.Repositories.SqlServerEF/UserRepository.cs

# · C# · 330 lines · 285 code · 37 blank · 8 comment · 40 complexity · fa4166021c61b5e14122031ddb2eacb4 MD5 · raw file

  1. using System;
  2. using System.Data;
  3. using System.Data.Entity;
  4. using System.Linq;
  5. using Xoohoo.Extensions;
  6. using Xoohoo.Models.InputModels;
  7. using XM = Xoohoo.Models;
  8. using Xoohoo.Repositories.SqlServerEF.Entities;
  9. using System.Linq.Expressions;
  10. using System.Data.SqlClient;
  11. using System.Collections.Generic;
  12. using System.Collections;
  13. namespace Xoohoo.Repositories.SqlServerEF
  14. {
  15. public class UserRepository : RepositoryBase<User>, IUserRepository
  16. {
  17. private readonly Expression<Func<User, XM.User>> _selector;
  18. public UserRepository()
  19. {
  20. _selector = u => new XM.User
  21. {
  22. UserID = u.UserID,
  23. Username = u.Username,
  24. DisplayName = u.DisplayName,
  25. Email = u.Email,
  26. Password = u.Password,
  27. CreationDate = u.CreationDate,
  28. Status = (XM.UserStatus)u.Status,
  29. UserGroup = new XM.UserGroupBasic
  30. {
  31. UserGroupID = u.UserGroup.UserGroupID,
  32. UserGroupName = u.UserGroup.UserGroupName,
  33. IsSystem = u.UserGroup.IsSystem
  34. },
  35. Roles = from ur in u.Roles
  36. select new XM.RoleBasic
  37. {
  38. RoleID = ur.RoleID,
  39. RoleName = ur.RoleName,
  40. IsSystem = ur.IsSystem,
  41. DisplayOrder = ur.DisplayOrder
  42. },
  43. Permissions = from up in u.Permissions
  44. select new XM.PermissionBasic
  45. {
  46. ModuleName = up.ModuleName,
  47. PermissionID = up.PermissionID,
  48. PermissionName = up.PermissionName
  49. },
  50. UserGroupRoles = from ugr in u.UserGroup.Roles
  51. select new XM.RoleBasic
  52. {
  53. RoleID = ugr.RoleID,
  54. RoleName = ugr.RoleName,
  55. IsSystem = ugr.IsSystem,
  56. DisplayOrder = ugr.DisplayOrder
  57. },
  58. };
  59. }
  60. #region IUserRepository 成员
  61. public XM.User GetItem(string name,XM.UserStatus status)
  62. {
  63. XM.User user = DbSet.Where(m => m.Status == (int)status).Select(_selector).FirstOrDefault(m => m.Username == name);
  64. ProjectUser(user);
  65. return user;
  66. }
  67. public XM.User GetItem(int userID)
  68. {
  69. XM.User user = DbSet.Select(_selector).FirstOrDefault(m => m.UserID == userID);
  70. ProjectUser(user);
  71. return user;
  72. }
  73. public XM.User GetItem(string name)
  74. {
  75. XM.User user = DbSet.Select(_selector).FirstOrDefault(m => m.Username == name);
  76. ProjectUser(user);
  77. return user;
  78. }
  79. public bool IsExists(string name)
  80. {
  81. return DbSet.Any(m=>m.Username == name);
  82. }
  83. public bool IsExists(int userID)
  84. {
  85. return DbSet.Any(m => m.UserID == userID);
  86. }
  87. public bool ValidateUser(int userID, string name)
  88. {
  89. return DbSet.Any(m => m.UserID != userID&&m.Username==name);
  90. }
  91. public XM.IPagedList<XM.UserBasic> GetList(XM.UserSearchCriteria criteria, XM.PagingInfo pageingInfo)
  92. {
  93. Expression<Func<User, XM.UserBasic>> selector = u => new XM.UserBasic
  94. {
  95. UserID = u.UserID,
  96. Username = u.Username,
  97. DisplayName = u.DisplayName,
  98. Email = u.Email,
  99. CreationDate = u.CreationDate,
  100. Status = (XM.UserStatus)u.Status,
  101. UserGroup = new XM.UserGroupBasic
  102. {
  103. UserGroupID = u.UserGroup.UserGroupID,
  104. UserGroupName = u.UserGroup.UserGroupName,
  105. IsSystem = u.UserGroup.IsSystem
  106. },
  107. };
  108. IQueryable<User> list = DbSet;
  109. if (criteria != null)
  110. {
  111. if (criteria.UserGroupID.HasValue)
  112. list = list.Where(m=>m.UserGroupID==criteria.UserGroupID.Value);
  113. if (criteria.Status.HasValue)
  114. {
  115. int status = (int)criteria.Status.Value;
  116. list = list.Where(m => m.Status == status);
  117. }
  118. if (!criteria.Keyword.IsNullOrWhiteSpace())
  119. list = list.Where(m=>m.Username.Contains(criteria.Keyword)||m.DisplayName.Contains(criteria.Keyword));
  120. }
  121. int count = list.Count();
  122. return new XM.PagedList<XM.UserBasic>(list
  123. .OrderBy(m=>m.CreationDate)
  124. .Skip(pageingInfo.Index * pageingInfo.Size)
  125. .Take(pageingInfo.Size)
  126. .Select(selector)
  127. .ToList(),
  128. pageingInfo.Index,
  129. pageingInfo.Size,
  130. count);
  131. }
  132. public XM.User Save(UserInput userInput)
  133. {
  134. User userToSave;
  135. if (userInput.UserID.HasValue)
  136. {
  137. userToSave = DbSet.FirstOrDefault(m => m.UserID == userInput.UserID.Value);
  138. if (userToSave == null) return null;
  139. }
  140. else
  141. {
  142. userToSave = DbSet.Create();
  143. DbSet.Add(userToSave);
  144. userToSave.Status = (int)XM.UserStatus.Normal;
  145. userToSave.CreationDate = DateTime.Now;
  146. }
  147. userToSave.UserGroupID = userInput.UserGroupID;
  148. userToSave.Username = userInput.Username;
  149. userToSave.DisplayName = userInput.DisplayName;
  150. userToSave.Email = userInput.Email;
  151. #region 用户角色
  152. //移除项
  153. if (!userToSave.Roles.IsNullOrEmpty())
  154. {
  155. if (!userInput.Roles.IsNullOrEmpty())
  156. {
  157. List<Role> roleToRemove = (from p in userToSave.Roles
  158. where !userInput.Roles.Contains(p.RoleID)
  159. select p).ToList();
  160. for (int i = 0; i < roleToRemove.Count; i++)
  161. userToSave.Roles.Remove(roleToRemove[i]);
  162. }
  163. else
  164. {
  165. userToSave.Roles.Clear();
  166. }
  167. }
  168. //添加项
  169. if (!userInput.Roles.IsNullOrEmpty())
  170. {
  171. //要添加的ID集
  172. List<Guid> roleIDToAdd = (from p in userInput.Roles
  173. where !userToSave.Roles.Any(m => m.RoleID == p)
  174. select p).ToList();
  175. //要添加的项
  176. List<Role> roleToAdd = (from p in DB.Roles
  177. where roleIDToAdd.Contains(p.RoleID)
  178. select p).ToList();
  179. foreach (var item in roleToAdd)
  180. userToSave.Roles.Add(item);
  181. }
  182. #endregion
  183. #region 用户权限
  184. //移除项
  185. if (!userToSave.Permissions.IsNullOrEmpty())
  186. {
  187. if (!userInput.Permissions.IsNullOrEmpty())
  188. {
  189. List<Permission> permissionToRemove = (from p in userToSave.Permissions
  190. where !userInput.Permissions.Contains(p.PermissionID)
  191. select p).ToList();
  192. for (int i = 0; i < permissionToRemove.Count; i++)
  193. userToSave.Permissions.Remove(permissionToRemove[i]);
  194. }
  195. else
  196. {
  197. userToSave.Permissions.Clear();
  198. }
  199. }
  200. //添加项
  201. if (!userInput.Permissions.IsNullOrEmpty())
  202. {
  203. //要添加的ID集
  204. List<Guid> permissionIDToAdd = (from p in userInput.Permissions
  205. where !userToSave.Permissions.Any(m => m.PermissionID == p)
  206. select p).ToList();
  207. //要添加的项
  208. List<Permission> permissionToAdd = (from p in DB.Permissions
  209. where permissionIDToAdd.Contains(p.PermissionID)
  210. select p).ToList();
  211. foreach (var item in permissionToAdd)
  212. userToSave.Permissions.Add(item);
  213. }
  214. #endregion
  215. DB.SaveChanges();
  216. return GetItem(userToSave.Username);
  217. }
  218. public bool SetPassword(int userID, string password)
  219. {
  220. var user = DbSet.FirstOrDefault(m => m.UserID == userID);
  221. if (user == null)
  222. return false;
  223. user.Password = password;
  224. DB.SaveChanges();
  225. return true;
  226. }
  227. public bool Remove(int userID)
  228. {
  229. User user = DbSet.FirstOrDefault(m => m.UserID == userID);
  230. if (user == null) return false;
  231. string sql = "Delete UserRoleRelationShip Where UserID=@UserID";
  232. DB.Database.ExecuteSqlCommand(sql,new SqlParameter("UserID",userID));
  233. sql = "Delete UserPermissionRelationShip Where UserID=@UserID";
  234. DB.Database.ExecuteSqlCommand(sql,new SqlParameter("UserID",userID));
  235. sql = "Delete UserModuleData Where UserID=@UserID";
  236. DB.Database.ExecuteSqlCommand(sql,new SqlParameter("UserID",userID));
  237. DbSet.Remove(user);
  238. DB.SaveChanges();
  239. return true;
  240. }
  241. public bool ChangeStatus(int userID, XM.UserStatus status)
  242. {
  243. User user = DbSet.FirstOrDefault(m => m.UserID == userID);
  244. if (user == null) return false;
  245. user.Status = (int)status;
  246. DB.SaveChanges();
  247. return true;
  248. }
  249. #endregion
  250. #region Private Methods
  251. private void ProjectUser(XM.User user)
  252. {
  253. if (user == null) return;
  254. SetIdentity(user);
  255. SetRolePermissions(user);
  256. SetUserGroupRolePermissions(user);
  257. }
  258. private void SetIdentity(XM.User user)
  259. {
  260. user.Identity = new Xoohoo.Infrastructure.UserIdentity(null, true, user.Username);
  261. }
  262. private void SetRolePermissions(XM.User user)
  263. {
  264. if (user.Roles.IsNullOrEmpty()) return;
  265. var userRoles = user.Roles.Select(m => m.RoleID);
  266. var permissions = from r in DB.Roles
  267. from p in r.Permissions
  268. where userRoles.Contains(r.RoleID)
  269. select new XM.PermissionBasic
  270. {
  271. ModuleName = p.ModuleName,
  272. PermissionID = p.PermissionID,
  273. PermissionName = p.PermissionName
  274. };
  275. user.RolePermissions = permissions.ToList();
  276. }
  277. private void SetUserGroupRolePermissions(XM.User user)
  278. {
  279. var permissions = from ug in DB.UserGroups
  280. from r in ug.Roles
  281. from p in r.Permissions
  282. where ug.UserGroupID == user.UserGroup.UserGroupID
  283. select new XM.PermissionBasic
  284. {
  285. ModuleName = p.ModuleName,
  286. PermissionID = p.PermissionID,
  287. PermissionName = p.PermissionName
  288. };
  289. user.UserGroupRolePermissions = permissions.ToList();
  290. }
  291. #endregion
  292. }
  293. }