Skip to content

Commit 32d3fa9

Browse files
committed
Permission for readonly fields
1 parent 8113e97 commit 32d3fa9

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/ManyField.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,20 +228,32 @@ public function setCanAdd($bool) {
228228
* @return boolean
229229
*/
230230
public function canAdd() {
231+
if ($this->readonly) {
232+
return false;
233+
}
234+
231235
return $this->canAdd;
232236
}
233237

234238
/**
235239
* @return boolean
236240
*/
237241
public function canRemove() {
242+
if ($this->readonly) {
243+
return false;
244+
}
245+
238246
return $this->canRemove;
239247
}
240248

241249
/**
242250
* @return boolean
243251
*/
244252
public function canSort() {
253+
if ($this->readonly) {
254+
return false;
255+
}
256+
245257
return $this->canSort;
246258
}
247259

@@ -369,6 +381,10 @@ public function saveRecord()
369381
return Controller::curr()->httpError(400);
370382
}
371383

384+
if ($this->readonly) {
385+
return Controller::curr()->httpError(401);
386+
}
387+
372388
$index = Controller::curr()->getRequest()->requestVar('ID');
373389
$class = Controller::curr()->getRequest()->requestVar('ClassName');
374390

@@ -437,15 +453,14 @@ public function recordForm()
437453

438454
$record = $class::get()->byId($index);
439455

440-
if (!$record || !$record->canEdit()) {
456+
if (!$record || !$record->canView()) {
441457
return Controller::curr()->httpError(404);
442458
}
443459

444460
$response = new HTTPResponse();
445461

446462
$edit = $this->generateRow(0, $record, false);
447463
$edit->removeExtraClass('row manyfield__row');
448-
449464
$response->setBody($edit->FieldHolder());
450465

451466
return $response;
@@ -462,6 +477,10 @@ public function deleteRecord()
462477
return Controller::curr()->httpError(400);
463478
}
464479

480+
if ($this->readonly) {
481+
return Controller::curr()->httpError(401);
482+
}
483+
465484
$index = Controller::curr()->getRequest()->getVar('ID');
466485
$class = Controller::curr()->getRequest()->getVar('ClassName');
467486

@@ -638,6 +657,9 @@ public function generateRow($index, $value = null, $prefixName = true)
638657
$field = clone $child;
639658
$field = $this->updateManyNestedField($field, $index, $value, $prefixName);
640659

660+
$field = $field->setReadonly($this->readonly);
661+
$field = $field->setDisabled($this->readonly);
662+
641663
$row->push($field);
642664
}
643665

templates/ManyFieldModal.ss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
</div>
3636

3737
<div class="modal-footer">
38-
<button type="button" class="btn btn-primary manyfield__save">Save</button>
38+
<% if not isReadonly %><button type="button" class="btn btn-primary manyfield__save">Save</button><% end_if %>
3939
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
4040
</div>
4141
</div><!-- /.modal-content -->

0 commit comments

Comments
 (0)