@@ -10,37 +10,86 @@ import {
10
10
PermissionsInput ,
11
11
Role ,
12
12
RolesPermission ,
13
- User ,
14
13
} from './types.js' ;
15
14
import { Map } from './util.js' ;
16
15
17
16
export interface Roles {
17
+ /**
18
+ * Retrieve all the roles in the system.
19
+ *
20
+ * @returns {Promise<Record<string, Role>> } A map of role names to their respective roles.
21
+ */
18
22
listAll : ( ) => Promise < Record < string , Role > > ;
19
- ofCurrentUser : ( ) => Promise < Record < string , Role > > ;
23
+ /**
24
+ * Retrieve a role by its name.
25
+ *
26
+ * @param {string } roleName The name of the role to retrieve.
27
+ * @returns {Promise<Role | null> } The role if it exists, or null if it does not.
28
+ */
20
29
byName : ( roleName : string ) => Promise < Role | null > ;
21
- byUser : ( user : string ) => Promise < Record < string , Role > > ;
22
- assignedUsers : ( roleName : string ) => Promise < Record < string , User > > ;
30
+ /**
31
+ * Retrieve the user IDs assigned to a role.
32
+ *
33
+ * @param {string } roleName The name of the role to retrieve the assigned user IDs for.
34
+ * @returns {Promise<string[]> } The user IDs assigned to the role.
35
+ */
36
+ assignedUserIds : ( roleName : string ) => Promise < string [ ] > ;
37
+ /**
38
+ * Delete a role by its name.
39
+ *
40
+ * @param {string } roleName The name of the role to delete.
41
+ * @returns {Promise<void> } A promise that resolves when the role is deleted.
42
+ */
23
43
delete : ( roleName : string ) => Promise < void > ;
44
+ /**
45
+ * Create a new role.
46
+ *
47
+ * @param {string } roleName The name of the new role.
48
+ * @param {PermissionsInput } permissions The permissions to assign to the new role.
49
+ * @returns {Promise<Role> } The newly created role.
50
+ */
24
51
create : ( roleName : string , permissions : PermissionsInput ) => Promise < Role > ;
25
- assignToUser : ( roleNames : string | string [ ] , user : string ) => Promise < void > ;
52
+ /**
53
+ * Check if a role exists.
54
+ *
55
+ * @param {string } roleName The name of the role to check for.
56
+ * @returns {Promise<boolean> } A promise that resolves to true if the role exists, or false if it does not.
57
+ */
26
58
exists : ( roleName : string ) => Promise < boolean > ;
27
- revokeFromUser : ( roleNames : string | string [ ] , user : string ) => Promise < void > ;
59
+ /**
60
+ * Add permissions to a role.
61
+ *
62
+ * @param {string } roleName The name of the role to add permissions to.
63
+ * @param {PermissionsInput } permissions The permissions to add.
64
+ * @returns {Promise<void> } A promise that resolves when the permissions are added.
65
+ */
28
66
addPermissions : ( roleName : string , permissions : PermissionsInput ) => Promise < void > ;
67
+ /**
68
+ * Remove permissions from a role.
69
+ *
70
+ * @param {string } roleName The name of the role to remove permissions from.
71
+ * @param {PermissionsInput } permissions The permissions to remove.
72
+ * @returns {Promise<void> } A promise that resolves when the permissions are removed.
73
+ */
29
74
removePermissions : ( roleName : string , permissions : PermissionsInput ) => Promise < void > ;
30
- hasPermission : ( roleName : string , permission : Permission ) => Promise < boolean > ;
75
+ /**
76
+ * Check if a role has the specified permissions.
77
+ *
78
+ * @param {string } roleName The name of the role to check.
79
+ * @param {Permission | Permission[] } permission The permission or permissions to check for.
80
+ * @returns {Promise<boolean> } A promise that resolves to true if the role has the permissions, or false if it does not.
81
+ */
82
+ hasPermissions : ( roleName : string , permission : Permission | Permission [ ] ) => Promise < boolean > ;
31
83
}
32
84
33
85
const roles = ( connection : ConnectionREST ) : Roles => {
34
86
return {
35
87
listAll : ( ) => connection . get < WeaviateRole [ ] > ( '/authz/roles' ) . then ( Map . roles ) ,
36
- ofCurrentUser : ( ) => connection . get < WeaviateRole [ ] > ( '/authz/users/own-roles' ) . then ( Map . roles ) ,
37
88
byName : ( roleName : string ) =>
38
89
connection . get < WeaviateRole > ( `/authz/roles/${ roleName } ` ) . then ( Map . roleFromWeaviate ) ,
39
- byUser : ( user : string ) => connection . get < WeaviateRole [ ] > ( `/authz/users/${ user } /roles` ) . then ( Map . roles ) ,
40
- assignedUsers : ( roleName : string ) =>
41
- connection . get < string [ ] > ( `/authz/roles/${ roleName } /users` ) . then ( Map . users ) ,
90
+ assignedUserIds : ( roleName : string ) => connection . get < string [ ] > ( `/authz/roles/${ roleName } /users` ) ,
42
91
create : ( roleName : string , permissions : PermissionsInput ) => {
43
- const perms = Map . flattenPermissions ( permissions ) . map ( Map . permissionToWeaviate ) ;
92
+ const perms = Map . flattenPermissions ( permissions ) . flatMap ( Map . permissionToWeaviate ) ;
44
93
return connection
45
94
. postEmpty < WeaviateRole > ( '/authz/roles' , {
46
95
name : roleName ,
@@ -54,43 +103,34 @@ const roles = (connection: ConnectionREST): Roles => {
54
103
. get ( `/authz/roles/${ roleName } ` )
55
104
. then ( ( ) => true )
56
105
. catch ( ( ) => false ) ,
57
- assignToUser : ( roleNames : string | string [ ] , user : string ) =>
58
- connection . postEmpty ( `/authz/users/${ user } /assign` , {
59
- roles : Array . isArray ( roleNames ) ? roleNames : [ roleNames ] ,
60
- } ) ,
61
- revokeFromUser : ( roleNames : string | string [ ] , user : string ) =>
62
- connection . postEmpty ( `/authz/users/${ user } /revoke` , {
63
- roles : Array . isArray ( roleNames ) ? roleNames : [ roleNames ] ,
64
- } ) ,
65
106
addPermissions : ( roleName : string , permissions : PermissionsInput ) =>
66
107
connection . postEmpty ( `/authz/roles/${ roleName } /add-permissions` , { permissions } ) ,
67
108
removePermissions : ( roleName : string , permissions : PermissionsInput ) =>
68
109
connection . postEmpty ( `/authz/roles/${ roleName } /remove-permissions` , { permissions } ) ,
69
- hasPermission : ( roleName : string , permission : Permission ) =>
70
- connection . postReturn < WeaviatePermission , boolean > (
71
- `/authz/roles/${ roleName } /has-permission` ,
72
- Map . permissionToWeaviate ( permission )
73
- ) ,
110
+ hasPermissions : ( roleName : string , permission : Permission | Permission [ ] ) =>
111
+ Promise . all (
112
+ ( Array . isArray ( permission ) ? permission : [ permission ] )
113
+ . flatMap ( ( p ) => Map . permissionToWeaviate ( p ) )
114
+ . map ( ( p ) =>
115
+ connection . postReturn < WeaviatePermission , boolean > ( `/authz/roles/${ roleName } /has-permission` , p )
116
+ )
117
+ ) . then ( ( r ) => r . every ( ( b ) => b ) ) ,
74
118
} ;
75
119
} ;
76
120
77
121
export const permissions = {
78
122
backup : ( args : { collection : string | string [ ] ; manage ?: boolean } ) : BackupsPermission [ ] => {
79
123
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
80
124
return collections . flatMap ( ( collection ) => {
81
- const out : BackupsPermission [ ] = [ ] ;
82
- if ( args . manage ) {
83
- out . push ( { collection, action : 'manage_backups' } ) ;
84
- }
125
+ const out : BackupsPermission = { collection, actions : [ ] } ;
126
+ if ( args . manage ) out . actions . push ( 'manage_backups' ) ;
85
127
return out ;
86
128
} ) ;
87
129
} ,
88
130
cluster : ( args : { read ?: boolean } ) : ClusterPermission [ ] => {
89
- const out : ClusterPermission [ ] = [ ] ;
90
- if ( args . read ) {
91
- out . push ( { action : 'read_cluster' } ) ;
92
- }
93
- return out ;
131
+ const out : ClusterPermission = { actions : [ ] } ;
132
+ if ( args . read ) out . actions . push ( 'read_cluster' ) ;
133
+ return [ out ] ;
94
134
} ,
95
135
collections : ( args : {
96
136
collection : string | string [ ] ;
@@ -101,19 +141,11 @@ export const permissions = {
101
141
} ) : CollectionsPermission [ ] => {
102
142
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
103
143
return collections . flatMap ( ( collection ) => {
104
- const out : CollectionsPermission [ ] = [ ] ;
105
- if ( args . create_collection ) {
106
- out . push ( { collection, action : 'create_collections' } ) ;
107
- }
108
- if ( args . read_config ) {
109
- out . push ( { collection, action : 'read_collections' } ) ;
110
- }
111
- if ( args . update_config ) {
112
- out . push ( { collection, action : 'update_collections' } ) ;
113
- }
114
- if ( args . delete_collection ) {
115
- out . push ( { collection, action : 'delete_collections' } ) ;
116
- }
144
+ const out : CollectionsPermission = { collection, actions : [ ] } ;
145
+ if ( args . create_collection ) out . actions . push ( 'create_collections' ) ;
146
+ if ( args . read_config ) out . actions . push ( 'read_collections' ) ;
147
+ if ( args . update_config ) out . actions . push ( 'update_collections' ) ;
148
+ if ( args . delete_collection ) out . actions . push ( 'delete_collections' ) ;
117
149
return out ;
118
150
} ) ;
119
151
} ,
@@ -126,19 +158,11 @@ export const permissions = {
126
158
} ) : DataPermission [ ] => {
127
159
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
128
160
return collections . flatMap ( ( collection ) => {
129
- const out : DataPermission [ ] = [ ] ;
130
- if ( args . create ) {
131
- out . push ( { collection, action : 'create_data' } ) ;
132
- }
133
- if ( args . read ) {
134
- out . push ( { collection, action : 'read_data' } ) ;
135
- }
136
- if ( args . update ) {
137
- out . push ( { collection, action : 'update_data' } ) ;
138
- }
139
- if ( args . delete ) {
140
- out . push ( { collection, action : 'delete_data' } ) ;
141
- }
161
+ const out : DataPermission = { collection, actions : [ ] } ;
162
+ if ( args . create ) out . actions . push ( 'create_data' ) ;
163
+ if ( args . read ) out . actions . push ( 'read_data' ) ;
164
+ if ( args . update ) out . actions . push ( 'update_data' ) ;
165
+ if ( args . delete ) out . actions . push ( 'delete_data' ) ;
142
166
return out ;
143
167
} ) ;
144
168
} ,
@@ -149,23 +173,21 @@ export const permissions = {
149
173
} ) : NodesPermission [ ] => {
150
174
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
151
175
return collections . flatMap ( ( collection ) => {
152
- const out : NodesPermission [ ] = [ ] ;
153
- if ( args . read ) {
154
- out . push ( { collection, action : 'read_nodes' , verbosity : args . verbosity || 'verbose' } ) ;
155
- }
176
+ const out : NodesPermission = {
177
+ collection,
178
+ actions : [ ] ,
179
+ verbosity : args . verbosity || 'verbose' ,
180
+ } ;
181
+ if ( args . read ) out . actions . push ( 'read_nodes' ) ;
156
182
return out ;
157
183
} ) ;
158
184
} ,
159
185
roles : ( args : { role : string | string [ ] ; read ?: boolean ; manage ?: boolean } ) : RolesPermission [ ] => {
160
186
const roles = Array . isArray ( args . role ) ? args . role : [ args . role ] ;
161
187
return roles . flatMap ( ( role ) => {
162
- const out : RolesPermission [ ] = [ ] ;
163
- if ( args . read ) {
164
- out . push ( { role, action : 'read_roles' } ) ;
165
- }
166
- if ( args . manage ) {
167
- out . push ( { role, action : 'manage_roles' } ) ;
168
- }
188
+ const out : RolesPermission = { role, actions : [ ] } ;
189
+ if ( args . read ) out . actions . push ( 'read_roles' ) ;
190
+ if ( args . manage ) out . actions . push ( 'manage_roles' ) ;
169
191
return out ;
170
192
} ) ;
171
193
} ,
0 commit comments