UserRoleLogic.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using CZFW.Framework.Interface;
  2. using CZFW.Framework.Model.Entity;
  3. using CZFW.Framework.Model.ViewModel;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using CZFW.Framework.Model;
  7. namespace CZFW.Framework.Logic
  8. {
  9. public class UserRoleLogic : LogicBase<UserRoleEntity>, IUserRole
  10. {
  11. public UserRoleModel GetModelById(int id)
  12. {
  13. var res = (from ur in GetQueryable()
  14. join role in DbContext.Set<RoleEntity>() on ur.RoleId equals role.Id
  15. join user in DbContext.Set<UserInfoEntity>() on ur.UserId equals user.Id
  16. where ur.Id == ur.Id
  17. select new UserRoleModel()
  18. {
  19. Id = ur.Id,
  20. UserId = ur.UserId,
  21. RoleId = ur.RoleId,
  22. RoleName = role.Name,
  23. UserDisplayName = user.Name,
  24. SortOrder = ur.SortOrder ?? 0
  25. }).FirstOrDefault();
  26. return res;
  27. }
  28. public IList<RoleModel> GetRolesForUser(int userId)
  29. {
  30. var roles = (from ru in DbContext.Set<UserRoleEntity>()
  31. join role in DbContext.Set<RoleEntity>()
  32. on ru.RoleId equals role.Id
  33. where ru.UserId == userId
  34. select new RoleModel()
  35. {
  36. Id = ru.RoleId,
  37. Name = role.Name,
  38. Description = role.Description
  39. }).ToList();
  40. return roles;
  41. }
  42. public IList<SimpleUserInfoModel> GetUsersInRole(int roleId)
  43. {
  44. var users = (from ru in GetQueryable()
  45. join user in DbContext.Set<UserInfoEntity>()
  46. on ru.UserId equals user.Id
  47. where ru.RoleId == roleId
  48. select new SimpleUserInfoModel()
  49. {
  50. Id = ru.UserId,
  51. Name = user.Name,
  52. UserName = user.UserName,
  53. State = user.State
  54. }).ToList();
  55. return users;
  56. }
  57. public ResultModel Revoke(int userId, int roleId)
  58. {
  59. var res = RemoveEntities(x => x.UserId == userId && x.RoleId == roleId);
  60. return res;
  61. }
  62. }
  63. }