@descix/platform-api 1.0.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.
@@ -0,0 +1,13 @@
1
+ /**
2
+ * MOVED. The owner of this surface is now `@descix/cloud-core/naming`.
3
+ *
4
+ * This file is a RE-EXPORT, not a copy, and that distinction is the point: during the move
5
+ * there must never be an instant at which two locations hold the same fact. A copy left here
6
+ * would be a second owner for as long as the companion repoint takes; a pointer is the same
7
+ * owner reached by an older name.
8
+ *
9
+ * It exists only until DeSciX_Cloud's imports are repointed at `@descix/cloud-core/naming`,
10
+ * after which this file and its subpath export are DELETED. That repoint is a companion row
11
+ * and is deliberately NOT part of this change.
12
+ */
13
+ export * from '@descix/cloud-core/naming';
@@ -0,0 +1,157 @@
1
+ /**
2
+ * @descix/platform-api - Permissions
3
+ *
4
+ * Pure role-based permission checks. No Google Groups dependency.
5
+ * Google Groups-based admin checks (isPlatformAdmin, isCommunityAdmin, isAppAdmin,
6
+ * checkUserPermission) remain in DeSciX_Cloud/microservice/services/permissionService.js
7
+ * as they depend on Cloud-specific infrastructure (googleGroupsService.js).
8
+ */
9
+
10
+ import { CacheFirestore, FirestoreCollections } from '@descix/cloud-core';
11
+
12
+ /**
13
+ * Role-based permission check using Firestore role definitions.
14
+ * Checks if a user has a specific permission via any of their assigned roles.
15
+ *
16
+ * @param {Object} user - The user object with a roles Map
17
+ * @param {string} requiredPermission - The permission string to check
18
+ * @param {Object} scopeContext - Context { communityId?, appId? } for scoped permissions
19
+ * @returns {Promise<boolean>} - True if the user has the permission via roles
20
+ */
21
+ export async function checkRoleBasedPermission(user, requiredPermission, scopeContext = {}) {
22
+ if (!user || !user.roles || user.roles.size === 0) {
23
+ return false;
24
+ }
25
+
26
+ const db = new CacheFirestore();
27
+
28
+ for (const [roleId] of user.roles.entries()) {
29
+ // Parse scope and ID from the roleId string
30
+ let roleDefScope = 'UNKNOWN';
31
+ let roleDefScopeId = null;
32
+ let roleDefRoleId = roleId;
33
+
34
+ const roleParts = roleId.split('_');
35
+ if (roleParts[0].toLowerCase() === 'platform') {
36
+ roleDefScope = 'PLATFORM';
37
+ roleDefScopeId = 'descix';
38
+ } else if (roleParts[0].toLowerCase() === 'community' && roleParts.length >= 3) {
39
+ roleDefScope = 'COMMUNITY';
40
+ roleDefScopeId = roleParts[1];
41
+ } else if (roleParts[0].toLowerCase() === 'app' && roleParts.length >= 3) {
42
+ roleDefScope = 'APP';
43
+ roleDefScopeId = roleParts[1];
44
+ } else {
45
+ continue;
46
+ }
47
+
48
+ try {
49
+ const collectionPath = FirestoreCollections.ROLES(roleDefScope, roleDefScopeId);
50
+ const roleData = await db.get_doc(collectionPath, roleDefRoleId);
51
+
52
+ if (roleData && roleData.permissions?.includes(requiredPermission)) {
53
+ const [roleCommunityId, roleAppId] = String(roleData.scope_id).split('/');
54
+ const hasPermissionInScope = () => {
55
+ switch (roleData.scope?.toUpperCase()) {
56
+ case 'PLATFORM':
57
+ return true;
58
+ case 'COMMUNITY':
59
+ return scopeContext.communityId === roleData.scope_id ||
60
+ (scopeContext.appId && scopeContext.communityId === roleData.scope_id);
61
+ case 'APP':
62
+ return scopeContext.communityId === roleCommunityId && scopeContext.appId === roleAppId;
63
+ default:
64
+ return false;
65
+ }
66
+ };
67
+
68
+ if (hasPermissionInScope()) {
69
+ return true;
70
+ }
71
+ }
72
+ } catch (error) {
73
+ console.error(`[checkRoleBasedPermission] Error fetching role definition for ${roleId}:`, error);
74
+ }
75
+ }
76
+
77
+ // NFT check for community management (backward compatibility)
78
+ if (requiredPermission === 'COMMUNITY_MANAGE_APPS' && scopeContext.communityId) {
79
+ if (user.nfts instanceof Map) {
80
+ for (const nft of user.nfts.values()) {
81
+ if (nft.community_id === scopeContext.communityId) {
82
+ return true;
83
+ }
84
+ }
85
+ } else if (user.nfts && typeof user.nfts === 'object') {
86
+ for (const nft of Object.values(user.nfts)) {
87
+ if (nft.community_id === scopeContext.communityId) {
88
+ return true;
89
+ }
90
+ }
91
+ }
92
+ }
93
+
94
+ return false;
95
+ }
96
+
97
+ /**
98
+ * Quick check if user has ANY admin access based on roles.
99
+ * This is the role-only version — Google Groups admin checks remain in Cloud.
100
+ *
101
+ * @param {Object} user - The user object
102
+ * @returns {Promise<boolean>} - True if user has any elevated role
103
+ */
104
+ export async function hasAnyAdminAccess(user) {
105
+ if (!user || !user.roles || user.roles.size === 0) {
106
+ return false;
107
+ }
108
+
109
+ for (const [roleId] of user.roles.entries()) {
110
+ if (roleId.toLowerCase().startsWith('platform_')) {
111
+ return true;
112
+ }
113
+ }
114
+
115
+ return false;
116
+ }
117
+
118
+ /**
119
+ * Get user's admin scope based on their assigned roles.
120
+ * Returns what level of admin access the user has from role assignments only.
121
+ * Google Groups admin checks are handled by Cloud's permissionService.js.
122
+ *
123
+ * @param {Object} user - The user object
124
+ * @param {string} communityId - Optional community context
125
+ * @param {string} appId - Optional app context
126
+ * @returns {Promise<Object>} - { isPlatformAdmin, isCommunityAdmin, isAppAdmin }
127
+ */
128
+ export async function getUserAdminScope(user, communityId = null, appId = null) {
129
+ const scope = {
130
+ isPlatformAdmin: false,
131
+ isCommunityAdmin: false,
132
+ isAppAdmin: false
133
+ };
134
+
135
+ if (!user || !user.roles || user.roles.size === 0) {
136
+ return scope;
137
+ }
138
+
139
+ for (const [roleId] of user.roles.entries()) {
140
+ const lower = roleId.toLowerCase();
141
+ if (lower.startsWith('platform_')) {
142
+ scope.isPlatformAdmin = true;
143
+ scope.isCommunityAdmin = true;
144
+ scope.isAppAdmin = true;
145
+ return scope;
146
+ }
147
+ if (communityId && lower.startsWith(`community_${communityId}_`)) {
148
+ scope.isCommunityAdmin = true;
149
+ scope.isAppAdmin = true;
150
+ }
151
+ if (communityId && appId && lower.startsWith(`app_${communityId}_${appId}_`)) {
152
+ scope.isAppAdmin = true;
153
+ }
154
+ }
155
+
156
+ return scope;
157
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * MOVED. The owner of this surface is now `@descix/cloud-core/service-bootstrap`.
3
+ *
4
+ * This file is a RE-EXPORT, not a copy, and that distinction is the point: during the move
5
+ * there must never be an instant at which two locations hold the same fact. A copy left here
6
+ * would be a second owner for as long as the companion repoint takes; a pointer is the same
7
+ * owner reached by an older name.
8
+ *
9
+ * It exists only until DeSciX_Cloud's imports are repointed at `@descix/cloud-core/service-bootstrap`,
10
+ * after which this file and its subpath export are DELETED. That repoint is a companion row
11
+ * and is deliberately NOT part of this change.
12
+ */
13
+ export * from '@descix/cloud-core/service-bootstrap';