fix: redis cache invalidation in permission service + permission check in entity_permissions route + upsert cache invalidation + remove TopBar quick-create
This commit is contained in:
@@ -109,6 +109,7 @@ async def create_permission(
|
||||
permission_level: str,
|
||||
expires_at: datetime | None = None,
|
||||
created_by: uuid.UUID | None = None,
|
||||
redis: Any = None,
|
||||
) -> dict:
|
||||
"""Create or update a permission entry (upsert)."""
|
||||
entity_uuid = uuid.UUID(entity_id)
|
||||
@@ -149,6 +150,17 @@ async def create_permission(
|
||||
entity_type=entity_type,
|
||||
entity_id=entity_uuid,
|
||||
)
|
||||
# Invalidate cache for upsert
|
||||
if principal_type == "user":
|
||||
await _invalidate_user_cache(redis, tenant_id, principal_uuid, entity_type)
|
||||
elif principal_type == "group":
|
||||
members_q2 = await db.execute(
|
||||
select(UserGroup.user_id)
|
||||
.where(UserGroup.group_id == principal_uuid)
|
||||
.where(UserGroup.tenant_id == tenant_id)
|
||||
)
|
||||
for (uid2,) in members_q2:
|
||||
await _invalidate_user_cache(redis, tenant_id, uid2, entity_type)
|
||||
return _serialize_permission(existing, names.get(existing.principal_id))
|
||||
|
||||
perm = EntityPermission(
|
||||
@@ -167,7 +179,7 @@ async def create_permission(
|
||||
|
||||
# Invalidate cache for this principal
|
||||
if principal_type == "user":
|
||||
await _invalidate_user_cache(None, tenant_id, principal_uuid, entity_type)
|
||||
await _invalidate_user_cache(redis, tenant_id, principal_uuid, entity_type)
|
||||
elif principal_type == "group":
|
||||
# Invalidate for all group members
|
||||
members_q = await db.execute(
|
||||
@@ -176,7 +188,7 @@ async def create_permission(
|
||||
.where(UserGroup.tenant_id == tenant_id)
|
||||
)
|
||||
for (uid,) in members_q:
|
||||
await _invalidate_user_cache(None, tenant_id, uid, entity_type)
|
||||
await _invalidate_user_cache(redis, tenant_id, uid, entity_type)
|
||||
|
||||
# Audit log for new permission
|
||||
await log_audit(
|
||||
@@ -207,6 +219,7 @@ async def update_permission(
|
||||
permission_id: str,
|
||||
permission_level: str,
|
||||
expires_at: datetime | None = None,
|
||||
redis: Any = None,
|
||||
) -> dict:
|
||||
"""Update an existing permission entry."""
|
||||
perm_uuid = uuid.UUID(permission_id)
|
||||
@@ -232,7 +245,7 @@ async def update_permission(
|
||||
|
||||
# Invalidate cache
|
||||
if old_principal_type == "user":
|
||||
await _invalidate_user_cache(None, tenant_id, old_principal_id, old_entity_type)
|
||||
await _invalidate_user_cache(redis, tenant_id, old_principal_id, old_entity_type)
|
||||
elif old_principal_type == "group":
|
||||
members_q = await db.execute(
|
||||
select(UserGroup.user_id)
|
||||
@@ -240,7 +253,7 @@ async def update_permission(
|
||||
.where(UserGroup.tenant_id == tenant_id)
|
||||
)
|
||||
for (uid,) in members_q:
|
||||
await _invalidate_user_cache(None, tenant_id, uid, old_entity_type)
|
||||
await _invalidate_user_cache(redis, tenant_id, uid, old_entity_type)
|
||||
|
||||
# Audit log for permission update
|
||||
await log_audit(
|
||||
@@ -256,7 +269,7 @@ async def update_permission(
|
||||
|
||||
|
||||
async def delete_permission(
|
||||
db: AsyncSession, tenant_id: uuid.UUID, permission_id: str
|
||||
db: AsyncSession, tenant_id: uuid.UUID, permission_id: str, redis: Any = None
|
||||
) -> None:
|
||||
"""Delete a permission entry."""
|
||||
perm_uuid = uuid.UUID(permission_id)
|
||||
@@ -298,7 +311,7 @@ async def delete_permission(
|
||||
|
||||
# Invalidate cache
|
||||
if old_principal_type == "user":
|
||||
await _invalidate_user_cache(None, tenant_id, old_principal_id, old_entity_type)
|
||||
await _invalidate_user_cache(redis, tenant_id, old_principal_id, old_entity_type)
|
||||
elif old_principal_type == "group":
|
||||
members_q = await db.execute(
|
||||
select(UserGroup.user_id)
|
||||
@@ -306,7 +319,7 @@ async def delete_permission(
|
||||
.where(UserGroup.tenant_id == tenant_id)
|
||||
)
|
||||
for (uid,) in members_q:
|
||||
await _invalidate_user_cache(None, tenant_id, uid, old_entity_type)
|
||||
await _invalidate_user_cache(redis, tenant_id, uid, old_entity_type)
|
||||
|
||||
|
||||
async def get_effective_access(
|
||||
|
||||
Reference in New Issue
Block a user