From e39d13aa52db91905bca1c98a6517a8b45f1e6e4 Mon Sep 17 00:00:00 2001 From: Christian Boltz Date: Sat, 1 Nov 2014 19:51:25 +0000 Subject: [PATCH] PFAHandler: Add $can_edit and $can_delete flags. This makes it possible to make some, but not all items non-editable or non-deletable (based on a database column/query or read_from_db_postprocess()) - add $can_edit and $can_delete - after initStruct, check if $struct contains _can_edit and _can_delete. If not, fill with default values (allowed) - init(): set $this->can_edit and $this->can_delete (only in view/edit mode) - set(): abort if !$this->can_edit git-svn-id: https://svn.code.sf.net/p/postfixadmin/code/trunk@1716 a1433add-5e2c-0410-b055-b7f2511e0802 --- model/PFAHandler.php | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/model/PFAHandler.php b/model/PFAHandler.php index 8d42ae65..4b920328 100644 --- a/model/PFAHandler.php +++ b/model/PFAHandler.php @@ -66,6 +66,15 @@ abstract class PFAHandler { # filled in domain_from_id() via init() protected $domain = null; + # can this item be edited? + # filled in init() (only in edit mode) + protected $can_edit = 1; + + # can this item be deleted? + # filled in init() (only in edit mode) + protected $can_delete = 1; + # TODO: needs to be implemented in delete() + # structure of the database table, list, edit form etc. # filled in initStruct() protected $struct = array(); @@ -132,6 +141,22 @@ abstract class PFAHandler { $this->initStruct(); + if (!isset($this->struct['_can_edit'])) { + $this->struct['_can_edit'] = pacol( 0, 0, 1, 'vnum', '' , '' , '', '', + /*not_in_db*/ 0, + /*dont_write_to_db*/ 1, + /*select*/ '1 as _can_edit' + ); + } + + if (!isset($this->struct['_can_delete'])) { + $this->struct['_can_delete'] = pacol( 0, 0, 1, 'vnum', '' , '' , '', '', + /*not_in_db*/ 0, + /*dont_write_to_db*/ 1, + /*select*/ '1 as _can_delete' + ); + } + $struct_hook = Config::read($this->db_table . '_struct_hook'); if ( $struct_hook != 'NO' && function_exists($struct_hook) ) { $this->struct = $struct_hook($this->struct); @@ -239,11 +264,13 @@ abstract class PFAHandler { # } else { # return true; } - } else { # edit mode + } else { # view or edit mode if (!$exists) { $this->errormsg[$this->id_field] = Config::lang($this->msg['error_does_not_exist']); return false; -# } else { + } else { + $this->can_edit = $this->result['_can_edit']; + $this->can_delete = $this->result['_can_delete']; # return true; } } @@ -298,6 +325,11 @@ abstract class PFAHandler { * error messages (if any) are stored in $this->errormsg */ public function set($values) { + if ( !$this->can_edit ) { + $this->errormsg[] = Config::Lang_f('edit_not_allowed', $this->id); + return false; + } + if ($this->new == 1) { $values[$this->id_field] = $this->id; }