-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathUsersController.cs
More file actions
87 lines (78 loc) · 3.31 KB
/
Copy pathUsersController.cs
File metadata and controls
87 lines (78 loc) · 3.31 KB
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
using redb.Identity.Contracts.Common;
using redb.Identity.Contracts.Users;
using redb.Identity.Contracts.Routes;
using redb.Route.Controllers.Attributes;
namespace redb.Identity.Http.Controllers;
/// <summary>
/// REST management API for user accounts.
/// Forwards to <c>direct-vm://identity-manage-users</c> route.
/// </summary>
[Route("users")]
public class UsersController : IdentityControllerBase
{
[HttpGet]
public async Task<object?> List(
[FromQuery("offset")] int offset = 0,
[FromQuery("count")] int count = 25)
{
return await Forward(IdentityEndpoints.ManageUsers, "list",
new ListRequest { Offset = offset, Count = count });
}
[HttpGet("{id}")]
public async Task<object?> Get([FromRoute("id")] string id)
{
return await Forward(IdentityEndpoints.ManageUsers, "read", ParseIdBody(id, stringKey: "login"));
}
[HttpPost]
public async Task<object?> Create([FromBody] CreateUserRequest request)
{
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.ManageUsers, "create", request);
}
[HttpPut("{id}")]
public async Task<object?> Update([FromRoute("id")] string id, [FromBody] UpdateUserRequest request)
{
if (long.TryParse(id, out var numericId))
request.Id = numericId;
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.ManageUsers, "update", request);
}
[HttpDelete("{id}")]
public async Task<object?> Delete([FromRoute("id")] string id)
{
return await Forward(IdentityEndpoints.ManageUsers, "delete", ParseIdBody(id, stringKey: "login"));
}
[HttpPost("{id}/change-password")]
public async Task<object?> ChangePassword([FromRoute("id")] string id, [FromBody] ChangePasswordRequest request)
{
if (long.TryParse(id, out var numericId))
request.Id = numericId;
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.ManageUsers, "change-password", request);
}
/// <summary>
/// Admin-side password reset (no OldPassword challenge — admin scope IS the
/// authorization). Use for "user forgot the password and the operator
/// resets it out-of-band" — the regular user-self change-password flow
/// stays as it was.
/// </summary>
[HttpPost("{id}/admin-reset-password")]
public async Task<object?> AdminResetPassword([FromRoute("id")] string id, [FromBody] AdminResetPasswordRequest request)
{
if (long.TryParse(id, out var numericId))
request.Id = numericId;
if (ValidateRequest(request) is { } problem) return problem;
return await Forward(IdentityEndpoints.ManageUsers, "admin-reset-password", request);
}
[HttpGet("search")]
public async Task<object?> Search(
[FromQuery("query")] string query,
[FromQuery("offset")] string? offset = null,
[FromQuery("count")] string? count = null)
{
var body = new Dictionary<string, object> { ["query"] = query };
if (!string.IsNullOrEmpty(offset)) body["offset"] = offset;
if (!string.IsNullOrEmpty(count)) body["count"] = count;
return await Forward(IdentityEndpoints.ManageUsers, "search", body);
}
}