from gl_iam.core.roles import StandardRoleif user.has_standard_role(StandardRole.ORG_ADMIN):print("User has admin access")# Also true for PLATFORM_ADMIN (hierarchy)
Role Hierarchy
Role
Implies
PLATFORM_ADMIN
ORG_ADMIN, ORG_MEMBER
ORG_ADMIN
ORG_MEMBER
ORG_MEMBER
(none)
Higher roles automatically include lower role access.
Step-by-Step
1
Import Standard Roles
2
Check Role with Hierarchy
3
Check Exact Role
4
Get User's Roles
5
Expected Output
You can check standard roles!
Complete Example
Create standard_roles.py:
Run it:
Expected output:
Provider Mapping
GL IAM maps provider-specific roles to standard roles:
Provider
Admin
Member
PostgreSQL
org_admin
org_member
Stack Auth
$admin
$member
Keycloak
admin
member
Common Pitfalls
Pitfall
Solution
Forgetting hierarchy
Default respects hierarchy - admins pass member checks
Hard-coding role strings
Use StandardRole enum for provider independence
Wrong status code
Role check is authorization (403), not authentication (401)
# Returns True if user is ORG_ADMIN or PLATFORM_ADMIN
if user.has_standard_role(StandardRole.ORG_ADMIN):
print("User has admin access")
# Only returns True if user is exactly ORG_MEMBER
if user.has_standard_role(StandardRole.ORG_MEMBER, respect_hierarchy=False):
print("User is exactly ORG_MEMBER")
roles = user.get_standard_roles()
print(f"User roles: {[r.value for r in roles]}")
User has admin access
User roles: ['org_admin']
"""GL IAM Standard Roles Example."""
import asyncio
from gl_iam import IAMGateway
from gl_iam.core.roles import StandardRole
from gl_iam.core.types import PasswordCredentials
from gl_iam.providers.postgresql import PostgreSQLProvider, PostgreSQLConfig
DATABASE_URL = "postgresql+asyncpg://postgres:postgres@localhost:5432/gliam"
async def main():
# Setup
config = PostgreSQLConfig(database_url=DATABASE_URL)
provider = PostgreSQLProvider(config)
gateway = IAMGateway.from_fullstack_provider(provider)
# Login to get user
result = await gateway.authenticate(
credentials=PasswordCredentials(email="alice@example.com", password="SecurePass123"),
organization_id="default",
)
user, token = result.unwrap()
print(f"User: {user.email}")
print(f"Raw roles: {user.roles}")
# Check with hierarchy (default)
print("\n--- With Hierarchy ---")
print(f"Is PLATFORM_ADMIN: {user.has_standard_role(StandardRole.PLATFORM_ADMIN)}")
print(f"Is ORG_ADMIN: {user.has_standard_role(StandardRole.ORG_ADMIN)}")
print(f"Is ORG_MEMBER: {user.has_standard_role(StandardRole.ORG_MEMBER)}")
# Check exact role
print("\n--- Exact Check ---")
print(f"Is exactly ORG_MEMBER: {user.has_standard_role(StandardRole.ORG_MEMBER, respect_hierarchy=False)}")
await provider.close()
if __name__ == "__main__":
asyncio.run(main())
uv run standard_roles.py
User: alice@example.com
Raw roles: ['org_member']
--- With Hierarchy ---
Is PLATFORM_ADMIN: False
Is ORG_ADMIN: False
Is ORG_MEMBER: True
--- Exact Check ---
Is exactly ORG_MEMBER: True