mirror of
https://github.com/Jetsparrow/jetherald.git
synced 2026-01-21 07:56:09 +03:00
add user list page
This commit is contained in:
parent
6b6883bb20
commit
c5d4db1c4c
61
JetHerald/Controllers/Ui/AdminUsersController.cs
Normal file
61
JetHerald/Controllers/Ui/AdminUsersController.cs
Normal file
@ -0,0 +1,61 @@
|
|||||||
|
using JetHerald.Authorization;
|
||||||
|
using JetHerald.Contracts;
|
||||||
|
using JetHerald.Services;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.AspNetCore.Mvc.ModelBinding;
|
||||||
|
|
||||||
|
namespace JetHerald.Controllers.Ui;
|
||||||
|
|
||||||
|
[Permission("admin.users")]
|
||||||
|
[Route("ui/admin/users")]
|
||||||
|
public class AdminUsersController : Controller
|
||||||
|
{
|
||||||
|
Db Db { get; }
|
||||||
|
public AdminUsersController(Db db)
|
||||||
|
{
|
||||||
|
Db = db;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpGet]
|
||||||
|
public async Task<IActionResult> Index()
|
||||||
|
{
|
||||||
|
using var ctx = await Db.GetContext();
|
||||||
|
|
||||||
|
var users = await ctx.GetUsers();
|
||||||
|
var plans = await ctx.GetPlans();
|
||||||
|
var roles = await ctx.GetRoles();
|
||||||
|
|
||||||
|
return View(new AdminUsersModel
|
||||||
|
{
|
||||||
|
Users = users.ToArray(),
|
||||||
|
Plans = plans.ToDictionary(p => p.PlanId),
|
||||||
|
Roles = roles.ToDictionary(r => r.RoleId)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public class SetPermsRequest
|
||||||
|
{
|
||||||
|
[BindProperty(Name = "planId"), BindRequired]
|
||||||
|
public uint PlanId { get; set; }
|
||||||
|
[BindProperty(Name = "roleId"), BindRequired]
|
||||||
|
public uint RoleId { get; set; }
|
||||||
|
}
|
||||||
|
|
||||||
|
[Permission("admin.users.setperms")]
|
||||||
|
[HttpPost, Route("setperms/{userId?}")]
|
||||||
|
public async Task<IActionResult> SetPerms([FromRoute] uint userId, SetPermsRequest req)
|
||||||
|
{
|
||||||
|
using var ctx = await Db.GetContext();
|
||||||
|
await ctx.UpdatePerms(userId, req.PlanId, req.RoleId);
|
||||||
|
ctx.Commit();
|
||||||
|
|
||||||
|
return RedirectToAction(nameof(Index));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AdminUsersModel
|
||||||
|
{
|
||||||
|
public User[] Users { get; set; }
|
||||||
|
public Dictionary<uint, Plan> Plans { get; set; }
|
||||||
|
public Dictionary<uint, Role> Roles { get; set; }
|
||||||
|
}
|
||||||
@ -9,8 +9,8 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Dapper.Transaction" Version="2.0.35.2" />
|
<PackageReference Include="Dapper.Transaction" Version="2.0.35.2" />
|
||||||
<PackageReference Include="DSharpPlus" Version="4.1.0" />
|
<PackageReference Include="DSharpPlus" Version="4.2.0" />
|
||||||
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.1.0" />
|
<PackageReference Include="DSharpPlus.CommandsNext" Version="4.2.0" />
|
||||||
<PackageReference Include="MySql.Data" Version="8.0.28" />
|
<PackageReference Include="MySql.Data" Version="8.0.28" />
|
||||||
<PackageReference Include="NLog.Web.AspNetCore" Version="5.0.0-rc2" />
|
<PackageReference Include="NLog.Web.AspNetCore" Version="5.0.0-rc2" />
|
||||||
<PackageReference Include="Telegram.Bot.Extensions.Polling" Version="1.0.2" />
|
<PackageReference Include="Telegram.Bot.Extensions.Polling" Version="1.0.2" />
|
||||||
|
|||||||
@ -51,6 +51,15 @@ public class DbContext : IDisposable
|
|||||||
=> Tran.QueryAsync<Topic>(
|
=> Tran.QueryAsync<Topic>(
|
||||||
" SELECT * FROM topic WHERE CreatorId = @userId",
|
" SELECT * FROM topic WHERE CreatorId = @userId",
|
||||||
new { userId });
|
new { userId });
|
||||||
|
|
||||||
|
public Task UpdatePerms(uint userId, uint planId, uint roleId)
|
||||||
|
=> Tran.ExecuteAsync(@"
|
||||||
|
UPDATE user
|
||||||
|
SET PlanId = @planId,
|
||||||
|
RoleId = @roleId
|
||||||
|
WHERE UserId = @userId",
|
||||||
|
new { userId, planId, roleId });
|
||||||
|
|
||||||
public Task<IEnumerable<Plan>> GetPlans()
|
public Task<IEnumerable<Plan>> GetPlans()
|
||||||
=> Tran.QueryAsync<Plan>("SELECT * FROM plan");
|
=> Tran.QueryAsync<Plan>("SELECT * FROM plan");
|
||||||
public Task<IEnumerable<Role>> GetRoles()
|
public Task<IEnumerable<Role>> GetRoles()
|
||||||
@ -60,6 +69,10 @@ public class DbContext : IDisposable
|
|||||||
SELECT ui.*, u.Login as RedeemedByLogin
|
SELECT ui.*, u.Login as RedeemedByLogin
|
||||||
FROM userinvite ui
|
FROM userinvite ui
|
||||||
LEFT JOIN user u ON ui.RedeemedBy = u.UserId");
|
LEFT JOIN user u ON ui.RedeemedBy = u.UserId");
|
||||||
|
public Task<IEnumerable<User>> GetUsers()
|
||||||
|
=> Tran.QueryAsync<User>(@"
|
||||||
|
SELECT u.*
|
||||||
|
FROM user u;");
|
||||||
|
|
||||||
public Task<IEnumerable<Heart>> GetHeartsForUser(uint userId)
|
public Task<IEnumerable<Heart>> GetHeartsForUser(uint userId)
|
||||||
=> Tran.QueryAsync<Heart>(
|
=> Tran.QueryAsync<Heart>(
|
||||||
|
|||||||
50
JetHerald/Views/AdminUsers/Index.cshtml
Normal file
50
JetHerald/Views/AdminUsers/Index.cshtml
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
@model AdminUsersModel
|
||||||
|
|
||||||
|
<h3>Invites</h3>
|
||||||
|
<ul class="issues-list">
|
||||||
|
@foreach (var user in Model.Users)
|
||||||
|
{
|
||||||
|
<li>
|
||||||
|
@user.Name <span class="username">@@@user.Login</span> (
|
||||||
|
@if (Context.UserCan("admin.users.setperms"))
|
||||||
|
{
|
||||||
|
<form asp-controller="AdminUsers" asp-action="SetPerms" asp-route-userid="@user.UserId" method="POST"
|
||||||
|
enctype="application/x-www-form-urlencoded" style="display:inline">
|
||||||
|
r:<select name="roleId" required class="blueunderline">
|
||||||
|
@foreach (var role in Model.Roles.Values)
|
||||||
|
{
|
||||||
|
if (role.RoleId == user.RoleId)
|
||||||
|
{
|
||||||
|
<option value="@role.RoleId" selected>@role.Name</option>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<option value="@role.RoleId">@role.Name</option>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</select>,
|
||||||
|
p:<select name="planId" required class="blueunderline">
|
||||||
|
@foreach (var plan in Model.Plans.Values)
|
||||||
|
{
|
||||||
|
if (plan.PlanId == user.PlanId)
|
||||||
|
{
|
||||||
|
<option value="@plan.PlanId" selected>@plan.Name</option>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
<option value="@plan.PlanId">@plan.Name</option>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</select>
|
||||||
|
<input type="submit" value="Set permissions">
|
||||||
|
</form>
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
@: r:@Model.Roles[user.RoleId].Name,
|
||||||
|
@: p:@Model.Plans[user.PlanId].Name
|
||||||
|
}
|
||||||
|
)
|
||||||
|
</li>
|
||||||
|
}
|
||||||
|
</ul>
|
||||||
@ -396,3 +396,7 @@ td.numeric {
|
|||||||
a.show-button {
|
a.show-button {
|
||||||
display: inline;
|
display: inline;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.username {
|
||||||
|
color: gray;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user