Skip to content

Commit 89398dd

Browse files
authored
Merge pull request formio#2141 from formio/bugfix/user-permissions
Fixed userPermissions function not taking into account Group Permissions
2 parents c52c7a4 + 2c95085 commit 89398dd

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

Changelog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file
44
The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

7+
## [Unreleased]
8+
### Fixed
9+
- ```userPermissions``` function not taking into account Group Permissions
10+
711
## 4.8.0-beta.5
812
### Fixed
913
- Problem where the accordion parent names are incorrect.

src/Formio.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import _intersection from 'lodash/intersection';
1111
import _get from 'lodash/get';
1212
import _cloneDeep from 'lodash/cloneDeep';
1313
import _defaults from 'lodash/defaults';
14+
import { eachComponent } from './utils/utils';
15+
1416
const { 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

Comments
 (0)