Projekt

Obecné

Profil

Stáhnout (6.61 KB) Statistiky
| Větev: | Tag: | Revize:
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 faa3619b Vojtěch Bartička
using Microsoft.EntityFrameworkCore;
14 897851f8 Vojtěch Bartička
15
namespace Core.Services
16
{
17
    public class UserServiceEF : IUserService
18
    {
19
        private readonly DatabaseContext _databaseContext;
20
        private readonly ILogger _logger;
21 a70f3c5f Vojtěch Bartička
        private readonly IMapper _mapper;
22 897851f8 Vojtěch Bartička
23 a70f3c5f Vojtěch Bartička
        public UserServiceEF(DatabaseContext context, ILogger logger, IMapper mapper)
24 897851f8 Vojtěch Bartička
        {
25
            _databaseContext = context;
26
            _logger = logger;
27 a70f3c5f Vojtěch Bartička
            _mapper = mapper;
28 897851f8 Vojtěch Bartička
        }
29
30
        public User ChangePassword(User user, string newPassword)
31
        {
32 4ebfa716 Vojtěch Bartička
            if (newPassword == "")
33
            {
34
                throw new InvalidOperationException("Empty password.");
35
            }
36
37 897851f8 Vojtěch Bartička
            // Check if the user is tracked by EF
38
            if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
39
            {
40
                _logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and password change cannot be done.");
41
                throw new InvalidOperationException("User is untracked by EF.");
42
            }
43
44
            user.Password = BCrypt.Net.BCrypt.HashPassword(newPassword);
45
            _databaseContext.SaveChanges();
46
            return user;
47
        }
48
49 a9acbe07 Vojtěch Bartička
        public User? ChangePassword(Guid userId, string newPassword)
50
        {
51
            User? user = null;
52
            user = _databaseContext.Users.First(u => u.Id == userId);
53
54
            return ChangePassword(user, newPassword);
55
        }
56
57 897851f8 Vojtěch Bartička
        public User? CheckUsernamePassword(string username, string password)
58
        {
59
            try
60
            {
61
                // Throws exception if user does not exist
62
                User u = _databaseContext.Users.First(u => u.Username == username);
63
                if (!BCrypt.Net.BCrypt.Verify(password, u.Password))
64
                {
65 c349bab3 Lukáš Vlček
                    _logger.Information($"Password for user {username} doesn't match.");
66 897851f8 Vojtěch Bartička
                    return null;
67
                }
68
                return u;
69
            }
70 fc0e5b36 Vojtěch Bartička
            catch (Exception)
71 897851f8 Vojtěch Bartička
            {
72
                _logger.Information($"No user with username {username} found.");
73
                return null;
74
            }
75
        }
76
77
        public User? CreateUser(string username, string name, string surname, string password, ERole role)
78
        {
79 4ebfa716 Vojtěch Bartička
            if (password == "")
80
            {
81
                return null;
82
            }
83
84 897851f8 Vojtěch Bartička
            // Check if username already used
85
            if (_databaseContext.Users.Any(u => u.Username == username))
86
            {
87
                _logger.Information($"Username {username} is already used. Cannot create new user");
88
                return null;
89
            }
90
91 fc0e5b36 Vojtěch Bartička
            User user = new User()
92
            {
93
                Username = username,
94
                Name = name,
95
                Surname = surname,
96
                Password = BCrypt.Net.BCrypt.HashPassword(password),
97
                Role = role
98
            };
99 897851f8 Vojtěch Bartička
            _databaseContext.Users.Add(user);
100
            _databaseContext.SaveChanges();
101
            return user;
102
        }
103
104
        public User? GetUserByUsername(string username)
105
        {
106
            try
107
            {
108
                // Throws exception on no user found
109
                User user = _databaseContext.Users.First(u => u.Username == username);
110
                return user;
111
            }
112 fc0e5b36 Vojtěch Bartička
            catch (InvalidOperationException)
113 897851f8 Vojtěch Bartička
            {
114
                _logger.Warning($"No user with the username {username} found.");
115
                return null;
116
            }
117
        }
118
119 c349bab3 Lukáš Vlček
        public User? GetUserById(Guid id)
120 897851f8 Vojtěch Bartička
        {
121
            try
122
            {
123
                // Throws exception on no user found
124
                User user = _databaseContext.Users.First(u => u.Id == id);
125
                return user;
126
            }
127 fc0e5b36 Vojtěch Bartička
            catch (InvalidOperationException)
128 897851f8 Vojtěch Bartička
            {
129
                _logger.Warning($"No user with the GUID {id} found.");
130
                return null;
131
            }
132
        }
133
134
        public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null)
135
        {
136
            // Check if the user is tracked by EF
137
            if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
138
            {
139
                _logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and update cannot be done.");
140
                throw new InvalidOperationException("User is untracked by EF.");
141
            }
142
143 4ebfa716 Vojtěch Bartička
            if (username is not null && username != "")
144 897851f8 Vojtěch Bartička
            {
145
                user.Username = username;
146
            }
147
148 4ebfa716 Vojtěch Bartička
            if (name is not null && name != "")
149 897851f8 Vojtěch Bartička
            {
150
                user.Name = name;
151
            }
152
153 4ebfa716 Vojtěch Bartička
            if (surname is not null && surname != "")
154 897851f8 Vojtěch Bartička
            {
155
                user.Surname = surname;
156
            }
157
158
            if (role is not null)
159
            {
160
                // We know it is not null
161
                user.Role = (ERole)role;
162
            }
163
164
            _databaseContext.SaveChanges();
165
            return user;
166
        }
167 a70f3c5f Vojtěch Bartička
168 a9acbe07 Vojtěch Bartička
        public User? UpdateUser(Guid userId, string? username = null, string? name = null, string? surname = null, ERole? role = null)
169
        {
170
            try
171
            {
172
                User user = _databaseContext.Users.First(u => u.Id == userId);
173
                return UpdateUser(user, username, name, surname, role);
174
            }
175
            catch (Exception ex)
176
            {
177
                return null;
178
            }
179
        }
180
181
182 faa3619b Vojtěch Bartička
        public UserList GetUsers()
183 a70f3c5f Vojtěch Bartička
        {
184 faa3619b Vojtěch Bartička
            var userList = new UserList();
185
            var users = _databaseContext.Users.ToList();
186
            foreach (var user in users)
187
            {
188
                var userInfo = _mapper.Map<UserInfo>(user);
189 8c9ce202 Vojtěch Bartička
                userInfo.AssignedDocumentsCount = _databaseContext.Annotations
190
                    .Include(a => a.User)
191
                    .Where(a => !(a is FinalAnnotation))
192
                    .Where(a => a.User == user)
193
                    .Count();
194 faa3619b Vojtěch Bartička
                userList.Users.Add(userInfo);
195
            }
196
197
            return userList;
198
        }
199
200 f37d2b59 Vojtěch Bartička
201
        public void DeleteUser(Guid userId)
202
        {
203
            try
204
            {
205
                var user = _databaseContext.Users.Where(u => u.Id == userId).First();
206
                _databaseContext.Users.Remove(user);
207 78122c3a Vojtěch Bartička
                _databaseContext.SaveChanges();
208 f37d2b59 Vojtěch Bartička
            }
209
            catch (Exception)
210
            {
211
                throw new InvalidOperationException("User not found");
212
            }
213
        }
214 897851f8 Vojtěch Bartička
    }
215
}