Skip to content

Commit

Permalink
feat: User roles
Browse files Browse the repository at this point in the history
  • Loading branch information
NexusrexDev committed May 8, 2024
1 parent 7350a29 commit c9b1af7
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 20 deletions.
13 changes: 12 additions & 1 deletion Controllers/ProjectController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

namespace ia_back.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class ProjectController : Controller
Expand All @@ -28,6 +27,7 @@ public ProjectController(IMapper mapper, IDataRepository<Project> projectReposit
}


[Authorize]
[HttpGet]
public async Task<IActionResult> GetAllProjects()
{
Expand All @@ -43,6 +43,7 @@ public async Task<IActionResult> GetAllProjects()
}


[Authorize]
[HttpGet("user/{id}")]
public async Task<IActionResult> GetProjectsByUser(int id)
{
Expand All @@ -61,6 +62,7 @@ public async Task<IActionResult> GetProjectsByUser(int id)
}


[Authorize(Roles = "TeamLeader")]
[HttpPost]
public async Task<IActionResult> CreateProject(ProjectEntryDTO projectInfo)
{
Expand Down Expand Up @@ -103,6 +105,7 @@ public async Task<IActionResult> CreateProject(ProjectEntryDTO projectInfo)
}


[Authorize(Roles = "TeamLeader")]
[HttpDelete("id")]
public async Task<IActionResult> DeleteProject(int id)
{
Expand All @@ -119,6 +122,7 @@ public async Task<IActionResult> DeleteProject(int id)
}


[Authorize(Roles = "TeamLeader")]
[HttpPatch("{id}/{newName}")]
public async Task<IActionResult> UpdateProjectName(int id, string newName)
{
Expand All @@ -136,6 +140,7 @@ public async Task<IActionResult> UpdateProjectName(int id, string newName)
}


[Authorize]
[HttpGet("{id}")]
public async Task<IActionResult> GetProject(int id)
{
Expand All @@ -156,6 +161,7 @@ public async Task<IActionResult> GetProject(int id)
}


[Authorize(Roles = "TeamLeader")]
[HttpPost("{id}/developer/{developerUserName}")]
public async Task<IActionResult> AssignDeveloperToProject(int id, string developerUserName)
{
Expand All @@ -173,6 +179,10 @@ public async Task<IActionResult> AssignDeveloperToProject(int id, string develop
{
return NotFound("Developer doesn't exist");
}
if (developer.Role != Role.Developer)
{
return BadRequest("Developer is not a developer");
}
if (project.RequestedDevelopers.Contains(developer) || project.AssignedDevelopers.Contains(developer))
{
return BadRequest("Developer is already in the project");
Expand All @@ -192,6 +202,7 @@ public async Task<IActionResult> AssignDeveloperToProject(int id, string develop
}


[Authorize(Roles = "TeamLeader")]
[HttpDelete("{id}/developer/{developerUserName}")]
public async Task<IActionResult> RemoveDeveloperFromProject(int id, string developerUserName)
{
Expand Down
42 changes: 26 additions & 16 deletions Controllers/ProjectTaskController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace ia_back.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class ProjectTaskController : Controller
Expand All @@ -32,6 +31,7 @@ public ProjectTaskController(IMapper mapper,
}


[Authorize]
[HttpGet("project/{id}")]
public async Task<IActionResult> GetProjectTasksByProject(int id)
{
Expand All @@ -50,6 +50,7 @@ public async Task<IActionResult> GetProjectTasksByProject(int id)
}


[Authorize(Roles = "TeamLeader")]
[HttpPost]
public async Task<IActionResult> CreateProjectTask(TaskEntryDTO projectTaskInfo)
{
Expand Down Expand Up @@ -92,6 +93,7 @@ public async Task<IActionResult> CreateProjectTask(TaskEntryDTO projectTaskInfo)
}


[Authorize]
[HttpGet("{id}")]
public async Task<IActionResult> GetProjectTask(int id)
{
Expand All @@ -107,6 +109,7 @@ public async Task<IActionResult> GetProjectTask(int id)
}


[Authorize(Roles = "Developer")]
[HttpPatch("{id}/status")]
public async Task<IActionResult> UpdateTaskStatus(int id, ProjectStatus newStatus)
{
Expand All @@ -132,6 +135,8 @@ public async Task<IActionResult> UpdateTaskStatus(int id, ProjectStatus newStatu
return Ok();
}


[Authorize(Roles = "TeamLeader")]
[HttpPatch("{id}")]
public async Task<IActionResult> UpdateTask(int id, TaskEntryDTO projectTaskInfo)
{
Expand Down Expand Up @@ -177,6 +182,24 @@ public async Task<IActionResult> UpdateTask(int id, TaskEntryDTO projectTaskInfo
}


[Authorize(Roles = "TeamLeader")]
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProjectTask(int id)
{
var projectTask = await _projectTaskRepository.GetByIdAsync(id);
if (projectTask == null)
{
return NotFound();
}

await _projectTaskRepository.DeleteAsync(projectTask);
await _projectTaskRepository.Save();

return Ok("Task deleted successfully");
}


[Authorize(Roles = "Developer")]
[HttpPost("{id}/UploadAttachment")]
public async Task<IActionResult> UploadAttachment(int id, [FromForm] IFormFile file)
{
Expand Down Expand Up @@ -210,22 +233,8 @@ public async Task<IActionResult> UploadAttachment(int id, [FromForm] IFormFile f

}

[HttpDelete("{id}")]
public async Task<IActionResult> DeleteProjectTask(int id)
{
var projectTask = await _projectTaskRepository.GetByIdAsync(id);
if (projectTask == null)
{
return NotFound();
}

await _projectTaskRepository.DeleteAsync(projectTask);
await _projectTaskRepository.Save();

return Ok("Task deleted successfully");
}


[Authorize]
[HttpGet("{id}/AttachmentFile")]
public async Task<IActionResult> GetAttachmentFile(int id){
var projectTask = await _projectTaskRepository.GetByIdAsync(id);
Expand Down Expand Up @@ -257,6 +266,7 @@ public async Task<IActionResult> GetAttachmentFile(int id){
}


[Authorize]
[HttpGet("{id}/AttachmentName")]
public async Task<IActionResult> GetAttachmentName(int id){
var projectTask = await _projectTaskRepository.GetByIdAsync(id);
Expand Down
8 changes: 5 additions & 3 deletions Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ private string CreateToken(User user){
{
new Claim(ClaimTypes.Name, user.Username),
new Claim(ClaimTypes.NameIdentifier, user.Id.ToString()),
new Claim(ClaimTypes.Role, user.Role.ToString()),
new Claim(JwtRegisteredClaimNames.Aud, _configuration["Jwt:Audience"]),
new Claim(JwtRegisteredClaimNames.Iss, _configuration["Jwt:Issuer"])
};
Expand Down Expand Up @@ -114,7 +115,8 @@ public async Task<IActionResult> Register(RegisterDTO register)
Email = register.Email,
Username = register.Username,
PasswordHash = passwordHash,
PasswordSalt = passwordSalt
PasswordSalt = passwordSalt,
Role = register.Role
};

await _userRepository.AddAsync(registeringUser);
Expand All @@ -133,7 +135,7 @@ private void CreatePasswordHash(string password, out byte[] passwordHash, out by
}


[Authorize]
[Authorize(Roles = "Developer")]
[HttpPost("acceptRequest")]
public async Task<IActionResult> AcceptRequest(RequestDTO request)
{
Expand All @@ -158,7 +160,7 @@ public async Task<IActionResult> AcceptRequest(RequestDTO request)
}


[Authorize]
[Authorize(Roles = "Developer")]
[HttpPost("rejectRequest")]
public async Task<IActionResult> RejectRequest(RequestDTO request)
{
Expand Down
3 changes: 3 additions & 0 deletions DTOs/Login/RegisterDTO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using ia_back.Models;

namespace ia_back.DTOs.Login
{
Expand All @@ -13,5 +14,7 @@ public class RegisterDTO
public string Username { get; set; }
[Required]
public string Password { get; set; }
[Required]
public Role Role { get; set; }
}
}
Loading

0 comments on commit c9b1af7

Please sign in to comment.