Skip to content

Commit 30d31e1

Browse files
added administration functions
1 parent bcb335b commit 30d31e1

File tree

4 files changed

+138
-17
lines changed

4 files changed

+138
-17
lines changed

jjodel-persistence/jjodel-persistence/Controllers/API/ProjectController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public async Task<IActionResult> GetByJJodelId(string Id) {
125125
Project result = await this._projectService.GetByJJodelId(Id);
126126

127127
//todo: remove
128-
result.Collaborators.Add(await this._userManager.FindByNameAsync(this.User.Identity.Name));
129-
await this._projectService.Save();
128+
//result.Collaborators.Add(await this._userManager.FindByNameAsync(this.User.Identity.Name));
129+
//await this._projectService.Save();
130130

131131

132132
if(result == null) {

jjodel-persistence/jjodel-persistence/Controllers/Web/AccountController.cs

Lines changed: 73 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
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;
62
using jjodel_persistence.Models.Entity;
3+
using jjodel_persistence.Models.Mail;
74
using jjodel_persistence.Models.Settings;
85
using jjodel_persistence.Services;
9-
using Microsoft.AspNetCore.Authentication.Cookies;
106
using Microsoft.AspNetCore.Authentication;
7+
using Microsoft.AspNetCore.Authentication.Cookies;
118
using Microsoft.AspNetCore.Authorization;
129
using Microsoft.AspNetCore.Identity;
1310
using Microsoft.AspNetCore.Mvc;
1411
using Microsoft.EntityFrameworkCore;
1512
using 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

1919
namespace 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
}

jjodel-persistence/jjodel-persistence/Views/Account/Index.cshtml

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,27 @@
7878
OpenConfirmModal(id, "Are you sure you want to delete this user? This action cannot be undone.", "DeleteUser('"+id+"')");
7979
}
8080
81+
function EnableUser(id){
82+
fetch(`/Account/EnableUser/${id}`)
83+
.then(response => response.text())
84+
.then(data => {
85+
var jsonData = JSON.parse(data)
86+
if(jsonData.success){
87+
OpenSuccessModal(jsonData.message, null);
88+
List();
89+
}
90+
else{
91+
OpenErrorModal(jsonData.message)
92+
}
93+
})
94+
.catch(error => OpenErrorModal(error) );
95+
}
96+
97+
function EnableUserConfirm(id){
98+
OpenConfirmModal(id, "Are you sure you want to enable this user? This action cannot be undone.", "EnableUser('"+id+"')");
99+
100+
}
101+
81102
function List(){
82103
83104
fetch('/Account/List')
@@ -122,7 +143,28 @@
122143
.catch(error => OpenErrorModal(error) );
123144
}
124145
125-
function SaveUser() {
146+
function ResetPasswordConfirm(id){
147+
OpenConfirmModal(id, "Are you sure you want to reset the password for this user? This action cannot be undone.", "ResetPassword('"+id+"')");
148+
149+
}
150+
151+
function ResetPassword(id){
152+
fetch(`/Account/ResetPasswordByAdmin/${id}`)
153+
.then(response => response.text())
154+
.then(data => {
155+
var jsonData = JSON.parse(data)
156+
if(jsonData.success){
157+
OpenSuccessModal(jsonData.message, null);
158+
List();
159+
}
160+
else{
161+
OpenErrorModal(jsonData.message)
162+
}
163+
})
164+
.catch(error => OpenErrorModal(error) );
165+
}
166+
167+
function SaveUser() {
126168
let form = document.getElementById("editUserForm");
127169
let formData = new FormData(form);
128170

jjodel-persistence/jjodel-persistence/Views/Shared/UC_UserList.cshtml

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,26 @@
6262

6363
<td>
6464
<div class="d-grid gap-2 d-flex justify-content-center">
65-
<button class="btn btn-primary editUserBtn" onclick="EditUser('@user.Id')" title="Edit User">
66-
<i class="bi bi-pencil-square"></i>
67-
</button>
68-
<button class="btn btn-danger deleteUserBtn" onclick="DeleteUserConfirm('@user.Id')" title="Delete User">
69-
<i class="bi bi-trash"></i>
70-
</button>
65+
66+
@if (!user.EmailConfirmed){
67+
<button class="btn btn-warning " onclick="EnableUserConfirm('@user.Id')" title="Enable User">
68+
<i class="bi bi-check-lg"></i>
69+
</button>
70+
}
71+
else {
72+
<button class="btn btn-primary " onclick="EditUser('@user.Id')" title="Edit User">
73+
<i class="bi bi-pencil-square"></i>
74+
</button>
75+
<button class="btn btn-warning " onclick="ResetPasswordConfirm('@user.Id')" title="Reset Password">
76+
<i class="bi bi-unlock"></i>
77+
</button>
78+
79+
<button class="btn btn-danger " onclick="DeleteUserConfirm('@user.Id')" title="Delete User">
80+
<i class="bi bi-trash"></i>
81+
</button>
82+
83+
}
84+
7185
</div>
7286
</td>
7387
</tr>

0 commit comments

Comments
 (0)