@@ -66,6 +66,16 @@ const errorHandler = async (ctx, next) => {
6666 }
6767} ;
6868
69+ const allowedToGenerate = ( user ) => {
70+ if ( ! user || ! user . email ) return false ;
71+
72+ if ( user . groups && user . groups . includes ( GROUP_GENERATE ) ) {
73+ return true ;
74+ }
75+
76+ return false ;
77+ } ;
78+
6979const authMiddleware = async ( ctx , next ) => {
7080 const endpointsNotRequiringAuthentication = [ '/login' , '/logout' , '/session' ] ;
7181 if ( endpointsNotRequiringAuthentication . includes ( ctx . path ) ) {
@@ -82,6 +92,14 @@ const authMiddleware = async (ctx, next) => {
8292 // Not authenticated, throw 401
8393 ctx . throw ( 401 ) ;
8494 } else {
95+ // If the request is CRUD, check if the user has privileges to perform the action
96+ if ( ctx . method !== 'GET' && ctx . method !== 'HEAD' ) {
97+ const user = authResponse . body ;
98+ if ( ! allowedToGenerate ( user ) ) {
99+ ctx . throw ( 403 , 'User does not have privileges to perform this action.' ) ;
100+ }
101+ }
102+
85103 await next ( ) ;
86104 }
87105 }
@@ -150,20 +168,6 @@ async function main() {
150168 } ) ;
151169
152170 router . delete ( '/builds/:id' , async ( ctx ) => {
153- const authResponse = await authEndpoints . checkExistingSession (
154- ctx . request ,
155- ctx . response ,
156- ctx . session ,
157- ) ;
158-
159- if ( ! authResponse . body . isOk ) {
160- ctx . throw ( 401 , 'Not allowed.' ) ;
161- }
162-
163- if ( ! authResponse . body . groups . includes ( GROUP_GENERATE ) ) {
164- ctx . throw ( 403 , 'User does not have permission to modify builds.' ) ;
165- }
166-
167171 const { id } = ctx . params ;
168172 const build = await removeBuild ( { id } ) ;
169173 ctx . body = build ;
0 commit comments