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

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