-
Notifications
You must be signed in to change notification settings - Fork 422
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
split permissions server and ui code
- Loading branch information
Showing
7 changed files
with
95 additions
and
95 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
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,60 @@ | ||
import { json } from '@remix-run/node' | ||
import { requireUserId } from './auth.server.ts' | ||
import { prisma } from './db.server.ts' | ||
import { type PermissionString, parsePermissionString } from './permissions.ts' | ||
|
||
export async function requireUserWithPermission( | ||
request: Request, | ||
permission: PermissionString, | ||
) { | ||
const userId = await requireUserId(request) | ||
const permissionData = parsePermissionString(permission) | ||
const user = await prisma.user.findFirst({ | ||
select: { id: true }, | ||
where: { | ||
id: userId, | ||
roles: { | ||
some: { | ||
permissions: { | ||
some: { | ||
...permissionData, | ||
access: permissionData.access | ||
? { in: permissionData.access } | ||
: undefined, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
if (!user) { | ||
throw json( | ||
{ | ||
error: 'Unauthorized', | ||
requiredPermission: permissionData, | ||
message: `Unauthorized: required permissions: ${permission}`, | ||
}, | ||
{ status: 403 }, | ||
) | ||
} | ||
return user.id | ||
} | ||
|
||
export async function requireUserWithRole(request: Request, name: string) { | ||
const userId = await requireUserId(request) | ||
const user = await prisma.user.findFirst({ | ||
select: { id: true }, | ||
where: { id: userId, roles: { some: { name } } }, | ||
}) | ||
if (!user) { | ||
throw json( | ||
{ | ||
error: 'Unauthorized', | ||
requiredRole: name, | ||
message: `Unauthorized: required role: ${name}`, | ||
}, | ||
{ status: 403 }, | ||
) | ||
} | ||
return user.id | ||
} |
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