1 |
897851f8
|
Vojtěch Bartička
|
using Core.Contexts;
|
2 |
|
|
using Core.Entities;
|
3 |
cd91f841
|
Vojtěch Bartička
|
using Models.Enums;
|
4 |
897851f8
|
Vojtěch Bartička
|
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 |
a70f3c5f
|
Vojtěch Bartička
|
using Models.Users;
|
12 |
|
|
using AutoMapper;
|
13 |
897851f8
|
Vojtěch Bartička
|
|
14 |
|
|
namespace Core.Services
|
15 |
|
|
{
|
16 |
|
|
public class UserServiceEF : IUserService
|
17 |
|
|
{
|
18 |
|
|
private readonly DatabaseContext _databaseContext;
|
19 |
|
|
private readonly ILogger _logger;
|
20 |
a70f3c5f
|
Vojtěch Bartička
|
private readonly IMapper _mapper;
|
21 |
897851f8
|
Vojtěch Bartička
|
|
22 |
a70f3c5f
|
Vojtěch Bartička
|
public UserServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
|
23 |
897851f8
|
Vojtěch Bartička
|
{
|
24 |
|
|
_databaseContext = context;
|
25 |
|
|
_logger = logger;
|
26 |
a70f3c5f
|
Vojtěch Bartička
|
_mapper = mapper;
|
27 |
897851f8
|
Vojtěch Bartička
|
}
|
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 |
a9acbe07
|
Vojtěch Bartička
|
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 |
897851f8
|
Vojtěch Bartička
|
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 |
c349bab3
|
Lukáš Vlček
|
_logger.Information($"Password for user {username} doesn't match.");
|
60 |
897851f8
|
Vojtěch Bartička
|
return null;
|
61 |
|
|
}
|
62 |
|
|
return u;
|
63 |
|
|
}
|
64 |
fc0e5b36
|
Vojtěch Bartička
|
catch (Exception)
|
65 |
897851f8
|
Vojtěch Bartička
|
{
|
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 |
fc0e5b36
|
Vojtěch Bartička
|
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 |
897851f8
|
Vojtěch Bartička
|
_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 |
fc0e5b36
|
Vojtěch Bartička
|
catch (InvalidOperationException)
|
102 |
897851f8
|
Vojtěch Bartička
|
{
|
103 |
|
|
_logger.Warning($"No user with the username {username} found.");
|
104 |
|
|
return null;
|
105 |
|
|
}
|
106 |
|
|
}
|
107 |
|
|
|
108 |
c349bab3
|
Lukáš Vlček
|
public User? GetUserById(Guid id)
|
109 |
897851f8
|
Vojtěch Bartička
|
{
|
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 |
fc0e5b36
|
Vojtěch Bartička
|
catch (InvalidOperationException)
|
117 |
897851f8
|
Vojtěch Bartička
|
{
|
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 |
a70f3c5f
|
Vojtěch Bartička
|
|
157 |
a9acbe07
|
Vojtěch Bartička
|
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 |
1524324f
|
Vojtěch Bartička
|
public UserList GetUsers() => new UserList()
|
172 |
a70f3c5f
|
Vojtěch Bartička
|
{
|
173 |
1524324f
|
Vojtěch Bartička
|
Users = _databaseContext.Users.Select(u => _mapper.Map<UserInfo>(u)).ToList()
|
174 |
|
|
};
|
175 |
897851f8
|
Vojtěch Bartička
|
}
|
176 |
|
|
}
|