API
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

RegisterRepository.cs 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. using Microsoft.AspNetCore.Authorization;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnivateProperties_API.Containers.Users;
  5. using UnivateProperties_API.Context;
  6. using UnivateProperties_API.Helpers;
  7. using UnivateProperties_API.Model.Users;
  8. namespace UnivateProperties_API.Repository.Users
  9. {
  10. public class RegisterRepository : IRegisterRepository
  11. {
  12. private readonly DataContext _dbContext;
  13. public RegisterRepository(DataContext dbContext)
  14. {
  15. _dbContext = dbContext;
  16. }
  17. public User Authenticate(string username, string password)
  18. {
  19. if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password))
  20. return null;
  21. var user = _dbContext.Users.SingleOrDefault(x => x.Username == username);
  22. // check if username exists
  23. if (user == null)
  24. throw new AppException("Username is incorrect");
  25. // check if password is correct
  26. if (!MyCommon.VerifyPasswordHash(password, user.PasswordHash, user.PasswordSalt))
  27. throw new AppException("Password is incorrect");
  28. // authentication successful
  29. return user;
  30. }
  31. public User Create(User user, string password, bool save)
  32. {
  33. // validation
  34. if (string.IsNullOrWhiteSpace(password))
  35. throw new AppException("Password is required");
  36. if (_dbContext.Users.Any(x => x.Username == user.Username))
  37. throw new AppException("Username \"" + user.Username + "\" is already taken");
  38. MyCommon.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
  39. user.PasswordHash = passwordHash;
  40. user.PasswordSalt = passwordSalt;
  41. user.Id = NewUserId();
  42. _dbContext.Users.Add(user);
  43. if (save)
  44. {
  45. _dbContext.SaveChanges();
  46. }
  47. return user;
  48. }
  49. public Agency CreateAgency(AgencyDto agency)
  50. {
  51. // validation
  52. if (string.IsNullOrWhiteSpace(agency.EaabeffcNumber))
  53. throw new AppException("eaabeffcNumber is required");
  54. if (_dbContext.Agencies.Any(x => x.EAABEFFCNumber == agency.EaabeffcNumber))
  55. throw new AppException("eaabeffcNumber \"" + agency.EaabeffcNumber + "\" already exists");
  56. Agency a = new Agency()
  57. {
  58. AgencyName = agency.Name,
  59. EAABEFFCNumber = agency.EaabeffcNumber,
  60. CompanyRegNumber = agency.RegNo
  61. };
  62. a.Id = NewAgencyId();
  63. _dbContext.Agencies.Add(a);
  64. CreatePerson(agency.User, PersonType.Agent, false, a);
  65. _dbContext.SaveChanges();
  66. return a;
  67. }
  68. public void CreatePerson(UserDto individual, PersonType personType, bool save, Agency agency)
  69. {
  70. // validation
  71. if (string.IsNullOrWhiteSpace(individual.Password))
  72. throw new AppException("Password is required");
  73. if (_dbContext.Users.Any(x => x.Username == individual.Username))
  74. throw new AppException("Individual \"" + individual.Username + "\" is already taken");
  75. MyCommon.CreatePasswordHash(individual.Password, out byte[] passwordHash, out byte[] passwordSalt);
  76. User createUser = new User(individual.Username, individual.Password);
  77. Create(createUser, individual.Password, false);
  78. if (personType == PersonType.Agent)
  79. {
  80. Agent agent = new Agent()
  81. {
  82. Name = individual.Name,
  83. Surname = individual.Surname,
  84. User = createUser,
  85. Email = individual.Email,
  86. CellNumber = individual.CellNumber,
  87. Telephone = individual.Telephone,
  88. Agency = agency
  89. };
  90. agent.Id = NewAgentId();
  91. _dbContext.Agents.Add(agent);
  92. }
  93. else if (personType == PersonType.Individual)
  94. {
  95. Individual i = new Individual()
  96. {
  97. Name = individual.Name,
  98. Surname = individual.Surname,
  99. User = createUser,
  100. Email = individual.Email,
  101. CellNumber = individual.CellNumber,
  102. Telephone = individual.Telephone
  103. };
  104. i.Id = NewIndividualId();
  105. _dbContext.Individuals.Add(i);
  106. }
  107. if (save)
  108. {
  109. Save();
  110. }
  111. }
  112. public void Update(User userParam, string password = null)
  113. {
  114. var user = _dbContext.Users.Find(userParam.Id);
  115. if (user == null)
  116. throw new AppException("User not found");
  117. if (userParam.Username != user.Username)
  118. {
  119. // username has changed so check if the new username is already taken
  120. if (_dbContext.Users.Any(x => x.Username == userParam.Username))
  121. throw new AppException("Username " + userParam.Username + " is already taken");
  122. }
  123. // update user properties
  124. user.Username = userParam.Username;
  125. // update password if it was entered
  126. if (!string.IsNullOrWhiteSpace(password))
  127. {
  128. MyCommon.CreatePasswordHash(password, out byte[] passwordHash, out byte[] passwordSalt);
  129. user.PasswordHash = passwordHash;
  130. user.PasswordSalt = passwordSalt;
  131. }
  132. _dbContext.Users.Update(user);
  133. _dbContext.SaveChanges();
  134. }
  135. [Authorize(Roles = Role.SuperAdmin)]
  136. public IEnumerable<User> GetAllUsers()
  137. {
  138. return _dbContext.Users;
  139. }
  140. [Authorize(Roles = Role.SuperAdmin)]
  141. public IEnumerable<Agency> GetAllAgencies()
  142. {
  143. return _dbContext.Agencies;
  144. }
  145. [Authorize(Roles = Role.SuperAdmin)]
  146. public IEnumerable<Individual> GetAllIndividuals()
  147. {
  148. return _dbContext.Individuals;
  149. }
  150. public User GetById(int id)
  151. {
  152. return _dbContext.Users.Find(id);
  153. }
  154. public Agency GetByAgencyId(int id)
  155. {
  156. return _dbContext.Agencies.Find(id);
  157. }
  158. public Individual GetByIndividualId(int id)
  159. {
  160. return _dbContext.Individuals.Find(id);
  161. }
  162. public void Delete(int id)
  163. {
  164. var user = _dbContext.Users.Find(id);
  165. if (user != null)
  166. {
  167. _dbContext.Users.Remove(user);
  168. _dbContext.SaveChanges();
  169. }
  170. }
  171. public void DeleteAgency(int id)
  172. {
  173. var agency = _dbContext.Agencies.Find(id);
  174. if (agency != null)
  175. {
  176. _dbContext.Remove(agency);
  177. _dbContext.SaveChanges();
  178. }
  179. }
  180. public void DeleteIndividual(int id)
  181. {
  182. var individual = _dbContext.Individuals.Find(id);
  183. if (individual != null)
  184. {
  185. _dbContext.Individuals.Remove(individual);
  186. _dbContext.SaveChanges();
  187. }
  188. }
  189. private void Save()
  190. {
  191. _dbContext.SaveChanges();
  192. }
  193. public int NewAgencyId()
  194. {
  195. int id = 0;
  196. if (_dbContext.Agencies.Count() > 0)
  197. {
  198. id = _dbContext.Agencies.Max(x => x.Id);
  199. }
  200. id += 1;
  201. return id;
  202. }
  203. public int NewAgentId()
  204. {
  205. int id = 0;
  206. if (_dbContext.Agents.Count() > 0)
  207. {
  208. id = _dbContext.Agents.Max(x => x.Id);
  209. }
  210. id += 1;
  211. return id;
  212. }
  213. public int NewIndividualId()
  214. {
  215. int id = 0;
  216. if (_dbContext.Individuals.Count() > 0)
  217. {
  218. id = _dbContext.Individuals.Max(x => x.Id);
  219. }
  220. id += 1;
  221. return id;
  222. }
  223. public int NewUserId()
  224. {
  225. int id = 0;
  226. if (_dbContext.Users.Count() > 0)
  227. {
  228. id = _dbContext.Users.Max(x => x.Id);
  229. }
  230. id += 1;
  231. return id;
  232. }
  233. }
  234. }