-
Notifications
You must be signed in to change notification settings - Fork 1
Authorization system #104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authorization system #104
Changes from all commits
3f0acc7
b89333d
f5e5690
ec1fcb8
d5f0edf
af319e4
e1ed86d
15ce9d6
a0681a6
b9ed184
8eac81d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -8,6 +8,6 @@ public interface IUploadService | |||||
| public Task<AssetDto> UploadImageAsync(IFormFile file, string userId); | ||||||
| public Task<AssetDto> FindAssetById(string id); | ||||||
|
|
||||||
| public Task<AssetDto> DeleteAssetById(string id, string userId); | ||||||
| Task DeleteAssetById(string id); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainVerify that this breaking change has been properly handled across the codebase. The method signature change removes the
Please run the following script to verify all implementations and usages have been updated: 🏁 Script executed: #!/bin/bash
# Description: Verify all implementations and usages of DeleteAssetById have been updated
echo "=== Checking for implementations of IUploadService ==="
ast-grep --pattern 'class $_ : $$$IUploadService$$$'
echo -e "\n=== Checking for DeleteAssetById method implementations ==="
ast-grep --pattern 'DeleteAssetById($_) {
$$$
}'
echo -e "\n=== Checking for calls to DeleteAssetById ==="
rg -A 3 'DeleteAssetById\(' --type cs
echo -e "\n=== Checking if any code still expects AssetDto return type ==="
rg -B 2 -A 2 'DeleteAssetById.*AssetDto' --type csLength of output: 1934 Controller call must be updated to match new signature The interface and service have been updated to • File: Dentizone.Presentaion/Controllers/UploadController.cs Recommended diff: - await uploadService.DeleteAssetById(id, userId);
+ await uploadService.DeleteAssetById(id);📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| // Dentizone.Application/Services/BaseService.cs | ||
|
|
||
| using System.Security.Claims; | ||
| using Dentizone.Domain.Enums; | ||
| using Dentizone.Domain.Exceptions; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Dentizone.Application.Services; | ||
|
|
||
| public abstract class BaseService | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| { | ||
| private readonly IHttpContextAccessor _httpContextAccessor; | ||
|
|
||
| protected BaseService(IHttpContextAccessor httpContextAccessor) | ||
| { | ||
| _httpContextAccessor = httpContextAccessor; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if the current user has the ADMIN role. | ||
| /// </summary> | ||
| protected bool IsAdmin() | ||
| { | ||
| var userRole = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.Role); | ||
| return Enum.TryParse<UserRoles>(userRole, out var role) && role == UserRoles.ADMIN; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Ensures the current user is either an Admin or the owner of the specified resource. | ||
| /// Throws UnauthorizedAccessException if the check fails. | ||
| /// </summary> | ||
| /// <param name="resourceId">The unique identifier of the resource to check.</param> | ||
| protected async Task AuthorizeAdminOrOwnerAsync(string resourceId) | ||
| { | ||
| // Admins are always authorized. | ||
| if (IsAdmin()) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| var currentUserId = _httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier); | ||
| if (string.IsNullOrEmpty(currentUserId)) | ||
| { | ||
| throw new UnauthorizedAccessException("Cannot verify user. No user is authenticated."); | ||
| } | ||
|
|
||
| // Get the owner ID from the concrete service implementation. | ||
| var ownerId = await GetOwnerIdAsync(resourceId); | ||
|
|
||
| if (string.IsNullOrEmpty(ownerId)) | ||
| { | ||
| throw new NotFoundException("Could not determine the owner of the resource."); | ||
| } | ||
|
|
||
| // If the user is not the owner, they are not authorized. | ||
| if (ownerId != currentUserId) | ||
| { | ||
| throw new UnauthorizedAccessException( | ||
| "You do not have permission to perform this action on this resource."); | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// When implemented in a derived class, this method retrieves the owner's ID for a given resource. | ||
| /// </summary> | ||
| /// <param name="resourceId">The ID of the resource.</param> | ||
| /// <returns>A Task that represents the asynchronous operation, containing the owner's user ID.</returns> | ||
| protected abstract Task<string> GetOwnerIdAsync(string resourceId); | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add validation attributes to the Email property.
The Email property lacks validation attributes that would ensure data integrity and provide clear validation feedback.
📝 Committable suggestion
🤖 Prompt for AI Agents