[f5f7c24] | 1 | using Dal.ApplicationStorage.DataAccess.Abstract;
|
---|
| 2 | using Microsoft.Build.Framework;
|
---|
| 3 | using Microsoft.EntityFrameworkCore;
|
---|
| 4 | using Microsoft.Extensions.Logging;
|
---|
| 5 | using Models.DatabaseModels;
|
---|
| 6 | using Models.DataTransferObjects;
|
---|
| 7 | using ocrent;
|
---|
| 8 | using System;
|
---|
| 9 | using System.Collections.Generic;
|
---|
| 10 | using System.Configuration;
|
---|
| 11 | using System.Linq;
|
---|
| 12 | using System.Security.Principal;
|
---|
| 13 | using System.Text;
|
---|
| 14 | using System.Threading.Tasks;
|
---|
| 15 |
|
---|
| 16 | namespace Dal.ApplicationStorage.DataAccess.Concrete
|
---|
| 17 | {
|
---|
| 18 | public class IdentityDa : IIdentityCustomDa
|
---|
| 19 | {
|
---|
| 20 | private readonly ApiContext _db;
|
---|
| 21 | private static ILogger<IdentityDa> _logger;
|
---|
| 22 |
|
---|
| 23 | public IdentityDa(ApiContext db,ILogger<IdentityDa> logger)
|
---|
| 24 | {
|
---|
| 25 | _db = db;
|
---|
| 26 | _logger = logger;
|
---|
| 27 |
|
---|
| 28 | }
|
---|
| 29 |
|
---|
| 30 | public async Task<bool> Register(RegisterDTO registerInfo)
|
---|
| 31 | {
|
---|
| 32 | try
|
---|
| 33 | {
|
---|
| 34 |
|
---|
| 35 | User user = new User();
|
---|
| 36 |
|
---|
| 37 | user.Email = registerInfo.Email;
|
---|
| 38 | user.FirstName = registerInfo.FirstName;
|
---|
| 39 | user.LastName = registerInfo.LastName;
|
---|
| 40 | user.Username = registerInfo.Username;
|
---|
| 41 | user.Pass = registerInfo.Pass;
|
---|
| 42 | user.CreatedOn = DateOnly.FromDateTime(DateTime.Now);
|
---|
| 43 |
|
---|
| 44 | _db.Users.Add(user);
|
---|
| 45 | await _db.SaveChangesAsync();
|
---|
| 46 |
|
---|
| 47 | var userIdFromDb = await _db.Users.Where(x => x.Email.Equals(registerInfo.Email)).Select(x => x.UserId).FirstOrDefaultAsync();
|
---|
| 48 | if (registerInfo.IsBusinessUser)
|
---|
| 49 | {
|
---|
| 50 | _db.BusinessUsers.Add(new BusinessUser() { UserId = userIdFromDb });
|
---|
| 51 | }
|
---|
| 52 | else
|
---|
| 53 | {
|
---|
| 54 | _db.Clients.Add(new Client()
|
---|
| 55 | {
|
---|
| 56 | UserId = userIdFromDb
|
---|
| 57 | });
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | await _db.SaveChangesAsync();
|
---|
| 61 |
|
---|
| 62 | return true;
|
---|
| 63 | }
|
---|
| 64 | catch (Exception e)
|
---|
| 65 | {
|
---|
| 66 | _logger.LogError(e.Message);
|
---|
| 67 | throw;
|
---|
| 68 | }
|
---|
| 69 | }
|
---|
| 70 | public async Task<LoginDTO> CheckLoginInformation(LoginDTO loginInfo)
|
---|
| 71 | {
|
---|
| 72 | try
|
---|
| 73 | {
|
---|
| 74 | var userFromDb = await _db.Users.Include(x => x.Administrator).Include(x => x.Client).Include(x => x.BusinessUser).Where(x => x.Email.Equals(loginInfo.Email)).FirstOrDefaultAsync();
|
---|
| 75 |
|
---|
| 76 | if(userFromDb != null)
|
---|
| 77 | {
|
---|
| 78 | loginInfo.UserId = userFromDb.UserId;
|
---|
| 79 | loginInfo.ValidEmail = true;
|
---|
| 80 | loginInfo.FirstName = userFromDb.FirstName;
|
---|
| 81 | if (userFromDb.Pass.Equals(loginInfo.Password))
|
---|
| 82 | {
|
---|
| 83 | loginInfo.ValidPassword = true;
|
---|
| 84 | }
|
---|
| 85 | else
|
---|
| 86 | {
|
---|
| 87 | loginInfo.ValidPassword = false;
|
---|
| 88 | }
|
---|
| 89 | if(userFromDb.Administrator != null)
|
---|
| 90 | {
|
---|
| 91 | loginInfo.Claim = "Administrator";
|
---|
| 92 | }
|
---|
| 93 | else if (userFromDb.BusinessUser != null)
|
---|
| 94 | {
|
---|
| 95 | loginInfo.Claim = "BusinessUser";
|
---|
| 96 | }
|
---|
| 97 | else
|
---|
| 98 | {
|
---|
| 99 | loginInfo.Claim = "Client";
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | }
|
---|
| 103 | else
|
---|
| 104 | {
|
---|
| 105 | loginInfo.ValidEmail = false;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | return loginInfo;
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | }
|
---|
| 112 | catch (Exception e)
|
---|
| 113 | {
|
---|
| 114 | _logger.LogError(e.Message);
|
---|
| 115 | throw;
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 | }
|
---|