@@ -54,32 +54,49 @@ async function getResources (currentUser, challengeId, roleId, memberId, memberH
54
54
perPage = perPage || config . DEFAULT_PAGE_SIZE
55
55
sortBy = sortBy || 'created'
56
56
sortOrder = sortOrder || 'asc'
57
- if ( ! validateUUID ( challengeId ) ) {
57
+ if ( ! challengeId && ! memberId && ! memberHandle ) {
58
+ throw new errors . BadRequestError ( 'At least one of the following parameters is required: [challengeId, memberId, memberHandle]' )
59
+ }
60
+ if ( challengeId && ! validateUUID ( challengeId ) ) {
58
61
throw new errors . BadRequestError ( `Challenge ID ${ challengeId } must be a valid v5 Challenge Id (UUID)` )
59
62
}
60
- try {
61
- // Verify that the challenge exists
62
- await helper . getRequest ( `${ config . CHALLENGE_API_URL } /${ challengeId } ` )
63
- } catch ( e ) {
64
- throw new errors . NotFoundError ( `Challenge ID ${ challengeId } not found` )
63
+ if ( challengeId ) {
64
+ try {
65
+ // Verify that the challenge exists
66
+ await helper . getRequest ( `${ config . CHALLENGE_API_URL } /${ challengeId } ` )
67
+ } catch ( e ) {
68
+ throw new errors . NotFoundError ( `Challenge ID ${ challengeId } not found` )
69
+ }
65
70
}
66
71
67
72
const boolQuery = [ ]
68
73
const mustQuery = [ ]
69
74
let hasFullAccess
70
75
71
76
// Check if the user has a resource with full access on the challenge
72
- if ( currentUser ) {
73
- const resources = await helper . query ( 'Resource' , { challengeId } )
74
- try {
75
- await checkAccess ( currentUser , resources )
76
- hasFullAccess = true
77
- } catch ( e ) {
78
- hasFullAccess = false
77
+ if ( currentUser && ! helper . hasAdminRole ( currentUser ) && ! hasFullAccess ) {
78
+ if ( challengeId ) {
79
+ const resources = await helper . query ( 'Resource' , { challengeId } )
80
+ try {
81
+ await checkAccess ( currentUser , resources )
82
+ hasFullAccess = true
83
+ } catch ( e ) {
84
+ hasFullAccess = false
85
+ }
86
+ }
87
+ if ( memberId && memberId !== currentUser . useId ) {
88
+ throw new errors . ForbiddenError ( 'You are not allowed to perform this operation!' )
89
+ }
90
+ if ( memberHandle && memberHandle !== currentUser . handle ) {
91
+ throw new errors . ForbiddenError ( 'You are not allowed to perform this operation!' )
79
92
}
80
93
}
81
94
82
- boolQuery . push ( { match_phrase : { challengeId } } )
95
+ if ( challengeId ) {
96
+ boolQuery . push ( { match_phrase : { challengeId } } )
97
+ } else if ( ! currentUser ) {
98
+ throw new errors . ForbiddenError ( 'You are not allowed to perform this operation!' )
99
+ }
83
100
84
101
if ( ! currentUser ) {
85
102
// if the user is not logged in, only return resources with submitter role ID
@@ -161,7 +178,7 @@ async function getResources (currentUser, challengeId, roleId, memberId, memberH
161
178
162
179
getResources . schema = {
163
180
currentUser : Joi . any ( ) ,
164
- challengeId : Joi . id ( ) ,
181
+ challengeId : Joi . optionalId ( ) ,
165
182
roleId : Joi . optionalId ( ) ,
166
183
memberId : Joi . string ( ) ,
167
184
memberHandle : Joi . string ( ) ,
0 commit comments