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 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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, false);
  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 InsertPerson(UserDto item)
  121. //{
  122. // Individual i = new Individual()
  123. // {
  124. // Name = item.Name,
  125. // Surname = item.Surname,
  126. // User = new User(item.Username, item.Password),
  127. // Email = item.Email,
  128. // CellNumber = item.CellNumber,
  129. // Telephone = item.Telephone
  130. // };
  131. // _dbContext.Add(i);
  132. // Save();
  133. //}
  134. public void Update(User userParam, string password = null)
  135. {
  136. var user = _dbContext.Users.Find(userParam.Id);
  137. if (user == null)
  138. throw new AppException("User not found");
  139. if (userParam.Username != user.Username)
  140. {
  141. // username has changed so check if the new username is already taken
  142. if (_dbContext.Users.Any(x => x.Username == userParam.Username))
  143. throw new AppException("Username " + userParam.Username + " is already taken");
  144. }
  145. // update user properties
  146. user.Username = userParam.Username;
  147. // update password if it was entered
  148. if (!string.IsNullOrWhiteSpace(password))
  149. {
  150. byte[] passwordHash, passwordSalt;
  151. MyCommon.CreatePasswordHash(password, out passwordHash, out passwordSalt);
  152. user.PasswordHash = passwordHash;
  153. user.PasswordSalt = passwordSalt;
  154. }
  155. _dbContext.Users.Update(user);
  156. _dbContext.SaveChanges();
  157. }
  158. [Authorize(Roles = Role.SuperAdmin)]
  159. public IEnumerable<User> GetAllUsers()
  160. {
  161. return _dbContext.Users;
  162. }
  163. [Authorize(Roles = Role.SuperAdmin)]
  164. public IEnumerable<Agency> GetAllAgencies()
  165. {
  166. return _dbContext.Agencies;
  167. }
  168. [Authorize(Roles = Role.SuperAdmin)]
  169. public IEnumerable<Individual> GetAllIndividuals()
  170. {
  171. return _dbContext.Individuals;
  172. }
  173. public User GetById(int id)
  174. {
  175. return _dbContext.Users.Find(id);
  176. }
  177. public Agency GetByAgencyId(int id)
  178. {
  179. return _dbContext.Agencies.Find(id);
  180. }
  181. public Individual GetByIndividualId(int id)
  182. {
  183. return _dbContext.Individuals.Find(id);
  184. }
  185. public void Delete(int id)
  186. {
  187. var user = _dbContext.Users.Find(id);
  188. if (user != null)
  189. {
  190. _dbContext.Users.Remove(user);
  191. _dbContext.SaveChanges();
  192. }
  193. }
  194. public void DeleteAgency(int id)
  195. {
  196. var agency = _dbContext.Agencies.Find(id);
  197. if (agency != null)
  198. {
  199. _dbContext.Remove(agency);
  200. _dbContext.SaveChanges();
  201. }
  202. }
  203. public void DeleteIndividual(int id)
  204. {
  205. var individual = _dbContext.Individuals.Find(id);
  206. if (individual != null)
  207. {
  208. _dbContext.Individuals.Remove(individual);
  209. _dbContext.SaveChanges();
  210. }
  211. }
  212. private void Save()
  213. {
  214. _dbContext.SaveChanges();
  215. }
  216. public string UserDetails(int userId)
  217. {
  218. var individual = _dbContext.Individuals.Where(i => i.UserId == userId).FirstOrDefault();
  219. if (individual == null)
  220. {
  221. var agent = _dbContext.Agents.Where(i => i.UserId == userId).FirstOrDefault();
  222. if (agent != null)
  223. {
  224. return agent.Name + " " + agent.Surname;
  225. }
  226. else
  227. {
  228. return "";
  229. }
  230. }
  231. else
  232. {
  233. return individual.Name + " " + individual.Surname;
  234. }
  235. }
  236. }
  237. }