source: FarmatikoServices/Auth/AuthService.cs@ 7520f88

Last change on this file since 7520f88 was d23bf72, checked in by DimitarSlezenkovski <dslezenkovski@…>, 4 years ago

Add SystemService, Auth, fix a lil bugs :)

  • Property mode set to 100644
File size: 2.0 KB
Line 
1using FarmatikoData.FarmatikoRepoInterfaces;
2using FarmatikoData.Models;
3using Microsoft.Extensions.Logging;
4using System;
5using System.Collections.Generic;
6using System.Linq;
7using System.Text;
8
9namespace 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
Note: See TracBrowser for help on using the repository browser.