1 | using FarmatikoData.FarmatikoRepoInterfaces;
|
---|
2 | using FarmatikoData.Models;
|
---|
3 | using Microsoft.Extensions.Logging;
|
---|
4 | using System;
|
---|
5 | using System.Collections.Generic;
|
---|
6 | using System.Linq;
|
---|
7 | using System.Text;
|
---|
8 |
|
---|
9 | namespace FarmatikoServices.Auth
|
---|
10 | {
|
---|
11 | public class AuthService : IAuthService
|
---|
12 | {
|
---|
13 | private readonly ILogger<AuthService> _logger;
|
---|
14 | private readonly IRepository _repository;
|
---|
15 |
|
---|
16 | private readonly IDictionary<string, string> _users = new Dictionary<string, string>();
|
---|
17 |
|
---|
18 | // inject your database here for user validation
|
---|
19 | public AuthService(ILogger<AuthService> logger, IRepository repository)
|
---|
20 | {
|
---|
21 | _logger = logger;
|
---|
22 | _repository = repository;
|
---|
23 | }
|
---|
24 |
|
---|
25 | public bool IsValidUserCredentials(string userName, string password)
|
---|
26 | {
|
---|
27 | _logger.LogInformation($"Validating user [{userName}]");
|
---|
28 | if (string.IsNullOrWhiteSpace(userName))
|
---|
29 | {
|
---|
30 | return false;
|
---|
31 | }
|
---|
32 |
|
---|
33 | if (string.IsNullOrWhiteSpace(password))
|
---|
34 | {
|
---|
35 | return false;
|
---|
36 | }
|
---|
37 |
|
---|
38 | return _repository.GetUsers().TryGetValue(userName, out var p) && p.Password == password;
|
---|
39 | }
|
---|
40 |
|
---|
41 | public bool IsAnExistingUser(string userName)
|
---|
42 | {
|
---|
43 | return _repository.GetUsers().ContainsKey(userName);
|
---|
44 | }
|
---|
45 |
|
---|
46 | public string GetUserRole(string userName)
|
---|
47 | {
|
---|
48 | if (!IsAnExistingUser(userName))
|
---|
49 | {
|
---|
50 | return string.Empty;
|
---|
51 | }
|
---|
52 |
|
---|
53 | var user = _repository.GetUsers().Where(x => x.Value.Email == userName).FirstOrDefault().Value.UserRole;
|
---|
54 |
|
---|
55 | /*if (userName == "admin@farmatiko.mk")
|
---|
56 | {
|
---|
57 | return nameof(user);
|
---|
58 | }*/
|
---|
59 | return user.ToString();
|
---|
60 | }
|
---|
61 | }
|
---|
62 |
|
---|
63 | public static class UserRoles
|
---|
64 | {
|
---|
65 | public const string Admin = nameof(User.Role.Admin);
|
---|
66 | public const string PharmacyHead = nameof(User.Role.PharmacyHead);
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|