@@ -67,74 +67,6 @@ async function ensureAccessibleForChallenge(user, challenge) {
67
67
}
68
68
}
69
69
70
- /**
71
- * Filter challenges by groups access
72
- * @param {Object } currentUser the user who perform operation
73
- * @param {Array } challenges the challenges to filter
74
- * @returns {Array } the challenges that can be accessed by current user
75
- */
76
- async function filterChallengesByGroupsAccess ( currentUser , challenges ) {
77
- const res = [ ] ;
78
- let userGroups ;
79
- const needToCheckForGroupAccess = ! currentUser
80
- ? true
81
- : ! currentUser . isMachine && ! hasAdminRole ( currentUser ) ;
82
- const subGroupsMap = { } ;
83
- for ( const challenge of challenges ) {
84
- challenge . groups = _ . filter (
85
- challenge . groups ,
86
- ( g ) => ! _ . includes ( [ "null" , "undefined" ] , _ . toString ( g ) . toLowerCase ( ) )
87
- ) ;
88
- let expandedGroups = [ ] ;
89
- if (
90
- ! challenge . groups ||
91
- _ . get ( challenge , "groups.length" , 0 ) === 0 ||
92
- ! needToCheckForGroupAccess
93
- ) {
94
- res . push ( challenge ) ;
95
- } else if ( currentUser ) {
96
- // get user groups if not yet
97
- if ( _ . isNil ( userGroups ) ) {
98
- userGroups = await helper . getUserGroups ( currentUser . userId ) ;
99
- }
100
- // Expand challenge groups by subGroups
101
- // results are being saved on a hashmap for efficiency
102
- for ( const group of challenge . groups ) {
103
- let subGroups ;
104
- if ( subGroupsMap [ group ] ) {
105
- subGroups = subGroupsMap [ group ] ;
106
- } else {
107
- subGroups = await helper . expandWithSubGroups ( group ) ;
108
- subGroupsMap [ group ] = subGroups ;
109
- }
110
- expandedGroups = [ ..._ . concat ( expandedGroups , subGroups ) ] ;
111
- }
112
- // check if there is matched group
113
- // logger.debug('Groups', challenge.groups, userGroups)
114
- if ( _ . find ( expandedGroups , ( group ) => ! ! _ . find ( userGroups , ( ug ) => ug . id === group ) ) ) {
115
- res . push ( challenge ) ;
116
- }
117
- }
118
- }
119
- return res ;
120
- }
121
-
122
- /**
123
- * Ensure the user can access the challenge by groups access
124
- * @param {Object } currentUser the user who perform operation
125
- * @param {Object } challenge the challenge to check
126
- */
127
- async function ensureAccessibleByGroupsAccess ( currentUser , challenge ) {
128
- const filtered = await filterChallengesByGroupsAccess ( currentUser , [ challenge ] ) ;
129
- if ( filtered . length === 0 ) {
130
- throw new errors . ForbiddenError ( `ensureAccessibleByGroupsAccess :: You don't have access to this group!
131
- Current User: ${ JSON . stringify ( currentUser ) }
132
- Challenge: ${ JSON . stringify ( challenge ) }
133
- Filtered: ${ JSON . stringify ( filtered ) }
134
- ` ) ;
135
- }
136
- }
137
-
138
70
/**
139
71
* Search challenges by legacyId
140
72
* @param {Object } currentUser the user who perform operation
@@ -2288,10 +2220,8 @@ async function deleteChallenge(currentUser, challengeId) {
2288
2220
if ( ! challenge ) {
2289
2221
throw new errors . NotFoundError ( `Challenge with id: ${ challengeId } doesn't exist or is not in New status` ) ;
2290
2222
}
2291
- // check groups authorization
2292
- await ensureAccessibleByGroupsAccess ( currentUser , challenge ) ;
2293
- // check if user are allowed to delete the challenge
2294
- await ensureAccessibleForChallenge ( currentUser , challenge ) ;
2223
+ // ensure user can modify challenge
2224
+ await helper . ensureUserCanModifyChallenge ( currentUser , challenge ) ;
2295
2225
// delete DB record
2296
2226
const { items : deletedItems } = await challengeDomain . delete ( getLookupCriteria ( "id" , challengeId ) ) ;
2297
2227
if ( ! _ . find ( deletedItems , { id : challengeId } ) ) {
0 commit comments