Skip to content

Commit

Permalink
feat: add analytics role (#491)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughcrt authored Aug 15, 2024
1 parent 07d5a0f commit d6fbb2f
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ops
Submodule ops updated from 75fec4 to 41bb41
1 change: 1 addition & 0 deletions packages/db/0040.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter type user_role add value 'analytics';
23 changes: 11 additions & 12 deletions packages/frontend/components/layout/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -485,14 +485,16 @@ export default function Sidebar() {
</Combobox.Footer>
</Combobox.Dropdown>
</Combobox>
<ActionIcon
variant="default"
size="sm"
component={Link}
href="/settings"
>
<IconSettings size={14} stroke={1} />
</ActionIcon>
{hasAccess(user.role, "billing", "read") && (
<ActionIcon
variant="default"
size="sm"
component={Link}
href="/settings"
>
<IconSettings size={14} stroke={1} />
</ActionIcon>
)}
</Group>

{user &&
Expand Down Expand Up @@ -570,10 +572,7 @@ export default function Sidebar() {
<Menu closeOnItemClick={false}>
<Menu.Target data-testid="account-sidebar-item">
<ActionIcon variant="subtle" radius="xl" size={32}>
<UserAvatar
size={26}
profile={user}
/>
<UserAvatar size={26} profile={user} />
</ActionIcon>
</Menu.Target>
<Menu.Dropdown>
Expand Down
14 changes: 13 additions & 1 deletion packages/frontend/pages/logs/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
useProject,
useProjectInfiniteSWR,
useRun,
useUser,
} from "@/utils/dataHooks"
import { fetcher } from "@/utils/fetcher"
import { formatDateTime } from "@/utils/format"
Expand All @@ -70,7 +71,7 @@ import { useRouter } from "next/router"

import IconPicker from "@/components/blocks/IconPicker"
import { useEvaluators } from "@/utils/dataHooks/evaluators"
import { deserializeLogic, serializeLogic } from "shared"
import { deserializeLogic, hasAccess, serializeLogic } from "shared"
import { useSortParams } from "@/utils/hooks"

export const defaultColumns = {
Expand Down Expand Up @@ -174,6 +175,7 @@ const DEFAULT_CHECK = ["AND"]

export default function Logs() {
const router = useRouter()
const { user } = useUser()
const { projectId } = useContext(ProjectContext)
const { project, isLoading: projectLoading, setProjectId } = useProject()
const { org } = useOrg()
Expand Down Expand Up @@ -234,6 +236,16 @@ export default function Logs() {

const { run: selectedRun, loading: runLoading } = useRun(selectedRunId)

useEffect(() => {
if (!hasAccess(user?.role, "settings", "read")) {
router.push("/analytics")
}
}, [user.role])

if (!user.role) {
return <Loader />
}

useEffect(() => {
const newColumns = { ...allColumns }
if (type === "llm" && Array.isArray(evaluators)) {
Expand Down
34 changes: 22 additions & 12 deletions packages/frontend/pages/settings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@ import {
Group,
Loader,
Popover,
SegmentedControl,
Stack,
Switch,
Tabs,
Text,
} from "@mantine/core"
import { NextSeo } from "next-seo"
import Router from "next/router"
import Router, { useRouter } from "next/router"

import RenamableField from "@/components/blocks/RenamableField"
import CheckPicker from "@/components/checks/Picker"
import { SettingsCard } from "@/components/blocks/SettingsCard"
import CheckPicker from "@/components/checks/Picker"
import config from "@/utils/config"
import { useOrg, useProject, useProjectRules, useUser } from "@/utils/dataHooks"
import errorHandler from "@/utils/errors"
import { fetcher } from "@/utils/fetcher"
import { modals } from "@mantine/modals"
import { notifications } from "@mantine/notifications"
import {
IconCheck,
IconPencil,
IconFilter,
IconRefreshAlert,
IconIdBadge,
IconPencil,
IconRefreshAlert,
} from "@tabler/icons-react"
import errorHandler from "@/utils/errors"
import { fetcher } from "@/utils/fetcher"
import { modals } from "@mantine/modals"
import { notifications } from "@mantine/notifications"
import { useOrg, useProject, useProjectRules, useUser } from "@/utils/dataHooks"
import Link from "next/link"
import { useEffect, useState } from "react"
import { CheckLogic, hasAccess } from "shared"
import useSWR from "swr"
import config from "@/utils/config"
import Link from "next/link"

function Keys() {
const [regenerating, setRegenerating] = useState(false)
Expand Down Expand Up @@ -276,6 +275,7 @@ export default function AppAnalytics() {
addSmartDataRule,
smartDataRuleLoading,
} = useProject()
const router = useRouter()

const { user } = useUser()

Expand All @@ -284,6 +284,16 @@ export default function AppAnalytics() {
project?.id && org && `/orgs/${org.id}/usage?projectId=${project?.id}`,
)

useEffect(() => {
if (!hasAccess(user?.role, "settings", "read")) {
router.push("/analytics")
}
}, [user.role])

if (projectUsageLoading || !user.role) {
return <Loader />
}

return (
<Container className="unblockable">
<NextSeo title="Settings" />
Expand Down
8 changes: 6 additions & 2 deletions packages/frontend/utils/dataHooks/views.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { useProjectMutation, useProjectSWR } from "."
import { hasAccess } from "shared"
import { useProjectMutation, useProjectSWR, useUser } from "."
import { fetcher } from "../fetcher"

export function useViews() {
const { data, isLoading, mutate } = useProjectSWR(`/views`)
const { user } = useUser()
const { data, isLoading, mutate } = useProjectSWR(
hasAccess(user?.role, "logs", "list") && `/views`,
)

const { trigger: insert, isMutating: isInserting } = useProjectMutation(
`/views`,
Expand Down
42 changes: 42 additions & 0 deletions packages/shared/access-control/roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export type ResourceName =
| "datasets"
| "checklists"
| "evaluations"
| "settings"

export type Role = "owner" | "admin" | "member" | "viewer" | "billing"
export type Action =
| "create"
Expand Down Expand Up @@ -116,6 +118,13 @@ export const roles: Record<
list: true,
run: true,
},
settings: {
create: true,
read: true,
update: true,
delete: true,
list: true,
},
},
},
admin: {
Expand Down Expand Up @@ -202,6 +211,13 @@ export const roles: Record<
list: true,
run: true,
},
settings: {
create: true,
read: true,
update: true,
delete: true,
list: true,
},
},
},
member: {
Expand Down Expand Up @@ -285,6 +301,13 @@ export const roles: Record<
list: true,
run: true,
},
settings: {
create: true,
read: true,
update: true,
delete: true,
list: true,
},
},
},
viewer: {
Expand Down Expand Up @@ -360,6 +383,10 @@ export const roles: Record<
list: true,
run: false,
},
settings: {
read: true,
list: true,
},
},
},
prompt_editor: {
Expand Down Expand Up @@ -395,6 +422,21 @@ export const roles: Record<
},
},
},
analytics: {
value: "analytics",
name: "Analytics Viewer",
description: "Can only access the Analytics page",
permissions: {
analytics: { read: true },
projects: {
read: true,
list: true,
},
users: {
list: true,
},
},
},
}

export function hasReadAccess(
Expand Down

0 comments on commit d6fbb2f

Please sign in to comment.