Skip to content

Commit cb4eac9

Browse files
committed
MOBILE-3200 database: Check permissions on add and edit entry
1 parent b0b8062 commit cb4eac9

File tree

5 files changed

+74
-30
lines changed

5 files changed

+74
-30
lines changed

scripts/langindex.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,11 +507,13 @@
507507
"addon.mod_data.foundrecords": "data",
508508
"addon.mod_data.gettinglocation": "local_moodlemobileapp",
509509
"addon.mod_data.latlongboth": "data",
510+
"addon.mod_data.locationnotenabled": "local_moodlemobileapp",
510511
"addon.mod_data.locationpermissiondenied": "local_moodlemobileapp",
511512
"addon.mod_data.menuchoose": "data",
512513
"addon.mod_data.modulenameplural": "data",
513514
"addon.mod_data.more": "data",
514515
"addon.mod_data.mylocation": "local_moodlemobileapp",
516+
"addon.mod_data.noaccess": "data",
515517
"addon.mod_data.nomatch": "data",
516518
"addon.mod_data.norecords": "data",
517519
"addon.mod_data.notapproved": "data",

src/addon/mod/data/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"modulenameplural": "Databases",
2929
"more": "More",
3030
"mylocation": "My location",
31+
"noaccess": "You do not have access to this page",
3132
"nomatch": "No matching entries found!",
3233
"norecords": "No entries in database",
3334
"notapproved": "Entry is not approved yet.",

src/addon/mod/data/pages/edit/edit.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
</ion-select>
1919
</ion-item>
2020

21-
<div class="addon-data-contents addon-data-entries-{{data.id}}" *ngIf="data">
22-
<core-style [css]="data.csstemplate" prefix=".addon-data-entries-{{data.id}}"></core-style>
21+
<div class="addon-data-contents {{cssClass}}" *ngIf="data">
22+
<core-style [css]="data.csstemplate" prefix=".{{cssClass}}"></core-style>
2323

2424
<form (ngSubmit)="save($event)" [formGroup]="editForm" #editFormEl>
2525
<core-compile-html [text]="editFormRender" [jsData]="jsData" [extraImports]="extraImports"></core-compile-html>

src/addon/mod/data/pages/edit/edit.ts

Lines changed: 68 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class AddonModDataEditPage {
9393
* View loaded.
9494
*/
9595
ionViewDidLoad(): void {
96-
this.fetchEntryData();
96+
this.fetchEntryData(true);
9797
}
9898

9999
/**
@@ -126,38 +126,78 @@ export class AddonModDataEditPage {
126126
/**
127127
* Fetch the entry data.
128128
*
129+
* @param [refresh] To refresh all downloaded data.
129130
* @return Resolved when done.
130131
*/
131-
protected fetchEntryData(): Promise<any> {
132-
return this.dataProvider.getDatabase(this.courseId, this.module.id).then((data) => {
133-
this.title = data.name || this.title;
134-
this.data = data;
135-
this.cssClass = 'addon-data-entries-' + data.id;
136-
137-
return this.dataProvider.getDatabaseAccessInformation(data.id, {cmId: this.module.id});
138-
}).then((accessData) => {
139-
if (this.entryId) {
140-
return this.groupsProvider.getActivityGroupInfo(this.data.coursemodule).then((groupInfo) => {
141-
this.groupInfo = groupInfo;
142-
this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, groupInfo);
143-
});
144-
}
145-
}).then(() => {
146-
return this.dataProvider.getFields(this.data.id, {cmId: this.module.id});
147-
}).then((fieldsData) => {
148-
this.fieldsArray = fieldsData;
149-
this.fields = this.utils.arrayToObject(fieldsData, 'id');
150-
151-
return this.dataHelper.fetchEntry(this.data, fieldsData, this.entryId);
152-
}).then((entry) => {
132+
protected async fetchEntryData(refresh: boolean = false): Promise<void> {
133+
try {
134+
this.data = await this.dataProvider.getDatabase(this.courseId, this.module.id);
135+
this.title = this.data.name || this.title;
136+
this.cssClass = 'addon-data-entries-' + this.data.id;
137+
138+
this.fieldsArray = await this.dataProvider.getFields(this.data.id, {cmId: this.module.id});
139+
this.fields = this.utils.arrayToObject(this.fieldsArray, 'id');
140+
141+
const entry = await this.dataHelper.fetchEntry(this.data, this.fieldsArray, this.entryId);
142+
153143
this.entry = entry.entry;
154144

145+
// Load correct group.
146+
this.selectedGroup = this.selectedGroup == null ? this.entry.groupid : this.selectedGroup;
147+
148+
// Check permissions when adding a new entry or offline entry.
149+
if (!this.isEditing) {
150+
let haveAccess = false;
151+
152+
if (refresh) {
153+
this.groupInfo = await this.groupsProvider.getActivityGroupInfo(this.data.coursemodule);
154+
this.selectedGroup = this.groupsProvider.validateGroupId(this.selectedGroup, this.groupInfo);
155+
this.initialSelectedGroup = this.selectedGroup;
156+
}
157+
158+
if (this.groupInfo.groups.length > 0) {
159+
if (refresh) {
160+
const canAddGroup = {};
161+
162+
await Promise.all(this.groupInfo.groups.map(async (group) => {
163+
const accessData = await this.dataProvider.getDatabaseAccessInformation(this.data.id, {
164+
cmId: this.module.id, groupId: group.id});
165+
166+
canAddGroup[group.id] = accessData.canaddentry;
167+
}));
168+
169+
this.groupInfo.groups = this.groupInfo.groups.filter((group) => {
170+
return !!canAddGroup[group.id];
171+
});
172+
173+
haveAccess = canAddGroup[this.selectedGroup];
174+
} else {
175+
// Groups already filtered, so it have access.
176+
haveAccess = true;
177+
}
178+
} else {
179+
const accessData = await this.dataProvider.getDatabaseAccessInformation(this.data.id, {cmId: this.module.id});
180+
haveAccess = accessData.canaddentry;
181+
}
182+
183+
if (!haveAccess) {
184+
// You shall not pass, go back.
185+
this.domUtils.showErrorModal('addon.mod_data.noaccess', true);
186+
187+
// Go back to entry list.
188+
this.forceLeave = true;
189+
this.navCtrl.pop();
190+
191+
return;
192+
}
193+
}
194+
155195
this.editFormRender = this.displayEditFields();
156-
}).catch((message) => {
196+
} catch (message) {
157197
this.domUtils.showErrorModalDefault(message, 'core.course.errorgetmodule', true);
158-
}).finally(() => {
159-
this.loaded = true;
160-
});
198+
}
199+
200+
this.loaded = true;
161201
}
162202

163203
/**
@@ -166,7 +206,7 @@ export class AddonModDataEditPage {
166206
* @param e Event.
167207
* @return Resolved when done.
168208
*/
169-
save(e: Event): Promise<any> {
209+
save(e: Event): Promise<void> {
170210
e.preventDefault();
171211
e.stopPropagation();
172212

src/assets/lang/en.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@
513513
"addon.mod_data.modulenameplural": "Databases",
514514
"addon.mod_data.more": "More",
515515
"addon.mod_data.mylocation": "My location",
516+
"addon.mod_data.noaccess": "You do not have access to this page",
516517
"addon.mod_data.nomatch": "No matching entries found!",
517518
"addon.mod_data.norecords": "No entries in database",
518519
"addon.mod_data.notapproved": "Entry is not approved yet.",

0 commit comments

Comments
 (0)