aboutsummaryrefslogtreecommitdiff
path: root/Timeline/Controllers/UserController.cs
blob: 7b441c3a7979d78846c5d07257393672d161b30a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Globalization;
using System.Threading.Tasks;
using Timeline.Authentication;
using Timeline.Helpers;
using Timeline.Models;
using Timeline.Models.Http;
using Timeline.Models.Validation;
using Timeline.Services;
using static Timeline.Resources.Controllers.UserController;

namespace Timeline
{
    public static partial class ErrorCodes
    {
        public static partial class Http
        {
            public static class User // bbb = 002
            {
                public static class Get // cc = 01
                {
                    public const int NotExist = 10020101; // dd = 01
                }

                public static class Patch // cc = 03
                {
                    public const int NotExist = 10020301; // dd = 01
                }

                public static class Op // cc = 1x
                {
                    public static class ChangeUsername // cc = 11
                    {
                        public const int NotExist = 10021101; // dd = 01
                        public const int AlreadyExist = 10021102; // dd = 02
                    }

                    public static class ChangePassword // cc = 12
                    {
                        public const int BadOldPassword = 10021201; // dd = 01
                    }
                }

            }
        }
    }
}

namespace Timeline.Controllers
{
    [ApiController]
    public class UserController : Controller
    {

        private readonly ILogger<UserController> _logger;
        private readonly IUserService _userService;

        public UserController(ILogger<UserController> logger, IUserService userService)
        {
            _logger = logger;
            _userService = userService;
        }

        [HttpGet("users"), AdminAuthorize]
        public async Task<ActionResult<UserInfo[]>> List()
        {
            return Ok(await _userService.ListUsers());
        }

        [HttpGet("users/{username}"), AdminAuthorize]
        public async Task<ActionResult<UserInfo>> Get([FromRoute][Username] string username)
        {
            var user = await _userService.GetUser(username);
            if (user == null)
            {
                _logger.LogInformation(Log.Format(LogGetUserNotExist, ("Username", username)));
                return NotFound(new CommonResponse(ErrorCodes.Http.User.Get.NotExist, ErrorGetUserNotExist));
            }
            return Ok(user);
        }

        [HttpPut("users/{username}"), AdminAuthorize]
        public async Task<ActionResult<CommonPutResponse>> Put([FromBody] UserPutRequest request, [FromRoute][Username] string username)
        {
            var result = await _userService.PutUser(username, request.Password, request.Administrator!.Value);
            switch (result)
            {
                case PutResult.Create:
                    _logger.LogInformation(Log.Format(LogPutCreate, ("Username", username)));
                    return CreatedAtAction("Get", new { username }, CommonPutResponse.Create());
                case PutResult.Modify:
                    _logger.LogInformation(Log.Format(LogPutModify, ("Username", username)));
                    return Ok(CommonPutResponse.Modify());
                default:
                    throw new InvalidBranchException();
            }
        }

        [HttpPatch("users/{username}"), AdminAuthorize]
        public async Task<ActionResult> Patch([FromBody] UserPatchRequest request, [FromRoute][Username] string username)
        {
            try
            {
                await _userService.PatchUser(username, request.Password, request.Administrator);
                return Ok();
            }
            catch (UserNotExistException e)
            {
                _logger.LogInformation(e, Log.Format(LogPatchUserNotExist, ("Username", username)));
                return NotFound(new CommonResponse(ErrorCodes.Http.User.Patch.NotExist, ErrorPatchUserNotExist));
            }
        }

        [HttpDelete("users/{username}"), AdminAuthorize]
        public async Task<ActionResult<CommonDeleteResponse>> Delete([FromRoute][Username] string username)
        {
            try
            {
                await _userService.DeleteUser(username);
                _logger.LogInformation(Log.Format(LogDeleteDelete, ("Username", username)));
                return Ok(CommonDeleteResponse.Delete());
            }
            catch (UserNotExistException e)
            {
                _logger.LogInformation(e, Log.Format(LogDeleteNotExist, ("Username", username)));
                return Ok(CommonDeleteResponse.NotExist());
            }
        }

        [HttpPost("userop/changeusername"), AdminAuthorize]
        public async Task<ActionResult> ChangeUsername([FromBody] ChangeUsernameRequest request)
        {
            try
            {
                await _userService.ChangeUsername(request.OldUsername, request.NewUsername);
                _logger.LogInformation(Log.Format(LogChangeUsernameSuccess,
                    ("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
                return Ok();
            }
            catch (UserNotExistException e)
            {
                _logger.LogInformation(e, Log.Format(LogChangeUsernameNotExist,
                    ("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
                return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangeUsername.NotExist,
                    string.Format(CultureInfo.CurrentCulture, ErrorChangeUsernameNotExist, request.OldUsername)));
            }
            catch (UsernameConfictException e)
            {
                _logger.LogInformation(e, Log.Format(LogChangeUsernameAlreadyExist,
                    ("Old Username", request.OldUsername), ("New Username", request.NewUsername)));
                return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangeUsername.AlreadyExist, ErrorChangeUsernameAlreadyExist));
            }
            // there is no need to catch bad format exception because it is already checked in model validation.
        }

        [HttpPost("userop/changepassword"), Authorize]
        public async Task<ActionResult> ChangePassword([FromBody] ChangePasswordRequest request)
        {
            try
            {
                await _userService.ChangePassword(User.Identity.Name!, request.OldPassword, request.NewPassword);
                _logger.LogInformation(Log.Format(LogChangePasswordSuccess, ("Username", User.Identity.Name)));
                return Ok();
            }
            catch (BadPasswordException e)
            {
                _logger.LogInformation(e, Log.Format(LogChangePasswordBadPassword,
                    ("Username", User.Identity.Name), ("Old Password", request.OldPassword)));
                return BadRequest(new CommonResponse(ErrorCodes.Http.User.Op.ChangePassword.BadOldPassword,
                    ErrorChangePasswordBadPassword));
            }
            // User can't be non-existent or the token is bad. 
        }
    }
}