@@ -10,37 +10,29 @@ 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 {
18
17
listAll : ( ) => Promise < Record < string , Role > > ;
19
- ofCurrentUser : ( ) => Promise < Record < string , Role > > ;
20
18
byName : ( roleName : string ) => Promise < Role | null > ;
21
- byUser : ( user : string ) => Promise < Record < string , Role > > ;
22
- assignedUsers : ( roleName : string ) => Promise < Record < string , User > > ;
19
+ assignedUserIds : ( roleName : string ) => Promise < string [ ] > ;
23
20
delete : ( roleName : string ) => Promise < void > ;
24
21
create : ( roleName : string , permissions : PermissionsInput ) => Promise < Role > ;
25
- assignToUser : ( roleNames : string | string [ ] , user : string ) => Promise < void > ;
26
22
exists : ( roleName : string ) => Promise < boolean > ;
27
- revokeFromUser : ( roleNames : string | string [ ] , user : string ) => Promise < void > ;
28
23
addPermissions : ( roleName : string , permissions : PermissionsInput ) => Promise < void > ;
29
24
removePermissions : ( roleName : string , permissions : PermissionsInput ) => Promise < void > ;
30
- hasPermission : ( roleName : string , permission : Permission ) => Promise < boolean > ;
25
+ hasPermissions : ( roleName : string , permission : Permission ) => Promise < boolean > ;
31
26
}
32
27
33
28
const roles = ( connection : ConnectionREST ) : Roles => {
34
29
return {
35
30
listAll : ( ) => connection . get < WeaviateRole [ ] > ( '/authz/roles' ) . then ( Map . roles ) ,
36
- ofCurrentUser : ( ) => connection . get < WeaviateRole [ ] > ( '/authz/users/own-roles' ) . then ( Map . roles ) ,
37
31
byName : ( roleName : string ) =>
38
32
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 ) ,
33
+ assignedUserIds : ( roleName : string ) => connection . get < string [ ] > ( `/authz/roles/${ roleName } /users` ) ,
42
34
create : ( roleName : string , permissions : PermissionsInput ) => {
43
- const perms = Map . flattenPermissions ( permissions ) . map ( Map . permissionToWeaviate ) ;
35
+ const perms = Map . flattenPermissions ( permissions ) . flatMap ( Map . permissionToWeaviate ) ;
44
36
return connection
45
37
. postEmpty < WeaviateRole > ( '/authz/roles' , {
46
38
name : roleName ,
@@ -54,43 +46,34 @@ const roles = (connection: ConnectionREST): Roles => {
54
46
. get ( `/authz/roles/${ roleName } ` )
55
47
. then ( ( ) => true )
56
48
. 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
49
addPermissions : ( roleName : string , permissions : PermissionsInput ) =>
66
50
connection . postEmpty ( `/authz/roles/${ roleName } /add-permissions` , { permissions } ) ,
67
51
removePermissions : ( roleName : string , permissions : PermissionsInput ) =>
68
52
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
- ) ,
53
+ hasPermissions : ( roleName : string , permission : Permission | Permission [ ] ) =>
54
+ Promise . all (
55
+ ( Array . isArray ( permission ) ? permission : [ permission ] )
56
+ . flatMap ( ( p ) => Map . permissionToWeaviate ( p ) )
57
+ . map ( ( p ) =>
58
+ connection . postReturn < WeaviatePermission , boolean > ( `/authz/roles/${ roleName } /has-permission` , p )
59
+ )
60
+ ) . then ( ( r ) => r . every ( ( b ) => b ) ) ,
74
61
} ;
75
62
} ;
76
63
77
64
export const permissions = {
78
65
backup : ( args : { collection : string | string [ ] ; manage ?: boolean } ) : BackupsPermission [ ] => {
79
66
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
80
67
return collections . flatMap ( ( collection ) => {
81
- const out : BackupsPermission [ ] = [ ] ;
82
- if ( args . manage ) {
83
- out . push ( { collection, action : 'manage_backups' } ) ;
84
- }
68
+ const out : BackupsPermission = { collection, actions : [ ] } ;
69
+ if ( args . manage ) out . actions . push ( 'manage_backups' ) ;
85
70
return out ;
86
71
} ) ;
87
72
} ,
88
73
cluster : ( args : { read ?: boolean } ) : ClusterPermission [ ] => {
89
- const out : ClusterPermission [ ] = [ ] ;
90
- if ( args . read ) {
91
- out . push ( { action : 'read_cluster' } ) ;
92
- }
93
- return out ;
74
+ const out : ClusterPermission = { actions : [ ] } ;
75
+ if ( args . read ) out . actions . push ( 'read_cluster' ) ;
76
+ return [ out ] ;
94
77
} ,
95
78
collections : ( args : {
96
79
collection : string | string [ ] ;
@@ -101,19 +84,11 @@ export const permissions = {
101
84
} ) : CollectionsPermission [ ] => {
102
85
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
103
86
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
- }
87
+ const out : CollectionsPermission = { collection, actions : [ ] } ;
88
+ if ( args . create_collection ) out . actions . push ( 'create_collections' ) ;
89
+ if ( args . read_config ) out . actions . push ( 'read_collections' ) ;
90
+ if ( args . update_config ) out . actions . push ( 'update_collections' ) ;
91
+ if ( args . delete_collection ) out . actions . push ( 'delete_collections' ) ;
117
92
return out ;
118
93
} ) ;
119
94
} ,
@@ -126,19 +101,11 @@ export const permissions = {
126
101
} ) : DataPermission [ ] => {
127
102
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
128
103
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
- }
104
+ const out : DataPermission = { collection, actions : [ ] } ;
105
+ if ( args . create ) out . actions . push ( 'create_data' ) ;
106
+ if ( args . read ) out . actions . push ( 'read_data' ) ;
107
+ if ( args . update ) out . actions . push ( 'update_data' ) ;
108
+ if ( args . delete ) out . actions . push ( 'delete_data' ) ;
142
109
return out ;
143
110
} ) ;
144
111
} ,
@@ -149,23 +116,21 @@ export const permissions = {
149
116
} ) : NodesPermission [ ] => {
150
117
const collections = Array . isArray ( args . collection ) ? args . collection : [ args . collection ] ;
151
118
return collections . flatMap ( ( collection ) => {
152
- const out : NodesPermission [ ] = [ ] ;
153
- if ( args . read ) {
154
- out . push ( { collection, action : 'read_nodes' , verbosity : args . verbosity || 'verbose' } ) ;
155
- }
119
+ const out : NodesPermission = {
120
+ collection,
121
+ actions : [ ] ,
122
+ verbosity : args . verbosity || 'verbose' ,
123
+ } ;
124
+ if ( args . read ) out . actions . push ( 'read_nodes' ) ;
156
125
return out ;
157
126
} ) ;
158
127
} ,
159
128
roles : ( args : { role : string | string [ ] ; read ?: boolean ; manage ?: boolean } ) : RolesPermission [ ] => {
160
129
const roles = Array . isArray ( args . role ) ? args . role : [ args . role ] ;
161
130
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
- }
131
+ const out : RolesPermission = { role, actions : [ ] } ;
132
+ if ( args . read ) out . actions . push ( 'read_roles' ) ;
133
+ if ( args . manage ) out . actions . push ( 'manage_roles' ) ;
169
134
return out ;
170
135
} ) ;
171
136
} ,
0 commit comments