@vite-hub/cms 0.0.0 → 0.3.1
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/LICENSE +201 -0
- package/README.md +75 -0
- package/dist/access.d.ts +69 -0
- package/dist/access.js +119 -0
- package/dist/index-eur9QR7_.d.ts +51 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +61 -0
- package/dist/options-CIxG4vtI.js +96 -0
- package/dist/vite.d.ts +374 -0
- package/dist/vite.js +960 -0
- package/package.json +103 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
# @vite-hub/cms
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@vite-hub/cms)
|
|
4
|
+
[](https://github.com/vite-hub/vitehub/blob/main/LICENSE)
|
|
5
|
+
[](https://vitehub.dev/docs)
|
|
6
|
+
|
|
7
|
+
`@vite-hub/cms` is the CMS of ViteHub. It stores the editable collections of [`@vite-hub/content`](https://www.npmjs.com/package/@vite-hub/content) without wiring: Content is the primitive, collections on a Workspace, and the CMS adds that Workspace and its Database tables.
|
|
8
|
+
|
|
9
|
+
## Turn it on
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// server/content.ts
|
|
13
|
+
import { defineContent } from "vite-hub/content"
|
|
14
|
+
import { documents } from "vite-hub/content/collections"
|
|
15
|
+
import cms from "vite-hub/cms"
|
|
16
|
+
import * as v from "valibot"
|
|
17
|
+
|
|
18
|
+
export default defineContent({
|
|
19
|
+
extends: cms,
|
|
20
|
+
collections: { posts: documents({ path: "posts/**/*.md", fields: { title: v.string() } }) },
|
|
21
|
+
})
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
[`vite-hub`](https://www.npmjs.com/package/vite-hub) then turns on `workspace`, `database`, and `blob` if you left them unset, and registers `hubCms()`. A setting you pass wins, and `false` keeps a feature off. Without `vite-hub`, import `cms` from `@vite-hub/cms` and add `hubCms()` from `@vite-hub/cms/vite` next to the Workspace, Database, and Blob plugins.
|
|
25
|
+
|
|
26
|
+
At build time `hubCms()` reads `server/content.ts` without running it, so import the preset from `vite-hub/cms` or `@vite-hub/cms`, pass it as `extends: cms` or `extends: [cms, { ... }]`, and give string literal options. It then:
|
|
27
|
+
|
|
28
|
+
- writes the Workspace Definition to `.vitehub/content/workspace.mjs` and contributes it as the `content` Workspace (option `workspace`);
|
|
29
|
+
- adds the `content-history` tables (Blob + Database store) and the `content-sessions` table to the default or only Database (option `database`);
|
|
30
|
+
- uses the implicit `default` Database of `@vite-hub/database` when the app has no Database Definition;
|
|
31
|
+
- checks that Workspace, Database, and Blob are enabled and that the Blob store exists (option `blob`, default `default`);
|
|
32
|
+
- restarts the dev server when the selection in `server/content.ts` changes.
|
|
33
|
+
|
|
34
|
+
## GitHub store
|
|
35
|
+
|
|
36
|
+
`extends: [cms, { repo: "owner/name" }]` keeps documents in a Git repository. `repo` selects the GitHub store; `store: "github"` without `repo` reads `VITEHUB_WORKSPACE_GITHUB_REPOSITORY` when first used. Published content is on `branch` (default `main`) and drafts on `draftBranch` (default `vitehub/content`), created by the first save. `root` (default `""`, the repository root) is the folder that holds the collection paths. `publish: "commit"` (default) commits to `branch`, and `publish: "pr"` opens a pull request. With `defineContent({ previews: true })` and a `github()` Source of the same repository, the publish result has `previews`: a signed `/pr/<number>/<url>` link of each document, valid for one hour, because publish branches are private. The GitHub store needs a Database for editor sessions and no Blob store. It cannot schedule publishes, so the Console does not ask for a `content.publishDue()` runner.
|
|
37
|
+
|
|
38
|
+
## Roles
|
|
39
|
+
|
|
40
|
+
`@vite-hub/cms/access` (`vite-hub/cms/access`) has the CMS roles for the Better Auth `admin` plugin. It needs `better-auth`; the root entry does not import it.
|
|
41
|
+
|
|
42
|
+
```ts
|
|
43
|
+
// server/auth.ts
|
|
44
|
+
import { admin } from "better-auth/plugins"
|
|
45
|
+
import { defineAuth } from "vite-hub/auth"
|
|
46
|
+
import { authDatabase } from "vite-hub/auth/database"
|
|
47
|
+
import { cmsAccess } from "vite-hub/cms/access"
|
|
48
|
+
|
|
49
|
+
export default defineAuth({ database: authDatabase(), plugins: [admin(cmsAccess)] })
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
| Role | Content | Comments |
|
|
53
|
+
| --- | --- | --- |
|
|
54
|
+
| `user` (default) | None | None |
|
|
55
|
+
| `viewer` | `read`, `read-draft` | None |
|
|
56
|
+
| `commenter` | `read`, `read-draft` | `read`, `create`, `resolve` |
|
|
57
|
+
| `editor` | `read`, `read-draft`, `write` | `read`, `create`, `resolve` |
|
|
58
|
+
| `publisher` | `read`, `read-draft`, `write`, `publish` | `read`, `create`, `resolve`, `delete` |
|
|
59
|
+
| `admin` | Everything, with the Better Auth admin permissions | Everything |
|
|
60
|
+
|
|
61
|
+
`user.role` holds one role, or several separated by commas. `cmsCan(user, { content: ["publish"] })` checks it without a request; a missing user or an unknown role is denied. Opt into the role policy with `extends: [cms, { access: "roles" }]` or `authorize: cmsContentAuthorize`. With `extends: cms` alone, every action stays denied, including for unrelated application roles. Your own `authorize` overrides the preset policy. `cmsCommentsAuthorize` and `cmsConsoleAuthorize` give Comments and the Console the same roles. `vitehub cms roles` prints the table. The role policy also decides `read`, the `/api/content/**` route, so visitors without a role get `401`. Set `defineContent({ access: "public" })` when visitors read that route. The roles still decide drafts, writes, and publishing.
|
|
62
|
+
|
|
63
|
+
## Inspect
|
|
64
|
+
|
|
65
|
+
`usesCms({ projectRoot, serverDirs })` and `cmsStore(...)` from `@vite-hub/cms/vite` read `server/content.ts` the same way, without running it. `vitehub inspect definitions --kind database` lists the tables that the CMS contributes, with `@vite-hub/cms/vite` as the contributor, and names it as a reason for the implicit `default` Database. `vitehub inspect definitions --kind cms` shows the store, the Workspace, and whether access uses the CMS roles, the app's `authorize`, or no policy.
|
|
66
|
+
|
|
67
|
+
## Errors
|
|
68
|
+
|
|
69
|
+
| Code | Meaning |
|
|
70
|
+
| --- | --- |
|
|
71
|
+
| `CONTENT_B0001` | The build cannot read the preset selection, or an option is unknown or not valid. |
|
|
72
|
+
| `CONTENT_B0002` | The app lacks something the preset needs: Workspace, Database, or Blob is off, the Database choice is ambiguous, or a named Database or Blob store does not exist. |
|
|
73
|
+
| `CONTENT_CMS_OPTION_INVALID` | An option passed to `defineContent({ extends: [cms, { ... }] })` is not valid at runtime. |
|
|
74
|
+
|
|
75
|
+
See the [CMS docs](https://vitehub.dev/docs/cms).
|
package/dist/access.d.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { AccessControl, Role, RoleAuthorizeRequest, Statements } from "better-auth/plugins/access";
|
|
2
|
+
import * as betterAuthAdminAccess from "better-auth/plugins/admin/access";
|
|
3
|
+
import { ContentAuthorize } from "@vite-hub/content";
|
|
4
|
+
import { CommentsAuthorizeContext } from "@vite-hub/comments";
|
|
5
|
+
//#region src/access.d.ts
|
|
6
|
+
/**
|
|
7
|
+
* The permissions of the Better Auth `admin` plugin, `user` and `session`, plus the CMS resources: `content` for the
|
|
8
|
+
* actions of `content.can()`, and `comment` for Comments. `comment:delete` removes threads of other people.
|
|
9
|
+
*/
|
|
10
|
+
export type CmsStatements = typeof betterAuthAdminAccess.defaultStatements & {
|
|
11
|
+
readonly content: readonly ["read", "read-draft", "write", "publish"];
|
|
12
|
+
readonly comment: readonly ["read", "create", "resolve", "delete"];
|
|
13
|
+
};
|
|
14
|
+
/** The statements of `cmsAc`. */
|
|
15
|
+
export declare const cmsStatements: CmsStatements;
|
|
16
|
+
/** The access control of the CMS roles, for `admin({ ac })` of Better Auth. */
|
|
17
|
+
export declare const cmsAc: AccessControl<CmsStatements>;
|
|
18
|
+
/**
|
|
19
|
+
* A CMS role. `user` is the default role of Better Auth and has no CMS permission. Each of `viewer`, `commenter`,
|
|
20
|
+
* `editor`, and `publisher` has the permissions of the role before it, and `admin` has every permission.
|
|
21
|
+
*/
|
|
22
|
+
export type CmsRoleName = "user" | "viewer" | "commenter" | "editor" | "publisher" | "admin";
|
|
23
|
+
/** A Better Auth role on `cmsAc`. */
|
|
24
|
+
export type CmsRole = Role<Statements, CmsStatements>;
|
|
25
|
+
/** What `cmsCan()` checks, such as `{ content: ["publish"] }`. Several actions must all be allowed. */
|
|
26
|
+
export type CmsPermissions = RoleAuthorizeRequest<CmsStatements>;
|
|
27
|
+
/** A signed-in user, such as the Better Auth session user. The `admin` plugin keeps its roles in `role`. */
|
|
28
|
+
export type CmsUser = Readonly<Record<string, unknown>>;
|
|
29
|
+
/** The CMS roles, for `admin({ roles })` of Better Auth. */
|
|
30
|
+
export declare const cmsRoles: Readonly<Record<CmsRoleName, CmsRole>>;
|
|
31
|
+
/** The options of the Better Auth `admin` plugin with the CMS roles. */
|
|
32
|
+
export interface CmsAccessOptions {
|
|
33
|
+
ac: AccessControl<CmsStatements>;
|
|
34
|
+
roles: Readonly<Record<CmsRoleName, CmsRole>>;
|
|
35
|
+
/** New users get `user`, which has no CMS permission. */
|
|
36
|
+
defaultRole: "user";
|
|
37
|
+
adminRoles: string[];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Better Auth `admin` plugin options with the CMS roles: `admin(cmsAccess)`. Users start with the `user` role, which
|
|
41
|
+
* has no CMS permission. Give a user a role with the admin API, such as `auth.api.setRole()`, or in the Console.
|
|
42
|
+
*/
|
|
43
|
+
export declare const cmsAccess: CmsAccessOptions;
|
|
44
|
+
/**
|
|
45
|
+
* Whether one of the CMS roles of `user` allows every action in `permissions`. A missing user, a user without a role,
|
|
46
|
+
* and unknown roles are denied. It reads only `user.role`, so it needs no request.
|
|
47
|
+
*/
|
|
48
|
+
export declare function cmsCan(user: CmsUser | null | undefined, permissions: CmsPermissions): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* `defineContent({ authorize })` from the CMS roles: `content.can()` allows an action when a role of the user has
|
|
51
|
+
* `content:<action>`. `defineContent({ extends: [cms, { access: "roles" }] })` uses it when the app sets no `authorize`.
|
|
52
|
+
* Only CMS roles have `content:read`, so `/api/content/**` needs `defineContent({ access: "public" })` to serve visitors.
|
|
53
|
+
*/
|
|
54
|
+
export declare const cmsContentAuthorize: ContentAuthorize;
|
|
55
|
+
/**
|
|
56
|
+
* `defineComments({ authorize })` from the CMS roles: `read` needs `comment:read`, `write` needs `comment:create`,
|
|
57
|
+
* `resolve` needs `comment:resolve`, and deleting others' comments or threads needs `comment:delete`.
|
|
58
|
+
*/
|
|
59
|
+
export declare function cmsCommentsAuthorize(context: Pick<CommentsAuthorizeContext, "user" | "action">): boolean;
|
|
60
|
+
/**
|
|
61
|
+
* `defineConsoleAuthorize()` from the CMS roles. `admin` uses every Console section. Other roles use the `content`
|
|
62
|
+
* section with `content:read-draft` and the `comments` section with `comment:read`. A user without either gets `403`.
|
|
63
|
+
*/
|
|
64
|
+
export declare function cmsConsoleAuthorize(context: {
|
|
65
|
+
user?: CmsUser | null;
|
|
66
|
+
}): true | {
|
|
67
|
+
sections: string[];
|
|
68
|
+
};
|
|
69
|
+
//#endregion
|
package/dist/access.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import * as betterAuthAccess from "better-auth/plugins/access";
|
|
2
|
+
import * as betterAuthAdminAccess from "better-auth/plugins/admin/access";
|
|
3
|
+
//#region src/access.ts
|
|
4
|
+
const { createAccessControl } = betterAuthAccess;
|
|
5
|
+
const { adminAc, defaultStatements, userAc } = betterAuthAdminAccess;
|
|
6
|
+
/** The statements of `cmsAc`. */
|
|
7
|
+
const cmsStatements = {
|
|
8
|
+
...defaultStatements,
|
|
9
|
+
content: [
|
|
10
|
+
"read",
|
|
11
|
+
"read-draft",
|
|
12
|
+
"write",
|
|
13
|
+
"publish"
|
|
14
|
+
],
|
|
15
|
+
comment: [
|
|
16
|
+
"read",
|
|
17
|
+
"create",
|
|
18
|
+
"resolve",
|
|
19
|
+
"delete"
|
|
20
|
+
]
|
|
21
|
+
};
|
|
22
|
+
/** The access control of the CMS roles, for `admin({ ac })` of Better Auth. */
|
|
23
|
+
const cmsAc = createAccessControl(cmsStatements);
|
|
24
|
+
const comments = [
|
|
25
|
+
"read",
|
|
26
|
+
"create",
|
|
27
|
+
"resolve"
|
|
28
|
+
];
|
|
29
|
+
/** The CMS roles, for `admin({ roles })` of Better Auth. */
|
|
30
|
+
const cmsRoles = {
|
|
31
|
+
user: cmsAc.newRole(userAc.statements),
|
|
32
|
+
viewer: cmsAc.newRole({ content: ["read", "read-draft"] }),
|
|
33
|
+
commenter: cmsAc.newRole({
|
|
34
|
+
content: ["read", "read-draft"],
|
|
35
|
+
comment: comments
|
|
36
|
+
}),
|
|
37
|
+
editor: cmsAc.newRole({
|
|
38
|
+
content: [
|
|
39
|
+
"read",
|
|
40
|
+
"read-draft",
|
|
41
|
+
"write"
|
|
42
|
+
],
|
|
43
|
+
comment: comments
|
|
44
|
+
}),
|
|
45
|
+
publisher: cmsAc.newRole({
|
|
46
|
+
content: [
|
|
47
|
+
"read",
|
|
48
|
+
"read-draft",
|
|
49
|
+
"write",
|
|
50
|
+
"publish"
|
|
51
|
+
],
|
|
52
|
+
comment: cmsStatements.comment
|
|
53
|
+
}),
|
|
54
|
+
admin: cmsAc.newRole({
|
|
55
|
+
...adminAc.statements,
|
|
56
|
+
content: cmsStatements.content,
|
|
57
|
+
comment: cmsStatements.comment
|
|
58
|
+
})
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* Better Auth `admin` plugin options with the CMS roles: `admin(cmsAccess)`. Users start with the `user` role, which
|
|
62
|
+
* has no CMS permission. Give a user a role with the admin API, such as `auth.api.setRole()`, or in the Console.
|
|
63
|
+
*/
|
|
64
|
+
const cmsAccess = {
|
|
65
|
+
ac: cmsAc,
|
|
66
|
+
roles: cmsRoles,
|
|
67
|
+
defaultRole: "user",
|
|
68
|
+
adminRoles: ["admin"]
|
|
69
|
+
};
|
|
70
|
+
function isCmsRoleName(name) {
|
|
71
|
+
return Object.hasOwn(cmsRoles, name);
|
|
72
|
+
}
|
|
73
|
+
/** The known CMS roles of a user. `role` holds one role, or several separated by commas, as Better Auth stores them. */
|
|
74
|
+
function cmsRolesOf(user) {
|
|
75
|
+
const role = user?.role;
|
|
76
|
+
if (String(role) !== role) return [];
|
|
77
|
+
return role.split(",").map((name) => name.trim()).filter(isCmsRoleName);
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Whether one of the CMS roles of `user` allows every action in `permissions`. A missing user, a user without a role,
|
|
81
|
+
* and unknown roles are denied. It reads only `user.role`, so it needs no request.
|
|
82
|
+
*/
|
|
83
|
+
function cmsCan(user, permissions) {
|
|
84
|
+
return cmsRolesOf(user).some((name) => cmsRoles[name].authorize(permissions).success);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* `defineContent({ authorize })` from the CMS roles: `content.can()` allows an action when a role of the user has
|
|
88
|
+
* `content:<action>`. `defineContent({ extends: [cms, { access: "roles" }] })` uses it when the app sets no `authorize`.
|
|
89
|
+
* Only CMS roles have `content:read`, so `/api/content/**` needs `defineContent({ access: "public" })` to serve visitors.
|
|
90
|
+
*/
|
|
91
|
+
const cmsContentAuthorize = ({ user, action }) => cmsCan(user, { content: [action] });
|
|
92
|
+
/** The `comment` permission of each Comments action. Only `publisher` and `admin` delete. */
|
|
93
|
+
const commentPermissions = {
|
|
94
|
+
read: "read",
|
|
95
|
+
write: "create",
|
|
96
|
+
resolve: "resolve",
|
|
97
|
+
delete: "delete"
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
100
|
+
* `defineComments({ authorize })` from the CMS roles: `read` needs `comment:read`, `write` needs `comment:create`,
|
|
101
|
+
* `resolve` needs `comment:resolve`, and deleting others' comments or threads needs `comment:delete`.
|
|
102
|
+
*/
|
|
103
|
+
function cmsCommentsAuthorize(context) {
|
|
104
|
+
if (!Object.hasOwn(commentPermissions, context.action)) return false;
|
|
105
|
+
return cmsCan(context.user, { comment: [commentPermissions[context.action]] });
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* `defineConsoleAuthorize()` from the CMS roles. `admin` uses every Console section. Other roles use the `content`
|
|
109
|
+
* section with `content:read-draft` and the `comments` section with `comment:read`. A user without either gets `403`.
|
|
110
|
+
*/
|
|
111
|
+
function cmsConsoleAuthorize(context) {
|
|
112
|
+
if (cmsRolesOf(context.user).includes("admin")) return true;
|
|
113
|
+
const sections = [];
|
|
114
|
+
if (cmsCan(context.user, { content: ["read-draft"] })) sections.push("content");
|
|
115
|
+
if (cmsCan(context.user, { comment: ["read"] })) sections.push("comments");
|
|
116
|
+
return { sections };
|
|
117
|
+
}
|
|
118
|
+
//#endregion
|
|
119
|
+
export { cmsAc, cmsAccess, cmsCan, cmsCommentsAuthorize, cmsConsoleAuthorize, cmsContentAuthorize, cmsRoles, cmsStatements };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { ContentPreset } from "@vite-hub/content";
|
|
2
|
+
//#region src/index.d.ts
|
|
3
|
+
/** Options of the `cms` Content preset. The build reads them from `server/content.ts`, so pass string literals. */
|
|
4
|
+
interface CmsPresetOptions {
|
|
5
|
+
/** Opt into the CMS roles of `admin(cmsAccess)`. Without it, set `defineContent({ authorize })` to allow access. */
|
|
6
|
+
access?: "roles";
|
|
7
|
+
/**
|
|
8
|
+
* Database that receives the Workspace history tables, and with the GitHub store only the editor sessions table.
|
|
9
|
+
* Defaults to the Database named `default`, or the only one. Without a Database Definition, the tables go to the
|
|
10
|
+
* implicit `default` Database.
|
|
11
|
+
*/
|
|
12
|
+
database?: string;
|
|
13
|
+
/** Blob store that holds file contents. Defaults to `default`. Not for the GitHub store. */
|
|
14
|
+
blob?: string;
|
|
15
|
+
/** Name of the Workspace that the preset adds. Defaults to `content`. */
|
|
16
|
+
workspace?: string;
|
|
17
|
+
/** Where documents live. `repo` selects `"github"`, which keeps them in a repository; otherwise `"blob-database"`. */
|
|
18
|
+
store?: "blob-database" | "github";
|
|
19
|
+
/**
|
|
20
|
+
* The repository, as `owner/name`. Setting it selects the GitHub store. A GitHub store without it (`store: "github"`)
|
|
21
|
+
* reads `VITEHUB_WORKSPACE_GITHUB_REPOSITORY` when first used.
|
|
22
|
+
*/
|
|
23
|
+
repo?: string;
|
|
24
|
+
/** GitHub store: the published branch. Defaults to `main`. */
|
|
25
|
+
branch?: string;
|
|
26
|
+
/** GitHub store: the branch that holds drafts. Defaults to `vitehub/content`. It is created at the first save. */
|
|
27
|
+
draftBranch?: string;
|
|
28
|
+
/**
|
|
29
|
+
* GitHub store: the folder of the repository that holds the collections' paths. Defaults to `""`, the repository
|
|
30
|
+
* root, where a collection path such as `content/posts/**\/*.md` starts at the root.
|
|
31
|
+
*/
|
|
32
|
+
root?: string;
|
|
33
|
+
/** GitHub store: how a publish reaches `branch`. `"commit"` (default) commits to it. `"pr"` opens a pull request from a `vitehub/publish/*` branch, which only Console access or a signed link previews. */
|
|
34
|
+
publish?: "commit" | "pr";
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Store editable collections without wiring. `vitehub()` turns on Workspace, Database, and Blob when they are unset.
|
|
38
|
+
* At build time `hubCms()` from `@vite-hub/cms/vite` adds a Workspace named `content` on the Blob + Database Store,
|
|
39
|
+
* using the `default` Blob store and the default or only Database, and adds the Workspace history tables to that
|
|
40
|
+
* Database. When the app has no Database Definition, that is the implicit `default` Database. `defineContent({ extends: cms })`
|
|
41
|
+
* then uses that Workspace.
|
|
42
|
+
*
|
|
43
|
+
* With `repo` (or `store: "github"`) the Workspace is a GitHub repository instead: published content on one branch, drafts on
|
|
44
|
+
* another, and no Blob store. The Database keeps editor sessions.
|
|
45
|
+
*
|
|
46
|
+
* With `access: "roles"` and no `authorize` in `defineContent()`, the CMS roles of the Better Auth `admin` plugin decide
|
|
47
|
+
* who reads drafts, writes, and publishes. See `cmsAccess` from `@vite-hub/cms/access`.
|
|
48
|
+
*/
|
|
49
|
+
declare const cms: ContentPreset<CmsPresetOptions>;
|
|
50
|
+
//#endregion
|
|
51
|
+
export { cms as n, CmsPresetOptions as t };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { a as cmsStoreOf, i as cmsOptionsProblem, n as cmsOptionKeys, o as cmsErrorDiagnostics, r as cmsOptionProblem, t as cmsGitHubDefaults } from "./options-CIxG4vtI.js";
|
|
2
|
+
import { defineContentPreset } from "@vite-hub/content";
|
|
3
|
+
//#region src/index.ts
|
|
4
|
+
/**
|
|
5
|
+
* The policy for `access: "roles"` when the app sets no `authorize`. A user without a string role is denied before
|
|
6
|
+
* Better Auth loads. The preset never interprets application roles unless the app explicitly opts in.
|
|
7
|
+
*/
|
|
8
|
+
const cmsDefaultAuthorize = async (context) => {
|
|
9
|
+
if (String(context.user?.role) !== context.user?.role) return false;
|
|
10
|
+
const { cmsContentAuthorize } = await import("./access.js");
|
|
11
|
+
return await cmsContentAuthorize(context);
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Store editable collections without wiring. `vitehub()` turns on Workspace, Database, and Blob when they are unset.
|
|
15
|
+
* At build time `hubCms()` from `@vite-hub/cms/vite` adds a Workspace named `content` on the Blob + Database Store,
|
|
16
|
+
* using the `default` Blob store and the default or only Database, and adds the Workspace history tables to that
|
|
17
|
+
* Database. When the app has no Database Definition, that is the implicit `default` Database. `defineContent({ extends: cms })`
|
|
18
|
+
* then uses that Workspace.
|
|
19
|
+
*
|
|
20
|
+
* With `repo` (or `store: "github"`) the Workspace is a GitHub repository instead: published content on one branch, drafts on
|
|
21
|
+
* another, and no Blob store. The Database keeps editor sessions.
|
|
22
|
+
*
|
|
23
|
+
* With `access: "roles"` and no `authorize` in `defineContent()`, the CMS roles of the Better Auth `admin` plugin decide
|
|
24
|
+
* who reads drafts, writes, and publishes. See `cmsAccess` from `@vite-hub/cms/access`.
|
|
25
|
+
*/
|
|
26
|
+
const cms = defineContentPreset({
|
|
27
|
+
name: "cms",
|
|
28
|
+
options: {
|
|
29
|
+
access: void 0,
|
|
30
|
+
blob: "default",
|
|
31
|
+
branch: void 0,
|
|
32
|
+
database: void 0,
|
|
33
|
+
draftBranch: void 0,
|
|
34
|
+
publish: void 0,
|
|
35
|
+
repo: void 0,
|
|
36
|
+
root: void 0,
|
|
37
|
+
store: void 0,
|
|
38
|
+
workspace: "content"
|
|
39
|
+
},
|
|
40
|
+
configure(options) {
|
|
41
|
+
for (const key of cmsOptionKeys) {
|
|
42
|
+
const problem = cmsOptionProblem(key, Reflect.get(options, key));
|
|
43
|
+
if (problem) throw cmsErrorDiagnostics.CONTENT_CMS_OPTION_INVALID({ problem: `option ${JSON.stringify(key)} ${problem}` });
|
|
44
|
+
}
|
|
45
|
+
const github = cmsStoreOf(options) === "github";
|
|
46
|
+
const set = {
|
|
47
|
+
...options,
|
|
48
|
+
blob: github && options.blob === "default" ? void 0 : options.blob
|
|
49
|
+
};
|
|
50
|
+
const problem = cmsOptionsProblem(set);
|
|
51
|
+
if (problem) throw cmsErrorDiagnostics.CONTENT_CMS_OPTION_INVALID({ problem });
|
|
52
|
+
return {
|
|
53
|
+
workspace: options.workspace,
|
|
54
|
+
...options.access === "roles" ? { authorize: cmsDefaultAuthorize } : {},
|
|
55
|
+
...github && options.publish === "pr" ? { publish: "pr" } : {},
|
|
56
|
+
...github ? { privateBranches: [options.draftBranch ?? cmsGitHubDefaults.draftBranch] } : {}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
//#endregion
|
|
61
|
+
export { cms, cms as default };
|