11import { ConnectionREST } from '../index.js' ;
22import { Role as WeaviateRole , WeaviateUserTypeInternal as UserTypeInternal , WeaviateUser , WeaviateDBUser } from '../openapi/types.js' ;
3+ import roles from '../roles/index.js' ;
34import { Role } from '../roles/types.js' ;
45import { Map } from '../roles/util.js' ;
56import { User , UserDB } from './types.js' ;
@@ -77,27 +78,31 @@ export interface OIDCUsers extends UsersBase {
7778}
7879
7980const users = ( connection : ConnectionREST ) : Users => {
80- const ns = namespaced ( connection ) ;
81+ const base = baseUsers ( connection ) ;
8182
8283 return {
8384 getMyUser : ( ) => connection . get < WeaviateUser > ( '/users/own-info' ) . then ( Map . user ) ,
8485 getAssignedRoles : ( userId : string ) =>
8586 connection . get < WeaviateRole [ ] > ( `/authz/users/${ userId } /roles` ) . then ( Map . roles ) ,
8687 assignRoles : ( roleNames : string | string [ ] , userId : string ) =>
87- ns . assignRoles ( roleNames , userId ) ,
88+ base . assignRoles ( roleNames , userId ) ,
8889 revokeRoles : ( roleNames : string | string [ ] , userId : string ) =>
89- ns . revokeRoles ( roleNames , userId ) ,
90+ base . revokeRoles ( roleNames , userId ) ,
9091 db : db ( connection ) ,
9192 oidc : oidc ( connection ) ,
9293 } ;
9394} ;
9495
9596const 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+ }
97102
98103 type APIKeyResponse = { apiKey : string } ;
99104 return {
100- getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( userId , 'db' , includePermissions ) ,
105+ getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( 'db' , userId , includePermissions ) ,
101106 assignRoles : ( roleNames : string | string [ ] , userId : string ) => ns . assignRoles ( roleNames , userId , 'db' ) ,
102107 revokeRoles : ( roleNames : string | string [ ] , userId : string ) => ns . revokeRoles ( roleNames , userId , 'db' ) ,
103108
@@ -108,41 +113,50 @@ const db = (connection: ConnectionREST): DBUsers => {
108113 rotateKey : ( userId : string ) => connection . postNoBody < APIKeyResponse > ( `users/db/${ userId } /rotate-key` )
109114 . then ( resp => resp . apiKey ) ,
110115 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 ) ) ,
112117 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 ) ) ,
114119 byName : ( userId : string ) => connection . get < WeaviateDBUser > ( `/users/db/${ userId } ` , true ) . then ( Map . dbUser ) ,
115120 listAll : ( ) => connection . get < WeaviateDBUser [ ] > ( '/users/db' , true ) . then ( Map . dbUsers ) ,
116121 } ;
117122}
118123
119124const oidc = ( connection : ConnectionREST ) : OIDCUsers => {
120- const ns = namespaced ( connection ) ;
125+ const ns = namespacedUsers ( connection ) ;
121126 return {
122- getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( userId , 'oidc' , includePermissions ) ,
127+ getAssignedRoles : ( userId : string , includePermissions ?: boolean ) => ns . getAssignedRoles ( 'oidc' , userId , includePermissions ) ,
123128 assignRoles : ( roleNames : string | string [ ] , userId : string ) => ns . assignRoles ( roleNames , userId , 'oidc' ) ,
124129 revokeRoles : ( roleNames : string | string [ ] , userId : string ) => ns . revokeRoles ( roleNames , userId , 'oidc' ) ,
125130 } ;
126131}
127132
128- // TODO: see if we can extend definitions of UsersBase with additional UserType arg
129133/** Internal interface for operations that MAY accept a 'db'/'oidc' namespace. */
130134interface 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 > > ;
132136 assignRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) => Promise < void > ;
133137 revokeRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) => Promise < void > ;
134138}
135139
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 => {
137151 return {
138- getAssignedRoles : ( userId : string , userType : UserTypeInternal , includePermissions ?: boolean ) =>
152+ getAssignedRoles : ( userType : UserTypeInternal , userId : string , includePermissions ?: boolean ) =>
139153 connection . get < WeaviateRole [ ] > (
140154 `/authz/users/${ userId } /roles/${ userType } ${ includePermissions ? '?&includeFullRoles=true' : '' } `
141155 ) . then ( Map . roles ) ,
142156 assignRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) =>
143157 connection . postEmpty ( `/authz/users/${ userId } /assign` , {
144158 roles : Array . isArray ( roleNames ) ? roleNames : [ roleNames ] ,
145- userType : userType
159+ userType : userType ,
146160 } ) ,
147161 revokeRoles : ( roleNames : string | string [ ] , userId : string , userType ?: UserTypeInternal ) =>
148162 connection . postEmpty ( `/authz/users/${ userId } /revoke` , {
0 commit comments