@@ -11,6 +11,8 @@ import _intersection from 'lodash/intersection';
1111import _get from 'lodash/get' ;
1212import _cloneDeep from 'lodash/cloneDeep' ;
1313import _defaults from 'lodash/defaults' ;
14+ import { eachComponent } from './utils/utils' ;
15+
1416const { fetch, Headers } = fetchPonyfill ( {
1517 Promise : NativePromise
1618} ) ;
@@ -558,10 +560,12 @@ export default class Formio {
558560 return NativePromise . all ( [
559561 ( form !== undefined ) ? NativePromise . resolve ( form ) : this . loadForm ( ) ,
560562 ( user !== undefined ) ? NativePromise . resolve ( user ) : this . currentUser ( ) ,
563+ ( submission !== undefined ) ? NativePromise . resolve ( submission ) : this . loadSubmission ( ) ,
561564 this . accessInfo ( )
562565 ] ) . then ( ( results ) => {
563566 const form = results . shift ( ) ;
564567 const user = results . shift ( ) || { _id : false , roles : [ ] } ;
568+ const submission = results . shift ( ) ;
565569 const access = results . shift ( ) ;
566570 const permMap = {
567571 create : 'create' ,
@@ -605,6 +609,39 @@ export default class Formio {
605609 }
606610 }
607611 }
612+ // check for Group Permissions
613+ if ( submission ) {
614+ // we would anyway need to loop through components for create permission, so we'll do that for all of them
615+ eachComponent ( form . components , ( component , path ) => {
616+ if ( component && component . defaultPermission ) {
617+ // we assume that there might be only single value of group component
618+ const value = _get ( submission . data , path ) ;
619+ if (
620+ value && value . _id && // group id is present
621+ user . roles . indexOf ( value . _id ) > - 1 // user has group id in his roles
622+ ) {
623+ if ( component . defaultPermission === 'read' ) {
624+ perms [ permMap . read ] = true ;
625+ }
626+ if ( component . defaultPermission === 'create' ) {
627+ perms [ permMap . create ] = true ;
628+ perms [ permMap . read ] = true ;
629+ }
630+ if ( component . defaultPermission === 'write' ) {
631+ perms [ permMap . create ] = true ;
632+ perms [ permMap . read ] = true ;
633+ perms [ permMap . update ] = true ;
634+ }
635+ if ( component . defaultPermission === 'admin' ) {
636+ perms [ permMap . create ] = true ;
637+ perms [ permMap . read ] = true ;
638+ perms [ permMap . update ] = true ;
639+ perms [ permMap . delete ] = true ;
640+ }
641+ }
642+ }
643+ } ) ;
644+ }
608645 return perms ;
609646 } ) ;
610647 }
0 commit comments