diff --git a/BLL/Interfaces/User.cs b/BLL/Interfaces/User.cs index af68fb5..75c2d81 100644 --- a/BLL/Interfaces/User.cs +++ b/BLL/Interfaces/User.cs @@ -15,5 +15,7 @@ public interface IUserService Task> GetAllUsersAsync(); Task GetByEmailAndByPasswordAsync(string Email, string Password); Task UpdateAsync(UserDTO User); + Task> GetPendingUsersAsync(); + } } diff --git a/BLL/Services/User.cs b/BLL/Services/User.cs index fda1c33..44cbcac 100644 --- a/BLL/Services/User.cs +++ b/BLL/Services/User.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Text; using System.Threading.Tasks; +using DAL.Repositories; namespace BLL.Services { @@ -116,5 +117,11 @@ public async Task UpdateAsync(UserDTO e) throw; } } + public async Task> GetPendingUsersAsync() + { + var pendingUsers = await UserRepository.GetPendingUsersAsync(); + return mapper.Map>(pendingUsers); + } + } } diff --git a/DAL/Interfaces/User.cs b/DAL/Interfaces/User.cs index ab0894d..03b7937 100644 --- a/DAL/Interfaces/User.cs +++ b/DAL/Interfaces/User.cs @@ -15,5 +15,6 @@ public interface IUserRepository Task UpdateAsync(User entity); Task AddAsync(User entity); Task DeleteAsync(int id); + Task> GetPendingUsersAsync(); } } diff --git a/DAL/Models/User.cs b/DAL/Models/User.cs index 17d6994..7196d63 100644 --- a/DAL/Models/User.cs +++ b/DAL/Models/User.cs @@ -19,8 +19,19 @@ public class User public string Email { get; set; } [Required] public string Password { get; set; } - + [Required] + public UserStatus Status { get; set; } public virtual ICollection Discussions { get; set; } public virtual ICollection Comments { get; set; } + } + public enum UserStatus + { + SimpleUser, + PendingApproval, + Approved, + LoggedIn, + Admin + } + } diff --git a/DAL/Repositories/User.cs b/DAL/Repositories/User.cs index 2f7fe43..3547909 100644 --- a/DAL/Repositories/User.cs +++ b/DAL/Repositories/User.cs @@ -121,5 +121,11 @@ public async Task UpdateAsync(User entity) throw; } } + public async Task> GetPendingUsersAsync() + { + return await context.Users.Where(u => u.Status == UserStatus.PendingApproval).ToListAsync(); + } + + } } diff --git a/DTO/classes/User.cs b/DTO/classes/User.cs index 0bd4785..6247b66 100644 --- a/DTO/classes/User.cs +++ b/DTO/classes/User.cs @@ -1,4 +1,5 @@ -using System; +using DAL.Models; +using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; @@ -13,6 +14,6 @@ public class UserDTO public required string Username { get; set; } public string Email { get; set; } public string Password { get; set; } - + public UserStatus Status { get; set; } } } diff --git a/WebApi/Controllers/UserController.cs b/WebApi/Controllers/UserController.cs index 1de4d34..60e45ec 100644 --- a/WebApi/Controllers/UserController.cs +++ b/WebApi/Controllers/UserController.cs @@ -7,6 +7,9 @@ using System.Threading.Tasks; using System; using Microsoft.AspNetCore.Cors; +using Microsoft.AspNetCore.Authorization; +using DAL.Models; +using BLL.Services; namespace WebApi.Controllers { @@ -23,8 +26,8 @@ public UserController(IUserService service, ILogger logger) this.logger = logger; } - [HttpGet] + [Authorize(Roles = "Admin")] public async Task GetAll() { try @@ -35,7 +38,7 @@ public async Task GetAll() catch (Exception ex) { logger.LogError("Failed to get all users: " + ex.Message); - return StatusCode(500, "Internal Server Error"); // HTTP 500 Internal Server Error + return StatusCode(500, "Internal Server Error");// HTTP 500 Internal Server Error } } @@ -64,10 +67,13 @@ public async Task GetByEmailAndPassword(string email, string pass try { var user = await UserService.GetByEmailAndByPasswordAsync(email, password); - if (user == null) + if (user == null || user.Status != UserStatus.Approved) { return NotFound("User not found with provided email and password"); // HTTP 404 Not Found } + + user.Status= UserStatus.LoggedIn; + await UserService.UpdateAsync(user); return Ok(user); // HTTP 200 OK } catch (Exception ex) @@ -87,6 +93,7 @@ public async Task Add([FromBody] UserDTO newUser) return BadRequest("User cannot be null"); // HTTP 400 Bad Request } + newUser.Status = UserStatus.PendingApproval; await UserService.AddNewUserAsync(newUser); return CreatedAtAction(nameof(GetById), new { id = newUser.Id }, newUser); // HTTP 201 Created } @@ -101,6 +108,45 @@ public async Task Add([FromBody] UserDTO newUser) return StatusCode(500, "Internal Server Error"); // HTTP 500 Internal Server Error } } + [HttpGet("pending")] + [Authorize(Roles = "Admin")] + public async Task GetPendingUsers() + { + try + { + var pendingUsers = await UserService.GetPendingUsersAsync(); + return Ok(pendingUsers); + } + catch (Exception ex) + { + logger.LogError("Failed to get pending users: " + ex.Message); + return StatusCode(500, "Internal Server Error"); + } + } + + [HttpPut("approve/{id}")] + [Authorize(Roles = "Admin")] + public async Task ApproveUser(int id) + { + try + { + var user = await UserService.GetByIdAsync(id); + if (user == null) + { + return NotFound($"User with ID {id} not found"); + } + + user.Status = UserStatus.Approved; + await UserService.UpdateAsync(user); + return Ok($"User with ID {id} has been approved."); + } + catch (Exception ex) + { + logger.LogError($"Failed to approve user with ID {id}: " + ex.Message); + return StatusCode(500, "Internal Server Error"); + } + } + [HttpPut] public async Task Update([FromBody] UserDTO user) diff --git a/WebApi/Program.cs b/WebApi/Program.cs index ae176f0..3c1cb7a 100644 --- a/WebApi/Program.cs +++ b/WebApi/Program.cs @@ -16,6 +16,10 @@ string clientUrl = Env.GetString("CLIENT_URL"); +// Configure DbContext +builder.Services.AddDbContext(); + + // Configure DbContext builder.Services.AddDbContext();