1- using System . Diagnostics ;
2- using System . IdentityModel . Tokens . Jwt ;
3- using System . Security . Claims ;
4- using System . Text ;
5- using jjodel_persistence . Models . Dto ;
1+ using jjodel_persistence . Models . Dto ;
62using jjodel_persistence . Models . Entity ;
3+ using jjodel_persistence . Models . Mail ;
74using jjodel_persistence . Models . Settings ;
85using jjodel_persistence . Services ;
9- using Microsoft . AspNetCore . Authentication . Cookies ;
106using Microsoft . AspNetCore . Authentication ;
7+ using Microsoft . AspNetCore . Authentication . Cookies ;
118using Microsoft . AspNetCore . Authorization ;
129using Microsoft . AspNetCore . Identity ;
1310using Microsoft . AspNetCore . Mvc ;
1411using Microsoft . EntityFrameworkCore ;
1512using Microsoft . Extensions . Options ;
16- using jjodel_persistence . Models . Mail ;
13+ using System . Diagnostics ;
14+ using System . IdentityModel . Tokens . Jwt ;
15+ using System . Security . Claims ;
16+ using System . Text ;
1717
1818
1919namespace jjodel_persistence . Controllers . Web {
@@ -128,6 +128,36 @@ public async Task<ActionResult> Edit(string Id) {
128128 return PartialView ( "~/Views/Shared/UC_UserForm.cshtml" , user ) ;
129129 }
130130
131+ [ HttpGet ]
132+ [ Route ( "EnableUser/{Id}" ) ]
133+ [ Authorize ( AuthenticationSchemes = CookieAuthenticationDefaults . AuthenticationScheme , Roles = "Admin" ) ]
134+ public async Task < ActionResult > EnableUser ( string Id ) {
135+ try {
136+ ApplicationUser admin = await _userManager . FindByNameAsync ( User . Identity . Name ) ;
137+
138+ this . _logger . LogWarning ( "Confirming Account by admin " + admin . Id + ":" + Id ) ;
139+
140+ ApplicationUser user = await this . _userManager . FindByIdAsync ( Id ) ;
141+ if ( user != null ) {
142+
143+ user . EmailConfirmed = true ;
144+ var result = await this . _userManager . UpdateAsync ( user ) ;
145+
146+ if ( result . Succeeded ) {
147+ this . _logger . LogInformation ( "Confirmed Account by admin " + admin . Id + ": " + Id ) ;
148+
149+ return Json ( new { success = true , Message = "Operation completed successfully." } ) ;
150+ }
151+ }
152+ this . _logger . LogInformation ( "Confirming Account failed for user: " + Id ) ;
153+ }
154+ catch ( Exception ex ) {
155+ this . _logger . LogError ( ex . Message ) ;
156+ }
157+ return Json ( new { success = false , message = "Error updating user" } ) ;
158+
159+ }
160+
131161 [ HttpGet ]
132162 [ Route ( "List" ) ]
133163 [ Authorize ( AuthenticationSchemes = CookieAuthenticationDefaults . AuthenticationScheme , Roles = "Admin" ) ]
@@ -414,7 +444,42 @@ public async Task<ActionResult> ResetPassword(ResetPasswordRequest resetPassword
414444 return View ( ) ;
415445 }
416446
447+ [ HttpGet ]
448+ [ Route ( "ResetPasswordByAdmin/{Id}" ) ]
449+ [ Authorize ( AuthenticationSchemes = CookieAuthenticationDefaults . AuthenticationScheme , Roles = "Admin" ) ]
450+ public async Task < ActionResult > ResetPasswordByAdmin ( string Id ) {
451+ try {
452+
453+ ApplicationUser admin = await _userManager . FindByNameAsync ( User . Identity . Name ) ;
454+
455+ this . _logger . LogWarning ( "Reset Password by admin " + admin . Id + ":" + Id ) ;
456+
457+ ApplicationUser user = await this . _userManager . FindByIdAsync ( Id ) ;
458+
459+ if ( user != null ) {
460+ // generate password.
461+ string password = _authService . GenerateRandomPassword ( ) ;
462+ var token = await _userManager . GeneratePasswordResetTokenAsync ( user ) ;
463+ var result = await _userManager . ResetPasswordAsync ( user , token , password ) ;
464+
465+ if ( ! result . Succeeded ) {
466+ this . _logger . LogWarning ( "Reset Password by admin " + admin . Id + " failed:" + Id ) ;
467+ return BadRequest ( ) ;
468+ }
469+ await _mailService . SendEmail ( new List < string > { user . Email } , "Reset Password" , "ResetPassword" , new ResetPassword ( ) { NewPassoword = password , Username = user . UserName } ) ;
470+ this . _logger . LogInformation ( "The password has been reset" ) ;
471+ return Json ( new { success = true , Message = "Operation completed successfully." } ) ;
472+
473+ }
474+
475+
476+ }
477+ catch ( Exception ex ) {
478+ this . _logger . LogError ( ex . Message ) ;
479+ }
480+ return Json ( new { success = false , message = "Error resetting password" } ) ;
481+
482+ }
417483
418-
419484 }
420485}
0 commit comments