( userId: string, entityType: string, entityId: string )
| 195 | * @returns Promise<PermissionType | null> - The highest permission the user has for the entity, or null if none |
| 196 | */ |
| 197 | export async function getUserEntityPermissions( |
| 198 | userId: string, |
| 199 | entityType: string, |
| 200 | entityId: string |
| 201 | ): Promise<PermissionType | null> { |
| 202 | if (entityType === 'workspace') { |
| 203 | const ws = await getWorkspaceWithOwner(entityId) |
| 204 | if (!ws) { |
| 205 | return null |
| 206 | } |
| 207 | return getEffectiveWorkspacePermission(userId, ws) |
| 208 | } |
| 209 | |
| 210 | const result = await db |
| 211 | .select({ permissionType: permissions.permissionType }) |
| 212 | .from(permissions) |
| 213 | .where( |
| 214 | and( |
| 215 | eq(permissions.userId, userId), |
| 216 | eq(permissions.entityType, entityType), |
| 217 | eq(permissions.entityId, entityId) |
| 218 | ) |
| 219 | ) |
| 220 | |
| 221 | if (result.length === 0) { |
| 222 | return null |
| 223 | } |
| 224 | |
| 225 | const highestPermission = result.reduce((highest, current) => { |
| 226 | return PERMISSION_RANK[current.permissionType] > PERMISSION_RANK[highest.permissionType] |
| 227 | ? current |
| 228 | : highest |
| 229 | }) |
| 230 | |
| 231 | return highestPermission.permissionType |
| 232 | } |
| 233 | |
| 234 | /** |
| 235 | * Retrieves a list of users with their associated permissions for a given workspace. |
no test coverage detected