Projekt

Obecné

Profil

Stáhnout (4.4 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Core.Contexts;
2
using Core.Entities;
3
using Core.Enums;
4
using System;
5
using System.Collections.Generic;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using Serilog;
10
using BCrypt.Net;
11

    
12
namespace Core.Services
13
{
14
    public class UserServiceEF : IUserService
15
    {
16
        private readonly DatabaseContext _databaseContext;
17
        private readonly ILogger _logger;
18

    
19
        public UserServiceEF(DatabaseContext context, ILogger logger)
20
        {
21
            _databaseContext = context;
22
            _logger = logger;
23
        }
24

    
25
        public User ChangePassword(User user, string newPassword)
26
        {
27
            // Check if the user is tracked by EF
28
            if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
29
            {
30
                _logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and password change cannot be done.");
31
                throw new InvalidOperationException("User is untracked by EF.");
32
            }
33

    
34
            user.Password = BCrypt.Net.BCrypt.HashPassword(newPassword);
35
            _databaseContext.SaveChanges();
36
            return user;
37
        }
38

    
39
        public User? CheckUsernamePassword(string username, string password)
40
        {
41
            try
42
            {
43
                // Throws exception if user does not exist
44
                User u = _databaseContext.Users.First(u => u.Username == username);
45
                if (!BCrypt.Net.BCrypt.Verify(password, u.Password))
46
                {
47
                    _logger.Information($"Password for user {username} don't match.");
48
                    return null;
49
                }
50
                return u;
51
            }
52
            catch (Exception ex)
53
            {
54
                _logger.Information($"No user with username {username} found.");
55
                return null;
56
            }
57
        }
58

    
59
        public User? CreateUser(string username, string name, string surname, string password, ERole role)
60
        {
61
            // Check if username already used
62
            if (_databaseContext.Users.Any(u => u.Username == username))
63
            {
64
                _logger.Information($"Username {username} is already used. Cannot create new user");
65
                return null;
66
            }
67

    
68
            User user = new User() { Username = username, Name = name, Surname = surname, Password = BCrypt.Net.BCrypt.HashPassword(password), Role = role };
69
            _databaseContext.Users.Add(user);
70
            _databaseContext.SaveChanges();
71
            return user;
72
        }
73

    
74
        public User? GetUserByUsername(string username)
75
        {
76
            try
77
            {
78
                // Throws exception on no user found
79
                User user = _databaseContext.Users.First(u => u.Username == username);
80
                return user;
81
            }
82
            catch (InvalidOperationException ex)
83
            {
84
                _logger.Warning($"No user with the username {username} found.");
85
                return null;
86
            }
87
        }
88

    
89
        public User? GetUserByGuid(Guid id)
90
        {
91
            try
92
            {
93
                // Throws exception on no user found
94
                User user = _databaseContext.Users.First(u => u.Id == id);
95
                return user;
96
            }
97
            catch (InvalidOperationException ex)
98
            {
99
                _logger.Warning($"No user with the GUID {id} found.");
100
                return null;
101
            }
102
        }
103

    
104
        public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null)
105
        {
106
            // Check if the user is tracked by EF
107
            if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
108
            {
109
                _logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and update cannot be done.");
110
                throw new InvalidOperationException("User is untracked by EF.");
111
            }
112

    
113
            if (username is not null)
114
            {
115
                user.Username = username;
116
            }
117

    
118
            if (name is not null)
119
            {
120
                user.Name = name;
121
            }
122

    
123
            if (surname is not null)
124
            {
125
                user.Surname = surname;
126
            }
127

    
128
            if (role is not null)
129
            {
130
                // We know it is not null
131
                user.Role = (ERole)role;
132
            }
133

    
134
            _databaseContext.SaveChanges();
135
            return user;
136
        }
137
    }
138
}
(2-2/2)