@shivaedev/admin 0.6.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +21 -12
- package/dist/client/UsersTable.d.ts +5 -4
- package/dist/client/UsersTable.d.ts.map +1 -1
- package/dist/client/UsersTable.js +13 -14
- package/dist/client/UsersTable.js.map +1 -1
- package/dist/client/UsersView.d.ts +4 -3
- package/dist/client/UsersView.d.ts.map +1 -1
- package/dist/client/UsersView.js +5 -5
- package/dist/client/UsersView.js.map +1 -1
- package/dist/jobs/page.d.ts +37 -36
- package/dist/jobs/page.d.ts.map +1 -1
- package/dist/jobs/page.js +2 -0
- package/dist/jobs/page.js.map +1 -1
- package/dist/jobs/retryPage.d.ts +38 -37
- package/dist/jobs/retryPage.d.ts.map +1 -1
- package/dist/jobs/retryPage.js +2 -0
- package/dist/jobs/retryPage.js.map +1 -1
- package/dist/page.d.ts +9 -5
- package/dist/page.d.ts.map +1 -1
- package/dist/page.js +6 -2
- package/dist/page.js.map +1 -1
- package/dist/permissions.d.ts +26 -5
- package/dist/permissions.d.ts.map +1 -1
- package/dist/permissions.js +6 -2
- package/dist/permissions.js.map +1 -1
- package/dist/server/users.d.ts +32 -10
- package/dist/server/users.d.ts.map +1 -1
- package/dist/server/users.js +17 -15
- package/dist/server/users.js.map +1 -1
- package/dist/server/usersHandlers.d.ts +3 -1
- package/dist/server/usersHandlers.d.ts.map +1 -1
- package/dist/server/usersHandlers.js +10 -9
- package/dist/server/usersHandlers.js.map +1 -1
- package/dist/users/page.d.ts +16 -15
- package/dist/users/page.d.ts.map +1 -1
- package/dist/users/page.js +26 -22
- package/dist/users/page.js.map +1 -1
- package/dist/users/roles.d.ts +8 -19
- package/dist/users/roles.d.ts.map +1 -1
- package/dist/users/roles.js +2 -6
- package/dist/users/roles.js.map +1 -1
- package/package.json +9 -9
- package/src/client/UsersTable.tsx +48 -51
- package/src/client/UsersView.ts +8 -7
- package/src/jobs/page.ts +2 -0
- package/src/jobs/retryPage.ts +2 -0
- package/src/page.ts +13 -5
- package/src/permissions.ts +5 -2
- package/src/server/users.ts +24 -15
- package/src/server/usersHandlers.ts +16 -10
- package/src/users/page.ts +27 -22
- package/src/users/roles.ts +6 -9
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/admin",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "Explicit admin page contracts and a protected native Effect RPC registry",
|
|
5
5
|
"homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/admin#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -42,19 +42,19 @@
|
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@shivaedev/access": "0.1.1",
|
|
45
|
-
"@shivaedev/contract": "0.
|
|
45
|
+
"@shivaedev/contract": "0.18.0",
|
|
46
46
|
"@shivaedev/errors": "0.1.0",
|
|
47
|
-
"@shivaedev/identity": "0.
|
|
48
|
-
"@shivaedev/jobs": "0.
|
|
49
|
-
"@shivaedev/react": "0.
|
|
50
|
-
"@shivaedev/sql": "0.
|
|
47
|
+
"@shivaedev/identity": "0.2.0",
|
|
48
|
+
"@shivaedev/jobs": "0.21.0",
|
|
49
|
+
"@shivaedev/react": "0.20.0",
|
|
50
|
+
"@shivaedev/sql": "0.23.0"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@effect/atom-react": "4.0.5",
|
|
54
54
|
"@shivaedev/quality": "0.42.0",
|
|
55
|
-
"@shivaedev/server": "0.
|
|
56
|
-
"@shivaedev/test-sql": "0.10.
|
|
57
|
-
"@shivaedev/test-story": "0.21.
|
|
55
|
+
"@shivaedev/server": "0.2.0",
|
|
56
|
+
"@shivaedev/test-sql": "0.10.1",
|
|
57
|
+
"@shivaedev/test-story": "0.21.1",
|
|
58
58
|
"@types/node": "24.19.2",
|
|
59
59
|
"@types/react": "19.3.0",
|
|
60
60
|
"@types/react-dom": "19.3.0",
|
|
@@ -1,71 +1,58 @@
|
|
|
1
1
|
import { DateTime } from "effect";
|
|
2
2
|
import type { UsersList } from "#users/page.ts";
|
|
3
|
-
import {
|
|
3
|
+
import type { AppUser, ChangeRoleInput } from "#users/roles.ts";
|
|
4
4
|
|
|
5
|
-
interface RoleControls {
|
|
6
|
-
readonly dispatch: (input:
|
|
5
|
+
interface RoleControls<TRole extends string> {
|
|
6
|
+
readonly dispatch: (input: ChangeRoleInput<TRole>) => void;
|
|
7
7
|
readonly pending: boolean;
|
|
8
|
+
readonly roles: readonly TRole[];
|
|
8
9
|
}
|
|
9
10
|
|
|
10
|
-
function
|
|
11
|
-
return (
|
|
12
|
-
<tr>
|
|
13
|
-
{["Name", "Email", "Roles", "Signed up"].map((label) => (
|
|
14
|
-
<th className="whitespace-nowrap p-3" key={label} scope="col">
|
|
15
|
-
{label}
|
|
16
|
-
</th>
|
|
17
|
-
))}
|
|
18
|
-
</tr>
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
function RoleCheckbox({ user, role, pending, dispatch }: RoleControls & { readonly user: typeof AppUser.Type; readonly role: typeof UserRole.Type }) {
|
|
23
|
-
return (
|
|
24
|
-
<label className="mr-3 inline-flex gap-1">
|
|
25
|
-
<input
|
|
26
|
-
aria-label={`${role} for ${user.email}`}
|
|
27
|
-
checked={user.roles.includes(role)}
|
|
28
|
-
disabled={pending}
|
|
29
|
-
onChange={(event) => dispatch({ grant: event.target.checked, role, userId: user.id })}
|
|
30
|
-
type="checkbox"
|
|
31
|
-
/>
|
|
32
|
-
{role}
|
|
33
|
-
</label>
|
|
34
|
-
);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function UserRoles({
|
|
11
|
+
function UserRole<TRole extends string>({
|
|
38
12
|
user,
|
|
39
13
|
canChangeRoles,
|
|
40
|
-
|
|
14
|
+
roles,
|
|
41
15
|
pending,
|
|
42
|
-
|
|
16
|
+
dispatch,
|
|
17
|
+
}: RoleControls<TRole> & {
|
|
43
18
|
readonly user: typeof AppUser.Type;
|
|
44
19
|
readonly canChangeRoles: boolean;
|
|
45
20
|
}) {
|
|
46
|
-
return (
|
|
47
|
-
<
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
21
|
+
return canChangeRoles ? (
|
|
22
|
+
<select
|
|
23
|
+
aria-label={`Role for ${user.email}`}
|
|
24
|
+
className="rounded-md border bg-background p-2"
|
|
25
|
+
disabled={pending}
|
|
26
|
+
onChange={(event) => {
|
|
27
|
+
const role = roles.find((candidate) => candidate === event.target.value);
|
|
28
|
+
if (role !== undefined) {
|
|
29
|
+
dispatch({ role, userId: user.id });
|
|
30
|
+
}
|
|
31
|
+
}}
|
|
32
|
+
value={user.role}
|
|
33
|
+
>
|
|
34
|
+
{roles.map((role) => (
|
|
35
|
+
<option key={role} value={role}>
|
|
36
|
+
{role}
|
|
37
|
+
</option>
|
|
38
|
+
))}
|
|
39
|
+
</select>
|
|
40
|
+
) : (
|
|
41
|
+
user.role
|
|
53
42
|
);
|
|
54
43
|
}
|
|
55
44
|
|
|
56
|
-
function UserRow({
|
|
45
|
+
function UserRow<TRole extends string>({
|
|
57
46
|
user,
|
|
47
|
+
canChangeRoles,
|
|
58
48
|
...controls
|
|
59
|
-
}: RoleControls & {
|
|
60
|
-
readonly user: typeof AppUser.Type;
|
|
61
|
-
readonly canChangeRoles: boolean;
|
|
62
|
-
}) {
|
|
49
|
+
}: RoleControls<TRole> & { readonly user: typeof AppUser.Type; readonly canChangeRoles: boolean }) {
|
|
63
50
|
return (
|
|
64
51
|
<tr className="border-b">
|
|
65
52
|
<td className="p-3">{user.name}</td>
|
|
66
53
|
<td className="p-3">{user.email}</td>
|
|
67
54
|
<td className="p-3">
|
|
68
|
-
<
|
|
55
|
+
<UserRole {...controls} canChangeRoles={canChangeRoles} user={user} />
|
|
69
56
|
</td>
|
|
70
57
|
<td className="whitespace-nowrap p-3">
|
|
71
58
|
<time dateTime={DateTime.formatIso(user.createdAt)}>{DateTime.formatIso(user.createdAt)}</time>
|
|
@@ -73,8 +60,7 @@ function UserRow({
|
|
|
73
60
|
</tr>
|
|
74
61
|
);
|
|
75
62
|
}
|
|
76
|
-
|
|
77
|
-
function UserRows({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
63
|
+
function UserRows<TRole extends string>({ data, ...controls }: RoleControls<TRole> & { readonly data: typeof UsersList.Type }) {
|
|
78
64
|
return (
|
|
79
65
|
<div className="overflow-x-auto rounded-md border">
|
|
80
66
|
<table className="w-full text-left text-sm">
|
|
@@ -91,12 +77,23 @@ function UserRows({ data, ...controls }: RoleControls & { readonly data: typeof
|
|
|
91
77
|
</div>
|
|
92
78
|
);
|
|
93
79
|
}
|
|
94
|
-
|
|
95
|
-
export function UsersTable({ data, ...controls }: RoleControls & { readonly data: typeof UsersList.Type }) {
|
|
80
|
+
export function UsersTable<TRole extends string>({ data, ...controls }: RoleControls<TRole> & { readonly data: typeof UsersList.Type }) {
|
|
96
81
|
return (
|
|
97
82
|
<div className="space-y-4">
|
|
98
|
-
{data.canChangeRoles ? null : <p>
|
|
83
|
+
{data.canChangeRoles ? null : <p>Permission to assign roles is required to change roles.</p>}
|
|
99
84
|
{data.users.length === 0 ? <p>No users.</p> : <UserRows {...controls} data={data} />}
|
|
100
85
|
</div>
|
|
101
86
|
);
|
|
102
87
|
}
|
|
88
|
+
|
|
89
|
+
function UserHeaders() {
|
|
90
|
+
return (
|
|
91
|
+
<tr>
|
|
92
|
+
{["Name", "Email", "Role", "Signed up"].map((label) => (
|
|
93
|
+
<th className="whitespace-nowrap p-3" key={label} scope="col">
|
|
94
|
+
{label}
|
|
95
|
+
</th>
|
|
96
|
+
))}
|
|
97
|
+
</tr>
|
|
98
|
+
);
|
|
99
|
+
}
|
package/src/client/UsersView.ts
CHANGED
|
@@ -7,23 +7,24 @@ import type { UsersList } from "#users/page.ts";
|
|
|
7
7
|
import type { ChangeRoleInput, UserListInput } from "#users/roles.ts";
|
|
8
8
|
import { UsersTable } from "./UsersTable.tsx";
|
|
9
9
|
|
|
10
|
-
export interface UsersViewProps {
|
|
11
|
-
readonly changeRole: Atom.AtomResultFn<
|
|
10
|
+
export interface UsersViewProps<TRole extends string> {
|
|
11
|
+
readonly changeRole: Atom.AtomResultFn<ChangeRoleInput<TRole>, void, unknown>;
|
|
12
12
|
readonly list: (input: typeof UserListInput.Type) => Atom.Atom<AsyncResult.AsyncResult<typeof UsersList.Type, unknown>>;
|
|
13
|
+
readonly roles: readonly TRole[];
|
|
13
14
|
}
|
|
14
15
|
|
|
15
16
|
function failure(cause: Cause.Cause<unknown>): string {
|
|
16
17
|
const error = Option.getOrUndefined(Cause.findErrorOption(cause));
|
|
17
|
-
if (Predicate.isTagged(error, "
|
|
18
|
-
return "The last
|
|
18
|
+
if (Predicate.isTagged(error, "LastRoleAssigner")) {
|
|
19
|
+
return "The last user who can assign roles cannot be removed. Assign another user permission to change roles first.";
|
|
19
20
|
}
|
|
20
21
|
if (Predicate.isTagged(error, "Forbidden")) {
|
|
21
|
-
return "
|
|
22
|
+
return "Permission required to assign roles.";
|
|
22
23
|
}
|
|
23
24
|
return "Could not change the role. Try again.";
|
|
24
25
|
}
|
|
25
26
|
|
|
26
|
-
export function UsersView({ list, changeRole }: UsersViewProps) {
|
|
27
|
+
export function UsersView<TRole extends string>({ list, changeRole, roles }: UsersViewProps<TRole>) {
|
|
27
28
|
const [cursors, setCursors] = useState<readonly string[]>([]);
|
|
28
29
|
const cursor = cursors.at(-1);
|
|
29
30
|
const state = useQuery(list({ ...(cursor === undefined ? {} : { cursor }), limit: 20 }));
|
|
@@ -40,7 +41,7 @@ export function UsersView({ list, changeRole }: UsersViewProps) {
|
|
|
40
41
|
state.pending ? createElement("p", { role: "status" }, "Loading users…") : null,
|
|
41
42
|
Option.isSome(state.cause) ? createElement("p", { role: "alert" }, "Could not load users. Try refreshing.") : null,
|
|
42
43
|
Option.isSome(action.cause) ? createElement("p", { role: "alert" }, failure(action.cause.value)) : null,
|
|
43
|
-
data === undefined ? null : createElement(UsersTable
|
|
44
|
+
data === undefined ? null : createElement(UsersTable<TRole>, { data, dispatch: action.dispatch, pending: action.pending, roles }),
|
|
44
45
|
data === undefined
|
|
45
46
|
? null
|
|
46
47
|
: createElement(
|
package/src/jobs/page.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Schema } from "effect";
|
|
|
2
2
|
import { contract } from "@shivaedev/contract/contract.ts";
|
|
3
3
|
import { query } from "@shivaedev/contract/operation.ts";
|
|
4
4
|
import { adminPage } from "#page.ts";
|
|
5
|
+
import { JobsPermission } from "#permissions.ts";
|
|
5
6
|
import { jobsKeys } from "./keys.ts";
|
|
6
7
|
import { deadLetteredSchema, JobsFailures, JobsHealth, type JobsQueue, jobPage, jobSchema, PageFields, QueueCounts } from "./schemas.ts";
|
|
7
8
|
|
|
@@ -39,6 +40,7 @@ export function jobsPage<const TQueues extends readonly JobsQueue[]>(...queues:
|
|
|
39
40
|
],
|
|
40
41
|
}),
|
|
41
42
|
id: "jobs",
|
|
43
|
+
permission: JobsPermission,
|
|
42
44
|
route: { path: "/admin/jobs", title: "Jobs" },
|
|
43
45
|
});
|
|
44
46
|
return Object.assign(page, { queues });
|
package/src/jobs/retryPage.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { Schema } from "effect";
|
|
|
2
2
|
import { contract } from "@shivaedev/contract/contract.ts";
|
|
3
3
|
import { command } from "@shivaedev/contract/operation.ts";
|
|
4
4
|
import { adminPage } from "#page.ts";
|
|
5
|
+
import { JobsPermission } from "#permissions.ts";
|
|
5
6
|
import { jobsKeys } from "./keys.ts";
|
|
6
7
|
import { jobsPage } from "./page.ts";
|
|
7
8
|
import { JobsFailures, type JobsQueue } from "./schemas.ts";
|
|
@@ -32,6 +33,7 @@ export function jobsPageWithRetry<const TQueues extends readonly JobsQueue[]>(..
|
|
|
32
33
|
queries: page.contract.declaration.queries,
|
|
33
34
|
}),
|
|
34
35
|
id: page.id,
|
|
36
|
+
permission: JobsPermission,
|
|
35
37
|
route: page.route,
|
|
36
38
|
}),
|
|
37
39
|
{ queues },
|
package/src/page.ts
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import type { Rpc, RpcGroup } from "effect/rpc";
|
|
1
|
+
import type { Rpc, RpcGroup, RpcMiddleware } from "effect/rpc";
|
|
2
|
+
import type { Permission } from "@shivaedev/access/permission.ts";
|
|
2
3
|
import type { Contract } from "@shivaedev/contract/contract.ts";
|
|
3
4
|
import type { CommandShape, QueryShape, StreamShape } from "@shivaedev/contract/operation.ts";
|
|
4
|
-
import { AdminPermission } from "#permissions.ts";
|
|
5
5
|
|
|
6
6
|
export interface AdminRoute {
|
|
7
7
|
readonly path: string;
|
|
8
|
+
readonly permission: Permission;
|
|
8
9
|
readonly title: string;
|
|
9
10
|
}
|
|
10
11
|
|
|
@@ -27,13 +28,20 @@ export function adminPage<
|
|
|
27
28
|
const TCommands extends readonly CommandShape[],
|
|
28
29
|
TRpcs extends Rpc.Any,
|
|
29
30
|
const TStreams extends readonly StreamShape[],
|
|
31
|
+
TMiddleware extends RpcMiddleware.AnyService & { readonly permission: Permission },
|
|
30
32
|
>(page: {
|
|
31
33
|
readonly id: TId;
|
|
32
|
-
readonly route: AdminRoute
|
|
34
|
+
readonly route: Omit<AdminRoute, "permission">;
|
|
35
|
+
readonly permission: TMiddleware;
|
|
33
36
|
readonly contract: Contract<TName, TQueries, TCommands, TRpcs, TStreams>;
|
|
34
37
|
}): AdminPage & {
|
|
35
38
|
readonly id: TId;
|
|
36
|
-
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs,
|
|
39
|
+
readonly contract: Contract<TName, TQueries, TCommands, Rpc.AddMiddleware<TRpcs, TMiddleware>, TStreams>;
|
|
37
40
|
} {
|
|
38
|
-
return {
|
|
41
|
+
return {
|
|
42
|
+
...page,
|
|
43
|
+
route: { ...page.route, permission: page.permission.permission },
|
|
44
|
+
[AdminPageTypeId]: true as const,
|
|
45
|
+
contract: page.contract.middleware(page.permission),
|
|
46
|
+
};
|
|
39
47
|
}
|
package/src/permissions.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { permission } from "@shivaedev/access/permission.ts";
|
|
2
2
|
import { permissionMiddleware } from "@shivaedev/identity/permission.ts";
|
|
3
3
|
|
|
4
|
-
export const
|
|
5
|
-
export
|
|
4
|
+
export const ReadUsers = permission("users.read");
|
|
5
|
+
export const AssignRoles = permission("roles.assign");
|
|
6
|
+
export const ReadJobs = permission("jobs.read");
|
|
7
|
+
export class UsersPermission extends permissionMiddleware<UsersPermission>()("@shivaedev/admin/UsersPermission", ReadUsers) {}
|
|
8
|
+
export class JobsPermission extends permissionMiddleware<JobsPermission>()("@shivaedev/admin/JobsPermission", ReadJobs) {}
|
package/src/server/users.ts
CHANGED
|
@@ -1,42 +1,51 @@
|
|
|
1
1
|
import { DateTime, Effect, Option, Schema } from "effect";
|
|
2
2
|
import { SqlClient, SqlSchema } from "effect/sql";
|
|
3
|
+
import type { roleSet } from "@shivaedev/access/roleSet.ts";
|
|
3
4
|
import { AuthUnavailable, NotFound } from "@shivaedev/errors/taxonomy.ts";
|
|
4
5
|
import { SessionUser } from "@shivaedev/identity/identity.ts";
|
|
5
6
|
import { requirePermission } from "@shivaedev/identity/permission.ts";
|
|
6
7
|
import { invalidateOnCommit, transact } from "@shivaedev/sql/transact.ts";
|
|
7
|
-
import {
|
|
8
|
+
import { AssignRoles, ReadUsers } from "#permissions.ts";
|
|
9
|
+
import { type ChangeRoleInput, LastRoleAssigner, type UserListInput } from "#users/roles.ts";
|
|
8
10
|
|
|
9
11
|
const StoredUser = Schema.Struct({
|
|
10
12
|
createdAt: Schema.Date,
|
|
11
13
|
email: Schema.String,
|
|
12
14
|
id: Schema.String,
|
|
13
15
|
name: Schema.String,
|
|
14
|
-
role: Schema.
|
|
16
|
+
role: Schema.String,
|
|
15
17
|
});
|
|
16
18
|
function unavailable() {
|
|
17
19
|
return new AuthUnavailable({ message: "Could not access app users" });
|
|
18
20
|
}
|
|
19
21
|
|
|
20
|
-
export const listUsers = Effect.fn("Platform.listUsers")(function*
|
|
22
|
+
export const listUsers = Effect.fn("Platform.listUsers")(function* <TRole extends string>(
|
|
23
|
+
access: Pick<ReturnType<typeof roleSet<TRole>>, "Role">,
|
|
24
|
+
input: typeof UserListInput.Type,
|
|
25
|
+
) {
|
|
26
|
+
yield* requirePermission(ReadUsers);
|
|
21
27
|
const sql = yield* SqlClient.SqlClient;
|
|
22
28
|
const read = SqlSchema.findAll({
|
|
23
29
|
execute: () => sql`select id, name, email, role, "createdAt" from "user"
|
|
24
30
|
where id > ${input.cursor ?? ""} order by id limit ${input.limit + 1}`,
|
|
25
31
|
Request: Schema.Void,
|
|
26
|
-
Result: StoredUser,
|
|
32
|
+
Result: Schema.Struct({ ...StoredUser.fields, role: access.Role }),
|
|
27
33
|
});
|
|
28
34
|
const rows = yield* read(undefined).pipe(Effect.mapError(unavailable));
|
|
29
35
|
const users = rows.slice(0, input.limit).map(({ role, createdAt, ...user }) => ({
|
|
30
36
|
...user,
|
|
31
37
|
createdAt: DateTime.makeUnsafe(createdAt),
|
|
32
|
-
|
|
38
|
+
role,
|
|
33
39
|
}));
|
|
34
40
|
return { next: rows.length > input.limit ? Option.fromUndefinedOr(users.at(-1)?.id) : Option.none<string>(), users };
|
|
35
41
|
});
|
|
36
42
|
|
|
37
|
-
export const changeUserRole = Effect.fn("Platform.changeUserRole")(function*
|
|
43
|
+
export const changeUserRole = Effect.fn("Platform.changeUserRole")(function* <TRole extends string>(
|
|
44
|
+
access: Pick<ReturnType<typeof roleSet<TRole>>, "Role" | "hasPermission">,
|
|
45
|
+
input: ChangeRoleInput<TRole>,
|
|
46
|
+
) {
|
|
38
47
|
const actor = yield* SessionUser;
|
|
39
|
-
yield* requirePermission(
|
|
48
|
+
yield* requirePermission(AssignRoles);
|
|
40
49
|
const sql = yield* SqlClient.SqlClient;
|
|
41
50
|
const changed = yield* Effect.gen(function* () {
|
|
42
51
|
yield* sql`select pg_advisory_xact_lock(hashtext('platform.auth.user-roles'))`;
|
|
@@ -45,18 +54,18 @@ export const changeUserRole = Effect.fn("Platform.changeUserRole")(function* (in
|
|
|
45
54
|
if (target === undefined) {
|
|
46
55
|
return yield* Effect.fail(new NotFound({ message: "User not found" }));
|
|
47
56
|
}
|
|
48
|
-
const
|
|
49
|
-
if (
|
|
57
|
+
const next = yield* Schema.decodeUnknownEffect(access.Role)(input.role).pipe(Effect.mapError(unavailable));
|
|
58
|
+
if (target.role === next) {
|
|
50
59
|
return false;
|
|
51
60
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
61
|
+
const assigners = access.Role.literals.filter((role) => access.hasPermission(role, AssignRoles));
|
|
62
|
+
if (assigners.some((role) => role === target.role) && !access.hasPermission(next, AssignRoles)) {
|
|
63
|
+
const holders = yield* sql<{ readonly id: string }>`select id from "user" where role in ${sql.in(assigners)}`;
|
|
64
|
+
if (holders.length === 1) {
|
|
65
|
+
return yield* Effect.fail(new LastRoleAssigner({ userId: input.userId }));
|
|
56
66
|
}
|
|
57
67
|
}
|
|
58
|
-
|
|
59
|
-
yield* sql`update "user" set role = ${next.join(",")}, "updatedAt" = now() where id = ${input.userId}`;
|
|
68
|
+
yield* sql`update "user" set role = ${next}, "updatedAt" = now() where id = ${input.userId}`;
|
|
60
69
|
yield* invalidateOnCommit(["admin.users"]);
|
|
61
70
|
return true;
|
|
62
71
|
}).pipe(transact({ onSqlError: unavailable }));
|
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
+
import type { roleSet } from "@shivaedev/access/roleSet.ts";
|
|
2
3
|
import { SessionUser } from "@shivaedev/identity/identity.ts";
|
|
3
4
|
import { hasPermission } from "@shivaedev/identity/permission.ts";
|
|
5
|
+
import { AssignRoles } from "#permissions.ts";
|
|
4
6
|
import { changeUserRole, listUsers } from "#server/users.ts";
|
|
5
|
-
import {
|
|
6
|
-
import { ChangeUserRoles } from "#users/roles.ts";
|
|
7
|
+
import type { usersPage } from "#users/page.ts";
|
|
7
8
|
|
|
8
|
-
export
|
|
9
|
-
|
|
10
|
-
"
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
9
|
+
export function usersHandlers<const TRole extends string>(
|
|
10
|
+
page: ReturnType<typeof usersPage<TRole>>,
|
|
11
|
+
access: Pick<ReturnType<typeof roleSet<TRole>>, "Role" | "hasPermission">,
|
|
12
|
+
) {
|
|
13
|
+
return page.contract.application.toLayer({
|
|
14
|
+
"admin.users.changeRole": (input) => changeUserRole(access, input),
|
|
15
|
+
"admin.users.list": (input) =>
|
|
16
|
+
Effect.gen(function* () {
|
|
17
|
+
const user = yield* SessionUser;
|
|
18
|
+
return { ...(yield* listUsers(access, input)), canChangeRoles: hasPermission(user, AssignRoles) };
|
|
19
|
+
}),
|
|
20
|
+
});
|
|
21
|
+
}
|
package/src/users/page.ts
CHANGED
|
@@ -1,32 +1,37 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
+
import type { roleSet } from "@shivaedev/access/roleSet.ts";
|
|
2
3
|
import { contract } from "@shivaedev/contract/contract.ts";
|
|
3
4
|
import { collection } from "@shivaedev/contract/keys.ts";
|
|
4
5
|
import { command, query } from "@shivaedev/contract/operation.ts";
|
|
5
6
|
import { AuthUnavailable, Forbidden, NotFound } from "@shivaedev/errors/taxonomy.ts";
|
|
6
7
|
import { adminPage } from "#page.ts";
|
|
7
|
-
import {
|
|
8
|
+
import { UsersPermission } from "#permissions.ts";
|
|
9
|
+
import { LastRoleAssigner, UserList, UserListInput } from "#users/roles.ts";
|
|
8
10
|
|
|
9
11
|
const users = collection("admin.users", Schema.String);
|
|
10
12
|
|
|
11
13
|
export const UsersList = Schema.Struct({ ...UserList.fields, canChangeRoles: Schema.Boolean });
|
|
12
|
-
export const
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
14
|
+
export function usersPage<const TRole extends string>(access: Pick<ReturnType<typeof roleSet<TRole>>, "Role">) {
|
|
15
|
+
return adminPage({
|
|
16
|
+
contract: contract("admin.users", {
|
|
17
|
+
commands: [
|
|
18
|
+
command("changeRole", {
|
|
19
|
+
invalidates: () => [users.list],
|
|
20
|
+
payload: { role: access.Role, userId: Schema.String },
|
|
21
|
+
rejections: { AuthUnavailable, Forbidden, LastRoleAssigner, NotFound },
|
|
22
|
+
}),
|
|
23
|
+
],
|
|
24
|
+
queries: [
|
|
25
|
+
query("list", {
|
|
26
|
+
payload: UserListInput.fields,
|
|
27
|
+
reads: () => [users.list],
|
|
28
|
+
rejections: { AuthUnavailable, Forbidden },
|
|
29
|
+
success: UsersList,
|
|
30
|
+
}),
|
|
31
|
+
],
|
|
32
|
+
}),
|
|
33
|
+
id: "users",
|
|
34
|
+
permission: UsersPermission,
|
|
35
|
+
route: { path: "/admin/users", title: "Users" },
|
|
36
|
+
});
|
|
37
|
+
}
|
package/src/users/roles.ts
CHANGED
|
@@ -1,15 +1,11 @@
|
|
|
1
1
|
import { Schema } from "effect";
|
|
2
|
-
import { permission } from "@shivaedev/access/permission.ts";
|
|
3
|
-
|
|
4
|
-
export const UserRole = Schema.Literals(["user", "admin", "owner"]);
|
|
5
|
-
export type UserRole = typeof UserRole.Type;
|
|
6
2
|
|
|
7
3
|
export const AppUser = Schema.Struct({
|
|
8
4
|
createdAt: Schema.DateTimeUtcFromString,
|
|
9
5
|
email: Schema.String,
|
|
10
6
|
id: Schema.String,
|
|
11
7
|
name: Schema.String,
|
|
12
|
-
|
|
8
|
+
role: Schema.String,
|
|
13
9
|
});
|
|
14
10
|
|
|
15
11
|
export const UserListInput = Schema.Struct({
|
|
@@ -17,7 +13,8 @@ export const UserListInput = Schema.Struct({
|
|
|
17
13
|
limit: Schema.Number.check(Schema.isInt(), Schema.isBetween({ maximum: 100, minimum: 1 })),
|
|
18
14
|
});
|
|
19
15
|
export const UserList = Schema.Struct({ next: Schema.OptionFromNullOr(Schema.String), users: Schema.Array(AppUser) });
|
|
20
|
-
export
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
16
|
+
export interface ChangeRoleInput<TRole extends string = string> {
|
|
17
|
+
readonly role: TRole;
|
|
18
|
+
readonly userId: string;
|
|
19
|
+
}
|
|
20
|
+
export class LastRoleAssigner extends Schema.TaggedError<LastRoleAssigner>()("LastRoleAssigner", { userId: Schema.String }) {}
|