yavsc/src/Yavsc.Org/Controllers/Administration/AdministrationController.cs

210 lines
7.7 KiB
C#
Raw Normal View History

2016-05-17 18:22:18 +02:00
2023-03-19 17:57:55 +00:00
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
2018-07-16 02:38:09 +02:00
using Yavsc.Abstract.Identity;
2023-03-19 17:57:55 +00:00
using Yavsc.Helpers;
2016-05-17 18:22:18 +02:00
using Yavsc.Models;
using Yavsc.Server.Helpers;
2019-06-22 19:34:39 +01:00
using Yavsc.ViewModels;
using Yavsc.ViewModels.Administration;
2016-05-17 18:22:18 +02:00
namespace Yavsc.Controllers
{
2017-03-29 01:55:25 +02:00
[Authorize()]
2016-05-17 18:22:18 +02:00
public class AdministrationController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
2019-06-22 22:39:11 +01:00
private readonly ApplicationDbContext _dbContext;
2016-05-17 18:22:18 +02:00
public AdministrationController(UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager,
ApplicationDbContext context)
2016-05-17 18:22:18 +02:00
{
_userManager = userManager;
_roleManager = roleManager;
2019-06-22 22:39:11 +01:00
this._dbContext = context;
2016-05-17 18:22:18 +02:00
}
2026-02-16 00:14:33 +00:00
[Authorize("AdministratorOnly")]
2025-08-19 21:31:49 +01:00
public async Task<IActionResult> Role(string id)
{
var role = await _roleManager.FindByIdAsync(id);
if (role == null) return NotFound();
RoleUserCollection roleUserCollection = new RoleUserCollection
{
Id = id,
Name = role.Name,
Users = (await this._userManager.GetUsersInRoleAsync(role.Name))
2026-02-16 00:14:33 +00:00
.Select(u => new UserInfo(id, u.UserName, u.Email, u.Avatar)).ToArray()
2025-08-19 21:31:49 +01:00
};
return View(roleUserCollection);
}
private async Task<bool> EnsureRoleList()
{
2017-10-10 20:50:05 +02:00
// ensure all roles existence
foreach (string roleName in new string[] {
2026-05-30 19:34:22 +01:00
YavscConstants.AdminGroupName,
YavscConstants.StarGroupName,
YavscConstants.PerformerGroupName,
YavscConstants.FrontOfficeGroupName,
YavscConstants.StarHunterGroupName,
YavscConstants.BlogModeratorGroupName
2017-10-10 20:50:05 +02:00
})
if (!await _roleManager.RoleExistsAsync(roleName))
{
var role = new IdentityRole { Name = roleName };
var resultCreate = await _roleManager.CreateAsync(role);
if (!resultCreate.Succeeded)
{
AddErrors(resultCreate);
}
}
2026-02-16 00:14:33 +00:00
if (ModelState.ErrorCount > 0) return false;
2025-08-19 21:31:49 +01:00
return true;
2017-10-10 20:50:05 +02:00
}
2026-02-16 00:14:33 +00:00
2016-05-17 18:22:18 +02:00
/// <summary>
2026-02-16 00:14:33 +00:00
/// Gives the new, when not existing, administrator role
/// to current authenticated user.
/// If nothing is to do, it returns a 404.
2016-05-17 18:22:18 +02:00
/// </summary>
/// <returns></returns>
[Produces("application/json")]
public async Task<IActionResult> Take()
{
// If some amdin already exists, make this method disapear
2026-05-30 19:34:22 +01:00
var admins = await _userManager.GetUsersInRoleAsync(YavscConstants.AdminGroupName);
2025-08-19 21:31:49 +01:00
if (admins != null && admins.Count > 0)
2017-10-10 20:50:05 +02:00
{
2018-12-13 15:32:37 +00:00
// All is ok, nothing to do here.
2026-05-30 19:34:22 +01:00
if (User.IsInMsRole(YavscConstants.AdminGroupName))
{
2025-08-19 21:31:49 +01:00
2018-12-13 15:32:37 +00:00
return Ok(new { message = "you already got it." });
}
2023-03-19 17:57:55 +00:00
return NotFound();
2017-10-10 20:50:05 +02:00
}
2025-08-19 21:31:49 +01:00
2016-05-17 18:22:18 +02:00
var user = await _userManager.FindByIdAsync(User.GetUserId());
2018-12-13 15:32:37 +00:00
// check all user groups exist
2025-08-19 21:31:49 +01:00
if (!await EnsureRoleList())
{
2018-12-13 15:32:37 +00:00
ModelState.AddModelError(null, "Could not ensure role list existence. aborting.");
return new BadRequestObjectResult(ModelState);
}
2016-05-17 18:22:18 +02:00
2026-05-30 19:34:22 +01:00
var addToRoleResult = await _userManager.AddToRoleAsync(user, YavscConstants.AdminGroupName);
2016-05-17 18:22:18 +02:00
if (!addToRoleResult.Succeeded)
{
AddErrors(addToRoleResult);
return new BadRequestObjectResult(ModelState);
}
return Ok(new { message = "you owned it." });
2016-05-30 13:53:56 +02:00
}
2017-03-29 01:55:25 +02:00
2025-08-18 11:27:13 +01:00
[Authorize("AdministratorOnly")]
public async Task<IActionResult> Index()
{
2016-05-30 13:53:56 +02:00
var adminCount = await _userManager.GetUsersInRoleAsync(
2026-05-30 19:34:22 +01:00
YavscConstants.AdminGroupName);
2019-06-22 22:39:11 +01:00
var userCount = await _dbContext.Users.CountAsync();
2016-05-30 13:53:56 +02:00
var youAreAdmin = await _userManager.IsInRoleAsync(
await _userManager.FindByIdAsync(User.GetUserId()),
2026-05-30 19:34:22 +01:00
YavscConstants.AdminGroupName);
2025-08-19 21:31:49 +01:00
var roles = await _roleManager.Roles.Select(x => new RoleInfo
{
Id = x.Id,
2025-08-19 21:31:49 +01:00
Name = x.Name
}).ToArrayAsync();
2024-11-06 13:00:34 +00:00
foreach (var role in roles)
{
var uinrole = await _userManager.GetUsersInRoleAsync(role.Name);
role.UserCount = uinrole.Count();
}
2017-12-19 11:44:34 +01:00
var assembly = GetType().Assembly;
2025-08-19 21:31:49 +01:00
ViewBag.ThisAssembly = assembly.FullName;
ViewBag.RunTimeVersion = assembly.ImageRuntimeVersion;
var rolesArray = roles.ToArray();
return View(new AdminViewModel
{
2024-11-06 13:00:34 +00:00
Roles = rolesArray,
AdminCount = adminCount.Count,
2017-05-27 16:22:58 +02:00
YouAreAdmin = youAreAdmin,
UserCount = userCount
2016-05-30 13:53:56 +02:00
});
}
2017-03-29 01:55:25 +02:00
2019-06-22 19:34:39 +01:00
[Authorize("AdministratorOnly")]
2019-06-22 22:39:11 +01:00
public IActionResult Enroll(string roleName)
2019-06-22 19:34:39 +01:00
{
2019-06-22 22:39:11 +01:00
ViewBag.UserId = new SelectList(_dbContext.Users, "Id", "UserName");
2025-08-19 21:31:49 +01:00
return View(new EnrolerViewModel { RoleName = roleName });
2019-06-22 19:34:39 +01:00
}
[Authorize("AdministratorOnly")]
[HttpPost()]
2019-06-22 22:39:11 +01:00
public async Task<IActionResult> Enroll(EnrolerViewModel model)
2019-06-22 19:34:39 +01:00
{
if (ModelState.IsValid)
{
2025-08-19 21:31:49 +01:00
var newAdmin = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == model.EnroledUserId);
if (newAdmin == null) return NotFound();
2019-06-22 22:39:11 +01:00
var addToRoleResult = await _userManager.AddToRoleAsync(newAdmin, model.RoleName);
if (addToRoleResult.Succeeded)
2019-06-22 19:34:39 +01:00
{
2019-06-22 22:39:11 +01:00
return RedirectToAction("Index");
2019-06-22 19:34:39 +01:00
}
AddErrors(addToRoleResult);
}
2019-06-22 22:39:11 +01:00
ViewBag.UserId = new SelectList(_dbContext.Users, "Id", "UserName");
return View(model);
}
[Authorize("AdministratorOnly")]
public async Task<IActionResult> Fire(string roleName, string userId)
{
2025-08-19 21:31:49 +01:00
var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == userId);
2023-03-19 17:57:55 +00:00
if (user == null) return NotFound();
2019-06-22 22:39:11 +01:00
2025-08-19 21:31:49 +01:00
return View(new FireViewModel { RoleName = roleName, EnroledUserId = userId, EnroledUserName = user.UserName });
2019-06-22 22:39:11 +01:00
}
[Authorize("AdministratorOnly")]
[HttpPost()]
public async Task<IActionResult> Fire(FireViewModel model)
{
if (ModelState.IsValid)
{
2025-08-19 21:31:49 +01:00
var oldEnroled = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == model.EnroledUserId);
if (oldEnroled == null) return NotFound();
2019-06-22 22:39:11 +01:00
var removeFromRole = await _userManager.RemoveFromRoleAsync(oldEnroled, model.RoleName);
if (removeFromRole.Succeeded)
{
return RedirectToAction("Index");
}
AddErrors(removeFromRole);
}
ViewBag.UserId = new SelectList(_dbContext.Users, "Id", "UserName");
return View(model);
2019-06-22 19:34:39 +01:00
}
2026-02-16 00:14:33 +00:00
2016-05-17 18:22:18 +02:00
private void AddErrors(IdentityResult result)
{
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
}
}
}