Projekt

Obecné

Profil

Stáhnout (5.45 KB) Statistiky
| Větev: | Tag: | Revize:
1
using Core.Contexts;
2
using Core.Entities;
3
using Models.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
using Models.Users;
12
using AutoMapper;
13

    
14
namespace Core.Services
15
{
16
    public class UserServiceEF : IUserService
17
    {
18
        private readonly DatabaseContext _databaseContext;
19
        private readonly ILogger _logger;
20
        private readonly IMapper _mapper;
21

    
22
        public UserServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
23
        {
24
            _databaseContext = context;
25
            _logger = logger;
26
            _mapper = mapper;
27
        }
28

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

    
38
            user.Password = BCrypt.Net.BCrypt.HashPassword(newPassword);
39
            _databaseContext.SaveChanges();
40
            return user;
41
        }
42

    
43
        public User? ChangePassword(Guid userId, string newPassword)
44
        {
45
            User? user = null;
46
            user = _databaseContext.Users.First(u => u.Id == userId);
47

    
48
            return ChangePassword(user, newPassword);
49
        }
50

    
51
        public User? CheckUsernamePassword(string username, string password)
52
        {
53
            try
54
            {
55
                // Throws exception if user does not exist
56
                User u = _databaseContext.Users.First(u => u.Username == username);
57
                if (!BCrypt.Net.BCrypt.Verify(password, u.Password))
58
                {
59
                    _logger.Information($"Password for user {username} doesn't match.");
60
                    return null;
61
                }
62
                return u;
63
            }
64
            catch (Exception)
65
            {
66
                _logger.Information($"No user with username {username} found.");
67
                return null;
68
            }
69
        }
70

    
71
        public User? CreateUser(string username, string name, string surname, string password, ERole role)
72
        {
73
            // Check if username already used
74
            if (_databaseContext.Users.Any(u => u.Username == username))
75
            {
76
                _logger.Information($"Username {username} is already used. Cannot create new user");
77
                return null;
78
            }
79

    
80
            User user = new User()
81
            {
82
                Username = username,
83
                Name = name,
84
                Surname = surname,
85
                Password = BCrypt.Net.BCrypt.HashPassword(password),
86
                Role = role
87
            };
88
            _databaseContext.Users.Add(user);
89
            _databaseContext.SaveChanges();
90
            return user;
91
        }
92

    
93
        public User? GetUserByUsername(string username)
94
        {
95
            try
96
            {
97
                // Throws exception on no user found
98
                User user = _databaseContext.Users.First(u => u.Username == username);
99
                return user;
100
            }
101
            catch (InvalidOperationException)
102
            {
103
                _logger.Warning($"No user with the username {username} found.");
104
                return null;
105
            }
106
        }
107

    
108
        public User? GetUserById(Guid id)
109
        {
110
            try
111
            {
112
                // Throws exception on no user found
113
                User user = _databaseContext.Users.First(u => u.Id == id);
114
                return user;
115
            }
116
            catch (InvalidOperationException)
117
            {
118
                _logger.Warning($"No user with the GUID {id} found.");
119
                return null;
120
            }
121
        }
122

    
123
        public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null)
124
        {
125
            // Check if the user is tracked by EF
126
            if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
127
            {
128
                _logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and update cannot be done.");
129
                throw new InvalidOperationException("User is untracked by EF.");
130
            }
131

    
132
            if (username is not null)
133
            {
134
                user.Username = username;
135
            }
136

    
137
            if (name is not null)
138
            {
139
                user.Name = name;
140
            }
141

    
142
            if (surname is not null)
143
            {
144
                user.Surname = surname;
145
            }
146

    
147
            if (role is not null)
148
            {
149
                // We know it is not null
150
                user.Role = (ERole)role;
151
            }
152

    
153
            _databaseContext.SaveChanges();
154
            return user;
155
        }
156

    
157
        public User? UpdateUser(Guid userId, string? username = null, string? name = null, string? surname = null, ERole? role = null)
158
        {
159
            try
160
            {
161
                User user = _databaseContext.Users.First(u => u.Id == userId);
162
                return UpdateUser(user, username, name, surname, role);
163
            }
164
            catch (Exception ex)
165
            {
166
                return null;
167
            }
168
        }
169

    
170

    
171
        public UserList GetUsers() => new UserList()
172
        {
173
            Users = _databaseContext.Users.Select(u => _mapper.Map<UserInfo>(u)).ToList()
174
        };
175
    }
176
}
177

    
(2-2/2)