UserService.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using System;
  2. using System.Collections.Generic;
  3. using System.DirectoryServices.Linq;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using GreenTree.Nachtragsmanagement.Core.Authentication;
  8. using GreenTree.Nachtragsmanagement.Core.Data;
  9. using GreenTree.Nachtragsmanagement.Core.Domain.User;
  10. using GreenTree.Nachtragsmanagement.Services.Configuration;
  11. namespace GreenTree.Nachtragsmanagement.Services.User
  12. {
  13. /// <summary>
  14. /// User service
  15. /// </summary>
  16. public class UserService : IUserService
  17. {
  18. #region Fields
  19. private readonly IRepository<Core.Domain.User.User> _userRepository;
  20. private readonly IRepository<Role> _roleRepository;
  21. private readonly IRepository<Function> _functionRepository;
  22. private readonly ActiveDirectoryContext _activeDirectoryContext;
  23. #endregion
  24. #region Ctor
  25. /// <summary>
  26. /// Initializes a new instance of the UserService class
  27. /// </summary>
  28. public UserService(
  29. IRepository<Core.Domain.User.User> userRepository,
  30. IRepository<Role> roleRepository,
  31. IRepository<Function> functionRepository,
  32. ActiveDirectoryContext activeDirectoryContext)
  33. {
  34. _userRepository = userRepository;
  35. _roleRepository = roleRepository;
  36. _functionRepository = functionRepository;
  37. _activeDirectoryContext = activeDirectoryContext;
  38. }
  39. #endregion
  40. #region User
  41. /// <summary>
  42. /// Gets all users
  43. /// </summary>
  44. public IList<Core.Domain.User.User> GetAllUsers()
  45. {
  46. return _userRepository.Table.ToList();
  47. }
  48. /// <summary>
  49. /// Gets a user by specified Id
  50. /// </summary>
  51. /// <param name="id">User identifier.</param>
  52. public Core.Domain.User.User GetUserById(int id)
  53. {
  54. return _userRepository.GetById(id);
  55. }
  56. /// <summary>
  57. /// Gets all users to the specified ids
  58. /// </summary>
  59. public IList<Core.Domain.User.User> GetUsersByIds(int[] ids)
  60. {
  61. return _userRepository.Table
  62. .Where(u => ids.Contains(u.Id))
  63. .ToList();
  64. }
  65. /// <summary>
  66. /// Gets a user by specified custom number
  67. /// </summary>
  68. /// <param name="customNumber">Customer number.</param>
  69. public Core.Domain.User.User GetUserByCustomNumber(string customNumber)
  70. {
  71. return _userRepository
  72. .Table.FirstOrDefault(u => u.CustomNumber == customNumber);
  73. }
  74. /// <summary>
  75. /// Insert a user
  76. /// </summary>
  77. /// <param name="user">User.</param>
  78. public void InsertUser(Core.Domain.User.User user)
  79. {
  80. _userRepository.Insert(user);
  81. }
  82. /// <summary>
  83. /// Update a user
  84. /// </summary>
  85. /// <param name="user">User.</param>
  86. public void UpdateUser(Core.Domain.User.User user)
  87. {
  88. _userRepository.Update(user);
  89. }
  90. /// <summary>
  91. /// Delete a user
  92. /// </summary>
  93. /// <param name="user">User.</param>
  94. public void DeleteUser(Core.Domain.User.User user)
  95. {
  96. _userRepository.Delete(user);
  97. }
  98. #endregion
  99. #region ActiveDirectory
  100. /// <summary>
  101. /// Searches the ActiveDirectory for users matching the searchValue
  102. /// </summary>
  103. /// <param name="searchValue">The text to be searched for.</param>
  104. public IList<ActiveDirectoryUser> SearchUsers(string searchValue)
  105. {
  106. var users = _activeDirectoryContext.Users
  107. .Where(u => u.AccountName.Contains(searchValue) || u.LastName.Contains(searchValue) || u.ForeName.Contains(searchValue))
  108. .ToList();
  109. return users;
  110. }
  111. #endregion
  112. #region Role
  113. /// <summary>
  114. /// Gets all roles
  115. /// </summary>
  116. public IList<Role> GetAllRoles()
  117. {
  118. return _roleRepository.Table.ToList();
  119. }
  120. /// <summary>
  121. /// Gets a role by specified Id
  122. /// </summary>
  123. /// <param name="id">Role identifier.</param>
  124. public Role GetRoleById(int id)
  125. {
  126. return _roleRepository.GetById(id);
  127. }
  128. /// <summary>
  129. /// Gets all roles to the specified ids
  130. /// </summary>
  131. public IList<Role> GetRolesByIds(int[] ids)
  132. {
  133. return _roleRepository.Table
  134. .Where(r => ids.Contains(r.Id))
  135. .ToList();
  136. }
  137. /// <summary>
  138. /// Gets all users to the corresponding role
  139. /// </summary>
  140. /// <param name="roleId">Role id.</param>
  141. public IList<Core.Domain.User.User> GetUsersByRole(int roleId)
  142. {
  143. return _userRepository.Table
  144. .Where(u => u.Roles
  145. .Select(r => r.Id)
  146. .Contains(roleId))
  147. .ToList();
  148. }
  149. /// <summary>
  150. /// Insert a user
  151. /// </summary>
  152. /// <param name="role">Role.</param>
  153. public void InsertRole(Role role)
  154. {
  155. _roleRepository.Insert(role);
  156. }
  157. /// <summary>
  158. /// Update a role
  159. /// </summary>
  160. /// <param name="role">Role.</param>
  161. public void UpdateRole(Role role)
  162. {
  163. _roleRepository.Update(role);
  164. }
  165. /// <summary>
  166. /// Delete a role
  167. /// </summary>
  168. /// <param name="role">Role.</param>
  169. public void DeleteRole(Role role)
  170. {
  171. _roleRepository.Delete(role);
  172. }
  173. #endregion
  174. #region Function
  175. /// <summary>
  176. /// Gets all functions
  177. /// </summary>
  178. public IList<Function> GetAllFunctions()
  179. {
  180. return _functionRepository.Table.ToList();
  181. }
  182. /// <summary>
  183. /// Gets a function by specified Id
  184. /// </summary>
  185. /// <param name="id">Function identifier.</param>
  186. public Function GetFunctionById(int id)
  187. {
  188. return _functionRepository.GetById(id);
  189. }
  190. /// <summary>
  191. /// Gets a function by specified name
  192. /// </summary>
  193. /// <param name="name">Function name.</param>
  194. public Function GetFunctionByName(string name)
  195. {
  196. return
  197. _functionRepository.Table
  198. .FirstOrDefault(f => f.Name == name);
  199. }
  200. /// <summary>
  201. /// Gets all functions to the specified ids
  202. /// </summary>
  203. public IList<Function> GetFunctionsByIds(int[] ids)
  204. {
  205. return _functionRepository.Table
  206. .Where(f => ids.Contains(f.Id))
  207. .ToList();
  208. }
  209. /// <summary>
  210. /// Insert a function
  211. /// </summary>
  212. /// <param name="function">Function.</param>
  213. public void InsertFunction(Function function)
  214. {
  215. _functionRepository.Insert(function);
  216. }
  217. /// <summary>
  218. /// Update a function
  219. /// </summary>
  220. /// <param name="function">Function.</param>
  221. public void UpdateFunction(Function function)
  222. {
  223. _functionRepository.Update(function);
  224. }
  225. /// <summary>
  226. /// Delete a function
  227. /// </summary>
  228. /// <param name="function">Function.</param>
  229. public void DeleteFunction(Function function)
  230. {
  231. _functionRepository.Delete(function);
  232. }
  233. #endregion
  234. }
  235. }