1
1
import { ConnectionREST } from '../index.js' ;
2
2
import { Role as WeaviateRole , WeaviateUserTypeInternal as UserTypeInternal , WeaviateUser , WeaviateDBUser } from '../openapi/types.js' ;
3
+ import roles from '../roles/index.js' ;
3
4
import { Role } from '../roles/types.js' ;
4
5
import { Map } from '../roles/util.js' ;
5
6
import { User , UserDB } from './types.js' ;
@@ -77,27 +78,31 @@ export interface OIDCUsers extends UsersBase {
77
78
}
78
79
79
80
const users = ( connection : ConnectionREST ) : Users => {
80
- const ns = namespaced ( connection ) ;
81
+ const base = baseUsers ( connection ) ;
81
82
82
83
return {
83
84
getMyUser : ( ) => connection . get < WeaviateUser > ( '/users/own-info' ) . then ( Map . user ) ,
84
85
getAssignedRoles : ( userId : string ) =>
85
86
connection . get < WeaviateRole [ ] > ( `/authz/users/${ userId } /roles` ) . then ( Map . roles ) ,
86
87
assignRoles : ( roleNames : string | string [ ] , userId : string ) =>
87
- ns . assignRoles ( roleNames , userId ) ,
88
+ base . assignRoles ( roleNames , userId ) ,
88
89
revokeRoles : ( roleNames : string | string [ ] , userId : string ) =>
89
- ns . revokeRoles ( roleNames , userId ) ,
90
+ base . revokeRoles ( roleNames , userId ) ,
90
91
db : db ( connection ) ,
91
92
oidc : oidc ( connection ) ,
92
93
} ;
93
94
} ;
94
95
95
96
const db = ( connection : ConnectionREST ) : DBUsers => {
96
- const ns = namespaced ( connection ) ;
97
+ const ns = namespacedUsers ( connection ) ;
98
+
99
+ const allowCode = ( code : number ) : ( reason : any ) => boolean => {
100
+ return reason => reason . code !== undefined && reason . code === code ;
101
+ }
97
102
98
103
type APIKeyResponse = { apiKey : string } ;
99
104
return {
100
- getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( userId , 'db' , includePermissions ) ,
105
+ getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( 'db' , userId , includePermissions ) ,
101
106
assignRoles : ( roleNames : string | string [ ] , userId : string ) => ns . assignRoles ( roleNames , userId , 'db' ) ,
102
107
revokeRoles : ( roleNames : string | string [ ] , userId : string ) => ns . revokeRoles ( roleNames , userId , 'db' ) ,
103
108
@@ -108,41 +113,50 @@ const db = (connection: ConnectionREST): DBUsers => {
108
113
rotateKey : ( userId : string ) => connection . postNoBody < APIKeyResponse > ( `users/db/${ userId } /rotate-key` )
109
114
. then ( resp => resp . apiKey ) ,
110
115
activate : ( userId : string ) => connection . postNoBody ( `/users/db/${ userId } /activate` )
111
- . then ( ( ) => true ) . catch ( reason => reason . code !== undefined ? reason . code === 409 : false ) ,
116
+ . then ( ( ) => true ) . catch ( allowCode ( 409 ) ) ,
112
117
deactivate : ( userId : string ) => connection . postNoBody ( `/users/db/${ userId } /deactivate` )
113
- . then ( ( ) => true ) . catch ( reason => reason . code !== undefined ? reason . code === 409 : false ) ,
118
+ . then ( ( ) => true ) . catch ( allowCode ( 409 ) ) ,
114
119
byName : ( userId : string ) => connection . get < WeaviateDBUser > ( `/users/db/${ userId } ` , true ) . then ( Map . dbUser ) ,
115
120
listAll : ( ) => connection . get < WeaviateDBUser [ ] > ( '/users/db' , true ) . then ( Map . dbUsers ) ,
116
121
} ;
117
122
}
118
123
119
124
const oidc = ( connection : ConnectionREST ) : OIDCUsers => {
120
- const ns = namespaced ( connection ) ;
125
+ const ns = namespacedUsers ( connection ) ;
121
126
return {
122
- getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( userId , 'oidc' , includePermissions ) ,
127
+ getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( 'oidc' , userId , includePermissions ) ,
123
128
assignRoles : ( roleNames : string | string [ ] , userId : string ) => ns . assignRoles ( roleNames , userId , 'oidc' ) ,
124
129
revokeRoles : ( roleNames : string | string [ ] , userId : string ) => ns . revokeRoles ( roleNames , userId , 'oidc' ) ,
125
130
} ;
126
131
}
127
132
128
- // TODO: see if we can extend definitions of UsersBase with additional UserType arg
129
133
/** Internal interface for operations that MAY accept a 'db'/'oidc' namespace. */
130
134
interface NamespacedUsers {
131
- getAssignedRoles : ( userId : string , userType : UserTypeInternal , includePermissions ?: boolean ) => Promise < Record < string , Role > > ;
135
+ getAssignedRoles : ( userType : UserTypeInternal , userId : string , includePermissions ?: boolean ) => Promise < Record < string , Role > > ;
132
136
assignRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) => Promise < void > ;
133
137
revokeRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) => Promise < void > ;
134
138
}
135
139
136
- const namespaced = ( connection : ConnectionREST ) : NamespacedUsers => {
140
+ const baseUsers = ( connection : ConnectionREST ) : UsersBase => {
141
+ const ns = namespacedUsers ( connection ) ;
142
+ return {
143
+ assignRoles : ( roleNames : string | string [ ] , userId : string ) =>
144
+ ns . assignRoles ( roleNames , userId ) ,
145
+ revokeRoles : ( roleNames : string | string [ ] , userId : string ) =>
146
+ ns . revokeRoles ( roleNames , userId ) ,
147
+ } ;
148
+ }
149
+
150
+ const namespacedUsers = ( connection : ConnectionREST ) : NamespacedUsers => {
137
151
return {
138
- getAssignedRoles : ( userId : string , userType : UserTypeInternal , includePermissions ?: boolean ) =>
152
+ getAssignedRoles : ( userType : UserTypeInternal , userId : string , includePermissions ?: boolean ) =>
139
153
connection . get < WeaviateRole [ ] > (
140
154
`/authz/users/${ userId } /roles/${ userType } ${ includePermissions ? '?&includeFullRoles=true' : '' } `
141
155
) . then ( Map . roles ) ,
142
156
assignRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) =>
143
157
connection . postEmpty ( `/authz/users/${ userId } /assign` , {
144
158
roles : Array . isArray ( roleNames ) ? roleNames : [ roleNames ] ,
145
- userType : userType
159
+ userType : userType ,
146
160
} ) ,
147
161
revokeRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) =>
148
162
connection . postEmpty ( `/authz/users/${ userId } /revoke` , {
0 commit comments