API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RegisterRepository.cs 7.7KB

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