@@ -28,7 +28,7 @@ export const getRoles = async (token: string) => {
28
28
return res ;
29
29
} ;
30
30
31
- export const addRole = async ( token : string , role : string ) => {
31
+ export const addRole = async ( token : string , roleName : string ) => {
32
32
let error = null ;
33
33
34
34
const res = await fetch ( `${ WEBUI_API_BASE_URL } /roles/` , {
@@ -38,7 +38,7 @@ export const addRole = async (token: string, role: string) => {
38
38
Authorization : `Bearer ${ token } `
39
39
} ,
40
40
body : JSON . stringify ( {
41
- role : role
41
+ role : roleName
42
42
} )
43
43
} )
44
44
. then ( async ( res ) => {
@@ -58,10 +58,40 @@ export const addRole = async (token: string, role: string) => {
58
58
return res ;
59
59
} ;
60
60
61
- export const deleteRole = async ( token : string , roleId : string ) => {
61
+ export const updateRole = async ( token : string , roleId : number , roleName : string ) => {
62
62
let error = null ;
63
63
64
64
const res = await fetch ( `${ WEBUI_API_BASE_URL } /roles/${ roleId } ` , {
65
+ method : 'POST' ,
66
+ headers : {
67
+ 'Content-Type' : 'application/json' ,
68
+ Authorization : `Bearer ${ token } `
69
+ } ,
70
+ body : JSON . stringify ( {
71
+ role : roleName
72
+ } )
73
+ } )
74
+ . then ( async ( res ) => {
75
+ if ( ! res . ok ) throw await res . json ( ) ;
76
+ return res . json ( ) ;
77
+ } )
78
+ . catch ( ( err ) => {
79
+ console . log ( err ) ;
80
+ error = err . detail ;
81
+ return null ;
82
+ } ) ;
83
+
84
+ if ( error ) {
85
+ throw error ;
86
+ }
87
+
88
+ return res ;
89
+ } ;
90
+
91
+ export const deleteRole = async ( token : string , roleName : string ) => {
92
+ let error = null ;
93
+
94
+ const res = await fetch ( `${ WEBUI_API_BASE_URL } /roles/${ roleName } ` , {
65
95
method : 'DELETE' ,
66
96
headers : {
67
97
'Content-Type' : 'application/json' ,
@@ -83,4 +113,97 @@ export const deleteRole = async (token: string, roleId: string) => {
83
113
}
84
114
85
115
return res ;
86
- } ;
116
+ } ;
117
+
118
+ export const getRolePermissions = async ( token : string , roleName : string ) => {
119
+ let error = null ;
120
+
121
+ const res = await fetch ( `${ WEBUI_API_BASE_URL } /roles/${ roleName } /permissions` , {
122
+ method : 'GET' ,
123
+ headers : {
124
+ 'Content-Type' : 'application/json' ,
125
+ Authorization : `Bearer ${ token } `
126
+ }
127
+ } )
128
+ . then ( async ( res ) => {
129
+ if ( ! res . ok ) throw await res . json ( ) ;
130
+ return res . json ( ) ;
131
+ } )
132
+ . catch ( ( err ) => {
133
+ console . log ( err ) ;
134
+ error = err . detail ;
135
+ return null ;
136
+ } ) ;
137
+
138
+ if ( error ) {
139
+ throw error ;
140
+ }
141
+
142
+ return res ;
143
+ } ;
144
+
145
+ // GET /api/v1/roles/{role_name}/permission Add New Default Permission With Role
146
+ // {
147
+ // "name": "string",
148
+ // "category": "workspace",
149
+ // "description": "string",
150
+ // "value": false
151
+ // }
152
+
153
+ export const linkRoleToPermissions = async ( token : string , roleName : string , categoryName : string , permissionName : string , value : boolean ) => {
154
+ let error = null ;
155
+ const res = await fetch ( `${ WEBUI_API_BASE_URL } /roles/${ roleName } /permission/link` , {
156
+ method : 'POST' ,
157
+ headers : {
158
+ 'Content-Type' : 'application/json' ,
159
+ Authorization : `Bearer ${ token } `
160
+ } ,
161
+ body : JSON . stringify ( {
162
+ permission_name : permissionName ,
163
+ category : categoryName ,
164
+ value : value
165
+ } )
166
+ } )
167
+ . then ( async ( res ) => {
168
+ if ( ! res . ok ) throw await res . json ( ) ;
169
+ return res . json ( ) ;
170
+ } )
171
+ . catch ( ( err ) => {
172
+ console . log ( err ) ;
173
+ error = err . detail ;
174
+ return null ;
175
+ } ) ;
176
+
177
+ if ( error ) {
178
+ throw error ;
179
+ }
180
+
181
+ return res ;
182
+ } ;
183
+
184
+ export const unlinkRoleFromPermissions = async ( token : string , roleName : string , categoryName : string , permissionName : string ) => {
185
+ let error = null ;
186
+
187
+ const res = await fetch ( `${ WEBUI_API_BASE_URL } /roles/${ roleName } /permission/${ categoryName } /${ permissionName } ` , {
188
+ method : 'DELETE' ,
189
+ headers : {
190
+ 'Content-Type' : 'application/json' ,
191
+ Authorization : `Bearer ${ token } `
192
+ } ,
193
+ } )
194
+ . then ( async ( res ) => {
195
+ if ( ! res . ok ) throw await res . json ( ) ;
196
+ return res . json ( ) ;
197
+ } )
198
+ . catch ( ( err ) => {
199
+ console . log ( err ) ;
200
+ error = err . detail ;
201
+ return null ;
202
+ } ) ;
203
+
204
+ if ( error ) {
205
+ throw error ;
206
+ }
207
+
208
+ return res ;
209
+ } ;
0 commit comments