@vite-hub/cms 0.5.1 → 0.6.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/dist/access.js CHANGED
@@ -1,62 +1,14 @@
1
+ import { t as createCmsRoles } from "./roles-B0I4QfG8.js";
1
2
  import * as betterAuthAccess from "better-auth/plugins/access";
2
3
  import * as betterAuthAdminAccess from "better-auth/plugins/admin/access";
3
4
  // src/access.ts
4
- const { createAccessControl } = betterAuthAccess;
5
- const { adminAc, defaultStatements, userAc } = betterAuthAdminAccess;
5
+ const cms = createCmsRoles(betterAuthAccess, betterAuthAdminAccess);
6
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
- };
7
+ const cmsStatements = cms.statements;
22
8
  /** 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
- ];
9
+ const cmsAc = cms.ac;
29
10
  /** 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
- };
11
+ const cmsRoles = cms.roles;
60
12
  /**
61
13
  * Better Auth `admin` plugin options with the CMS roles: `admin(cmsAccess)`. Users start with the `user` role, which
62
14
  * has no CMS permission. Give a user a role with the admin API, such as `auth.api.setRole()`, or in the Console.
@@ -67,20 +19,12 @@ const cmsAccess = {
67
19
  defaultRole: "user",
68
20
  adminRoles: ["admin"]
69
21
  };
70
- function isCmsRoleName(name) {
71
- return Object.hasOwn(cmsRoles, name);
72
- }
73
- function cmsRolesOf(user) {
74
- const role = user?.role;
75
- if (String(role) !== role) return [];
76
- return role.split(",").map((name) => name.trim()).filter(isCmsRoleName);
77
- }
78
22
  /**
79
23
  * Whether one of the CMS roles of `user` allows every action in `permissions`. A missing user, a user without a role,
80
24
  * and unknown roles are denied. It reads only `user.role`, so it needs no request.
81
25
  */
82
26
  function cmsCan(user, permissions) {
83
- return cmsRolesOf(user).some((name) => cmsRoles[name].authorize(permissions).success);
27
+ return cms.can(user, permissions);
84
28
  }
85
29
  /**
86
30
  * `defineContent({ authorize })` from the CMS roles: `content.can()` allows an action when a role of the user has
@@ -107,7 +51,7 @@ function cmsCommentsAuthorize(context) {
107
51
  * section with `content:read-draft` and the `comments` section with `comment:read`. A user without either gets `403`.
108
52
  */
109
53
  function cmsConsoleAuthorize(context) {
110
- if (cmsRolesOf(context.user).includes("admin")) return true;
54
+ if (cms.rolesOf(context.user).includes("admin")) return true;
111
55
  const sections = [];
112
56
  if (cmsCan(context.user, { content: ["read-draft"] })) sections.push("content");
113
57
  if (cmsCan(context.user, { comment: ["read"] })) sections.push("comments");
package/dist/index.js CHANGED
@@ -1,10 +1,15 @@
1
1
  import { a as cmsRenamedAccessProblem, i as cmsOptionsProblem, n as cmsOptionKeys, o as cmsStoreOf, r as cmsOptionProblem, s as cmsErrorDiagnostics, t as cmsGitHubDefaults } from "./options-C5YZwuGY.js";
2
+ import { t as createCmsRoles } from "./roles-B0I4QfG8.js";
2
3
  import { defineContentPreset } from "@vite-hub/content";
3
4
  // src/index.ts
4
- const cmsDefaultAuthorize = async (context) => {
5
- if (String(context.user?.role) !== context.user?.role) return false;
6
- const { cmsContentAuthorize } = await import("./access.js");
7
- return await cmsContentAuthorize(context);
5
+ let cmsRoleSet;
6
+ function loadCmsRoles() {
7
+ cmsRoleSet ??= Promise.all([import("better-auth/plugins/access"), import("better-auth/plugins/admin/access")]).then(([access, adminAccess]) => createCmsRoles(access, adminAccess));
8
+ return cmsRoleSet;
9
+ }
10
+ const cmsDefaultAuthorize = async ({ user, action }) => {
11
+ if (String(user?.role) !== user?.role) return false;
12
+ return (await loadCmsRoles()).can(user, { content: [action] });
8
13
  };
9
14
  const cms = defineContentPreset({
10
15
  name: "cms",
@@ -0,0 +1,73 @@
1
+ // src/roles.ts
2
+ /**
3
+ * Builds the CMS roles from the Better Auth modules that the caller loaded. The root entry loads `better-auth` with a
4
+ * dynamic import of the package, not of `./access.ts`: Nitro dev bundles the app into one file, and `./access.ts` would
5
+ * then load `better-auth` when the app starts, also in apps that do not install it.
6
+ */
7
+ function createCmsRoles(access, adminAccess) {
8
+ const statements = {
9
+ ...adminAccess.defaultStatements,
10
+ content: [
11
+ "read",
12
+ "read-draft",
13
+ "write",
14
+ "publish"
15
+ ],
16
+ comment: [
17
+ "read",
18
+ "create",
19
+ "resolve",
20
+ "delete"
21
+ ]
22
+ };
23
+ const ac = access.createAccessControl(statements);
24
+ const comments = [
25
+ "read",
26
+ "create",
27
+ "resolve"
28
+ ];
29
+ const roles = {
30
+ user: ac.newRole(adminAccess.userAc.statements),
31
+ viewer: ac.newRole({ content: ["read", "read-draft"] }),
32
+ commenter: ac.newRole({
33
+ content: ["read", "read-draft"],
34
+ comment: comments
35
+ }),
36
+ editor: ac.newRole({
37
+ content: [
38
+ "read",
39
+ "read-draft",
40
+ "write"
41
+ ],
42
+ comment: comments
43
+ }),
44
+ publisher: ac.newRole({
45
+ content: [
46
+ "read",
47
+ "read-draft",
48
+ "write",
49
+ "publish"
50
+ ],
51
+ comment: statements.comment
52
+ }),
53
+ admin: ac.newRole({
54
+ ...adminAccess.adminAc.statements,
55
+ content: statements.content,
56
+ comment: statements.comment
57
+ })
58
+ };
59
+ const isRoleName = (name) => Object.hasOwn(roles, name);
60
+ const rolesOf = (user) => {
61
+ const role = user?.role;
62
+ if (String(role) !== role) return [];
63
+ return role.split(",").map((name) => name.trim()).filter(isRoleName);
64
+ };
65
+ return {
66
+ statements,
67
+ ac,
68
+ roles,
69
+ rolesOf,
70
+ can: (user, permissions) => rolesOf(user).some((name) => roles[name].authorize(permissions).success)
71
+ };
72
+ }
73
+ export { createCmsRoles as t };
package/dist/vite.js CHANGED
@@ -108,7 +108,7 @@ new RegExp(String.raw`\b([\w-]*(?:${secretKeyPattern.source})[\w-]*["']?\s*[:=]\
108
108
  function isRecord(value) {
109
109
  return typeof value === "object" && value !== null && !Array.isArray(value);
110
110
  }
111
- // ../internal/dist/vite-CELQZdD3.js
111
+ // ../internal/dist/vite-D86Q14G4.js
112
112
  const projectRootDirectoryMarkers = [
113
113
  ["server", "agents"],
114
114
  ["server", "channels"],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vite-hub/cms",
3
- "version": "0.5.1",
3
+ "version": "0.6.0",
4
4
  "description": "Editable Content collections on a generated ViteHub Workspace, in Blob and Database or in a GitHub repository.",
5
5
  "keywords": [
6
6
  "cms",
@@ -46,18 +46,18 @@
46
46
  "./package.json": "./package.json"
47
47
  },
48
48
  "dependencies": {
49
- "@vite-hub/content": "0.5.1",
50
- "@vite-hub/source": "0.5.1",
49
+ "@vite-hub/content": "0.6.0",
50
+ "@vite-hub/source": "0.6.0",
51
51
  "nostics": "1.4.0"
52
52
  },
53
53
  "devDependencies": {
54
54
  "@comark/html": "^0.9.2",
55
55
  "@libsql/client": "^0.18.0",
56
56
  "@types/node": "^24.19.2",
57
- "@vite-hub/comments": "0.5.1",
58
- "@vite-hub/database": "0.5.1",
57
+ "@vite-hub/comments": "0.6.0",
58
+ "@vite-hub/database": "0.6.0",
59
59
  "@vite-hub/internal": "0.0.1",
60
- "@vite-hub/workspace": "0.5.1",
60
+ "@vite-hub/workspace": "0.6.0",
61
61
  "better-auth": "^1.7.27",
62
62
  "comark": "^0.9.2",
63
63
  "comark-ui": "^0.12.2",
@@ -71,10 +71,10 @@
71
71
  },
72
72
  "peerDependencies": {
73
73
  "@comark/html": "^0.9.2",
74
- "@vite-hub/blob": "0.5.1",
75
- "@vite-hub/comments": "0.5.1",
76
- "@vite-hub/database": "0.5.1",
77
- "@vite-hub/workspace": "0.5.1",
74
+ "@vite-hub/blob": "0.6.0",
75
+ "@vite-hub/comments": "0.6.0",
76
+ "@vite-hub/database": "0.6.0",
77
+ "@vite-hub/workspace": "0.6.0",
78
78
  "better-auth": "^1.7.27",
79
79
  "comark": "^0.9.2",
80
80
  "comark-ui": "^0.12.2",