Projekt

Obecné

Profil

Stáhnout (4.49 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)
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()
69
            {
70
                Username = username,
71
                Name = name,
72
                Surname = surname,
73
                Password = BCrypt.Net.BCrypt.HashPassword(password),
74
                Role = role
75
            };
76
            _databaseContext.Users.Add(user);
77
            _databaseContext.SaveChanges();
78
            return user;
79
        }
80

    
81
        public User? GetUserByUsername(string username)
82
        {
83
            try
84
            {
85
                // Throws exception on no user found
86
                User user = _databaseContext.Users.First(u => u.Username == username);
87
                return user;
88
            }
89
            catch (InvalidOperationException)
90
            {
91
                _logger.Warning($"No user with the username {username} found.");
92
                return null;
93
            }
94
        }
95

    
96
        public User? GetUserByGuid(Guid id)
97
        {
98
            try
99
            {
100
                // Throws exception on no user found
101
                User user = _databaseContext.Users.First(u => u.Id == id);
102
                return user;
103
            }
104
            catch (InvalidOperationException)
105
            {
106
                _logger.Warning($"No user with the GUID {id} found.");
107
                return null;
108
            }
109
        }
110

    
111
        public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null)
112
        {
113
            // Check if the user is tracked by EF
114
            if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
115
            {
116
                _logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and update cannot be done.");
117
                throw new InvalidOperationException("User is untracked by EF.");
118
            }
119

    
120
            if (username is not null)
121
            {
122
                user.Username = username;
123
            }
124

    
125
            if (name is not null)
126
            {
127
                user.Name = name;
128
            }
129

    
130
            if (surname is not null)
131
            {
132
                user.Surname = surname;
133
            }
134

    
135
            if (role is not null)
136
            {
137
                // We know it is not null
138
                user.Role = (ERole)role;
139
            }
140

    
141
            _databaseContext.SaveChanges();
142
            return user;
143
        }
144
    }
145
}
(2-2/2)