@@ -60,6 +60,23 @@ async function ensureAccessibleByGroupsAccess (currentUser, challenge) {
60
60
}
61
61
}
62
62
63
+ /**
64
+ * Ensure the user can access the groups being updated to
65
+ * @param {Object } currentUser the user who perform operation
66
+ * @param {Object } data the challenge data to be updated
67
+ * @param {String } challenge the original challenge data
68
+ */
69
+
70
+ async function ensureAcessibilityToModifiedGroups ( currentUser , data , challenge ) {
71
+ const userGroups = await helper . getUserGroups ( currentUser . userId )
72
+ const userGroupsNames = _ . map ( userGroups , group => group . name )
73
+ const updatedGroups = _ . difference ( _ . union ( challenge . groups , data . groups ) , _ . intersection ( challenge . groups , data . groups ) )
74
+ const filtered = updatedGroups . filter ( g => ! userGroupsNames . includes ( g ) )
75
+ if ( filtered . length > 0 ) {
76
+ throw new errors . ForbiddenError ( `You don't have access to this group!` )
77
+ }
78
+ }
79
+
63
80
/**
64
81
* Search challenges
65
82
* @param {Object } currentUser the user who perform operation
@@ -668,6 +685,11 @@ async function update (currentUser, challengeId, data, userToken, isFull) {
668
685
await ensureAccessibleByGroupsAccess ( currentUser , challenge )
669
686
console . log ( 'After checking group access' )
670
687
688
+ // check groups access to be updated group values
689
+ if ( data . groups ) {
690
+ await ensureAcessibilityToModifiedGroups ( currentUser , data , challenge )
691
+ }
692
+
671
693
console . log ( 'before fetching attachments' )
672
694
let newAttachments
673
695
if ( isFull || ! _ . isUndefined ( data . attachmentIds ) ) {
0 commit comments