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
|
if (newPassword == "")
|
32
|
{
|
33
|
throw new InvalidOperationException("Empty password.");
|
34
|
}
|
35
|
|
36
|
// Check if the user is tracked by EF
|
37
|
if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
|
38
|
{
|
39
|
_logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and password change cannot be done.");
|
40
|
throw new InvalidOperationException("User is untracked by EF.");
|
41
|
}
|
42
|
|
43
|
user.Password = BCrypt.Net.BCrypt.HashPassword(newPassword);
|
44
|
_databaseContext.SaveChanges();
|
45
|
return user;
|
46
|
}
|
47
|
|
48
|
public User? CheckUsernamePassword(string username, string password)
|
49
|
{
|
50
|
try
|
51
|
{
|
52
|
// Throws exception if user does not exist
|
53
|
User u = _databaseContext.Users.First(u => u.Username == username);
|
54
|
if (!BCrypt.Net.BCrypt.Verify(password, u.Password))
|
55
|
{
|
56
|
_logger.Information($"Password for user {username} doesn't match.");
|
57
|
return null;
|
58
|
}
|
59
|
return u;
|
60
|
}
|
61
|
catch (Exception)
|
62
|
{
|
63
|
_logger.Information($"No user with username {username} found.");
|
64
|
return null;
|
65
|
}
|
66
|
}
|
67
|
|
68
|
public User? CreateUser(string username, string name, string surname, string password, ERole role)
|
69
|
{
|
70
|
if (password == "")
|
71
|
{
|
72
|
return null;
|
73
|
}
|
74
|
|
75
|
// Check if username already used
|
76
|
if (_databaseContext.Users.Any(u => u.Username == username))
|
77
|
{
|
78
|
_logger.Information($"Username {username} is already used. Cannot create new user");
|
79
|
return null;
|
80
|
}
|
81
|
|
82
|
User user = new User()
|
83
|
{
|
84
|
Username = username,
|
85
|
Name = name,
|
86
|
Surname = surname,
|
87
|
Password = BCrypt.Net.BCrypt.HashPassword(password),
|
88
|
Role = role
|
89
|
};
|
90
|
_databaseContext.Users.Add(user);
|
91
|
_databaseContext.SaveChanges();
|
92
|
return user;
|
93
|
}
|
94
|
|
95
|
public User? GetUserByUsername(string username)
|
96
|
{
|
97
|
try
|
98
|
{
|
99
|
// Throws exception on no user found
|
100
|
User user = _databaseContext.Users.First(u => u.Username == username);
|
101
|
return user;
|
102
|
}
|
103
|
catch (InvalidOperationException)
|
104
|
{
|
105
|
_logger.Warning($"No user with the username {username} found.");
|
106
|
return null;
|
107
|
}
|
108
|
}
|
109
|
|
110
|
public User? GetUserById(Guid id)
|
111
|
{
|
112
|
try
|
113
|
{
|
114
|
// Throws exception on no user found
|
115
|
User user = _databaseContext.Users.First(u => u.Id == id);
|
116
|
return user;
|
117
|
}
|
118
|
catch (InvalidOperationException)
|
119
|
{
|
120
|
_logger.Warning($"No user with the GUID {id} found.");
|
121
|
return null;
|
122
|
}
|
123
|
}
|
124
|
|
125
|
public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null)
|
126
|
{
|
127
|
// Check if the user is tracked by EF
|
128
|
if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
|
129
|
{
|
130
|
_logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and update cannot be done.");
|
131
|
throw new InvalidOperationException("User is untracked by EF.");
|
132
|
}
|
133
|
|
134
|
if (username is not null && username != "")
|
135
|
{
|
136
|
user.Username = username;
|
137
|
}
|
138
|
|
139
|
if (name is not null && name != "")
|
140
|
{
|
141
|
user.Name = name;
|
142
|
}
|
143
|
|
144
|
if (surname is not null && surname != "")
|
145
|
{
|
146
|
user.Surname = surname;
|
147
|
}
|
148
|
|
149
|
if (role is not null)
|
150
|
{
|
151
|
// We know it is not null
|
152
|
user.Role = (ERole)role;
|
153
|
}
|
154
|
|
155
|
_databaseContext.SaveChanges();
|
156
|
return user;
|
157
|
}
|
158
|
|
159
|
public UserList GetUsers() => new UserList()
|
160
|
{
|
161
|
Users = _databaseContext.Users.Select(u => _mapper.Map<UserInfo>(u)).ToList()
|
162
|
};
|
163
|
}
|
164
|
}
|
165
|
|