|
| 1 | +using System.ComponentModel.DataAnnotations; |
| 2 | +using Humanizer; |
| 3 | +using Microsoft.AspNetCore.Authorization; |
| 4 | +using Microsoft.AspNetCore.Mvc; |
| 5 | +using Microsoft.AspNetCore.Mvc.Filters; |
| 6 | +using Microsoft.AspNetCore.Mvc.RazorPages; |
| 7 | +using TeachingRecordSystem.Core.DataStore.Postgres; |
| 8 | +using TeachingRecordSystem.Core.Services.Files; |
| 9 | +using TeachingRecordSystem.SupportUi.Infrastructure.DataAnnotations; |
| 10 | +using TeachingRecordSystem.SupportUi.Infrastructure.Security; |
| 11 | +using TeachingRecordSystem.SupportUi.Pages.EditUser; |
| 12 | + |
| 13 | +namespace TeachingRecordSystem.SupportUi.Pages.Users.EditUser |
| 14 | +{ |
| 15 | + [Authorize(Policy = AuthorizationPolicies.UserManagement)] |
| 16 | + public class DeactivateModel( |
| 17 | + TrsLinkGenerator linkGenerator, |
| 18 | + IFileService fileService, |
| 19 | + TrsDbContext dbContext, |
| 20 | + IClock clock) : PageModel |
| 21 | + { |
| 22 | + private Core.DataStore.Postgres.Models.User? _user; |
| 23 | + |
| 24 | + [FromRoute] |
| 25 | + public Guid UserId { get; set; } |
| 26 | + |
| 27 | + [BindProperty] |
| 28 | + [Display(Name = "Reason for deactivating user")] |
| 29 | + [Required(ErrorMessage = "Select a reason for deactivating this user")] |
| 30 | + public bool? HasAdditionalReason { get; set; } |
| 31 | + |
| 32 | + [BindProperty] |
| 33 | + [Display(Name = "Enter a reason for deactivating this user")] |
| 34 | + public string? AdditionalReasonDetail { get; set; } |
| 35 | + |
| 36 | + [BindProperty] |
| 37 | + [Display(Name = "Do you have more information?")] |
| 38 | + [Required(ErrorMessage = "Select yes if you want to provide more details")] |
| 39 | + public bool? HasMoreInformation { get; set; } |
| 40 | + |
| 41 | + [BindProperty] |
| 42 | + [Display(Name = "Enter details")] |
| 43 | + public string? MoreInformationDetail { get; set; } |
| 44 | + |
| 45 | + [BindProperty] |
| 46 | + [Display(Name = "Do you have evidence to upload?")] |
| 47 | + [Required(ErrorMessage = "Select yes if you want to upload evidence")] |
| 48 | + public bool? UploadEvidence { get; set; } |
| 49 | + |
| 50 | + [BindProperty] |
| 51 | + [EvidenceFile] |
| 52 | + [FileSize(DeactivateDefaults.MaxFileUploadSizeMb * 1024 * 1024, ErrorMessage = "The selected file must be smaller than 100MB")] |
| 53 | + public IFormFile? EvidenceFile { get; set; } |
| 54 | + |
| 55 | + [BindProperty] |
| 56 | + public Guid? EvidenceFileId { get; set; } |
| 57 | + |
| 58 | + [BindProperty] |
| 59 | + public string? EvidenceFileName { get; set; } |
| 60 | + |
| 61 | + [BindProperty] |
| 62 | + public string? EvidenceFileSizeDescription { get; set; } |
| 63 | + |
| 64 | + [BindProperty] |
| 65 | + public string? UploadedEvidenceFileUrl { get; set; } |
| 66 | + |
| 67 | + public void OnGet() |
| 68 | + { |
| 69 | + } |
| 70 | + |
| 71 | + public async Task<IActionResult> OnPostAsync() |
| 72 | + { |
| 73 | + if (_user is null) |
| 74 | + { |
| 75 | + return base.NotFound(); |
| 76 | + } |
| 77 | + |
| 78 | + if (!_user.Active) |
| 79 | + { |
| 80 | + return BadRequest(); |
| 81 | + } |
| 82 | + |
| 83 | + // Only admins can deactivate admins |
| 84 | + if (!User.IsInRole(UserRoles.Administrator) && _user.Role == UserRoles.Administrator) |
| 85 | + { |
| 86 | + return BadRequest(); |
| 87 | + } |
| 88 | + |
| 89 | + if (HasAdditionalReason == true && AdditionalReasonDetail is null) |
| 90 | + { |
| 91 | + ModelState.AddModelError(nameof(AdditionalReasonDetail), "Enter a reason"); |
| 92 | + } |
| 93 | + |
| 94 | + if (HasMoreInformation == true && MoreInformationDetail is null) |
| 95 | + { |
| 96 | + ModelState.AddModelError(nameof(MoreInformationDetail), "Enter more details"); |
| 97 | + } |
| 98 | + |
| 99 | + if (UploadEvidence == true && EvidenceFileId is null && EvidenceFile is null) |
| 100 | + { |
| 101 | + ModelState.AddModelError(nameof(EvidenceFile), "Select a file"); |
| 102 | + } |
| 103 | + |
| 104 | + // Delete any previously uploaded file if they're uploading a new one, |
| 105 | + // or choosing not to upload evidence (check for UploadEvidence != true because if |
| 106 | + // UploadEvidence somehow got set to null we still want to delete the file) |
| 107 | + if (EvidenceFileId.HasValue && (EvidenceFile is not null || UploadEvidence != true)) |
| 108 | + { |
| 109 | + await fileService.DeleteFileAsync(EvidenceFileId.Value); |
| 110 | + } |
| 111 | + |
| 112 | + // Upload the file even if the rest of the form is invalid |
| 113 | + // otherwise the user will have to re-upload every time they re-submit |
| 114 | + if (UploadEvidence == true) |
| 115 | + { |
| 116 | + // Upload the file and set the display fields |
| 117 | + if (EvidenceFile is not null) |
| 118 | + { |
| 119 | + using var stream = EvidenceFile.OpenReadStream(); |
| 120 | + var fileId = await fileService.UploadFileAsync(stream, EvidenceFile.ContentType); |
| 121 | + EvidenceFileName = EvidenceFile?.FileName; |
| 122 | + EvidenceFileSizeDescription = EvidenceFile?.Length.Bytes().Humanize(); |
| 123 | + UploadedEvidenceFileUrl = await fileService.GetFileUrlAsync(fileId, DeactivateDefaults.FileUrlExpiry); |
| 124 | + EvidenceFileId = fileId; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + if (!ModelState.IsValid) |
| 129 | + { |
| 130 | + return this.PageWithErrors(); |
| 131 | + } |
| 132 | + |
| 133 | + _user.Active = false; |
| 134 | + |
| 135 | + await dbContext.AddEventAndBroadcastAsync(new UserDeactivatedEvent |
| 136 | + { |
| 137 | + EventId = Guid.NewGuid(), |
| 138 | + User = EventModels.User.FromModel(_user), |
| 139 | + RaisedBy = User.GetUserId(), |
| 140 | + CreatedUtc = clock.UtcNow, |
| 141 | + AdditionalReason = (HasAdditionalReason ?? false) ? AdditionalReasonDetail : null, |
| 142 | + MoreInformation = (HasMoreInformation ?? false) ? MoreInformationDetail : null, |
| 143 | + EvidenceFileId = EvidenceFileId, |
| 144 | + }); |
| 145 | + |
| 146 | + await dbContext.SaveChangesAsync(); |
| 147 | + TempData.SetFlashSuccess(message: $"{_user.Name}’s account has been deactivated."); |
| 148 | + |
| 149 | + return Redirect(linkGenerator.Users()); |
| 150 | + } |
| 151 | + |
| 152 | + public override async Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) |
| 153 | + { |
| 154 | + _user = await dbContext.Users.SingleOrDefaultAsync(u => u.UserId == UserId); |
| 155 | + |
| 156 | + if (_user is null) |
| 157 | + { |
| 158 | + context.Result = NotFound(); |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + await next(); |
| 163 | + } |
| 164 | + |
| 165 | + public async Task<IActionResult> OnGetCancelAsync(Guid? evidenceFileId) |
| 166 | + { |
| 167 | + if (evidenceFileId.HasValue) |
| 168 | + { |
| 169 | + await fileService.DeleteFileAsync(evidenceFileId.Value); |
| 170 | + } |
| 171 | + |
| 172 | + return Redirect(linkGenerator.EditUser(UserId)); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
0 commit comments