aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcrupest <crupest@outlook.com>2021-01-19 15:38:03 +0800
committercrupest <crupest@outlook.com>2021-01-19 15:38:03 +0800
commitee419812021f8b2e8e35997750662e56c9db613a (patch)
treee828dce4c6ba4c35ea913dcb0329e3b9f2ecb808
parent7f815dba267fa7e5153b3312dc77a1db27c7622a (diff)
downloadtimeline-ee419812021f8b2e8e35997750662e56c9db613a.tar.gz
timeline-ee419812021f8b2e8e35997750662e56c9db613a.tar.bz2
timeline-ee419812021f8b2e8e35997750662e56c9db613a.zip
feat: Deprecate userop/createuser api and add users post api.
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs6
-rw-r--r--BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs2
-rw-r--r--BackEnd/Timeline/Controllers/UserController.cs34
3 files changed, 29 insertions, 13 deletions
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs
index f96acfea..4979224f 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/SearchTest.cs
@@ -41,10 +41,10 @@ namespace Timeline.Tests.IntegratedTests
var client = await CreateClientAsAdministrator();
{
- await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "hahaha", Password = "p" });
- await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "bababa", Password = "p" });
+ await client.TestPostAsync("users", new HttpCreateUserRequest { Username = "hahaha", Password = "p" });
+ await client.TestPostAsync("users", new HttpCreateUserRequest { Username = "bababa", Password = "p" });
await client.TestPatchAsync("users/bababa", new HttpUserPatchRequest { Nickname = "hahaha" });
- await client.TestPostAsync("userop/createuser", new HttpCreateUserRequest { Username = "gagaga", Password = "p" });
+ await client.TestPostAsync("users", new HttpCreateUserRequest { Username = "gagaga", Password = "p" });
}
{
diff --git a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs
index 56dbf92a..664a0604 100644
--- a/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs
+++ b/BackEnd/Timeline.Tests/IntegratedTests/UserTest.cs
@@ -207,7 +207,7 @@ namespace Timeline.Tests.IntegratedTests
await client.TestDeleteAssertForbiddenAsync("users/aaa!a");
}
- private const string createUserUrl = "userop/createuser";
+ private const string createUserUrl = "users";
[Fact]
public async Task Op_CreateUser()
diff --git a/BackEnd/Timeline/Controllers/UserController.cs b/BackEnd/Timeline/Controllers/UserController.cs
index e1a9d454..4091174c 100644
--- a/BackEnd/Timeline/Controllers/UserController.cs
+++ b/BackEnd/Timeline/Controllers/UserController.cs
@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
+using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Timeline.Auth;
@@ -60,6 +61,28 @@ namespace Timeline.Controllers
}
/// <summary>
+ /// Create a new user. You have to be administrator.
+ /// </summary>
+ /// <returns>The new user's info.</returns>
+ [HttpPost("users"), PermissionAuthorize(UserPermission.UserManagement)]
+ [ProducesResponseType(StatusCodes.Status200OK)]
+ [ProducesResponseType(StatusCodes.Status400BadRequest)]
+ [ProducesResponseType(StatusCodes.Status401Unauthorized)]
+ [ProducesResponseType(StatusCodes.Status403Forbidden)]
+ public async Task<ActionResult<HttpUser>> Post([FromBody] HttpCreateUserRequest body)
+ {
+ try
+ {
+ var user = await _userService.CreateUser(body.Username, body.Password);
+ return await _userMapper.MapToHttp(user, Url);
+ }
+ catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User)
+ {
+ return BadRequest(ErrorResponse.UserController.UsernameConflict());
+ }
+ }
+
+ /// <summary>
/// Get a user's info.
/// </summary>
/// <param name="username">Username of the user.</param>
@@ -168,17 +191,10 @@ namespace Timeline.Controllers
[ProducesResponseType(StatusCodes.Status400BadRequest)]
[ProducesResponseType(StatusCodes.Status401Unauthorized)]
[ProducesResponseType(StatusCodes.Status403Forbidden)]
+ [Obsolete("Use post instead.")]
public async Task<ActionResult<HttpUser>> CreateUser([FromBody] HttpCreateUserRequest body)
{
- try
- {
- var user = await _userService.CreateUser(body.Username, body.Password);
- return await _userMapper.MapToHttp(user, Url);
- }
- catch (EntityAlreadyExistException e) when (e.EntityName == EntityNames.User)
- {
- return BadRequest(ErrorResponse.UserController.UsernameConflict());
- }
+ return await Post(body);
}
/// <summary>