From 7a06ce1deb1c3fbc0fef719e568f32bcbe19452b Mon Sep 17 00:00:00 2001 From: Paul Schneider Date: Tue, 19 Aug 2025 21:31:49 +0100 Subject: [PATCH] User list --- .vscode/launch.json | 1 + Makefile | 3 + .../accounting/ProfileApiController.cs | 2 +- src/Yavsc.Abstract/Identity/UserInfo.cs | 6 +- src/Yavsc.Server/Hubs/ChatHub.cs | 8 +-- .../Models/HairCut/Views/HaircutQueryInfo.cs | 4 +- .../Accounting/AccountController.cs | 6 +- .../AdministrationController.cs | 61 +++++++++++------ .../Administration/EnrolerViewModel.cs | 8 +-- src/Yavsc/Views/Account/UserList.cshtml | 68 +++++++++++++++++++ src/Yavsc/Views/Administration/Role.cshtml | 4 +- 11 files changed, 129 insertions(+), 42 deletions(-) create mode 100644 src/Yavsc/Views/Account/UserList.cshtml diff --git a/.vscode/launch.json b/.vscode/launch.json index 865f541b..b3fcb29d 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,7 @@ // Pour plus d'informations, visitez : https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { "name": "cli .NET Core Launch (console)", "type": "coreclr", diff --git a/Makefile b/Makefile index 5336e90f..9612de36 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,9 @@ CONFIG=Debug FRAMEWORK=net8.0 DESTDIR=/tmp/yavsc +all: + @dotnet build --nologo 2>/dev/null |grep error + clean: dotnet clean diff --git a/src/Api/Controllers/accounting/ProfileApiController.cs b/src/Api/Controllers/accounting/ProfileApiController.cs index 4f9d316d..ce90b076 100644 --- a/src/Api/Controllers/accounting/ProfileApiController.cs +++ b/src/Api/Controllers/accounting/ProfileApiController.cs @@ -34,7 +34,7 @@ namespace Yavsc.ApiControllers.accounting public UserInfo[] GetUserHint(string name) { return _dbContext.Users.Where(u=>u.UserName.IndexOf(name)>0) - .Select(u=>new UserInfo(u.Id, u.UserName, u.Avatar)) + .Select(u=>new UserInfo(u.Id, u.UserName, u.Email, u.Avatar)) .Take(10).ToArray(); } } diff --git a/src/Yavsc.Abstract/Identity/UserInfo.cs b/src/Yavsc.Abstract/Identity/UserInfo.cs index f4d8c078..5ddf5e91 100644 --- a/src/Yavsc.Abstract/Identity/UserInfo.cs +++ b/src/Yavsc.Abstract/Identity/UserInfo.cs @@ -7,15 +7,17 @@ namespace Yavsc.Abstract.Identity } - public UserInfo(string userId, string userName, string avatar) + public UserInfo(string userId, string userName, string email, string avatar) { UserId = userId; UserName = userName; + Email = email; Avatar = avatar; } public string UserId { get; set; } - public string UserName { get; set; } + public string UserName { get; set; } + public string Email { get; } public string Avatar { get; set; } } } diff --git a/src/Yavsc.Server/Hubs/ChatHub.cs b/src/Yavsc.Server/Hubs/ChatHub.cs index 8d416d09..20da2014 100644 --- a/src/Yavsc.Server/Hubs/ChatHub.cs +++ b/src/Yavsc.Server/Hubs/ChatHub.cs @@ -56,10 +56,10 @@ namespace Yavsc _localizer = stringLocalizerFactory.Create(typeof(ChatHub)); _cxManager = connexionManager; - _cxManager.SetErrorHandler ((context, error) => - { - NotifyUser(NotificationTypes.Error, context, error); - }); + _cxManager.SetErrorHandler ((context, error) => + { + NotifyUser(NotificationTypes.Error, context, error); + }); _logger = loggerFactory.CreateLogger(); InputValidator = new HubInputValidator { NotifyUser = async (type, target, msg) => await this.NotifyUser(type, target, msg) }; } diff --git a/src/Yavsc.Server/Models/HairCut/Views/HaircutQueryInfo.cs b/src/Yavsc.Server/Models/HairCut/Views/HaircutQueryInfo.cs index 479e831f..d86da930 100644 --- a/src/Yavsc.Server/Models/HairCut/Views/HaircutQueryInfo.cs +++ b/src/Yavsc.Server/Models/HairCut/Views/HaircutQueryInfo.cs @@ -30,7 +30,7 @@ namespace Yavsc.Models.Haircut.Views public class HaircutQueryProviderInfo : HaircutQueryComonInfo { public HaircutQueryProviderInfo(HairCutQuery query) : base (query) { - ClientInfo = new UserInfo(query.Client.Id, query.Client.UserName, query.Client.Avatar); + ClientInfo = new UserInfo(query.Client.Id, query.Client.UserName, query.Client.Email, query.Client.Avatar); } public UserInfo ClientInfo { get; set; } @@ -39,7 +39,7 @@ namespace Yavsc.Models.Haircut.Views public HaircutQueryClientInfo(HairCutQuery query) : base (query) { var user = query.PerformerProfile.Performer; - ProviderInfo = new UserInfo(user.Id, user.UserName, user.Avatar); + ProviderInfo = new UserInfo(user.Id, user.UserName, user.Email, user.Avatar); } public UserInfo ProviderInfo { get; set; } diff --git a/src/Yavsc/Controllers/Accounting/AccountController.cs b/src/Yavsc/Controllers/Accounting/AccountController.cs index eae468ea..2c73354b 100644 --- a/src/Yavsc/Controllers/Accounting/AccountController.cs +++ b/src/Yavsc/Controllers/Accounting/AccountController.cs @@ -412,8 +412,8 @@ namespace Yavsc.Controllers } [Authorize("AdministratorOnly")] - [Route("Account/UserList/{pageNum}/{len?}")] - public async Task UserList(int pageNum, int pageLen = defaultLen) + [Route("Account/UserList/{pageNum?}/{len?}")] + public async Task UserList(int pageNum=0, int pageLen = defaultLen) { var users = _dbContext.Users.OrderBy(u=>u.UserName); var shown = pageNum * pageLen; @@ -423,8 +423,6 @@ namespace Yavsc.Controllers ViewBag.hasNext = users.Count() > (toShow.Count() + shown); ViewBag.nextpage = pageNum+1; ViewBag.pageLen = pageLen; - // ApplicationUser user; - // user.EmailConfirmed return View(toShow.ToArray()); } diff --git a/src/Yavsc/Controllers/Administration/AdministrationController.cs b/src/Yavsc/Controllers/Administration/AdministrationController.cs index 3979dae5..00499b08 100644 --- a/src/Yavsc/Controllers/Administration/AdministrationController.cs +++ b/src/Yavsc/Controllers/Administration/AdministrationController.cs @@ -30,11 +30,26 @@ namespace Yavsc.Controllers this._dbContext = context; } - private async Task EnsureRoleList () { + public async Task 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)) + .Select(u => new UserInfo (id, u.UserName, u.Email, u.Avatar)).ToArray() + }; + return View(roleUserCollection); + } + + private async Task EnsureRoleList() + { // ensure all roles existence foreach (string roleName in new string[] { Constants.AdminGroupName, - Constants.StarGroupName, + Constants.StarGroupName, Constants.PerformerGroupName, Constants.FrontOfficeGroupName, Constants.StarHunterGroupName, @@ -50,8 +65,8 @@ namespace Yavsc.Controllers return false; } } - return true; - + return true; + } /// /// Gives the (new if was not existing) administrator role @@ -65,20 +80,21 @@ namespace Yavsc.Controllers { // If some amdin already exists, make this method disapear var admins = await _userManager.GetUsersInRoleAsync(Constants.AdminGroupName); - if (admins != null && admins.Count > 0) + if (admins != null && admins.Count > 0) { // All is ok, nothing to do here. if (User.IsInMsRole(Constants.AdminGroupName)) { - + return Ok(new { message = "you already got it." }); } return NotFound(); } - + var user = await _userManager.FindByIdAsync(User.GetUserId()); // check all user groups exist - if (!await EnsureRoleList()) { + if (!await EnsureRoleList()) + { ModelState.AddModelError(null, "Could not ensure role list existence. aborting."); return new BadRequestObjectResult(ModelState); } @@ -103,11 +119,12 @@ namespace Yavsc.Controllers var youAreAdmin = await _userManager.IsInRoleAsync( await _userManager.FindByIdAsync(User.GetUserId()), Constants.AdminGroupName); - - var roles = await _roleManager.Roles.Select(x => new RoleInfo { + + var roles = await _roleManager.Roles.Select(x => new RoleInfo + { Id = x.Id, - Name = x.Name - }).ToArrayAsync(); + Name = x.Name + }).ToArrayAsync(); foreach (var role in roles) { var uinrole = await _userManager.GetUsersInRoleAsync(role.Name); @@ -115,9 +132,9 @@ namespace Yavsc.Controllers role.UserCount = uinrole.Count(); } var assembly = GetType().Assembly; - ViewBag.ThisAssembly = assembly.FullName; - ViewBag.RunTimeVersion = assembly.ImageRuntimeVersion; - var rolesArray = roles.ToArray(); + ViewBag.ThisAssembly = assembly.FullName; + ViewBag.RunTimeVersion = assembly.ImageRuntimeVersion; + var rolesArray = roles.ToArray(); return View(new AdminViewModel { Roles = rolesArray, @@ -131,7 +148,7 @@ namespace Yavsc.Controllers public IActionResult Enroll(string roleName) { ViewBag.UserId = new SelectList(_dbContext.Users, "Id", "UserName"); - return View(new EnrolerViewModel{ RoleName = roleName }); + return View(new EnrolerViewModel { RoleName = roleName }); } [Authorize("AdministratorOnly")] @@ -140,8 +157,8 @@ namespace Yavsc.Controllers { if (ModelState.IsValid) { - var newAdmin = await _dbContext.Users.FirstOrDefaultAsync(u=>u.Id==model.EnroledUserId); - if (newAdmin==null) return NotFound(); + var newAdmin = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == model.EnroledUserId); + if (newAdmin == null) return NotFound(); var addToRoleResult = await _userManager.AddToRoleAsync(newAdmin, model.RoleName); if (addToRoleResult.Succeeded) { @@ -156,10 +173,10 @@ namespace Yavsc.Controllers [Authorize("AdministratorOnly")] public async Task Fire(string roleName, string userId) { - var user = await _dbContext.Users.FirstOrDefaultAsync(u=>u.Id==userId); + var user = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == userId); if (user == null) return NotFound(); - return View(new FireViewModel{ RoleName = roleName, EnroledUserId = userId, EnroledUserName = user.UserName }); + return View(new FireViewModel { RoleName = roleName, EnroledUserId = userId, EnroledUserName = user.UserName }); } [Authorize("AdministratorOnly")] @@ -168,8 +185,8 @@ namespace Yavsc.Controllers { if (ModelState.IsValid) { - var oldEnroled = await _dbContext.Users.FirstOrDefaultAsync(u=>u.Id==model.EnroledUserId); - if (oldEnroled==null) return NotFound(); + var oldEnroled = await _dbContext.Users.FirstOrDefaultAsync(u => u.Id == model.EnroledUserId); + if (oldEnroled == null) return NotFound(); var removeFromRole = await _userManager.RemoveFromRoleAsync(oldEnroled, model.RoleName); if (removeFromRole.Succeeded) { diff --git a/src/Yavsc/ViewModels/Administration/EnrolerViewModel.cs b/src/Yavsc/ViewModels/Administration/EnrolerViewModel.cs index bf19fc38..1963e357 100644 --- a/src/Yavsc/ViewModels/Administration/EnrolerViewModel.cs +++ b/src/Yavsc/ViewModels/Administration/EnrolerViewModel.cs @@ -5,13 +5,11 @@ namespace Yavsc.ViewModels { public partial class EnrolerViewModel { - [Display(Name="EnroledLabel", ResourceType=typeof(EnrolerViewModel))] - [YaRequired] + [Required] public string EnroledUserId { get; set; } - [Display(Name="RoleNameLabel", ResourceType=typeof(EnrolerViewModel))] - [YaRequired] + [Required] public string RoleName { get; set; } } -} \ No newline at end of file +} diff --git a/src/Yavsc/Views/Account/UserList.cshtml b/src/Yavsc/Views/Account/UserList.cshtml new file mode 100644 index 00000000..81b5632a --- /dev/null +++ b/src/Yavsc/Views/Account/UserList.cshtml @@ -0,0 +1,68 @@ +@model ApplicationUser[] +@{ + ViewData["Title"] = "Liste des utilisateurs"; +} + +

@ViewData["Title"].

+ + + + + + +@foreach (var user in Model) +{ + + + + +} +
+ @SR["Public info"] + + @SR["AdminOnly"] +
+ @Html.DisplayFor(m=>user) + +
+
+ @SR["UserName"] +
+
+ @Html.DisplayFor(m=>user.UserName) +
+
+
+
+ @SR["FullName"] +
+
+ @Html.DisplayFor(m=>user.FullName) +
+
+
+
+ @SR["PostalAddress"] +
+
+ @Html.DisplayFor(m=>user.PostalAddress) +
+
+
+
+ @SR["Email"] +
+
+ @Html.DisplayFor(m=>user.Email) + @if (!user.EmailConfirmed) { + Envoyer une demande de confirmation + } + Supprimer +
+
+
+ +@if (ViewBag.hasNext) +{ + Next page +} diff --git a/src/Yavsc/Views/Administration/Role.cshtml b/src/Yavsc/Views/Administration/Role.cshtml index 290b87e6..68a5710f 100644 --- a/src/Yavsc/Views/Administration/Role.cshtml +++ b/src/Yavsc/Views/Administration/Role.cshtml @@ -12,7 +12,7 @@ @@ -22,7 +22,7 @@
- User"] + User
avatar - @user.UserName + @user.UserName <@(user.Email)>