2ff6e05ffc
Permit integration - part 1 * WIP permit integration * slim down build * Merge branch 'main' of bitbucket.org:aarete/query-orchestration into feature/permit-integration-1 * omit swagger from auth * clean build * comment * part 1 completed this is the initial permitio parts and tests without integration. Also testing.md since we have a new test target `task test:permitio` * feature flag for no jwt validation * permit integration * fix ci unit tests * ci fix * build fix * fix home handler * fix redirect * test fix * update docs for auth
93 lines
2.3 KiB
Go
93 lines
2.3 KiB
Go
//go:build permitio
|
|
|
|
package cognitoauth
|
|
|
|
// TestUser represents a test user with their assigned roles
|
|
type TestUser struct {
|
|
Key string
|
|
Name string
|
|
Email string
|
|
Roles []string
|
|
}
|
|
|
|
// TestUsers contains the users configured in the Permit.io test environment
|
|
var TestUsers = []TestUser{
|
|
{
|
|
Key: "e12bb510-30e1-7062-a69a-ca7f3f38d80e",
|
|
Name: "test user",
|
|
Email: "betot75403@isorax.com",
|
|
Roles: []string{"viewer", "editor"},
|
|
},
|
|
{
|
|
Key: "d12bf580-b071-7017-b1ff-a27961affc34",
|
|
Name: "john smith",
|
|
Email: "work.jay@gmail.com",
|
|
Roles: []string{"editor", "viewer"},
|
|
},
|
|
}
|
|
|
|
// TestResource represents a resource with its available actions and role permissions
|
|
type TestResource struct {
|
|
Name string
|
|
AvailableActions []string
|
|
RolePermissions map[string][]string // role -> actions
|
|
}
|
|
|
|
// TestResources contains the resources configured in the Permit.io test environment
|
|
var TestResources = map[string]TestResource{
|
|
"document": {
|
|
Name: "document",
|
|
AvailableActions: []string{"create", "delete", "read", "update"},
|
|
RolePermissions: map[string][]string{
|
|
"admin": {"create", "delete", "read", "update"},
|
|
"editor": {"create", "read", "update"}, // delete is unchecked
|
|
"viewer": {"read"}, // only read is checked
|
|
},
|
|
},
|
|
}
|
|
|
|
// GetUserByKey returns a test user by their key
|
|
func GetUserByKey(key string) (TestUser, bool) {
|
|
for _, user := range TestUsers {
|
|
if user.Key == key {
|
|
return user, true
|
|
}
|
|
}
|
|
return TestUser{}, false
|
|
}
|
|
|
|
// GetUserByName returns a test user by their name
|
|
func GetUserByName(name string) (TestUser, bool) {
|
|
for _, user := range TestUsers {
|
|
if user.Name == name {
|
|
return user, true
|
|
}
|
|
}
|
|
return TestUser{}, false
|
|
}
|
|
|
|
// HasPermission checks if a user should have permission for a resource/action based on test configuration
|
|
func HasPermission(userKey, resource, action string) bool {
|
|
user, exists := GetUserByKey(userKey)
|
|
if !exists {
|
|
return false
|
|
}
|
|
|
|
testResource, resourceExists := TestResources[resource]
|
|
if !resourceExists {
|
|
return false
|
|
}
|
|
|
|
// Check if any of the user's roles have permission for this action
|
|
for _, userRole := range user.Roles {
|
|
if allowedActions, roleExists := testResource.RolePermissions[userRole]; roleExists {
|
|
for _, allowedAction := range allowedActions {
|
|
if allowedAction == action {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
} |