API
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RegisterRepository.cs 11KB

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