Projekt

Obecné

Profil

Stáhnout (4.94 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? CheckUsernamePassword(string username, string password)
44
        {
45
            try
46
            {
47
                // Throws exception if user does not exist
48
                User u = _databaseContext.Users.First(u => u.Username == username);
49
                if (!BCrypt.Net.BCrypt.Verify(password, u.Password))
50
                {
51
                    _logger.Information($"Password for user {username} doesn't match.");
52
                    return null;
53
                }
54
                return u;
55
            }
56
            catch (Exception)
57
            {
58
                _logger.Information($"No user with username {username} found.");
59
                return null;
60
            }
61
        }
62

    
63
        public User? CreateUser(string username, string name, string surname, string password, ERole role)
64
        {
65
            // Check if username already used
66
            if (_databaseContext.Users.Any(u => u.Username == username))
67
            {
68
                _logger.Information($"Username {username} is already used. Cannot create new user");
69
                return null;
70
            }
71

    
72
            User user = new User()
73
            {
74
                Username = username,
75
                Name = name,
76
                Surname = surname,
77
                Password = BCrypt.Net.BCrypt.HashPassword(password),
78
                Role = role
79
            };
80
            _databaseContext.Users.Add(user);
81
            _databaseContext.SaveChanges();
82
            return user;
83
        }
84

    
85
        public User? GetUserByUsername(string username)
86
        {
87
            try
88
            {
89
                // Throws exception on no user found
90
                User user = _databaseContext.Users.First(u => u.Username == username);
91
                return user;
92
            }
93
            catch (InvalidOperationException)
94
            {
95
                _logger.Warning($"No user with the username {username} found.");
96
                return null;
97
            }
98
        }
99

    
100
        public User? GetUserById(Guid id)
101
        {
102
            try
103
            {
104
                // Throws exception on no user found
105
                User user = _databaseContext.Users.First(u => u.Id == id);
106
                return user;
107
            }
108
            catch (InvalidOperationException)
109
            {
110
                _logger.Warning($"No user with the GUID {id} found.");
111
                return null;
112
            }
113
        }
114

    
115
        public User UpdateUser(User user, string? username = null, string? name = null, string? surname = null, ERole? role = null)
116
        {
117
            // Check if the user is tracked by EF
118
            if (!_databaseContext.Users.Local.Any(u => u.Id == user.Id))
119
            {
120
                _logger.Information($"User {user.Username} with Guid {user.Id} is untracked by EF and update cannot be done.");
121
                throw new InvalidOperationException("User is untracked by EF.");
122
            }
123

    
124
            if (username is not null)
125
            {
126
                user.Username = username;
127
            }
128

    
129
            if (name is not null)
130
            {
131
                user.Name = name;
132
            }
133

    
134
            if (surname is not null)
135
            {
136
                user.Surname = surname;
137
            }
138

    
139
            if (role is not null)
140
            {
141
                // We know it is not null
142
                user.Role = (ERole)role;
143
            }
144

    
145
            _databaseContext.SaveChanges();
146
            return user;
147
        }
148

    
149
        public UserList GetUsers()
150
        {
151
            var users = _databaseContext.Users.Select(u => u);
152
            UserList userList = new UserList();
153
            foreach (var user in users)
154
            {
155
                userList.Users.Add(_mapper.Map<UserInfo>(user));
156
            }
157

    
158
            return userList;
159
        }
160
    }
161
}
(2-2/2)