This repository has been archived by the owner on Dec 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from swibly/chore/permission-usability
chore: Permission Usability
- Loading branch information
Showing
7 changed files
with
121 additions
and
27 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
admin: admin | ||
manage_user: manage_user | ||
manage_permissions: manage_permissions | ||
manage_projects: manage_projects | ||
manage_store: manage_store |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/devkcud/arkhon-foundation/arkhon-api/internal/model/dto" | ||
"github.com/devkcud/arkhon-foundation/arkhon-api/internal/service" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// GetPermissionsMiddleware must be after OptionalAuthMiddleware or AuthMiddleware | ||
func GetPermissionsMiddleware(ctx *gin.Context) { | ||
var issuer *dto.ProfileSearch = nil | ||
p, exists := ctx.Get("auth_user") | ||
|
||
if !exists { | ||
ctx.Set("permissions", []string{}) // Set to an empty string so it can be queried anyway | ||
ctx.Next() | ||
return | ||
} | ||
|
||
issuer = p.(*dto.ProfileSearch) | ||
|
||
permissions, err := service.Permission.GetPermissions(issuer.ID) | ||
if err != nil { | ||
ctx.Set("permissions", []string{}) | ||
ctx.Next() | ||
return | ||
} | ||
|
||
var list []string | ||
|
||
for _, permission := range permissions { | ||
list = append(list, permission.Name) | ||
} | ||
|
||
ctx.Set("permissions", list) | ||
} |
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,32 @@ | ||
package utils | ||
|
||
import ( | ||
"slices" | ||
|
||
"github.com/devkcud/arkhon-foundation/arkhon-api/config" | ||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// There is no need to pass anything to check for admin roles | ||
func HasPermissions(list []string, lookup ...string) bool { | ||
if slices.Contains(list, config.Permissions.Admin) { | ||
return true | ||
} | ||
|
||
if len(lookup) == 0 { | ||
return false | ||
} | ||
|
||
for _, perm := range lookup { | ||
if !slices.Contains(list, perm) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
// WARN: must have the GetPermissionsMiddleware in the route before using | ||
func HasPermissionsByContext(ctx *gin.Context, lookup ...string) bool { | ||
return HasPermissions(ctx.Keys["permissions"].([]string), lookup...) | ||
} |