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? ChangePassword(Guid userId, string newPassword)
|
49
|
{
|
50
|
User? user = null;
|
51
|
user = _databaseContext.Users.First(u => u.Id == userId);
|
52
|
|
53
|
return ChangePassword(user, newPassword);
|
54
|
}
|
55
|
|
56
|
public User? CheckUsernamePassword(string username, string password)
|
57
|
{
|
58
|
try
|
59
|
{
|
60
|
// Throws exception if user does not exist
|
61
|
User u = _databaseContext.Users.First(u => u.Username == username);
|
62
|
if (!BCrypt.Net.BCrypt.Verify(password, u.Password))
|
63
|
{
|
64
|
_logger.Information($"Password for user {username} doesn't match.");
|
65
|
return null;
|
66
|
}
|
67
|
return u;
|
68
|
}
|
69
|
catch (Exception)
|
70
|
{
|
71
|
_logger.Information($"No user with username {username} found.");
|
72
|
return null;
|
73
|
}
|
74
|
}
|
75
|
|
76
|
public User? CreateUser(string username, string name, string surname, string password, ERole role)
|
77
|
{
|
78
|
if (password == "")
|
79
|
{
|
80
|
return null;
|
81
|
}
|
82
|
|
83
|
// Check if username already used
|
84
|
if (_databaseContext.Users.Any(u => u.Username == username))
|
85
|
{
|
86
|
_logger.Information($"Username {username} is already used. Cannot create new user");
|
87
|
return null;
|
88
|
}
|
89
|
|
90
|
User user = new User()
|
91
|
{
|
92
|
Username = username,
|
93
|
Name = name,
|
94
|
Surname = surname,
|
95
|
Password = BCrypt.Net.BCrypt.HashPassword(password),
|
96
|
Role = role
|
97
|
};
|
98
|
_databaseContext.Users.Add(user);
|
99
|
_databaseContext.SaveChanges();
|
100
|
return user;
|
101
|
}
|
102
|
|
103
|
public User? GetUserByUsername(string username)
|
104
|
{
|
105
|
try
|
106
|
{
|
107
|
// Throws exception on no user found
|
108
|
User user = _databaseContext.Users.First(u => u.Username == username);
|
109
|
return user;
|
110
|
}
|
111
|
catch (InvalidOperationException)
|
112
|
{
|
113
|
_logger.Warning($"No user with the username {username} found.");
|
114
|
return null;
|
115
|
}
|
116
|
}
|
117
|
|
118
|
public User? GetUserById(Guid id)
|
119
|
{
|
120
|
try
|
121
|
{
|
122
|
// Throws exception on no user found
|
123
|
User user = _databaseContext.Users.First(u => u.Id == id);
|
124
|
return user;
|
125
|
}
|
126
|
catch (InvalidOperationException)
|
127
|
{
|
128
|
_logger.Warning($"No user with the GUID {id} found.");
|
129
|
return null;
|
130
|
}
|
131
|
}
|
132
|
|
133
|
public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null)
|
134
|
{
|
135
|
// Check if the user is tracked by EF
|
136
|
if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
|
137
|
{
|
138
|
_logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and update cannot be done.");
|
139
|
throw new InvalidOperationException("User is untracked by EF.");
|
140
|
}
|
141
|
|
142
|
if (username is not null && username != "")
|
143
|
{
|
144
|
user.Username = username;
|
145
|
}
|
146
|
|
147
|
if (name is not null && name != "")
|
148
|
{
|
149
|
user.Name = name;
|
150
|
}
|
151
|
|
152
|
if (surname is not null && surname != "")
|
153
|
{
|
154
|
user.Surname = surname;
|
155
|
}
|
156
|
|
157
|
if (role is not null)
|
158
|
{
|
159
|
// We know it is not null
|
160
|
user.Role = (ERole)role;
|
161
|
}
|
162
|
|
163
|
_databaseContext.SaveChanges();
|
164
|
return user;
|
165
|
}
|
166
|
|
167
|
public User? UpdateUser(Guid userId, string? username = null, string? name = null, string? surname = null, ERole? role = null)
|
168
|
{
|
169
|
try
|
170
|
{
|
171
|
User user = _databaseContext.Users.First(u => u.Id == userId);
|
172
|
return UpdateUser(user, username, name, surname, role);
|
173
|
}
|
174
|
catch (Exception ex)
|
175
|
{
|
176
|
return null;
|
177
|
}
|
178
|
}
|
179
|
|
180
|
|
181
|
public UserList GetUsers() => new UserList()
|
182
|
{
|
183
|
Users = _databaseContext.Users.Select(u => _mapper.Map<UserInfo>(u)).ToList()
|
184
|
};
|
185
|
|
186
|
public void DeleteUser(Guid userId)
|
187
|
{
|
188
|
try
|
189
|
{
|
190
|
var user = _databaseContext.Users.Where(u => u.Id == userId).First();
|
191
|
_databaseContext.Users.Remove(user);
|
192
|
_databaseContext.SaveChanges();
|
193
|
}
|
194
|
catch (Exception)
|
195
|
{
|
196
|
throw new InvalidOperationException("User not found");
|
197
|
}
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
|