Skip to content

Commit

Permalink
Merge pull request #26 from Star-Academy/role-authorization
Browse files Browse the repository at this point in the history
Role authorization phase updated and new api added
  • Loading branch information
Ftm-Sayadzadeh authored Aug 28, 2024
2 parents c9d9105 + 6011f36 commit 633ead6
Show file tree
Hide file tree
Showing 42 changed files with 350 additions and 267 deletions.
44 changes: 29 additions & 15 deletions mohaymen-codestar-Team02/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using mohaymen_codestar_Team02.Dto.Role;
using mohaymen_codestar_Team02.Dto.User;
using mohaymen_codestar_Team02.Dto.UserDtos;
using mohaymen_codestar_Team02.Dto.UserRole;
Expand All @@ -22,23 +21,23 @@ public AdminController(IAdminService adminService)
}

[HttpGet("users")]
public async Task<IActionResult> GetAllUsers()
public async Task<IActionResult> GetAllUsers([FromQuery] int pageNumber)
{
ServiceResponse<List<GetUserDto>?> response =
await _adminService.GetAllUsers();
var response =
await _adminService.GetUsersPaginated(pageNumber);
return StatusCode((int)response.Type, response);
}

[HttpGet("users/{username}")]
public async Task<IActionResult> GetSingleUser(string? username)
{
ServiceResponse<GetUserDto?> response =
var response =
await _adminService.GetUserByUsername(username);
return StatusCode((int)response.Type, response);
}

[HttpPost("users")]
public async Task<IActionResult> Register([FromBody] RegisterUserDto request)
public async Task<IActionResult> CreateUser([FromQuery] CreateUserDto request)
{
var user = new User
{
Expand All @@ -48,38 +47,53 @@ public async Task<IActionResult> Register([FromBody] RegisterUserDto request)
Email = request.Email
};

ServiceResponse<GetUserDto?> response =
await _adminService.Register(user, request.Password);
var response =
await _adminService.CreateUser(user, request.Password, request.Roles);

return StatusCode((int)response.Type, response);
}

[HttpDelete("users/{username}")]
public async Task<IActionResult> Delete(string username)
public async Task<IActionResult> DeleteUser(string username)
{
var user = new User
{
Username = username
};

ServiceResponse<GetUserDto?> response =
var response =
await _adminService.DeleteUser(user);

return StatusCode((int)response.Type, response);
}

[HttpPut("users/update/{username}")]
public async Task<IActionResult> UpdateUser([FromQuery] UpdateUserDto request, string username)
{
var updateUser = new User()
{
Username = username,
FirstName = request.FirstName,
LastName = request.LastName,
Email = request.Email
};

ServiceResponse<GetUserDto?> response = await _adminService.UpdateUser(updateUser);
return StatusCode((int)response.Type, response);
}

[HttpGet("roles")]
public async Task<IActionResult> GetAllRoles()
{
ServiceResponse<List<GetRoleDto>> response =
var response =
await _adminService.GetAllRoles();
return StatusCode((int)response.Type, response);
}

[HttpPut("users/{username}/roles")]
public async Task<IActionResult> AddRole([FromBody] AddUserRoleDto request, string username)
public async Task<IActionResult> AddRole([FromQuery] AddUserRoleDto request, string username)
{
ServiceResponse<GetUserDto?> response =
var response =
await _adminService.AddRole(
new User { Username = username },
new Role() { RoleType = request.RoleType }
Expand All @@ -89,9 +103,9 @@ await _adminService.AddRole(
}

[HttpDelete("users/{username}/roles")]
public async Task<IActionResult> DeleteRole([FromBody] DeleteUserRoleDto request, string username)
public async Task<IActionResult> DeleteRole([FromQuery] DeleteUserRoleDto request, string username)
{
ServiceResponse<GetUserDto?> response =
var response =
await _adminService.DeleteRole(
new User { Username = username },
new Role() { RoleType = request.RoleType }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using mohaymen_codestar_Team02.Dto.Permission;
using mohaymen_codestar_Team02.Dto.User;
using mohaymen_codestar_Team02.Dto.UserDtos;
using mohaymen_codestar_Team02.Models;
using mohaymen_codestar_Team02.Services.Authenticatoin;

namespace mohaymen_codestar_Team02.Controllers;
Expand All @@ -19,7 +16,7 @@ public AuthenticationController(IAuthenticationService authenticationService)
}

[HttpPost("login")]
public async Task<IActionResult> Login(LoginUserDto request)
public async Task<IActionResult> Login([FromQuery] LoginUserDto request)
{
var response = await _authenticationService.Login(request.Username, request.Password);
return StatusCode((int)response.Type, response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace mohaymen_codestar_Team02.Controllers;

[ApiController]
[Authorize]
[Authorize(Roles = $"{nameof(RoleType.DataAdmin)},{nameof(RoleType.SystemAdmin)}")]
public class DataAdminController : ControllerBase
{
private readonly IDataAdminService _dataAdminService;
Expand Down
4 changes: 2 additions & 2 deletions mohaymen-codestar-Team02/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public ProfileController(IProfileService profileService)
}

[HttpPatch("password")]
public async Task<IActionResult> ChangePassword([FromBody] ChangePasswordUserDto request)
public async Task<IActionResult> ChangePassword([FromQuery] ChangePasswordUserDto request)
{
ServiceResponse<object> response =
await _profileService.ChangePassword(request.PreviousPassword, request.NewPassword);
return StatusCode((int)response.Type, response);
}

[HttpPut("update")]
public async Task<IActionResult> UpdateUser([FromBody] UpdateUserDto request)
public async Task<IActionResult> UpdateUser([FromQuery] UpdateUserDto request)
{
ServiceResponse<GetUserDto?> response = await _profileService.UpdateUser(request);
return StatusCode((int)response.Type, response);
Expand Down
63 changes: 40 additions & 23 deletions mohaymen-codestar-Team02/Data/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 15 additions & 9 deletions mohaymen-codestar-Team02/Data/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<data name="UnauthorizedMessage">
<value>Unauthorized</value>
</data>
<data name="InvalidInpute">
<data name="InvalidInputeMessage">
<value>InvalidInput</value>
</data>
<data name="UserNotFoundMessage">
Expand All @@ -22,7 +22,7 @@
<data name="UserAlreadyExistsMessage">
<value>User Already Exists</value>
</data>
<data name="DontHaveThisRole">
<data name="DontHaveThisRoleMessage">
<value>This User Doesn't Have This Role</value>
</data>

Expand Down Expand Up @@ -58,7 +58,7 @@
<data name="RoleAddedSuccessfulyMassage" xml:space="preserve">
<value>Role Added Successfuly</value>
</data>
<data name="RoleAlreadyAssigned" xml:space="preserve">
<data name="RoleAlreadyAssignedMessage" xml:space="preserve">
<value>This Role Is Already Assigned To The User</value>
</data>
<data name="UserCreatedSuccessfullyMessage" xml:space="preserve">
Expand All @@ -67,16 +67,16 @@
<data name="PasswordChangedSuccessfulyMessage" xml:space="preserve">
<value>Password Changed Successfuly</value>
</data>
<data name="LogoutSuccessfuly" xml:space="preserve">
<data name="LogoutSuccessfulyMessage" xml:space="preserve">
<value>Logout Successfuly</value>
</data>
<data name="ProfileInfoUpdateSuccessfulyMessage" xml:space="preserve">
<value>Your Profile Info Changed Successfuly</value>
</data>
<data name="UserDeletionSuccessful" xml:space="preserve">
<data name="UserDeletionSuccessfulMessage" xml:space="preserve">
<value>User Deleted Successfuly</value>
</data>
<data name="CanNotDeleteYourself" xml:space="preserve">
<data name="CanNotDeleteYourselfMessage" xml:space="preserve">
<value>Can Not Delete Yourself</value>
</data>
<data name="UserRetrievedMassage" xml:space="preserve">
Expand All @@ -88,16 +88,22 @@
<data name="RolesRetrievedMassage" xml:space="preserve">
<value>Roles Retrieved Successfully</value>
</data>
<data name="GetPermissionsSuccessfuly" xml:space="preserve">
<data name="GetPermissionsSuccessfulyMessage" xml:space="preserve">
<value>Permissions Get Successfuly</value>
</data>
<data name="YourPasswordIsNotValidated" xml:space="preserve">
<data name="InvalidPasswordMessage" xml:space="preserve">
<value>Your Password Is Not Validated</value>
</data>
<data name="AuthorizedMessage" xml:space="preserve">
<value>Authorized Message</value>
</data>
<data name="GraphFetchedSuccessfully" xml:space="preserve">
<data name="GraphFetchedSuccessfullyMessage" xml:space="preserve">
<value>GraphFetchedSuccessfully</value>
</data>
<data name="SomeRolesAreInvalidMessage" xml:space="preserve">
<value>Some Roles Are Invalid</value>
</data>
<data name="UserUpdateSuccessfulyMessage" xml:space="preserve">
<value>User Update Successfuly</value>
</data>
</root>
2 changes: 1 addition & 1 deletion mohaymen-codestar-Team02/Dto/GraphDTO/DetailDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ namespace mohaymen_codestar_Team02.Dto.GraphDTO;

public class DetailDto
{
public Dictionary<string, string>? AttributeValue { get; set; } = new();
public Dictionary<string, string>? AttributeValue { get; init; } = new();
}
5 changes: 0 additions & 5 deletions mohaymen-codestar-Team02/Dto/InfoDto/InfoDto.cs

This file was deleted.

Loading

0 comments on commit 633ead6

Please sign in to comment.