-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
126ef2f
commit dda5d22
Showing
4 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
Exam.StockManagement.API/Attributes/IdentityFilterAttributes.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using Exam.StockManagement.Domain.Entities.Enums; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.Mvc.Filters; | ||
using System.Security.Claims; | ||
using System.Text.Json; | ||
|
||
namespace Exam.StockManagement.API.Attributes | ||
{ | ||
[AttributeUsage(AttributeTargets.Method)] // Permissionlarni nmani ustida ishlatatyotganimizni yozish kerak (bizda controllerni ichida bir method ustida ishlatmoqdamiz) | ||
public class IdentityFilterAttribute : Attribute, IAuthorizationFilter | ||
{ | ||
private readonly int _permissionId; | ||
public IdentityFilterAttribute(Persmissions permissions) | ||
{ | ||
_permissionId = (int)permissions; | ||
} | ||
public void OnAuthorization(AuthorizationFilterContext context) // IAuthorizationFilter interfacesini implementatsiyasi | ||
{ | ||
//User authorizatsiya qilgan tokenidan rolini tekshirib va joriy permissionga ruhsati bor yoqlikga tekshiradi | ||
// Ruhsati yoq bolsa Forbidden 403 qaytaradi. Aks holda hech nma qilmaydi | ||
ClaimsIdentity identity = context.HttpContext.User.Identity as ClaimsIdentity; | ||
string permmissionsJson = identity.FindFirst("permissions")!.Value; | ||
bool result = JsonSerializer.Deserialize<IEnumerable<int>>(permmissionsJson)!.Any(x => x == _permissionId); | ||
if (!result) | ||
{ | ||
context.Result = new ForbidResult(); | ||
return; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
Exam.StockManagement.Domain/Entities/Enums/Persmissions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
namespace Exam.StockManagement.Domain.Entities.Enums | ||
{ | ||
public enum Persmissions | ||
{ | ||
CreateProduct = 100, | ||
GetAllUser, | ||
UpdateProduct, | ||
DeleteProduct, | ||
CreateCategory, | ||
UpdateCategory, | ||
DeleteCategory, | ||
|
||
GetAllCategory = 200, | ||
GetSum, | ||
GetQuantity, | ||
GetByCategorySum, | ||
GetByCategoryProduct, | ||
GetByCategoryQuantity, | ||
GetAllProduct, | ||
} | ||
} |