source: Klonkt/src/services/PermissionsService.js@ b05eb97

main
Last change on this file since b05eb97 was 834bcc3, checked in by Robin Genis <roboburr@…>, 3 months ago

i18n: translate Dutch code comments to English across src/

Comments in routes/services/views/config/middleware/assets translated to
English for the public repo. A few dev-facing throw/console message strings
were Englished too. No user-facing UI strings or i18n dictionary values changed
(src/services/i18n.js untouched). Logic unchanged.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 3.3 KB
Line 
1/**
2 * PermissionsService — Inline permission checks
3 * Used in templates to show/hide edit buttons, delete buttons, etc.
4 */
5
6import db from '../config/database.js';
7
8class PermissionsService {
9 /**
10 * Check if user can edit a post
11 */
12 static canEditPost(user, post, site) {
13 if (!user) return false;
14 if (user.role === 'god') return true; // Gods can edit anything
15 if (user.id === post.author_id) return true; // Authors can edit their own
16 if (this.canAdminSite(user, site)) return true; // Site admins can edit
17 return false;
18 }
19
20 /**
21 * Check if user can delete a post
22 */
23 static canDeletePost(user, post, site) {
24 if (!user) return false;
25 if (user.role === 'god') return true;
26 if (user.id === post.author_id) return true;
27 if (this.canAdminSite(user, site)) return true;
28 return false;
29 }
30
31 /**
32 * Check if user can publish a post
33 */
34 static canPublishPost(user, post, site) {
35 if (!user) return false;
36 if (user.role === 'god') return true;
37 if (user.id === post.author_id) return true;
38 if (this.canAdminSite(user, site)) return true;
39 return false;
40 }
41
42 /**
43 * Check if user can create a post on this site
44 */
45 static canCreatePost(user, site) {
46 if (!user) return false;
47 if (user.role === 'god') return true;
48 if (!site) return false; // no site context (e.g. hub landing) -> nothing to post to
49 if (user.id === site.owner_id) return true; // Site owner
50 if (this.canAdminSite(user, site)) return true;
51 return false;
52 }
53
54 /**
55 * Check if user can admin a site
56 */
57 static canAdminSite(user, site) {
58 if (!user || !site) return false;
59 if (user.role === 'god') return true;
60 if (user.id === site.owner_id) return true;
61 // Assigned co-admin (collaborator) via site_members. Previously read from
62 // a never-populated user.siteRoles → dead code; now queried directly on
63 // the table (a few checks per page, indexed = cheap).
64 return !!db.prepare(
65 "SELECT 1 FROM site_members WHERE site_id = ? AND user_id = ? AND role = 'admin' LIMIT 1"
66 ).get(site.id, user.id);
67 }
68
69 /**
70 * Check if user can comment on a post
71 */
72 static canComment(user, site, post) {
73 if (!user && site.require_login_to_comment) return false;
74 if (!user) return true; // Anonymous allowed if not required
75 return true; // Logged-in users can always comment
76 }
77
78 /**
79 * Check if user can delete a comment
80 */
81 static canDeleteComment(user, comment, site) {
82 if (!user) return false;
83 if (user.role === 'god') return true;
84 if (user.id === comment.author_id) return true;
85 if (this.canAdminSite(user, site)) return true;
86 return false;
87 }
88
89 /**
90 * Check if user can send DMs
91 */
92 static canSendDM(user) {
93 return user !== null; // Only logged-in users
94 }
95
96 /**
97 * Check if user can configure site settings
98 */
99 static canConfigureSite(user, site) {
100 if (!user) return false;
101 if (user.role === 'god') return true;
102 if (user.id === site.owner_id) return true;
103 return false;
104 }
105
106 /**
107 * Check if user can manage users for a site
108 */
109 static canManageUsers(user, site) {
110 if (!user) return false;
111 if (user.role === 'god') return true;
112 if (user.id === site.owner_id) return true;
113 return false;
114 }
115}
116
117export default PermissionsService;
Note: See TracBrowser for help on using the repository browser.