Skip to content

Commit

Permalink
Move canUser to the appropriate location
Browse files Browse the repository at this point in the history
  • Loading branch information
augustuswm committed Mar 11, 2024
1 parent f214564 commit 77d9dfb
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
12 changes: 11 additions & 1 deletion app/services/rfd.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { Octokit } from 'octokit'
import { generateAuthors, type Author } from '~/components/rfd/RfdPreview'
import { isTruthy } from '~/utils/isTruthy'
import { parseRfdNum } from '~/utils/parseRfdNum'
import { canUser } from '~/utils/permission'
import { can, Permission } from '~/utils/permission'

Check warning on line 18 in app/services/rfd.server.ts

View workflow job for this annotation

GitHub Actions / ci

Import "Permission" is only used as types
import type { GroupResponse, RfdListResponseItem, RfdResponse } from '~/utils/rfdApi'

import type { Group, User } from './authn.server'
Expand Down Expand Up @@ -55,6 +55,16 @@ export type RfdListItem = {
const localRepo = process.env.LOCAL_RFD_REPO
export const isLocalMode = process.env.NODE_ENV === 'development' && localRepo

async function canUser(user: User, permission: Permission): Promise<boolean> {
const groups = (await fetchGroups(user)).filter((group) =>
user.groups.includes(group.name),
)
const allPermissions = user.permissions.concat(
groups.flatMap((group) => group.permissions),
)
return can(allPermissions, permission)
}

function findLineStartingWith(content: string, prefixRegex: string): string | undefined {
// (^|\n) is required to match either the first line (beginning of file) or
// subsequent lines
Expand Down
13 changes: 0 additions & 13 deletions app/utils/permission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,10 @@
* Copyright Oxide Computer Company
*/

import type { User } from '~/services/authn.server'
import { fetchGroups } from '~/services/rfd.server'

import type { RfdApiPermission } from './rfdApi'

export type Permission = { k: 'ReadDiscussions' } | { k: 'ReadRfd'; v: number }

export async function canUser(user: User, permission: Permission): Promise<boolean> {
const groups = (await fetchGroups(user)).filter((group) =>
user.groups.includes(group.name),
)
const allPermissions = user.permissions.concat(
groups.flatMap((group) => group.permissions),
)
return can(allPermissions, permission)
}

export function can(allPermissions: RfdApiPermission[], permission: Permission): boolean {
const checks = createChecks(permission)
const allowed = checks.some((check) => performCheck(allPermissions, check))
Expand Down

0 comments on commit 77d9dfb

Please sign in to comment.