pangloss-users 0.1.0__py3-none-any.whl
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.
- pangloss_users/__init__.py +53 -0
- pangloss_users/exceptions.py +14 -0
- pangloss_users/interface/README.md +8 -0
- pangloss_users/interface/package.json +23 -0
- pangloss_users/interface/src/api.ts +103 -0
- pangloss_users/interface/src/components/UserPage.tsx +85 -0
- pangloss_users/interface/src/components/UsersAdminSection.tsx +556 -0
- pangloss_users/interface/src/components/UsersPage.tsx +64 -0
- pangloss_users/interface/src/feed.ts +29 -0
- pangloss_users/interface/src/index.ts +21 -0
- pangloss_users/interface/src/state.test.ts +149 -0
- pangloss_users/interface/src/state.ts +151 -0
- pangloss_users/interface/tsconfig.json +18 -0
- pangloss_users/interface/vitest.config.ts +14 -0
- pangloss_users/interface_config.py +8 -0
- pangloss_users/models.py +126 -0
- pangloss_users/passwords.py +46 -0
- pangloss_users/py.typed +0 -0
- pangloss_users/service.py +342 -0
- pangloss_users/translations/ui.jsonc +47 -0
- pangloss_users-0.1.0.dist-info/METADATA +21 -0
- pangloss_users-0.1.0.dist-info/RECORD +24 -0
- pangloss_users-0.1.0.dist-info/WHEEL +4 -0
- pangloss_users-0.1.0.dist-info/licenses/LICENCE.txt +21 -0
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""Users of Pangloss: their models, passwords and the `service` functions (implementation_docs/users.md)"""
|
|
2
|
+
|
|
3
|
+
from contextvars import ContextVar
|
|
4
|
+
|
|
5
|
+
lazy from pangloss_users.exceptions import (
|
|
6
|
+
PanglossInvalidUserError,
|
|
7
|
+
PanglossUserDoesNotExistError,
|
|
8
|
+
PanglossUserError,
|
|
9
|
+
PanglossUserExistsError,
|
|
10
|
+
)
|
|
11
|
+
lazy from pangloss_users.models import (
|
|
12
|
+
AccessTokenInfo,
|
|
13
|
+
ToolchainCreate,
|
|
14
|
+
User,
|
|
15
|
+
UserCreate,
|
|
16
|
+
UserInDB,
|
|
17
|
+
UserItem,
|
|
18
|
+
UserItems,
|
|
19
|
+
UserSummary,
|
|
20
|
+
UserUpdate,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"AccessTokenInfo",
|
|
25
|
+
"PanglossInvalidUserError",
|
|
26
|
+
"PanglossUserDoesNotExistError",
|
|
27
|
+
"PanglossUserError",
|
|
28
|
+
"PanglossUserExistsError",
|
|
29
|
+
"ToolchainCreate",
|
|
30
|
+
"User",
|
|
31
|
+
"UserCreate",
|
|
32
|
+
"UserInDB",
|
|
33
|
+
"UserItem",
|
|
34
|
+
"UserItems",
|
|
35
|
+
"UserSummary",
|
|
36
|
+
"UserUpdate",
|
|
37
|
+
"current_request_is_admin",
|
|
38
|
+
"current_request_username",
|
|
39
|
+
"see_unpublished",
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
current_request_username = ContextVar(
|
|
43
|
+
"current_request_username", default="pangloss_default_user"
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# Whether the user of the request is an admin. True outside a request (scripts, commands, tests), as they may do
|
|
47
|
+
# anything; the API sets it for the user it finds.
|
|
48
|
+
current_request_is_admin = ContextVar("current_request_is_admin", default=True)
|
|
49
|
+
|
|
50
|
+
# Whether the request may see documents that are not published (`published: false` on the head node;
|
|
51
|
+
# see implementation_docs/review.md). True outside a request (scripts, commands, tests); the API sets it false for
|
|
52
|
+
# a request that is not logged in.
|
|
53
|
+
see_unpublished = ContextVar("see_unpublished", default=True)
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class PanglossUserError(Exception):
|
|
2
|
+
"""Base class for errors about users"""
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class PanglossUserExistsError(PanglossUserError):
|
|
6
|
+
"""The username or email is already taken"""
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class PanglossUserDoesNotExistError(PanglossUserError):
|
|
10
|
+
"""There is no such user"""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class PanglossInvalidUserError(PanglossUserError):
|
|
14
|
+
"""The details given for a user are not acceptable (e.g. a short password)"""
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
# Interface module of `pangloss_users`
|
|
2
|
+
|
|
3
|
+
A Solid 2.0 library that the interface includes when `pangloss_users` is in `INSTALLED_APPS`. For someone logged in:
|
|
4
|
+
|
|
5
|
+
- `/users` (`UsersPage`, linked from the navigation bar): everyone, by username and full name.
|
|
6
|
+
- `/users/:username` (`UserPage`): the entities and top-level documents the user has made or changed, the most recent
|
|
7
|
+
first, a page at a time.
|
|
8
|
+
- `userHref` in the manifest: the console's "created" and "modified" names link to that page.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@pangloss-module/pangloss_users",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "The interface module of pangloss_users",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./src/index.ts"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src"
|
|
12
|
+
],
|
|
13
|
+
"scripts": {
|
|
14
|
+
"typecheck": "tsc --noEmit -p tsconfig.json",
|
|
15
|
+
"test": "vitest run"
|
|
16
|
+
},
|
|
17
|
+
"peerDependencies": {
|
|
18
|
+
"@pangloss/interface-core": "*",
|
|
19
|
+
"@solidjs/router": "^2.0.0",
|
|
20
|
+
"@solidjs/web": "^2.0.1",
|
|
21
|
+
"solid-js": "^2.0.1"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import type { ApiClient } from "@pangloss/interface-core";
|
|
2
|
+
|
|
3
|
+
// What `/api/users` sends for these pages (packages/pangloss-users/src/pangloss_users/models.py)
|
|
4
|
+
|
|
5
|
+
export interface UserSummary {
|
|
6
|
+
username: string;
|
|
7
|
+
fullName?: string | null;
|
|
8
|
+
/** `toolchain`: a program named as a creator of the data it made; it cannot log in */
|
|
9
|
+
kind?: "user" | "toolchain";
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface UserItem {
|
|
13
|
+
id: string;
|
|
14
|
+
type: string;
|
|
15
|
+
label?: string | null;
|
|
16
|
+
action: "created" | "modified";
|
|
17
|
+
/** ISO time */
|
|
18
|
+
when?: string | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export interface UserItems {
|
|
22
|
+
user: UserSummary;
|
|
23
|
+
results: UserItem[];
|
|
24
|
+
count: number;
|
|
25
|
+
page: number;
|
|
26
|
+
pageSize: number;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** A user as an admin sees them */
|
|
30
|
+
export interface User extends UserSummary {
|
|
31
|
+
email?: string | null;
|
|
32
|
+
admin: boolean;
|
|
33
|
+
disabled: boolean;
|
|
34
|
+
/** ISO time */
|
|
35
|
+
createdWhen?: string | null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export interface NewUser {
|
|
39
|
+
username: string;
|
|
40
|
+
email: string;
|
|
41
|
+
password: string;
|
|
42
|
+
fullName: string | null;
|
|
43
|
+
admin: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export interface AccessToken {
|
|
47
|
+
id: string;
|
|
48
|
+
name: string;
|
|
49
|
+
scope: "read" | "write";
|
|
50
|
+
createdWhen: string;
|
|
51
|
+
expiresWhen?: string | null;
|
|
52
|
+
lastUsedWhen?: string | null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
const path = (username: string) => `/users/${encodeURIComponent(username)}`;
|
|
56
|
+
|
|
57
|
+
export const getAllUsers = (api: ApiClient): Promise<User[]> => api.get("/users");
|
|
58
|
+
|
|
59
|
+
export const createUser = (api: ApiClient, user: NewUser): Promise<User> => api.post("/users", user);
|
|
60
|
+
|
|
61
|
+
export const createToolchain = (
|
|
62
|
+
api: ApiClient,
|
|
63
|
+
toolchain: { username: string; fullName: string | null },
|
|
64
|
+
): Promise<User> => api.post("/users/toolchains", toolchain);
|
|
65
|
+
|
|
66
|
+
export const updateUser = (
|
|
67
|
+
api: ApiClient,
|
|
68
|
+
username: string,
|
|
69
|
+
changes: { email?: string | null; fullName?: string | null },
|
|
70
|
+
): Promise<User> => api.patch(path(username), changes);
|
|
71
|
+
|
|
72
|
+
export const setPassword = (api: ApiClient, username: string, password: string): Promise<unknown> =>
|
|
73
|
+
api.put(`${path(username)}/password`, { password });
|
|
74
|
+
|
|
75
|
+
export const setAdmin = (api: ApiClient, username: string, admin: boolean): Promise<User> =>
|
|
76
|
+
api.put(`${path(username)}/admin`, { admin });
|
|
77
|
+
|
|
78
|
+
export const setDisabled = (api: ApiClient, username: string, disabled: boolean): Promise<User> =>
|
|
79
|
+
api.put(`${path(username)}/disabled`, { disabled });
|
|
80
|
+
|
|
81
|
+
export const endSessions = (api: ApiClient, username: string): Promise<void> => api.post(`${path(username)}/logout-all`);
|
|
82
|
+
|
|
83
|
+
export const getTokens = (api: ApiClient, username: string): Promise<AccessToken[]> => api.get(`${path(username)}/tokens`);
|
|
84
|
+
|
|
85
|
+
/** The answer has the token itself, the only time it is seen */
|
|
86
|
+
export const createToken = (
|
|
87
|
+
api: ApiClient,
|
|
88
|
+
username: string,
|
|
89
|
+
token: { name: string; scope: "read" | "write"; expiresDays: number | null },
|
|
90
|
+
): Promise<AccessToken & { token: string }> => api.post(`${path(username)}/tokens`, token);
|
|
91
|
+
|
|
92
|
+
export const revokeToken = (api: ApiClient, username: string, id: string): Promise<void> =>
|
|
93
|
+
api.delete(`${path(username)}/tokens/${encodeURIComponent(id)}`);
|
|
94
|
+
|
|
95
|
+
export const getUsers = (api: ApiClient): Promise<UserSummary[]> => api.get("/users/directory");
|
|
96
|
+
|
|
97
|
+
/** What the user has made or changed, the most recent first */
|
|
98
|
+
export const getUserItems = (
|
|
99
|
+
api: ApiClient,
|
|
100
|
+
username: string,
|
|
101
|
+
options: { page: number; pageSize: number },
|
|
102
|
+
): Promise<UserItems> =>
|
|
103
|
+
api.get(`/users/${encodeURIComponent(username)}/items`, { page: options.page, page_size: options.pageSize });
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { PageTitle, RequireLogin, useApi, useComponent, useTranslations, PageFrame, firstParam, Pager, readableTime } from "@pangloss/interface-core";
|
|
2
|
+
import { useNavigate, useParams, useSearchParams } from "@solidjs/router";
|
|
3
|
+
import { createMemo, Errored, For, Loading, Show } from "solid-js";
|
|
4
|
+
import { getUserItems } from "../api";
|
|
5
|
+
|
|
6
|
+
/** How many items a page has */
|
|
7
|
+
export const ITEMS_PAGE_SIZE = 25;
|
|
8
|
+
|
|
9
|
+
/** What one user has made or changed, a page of it */
|
|
10
|
+
function UserItemList(props: { username: string; page: number; onPage: (page: number) => void }) {
|
|
11
|
+
const ReferenceChip = useComponent("ReferenceChip");
|
|
12
|
+
const api = useApi();
|
|
13
|
+
const { t, language } = useTranslations();
|
|
14
|
+
const items = createMemo(() => getUserItems(api, props.username, { page: props.page, pageSize: ITEMS_PAGE_SIZE }));
|
|
15
|
+
const pages = () => Math.max(1, Math.ceil(items().count / ITEMS_PAGE_SIZE));
|
|
16
|
+
return (
|
|
17
|
+
<>
|
|
18
|
+
<PageTitle section={props.username} />
|
|
19
|
+
<h1 class="mb-6 flex items-baseline gap-3 border-b-[0.5px] border-slate-400/70 pt-1 pb-2 text-2xl font-semibold">
|
|
20
|
+
{items().user.username}
|
|
21
|
+
<Show when={items().user.fullName}>
|
|
22
|
+
<span class="text-base font-normal text-slate-600">{items().user.fullName}</span>
|
|
23
|
+
</Show>
|
|
24
|
+
</h1>
|
|
25
|
+
<Show
|
|
26
|
+
when={items().results.length > 0}
|
|
27
|
+
fallback={<p class="py-8 text-center text-slate-500">{t("users_no_items", "Nothing made or changed yet")}</p>}
|
|
28
|
+
>
|
|
29
|
+
<ul>
|
|
30
|
+
<For each={items().results}>
|
|
31
|
+
{(item) => (
|
|
32
|
+
<li
|
|
33
|
+
class="flex items-center justify-between gap-4 border-b-[0.5px] border-slate-300 py-1.5"
|
|
34
|
+
data-testid="user-item"
|
|
35
|
+
>
|
|
36
|
+
<div class="min-w-0">
|
|
37
|
+
<ReferenceChip
|
|
38
|
+
type={item.type}
|
|
39
|
+
label={item.label ?? item.id}
|
|
40
|
+
href={`/${item.type}/${encodeURIComponent(item.id)}`}
|
|
41
|
+
/>
|
|
42
|
+
</div>
|
|
43
|
+
<span class="shrink-0 text-xs text-slate-600">
|
|
44
|
+
<Show when={item.action === "created"} fallback={t("modified", "Modified")}>
|
|
45
|
+
{t("created", "Created")}
|
|
46
|
+
</Show>
|
|
47
|
+
{" · "}
|
|
48
|
+
{readableTime(item.when, language())}
|
|
49
|
+
</span>
|
|
50
|
+
</li>
|
|
51
|
+
)}
|
|
52
|
+
</For>
|
|
53
|
+
</ul>
|
|
54
|
+
<Pager page={props.page} pages={pages()} onPage={props.onPage} />
|
|
55
|
+
</Show>
|
|
56
|
+
</>
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A user and what they have made or changed, the most recent first: `/users/:username`, for someone logged in */
|
|
61
|
+
export default function UserPage() {
|
|
62
|
+
const Console = useComponent("Console");
|
|
63
|
+
const ErrorMessage = useComponent("ErrorMessage");
|
|
64
|
+
const navigate = useNavigate();
|
|
65
|
+
const params = useParams<{ username: string }>();
|
|
66
|
+
const [search, setSearch] = useSearchParams();
|
|
67
|
+
const { t } = useTranslations();
|
|
68
|
+
const page = () => Math.max(1, Number.parseInt(firstParam(search.page) ?? "1", 10) || 1);
|
|
69
|
+
return (
|
|
70
|
+
<RequireLogin onCancel={() => navigate("/", { replace: true })}>
|
|
71
|
+
<PageFrame>
|
|
72
|
+
<Console type={t("users_user", "User")} label={params.username} />
|
|
73
|
+
<Errored fallback={(error) => <ErrorMessage error={error()} />}>
|
|
74
|
+
<Loading fallback={<p>{t("loading", "Loading…")}</p>}>
|
|
75
|
+
<UserItemList
|
|
76
|
+
username={params.username}
|
|
77
|
+
page={page()}
|
|
78
|
+
onPage={(next) => setSearch({ page: next > 1 ? String(next) : undefined })}
|
|
79
|
+
/>
|
|
80
|
+
</Loading>
|
|
81
|
+
</Errored>
|
|
82
|
+
</PageFrame>
|
|
83
|
+
</RequireLogin>
|
|
84
|
+
);
|
|
85
|
+
}
|