DB zasob ID * [name] => Table name * [opis] => Table opis * [fields] => array( * [$fieldID] => array( * [name] => name * [perms] => perms (FORM_TREAT) * [opis] => opis * ) * ) * [types] => array( * [$fieldID] => array( * [type] => type * [null] => bool * [default] => default value * ) * ) * ); */ class TableAcl { private $_zasobID = ''; private $_db = ''; private $_name = ''; private $_label = ''; private $_opis = ''; private $_fields = array(); private $_types = array(); private $_virtualFieldsIdList = array(); public function __construct($zasobID) { $this->_zasobID = $zasobID; } public function getID() { return $this->_zasobID; } public function setName($name) { $this->_name = $name; } public function setNameByTableId($tableID) { //used for init without knowing table name $sql="select `DESC` from CRM_LISTA_ZASOBOW where ID=".$tableID." and `TYPE`='TABELA'"; $res=DB::query($sql); $res_=DB::fetch($res); //DEBUG_S(-3,'setNameByTableId',$res_,__FILE__,__FUNCTION__,__LINE__); self::setName($res->DESC); } public function getName() { return $this->_name; } public function setOpis($opis) { $this->_opis = $opis; } public function getOpis() { return $this->_opis; } public function setLabel($label) { $this->_label = $label; } public function getLabel() { return $this->_label; } public function getRawLabel($posLimit = 20) { $label = $this->_label; if (empty($label) && !empty($this->_opis)) { $label = $this->_opis; if (mb_strlen($this->_opis) > $posLimit) { $pos = strpos($this->_opis, ' - '); if ($pos > $posLimit || $pos < 5) { $pos = $posLimit; $label = mb_substr($this->_opis, 0, $posLimit, 'utf-8') . '...'; } else { $label = mb_substr($this->_opis, 0, $pos, 'utf-8'); } } } if (empty($label)) { $label = $this->_name; } return $label; } public function getShortLabel($posLimit = 20) { $shortLabel = $this->getRawLabel($posLimit); $opis = $this->_opis; $shortLabel = '' . $shortLabel . ''; return $shortLabel; } public function getLongLabel($posLimit = 30) { $longLabel = $this->getRawLabel($posLimit); $opis = $this->_opis; if ($longLabel != $this->_name) { $longLabel .= ' ' . $this->_name . ''; } $longLabel = '' . $longLabel . ''; return $longLabel; } public function setDB($db) { $this->_db = $db; } public function getDB() { return $this->_db; } public function addField($fieldID, $name, $opis, $sort_prio, $label = '') { $field = array(); $field['name'] = $name; $field['perms'] = ''; $field['opis'] = $opis; $field['sort_prio'] = $sort_prio; $field['label'] = $label; $this->_fields[$fieldID] = $field; } public function getTableDbId($tableID) { return $this->_db; } public function getField($fieldID) { return $this->_fields[$fieldID]; } public function hasField($fieldID) { return array_key_exists($fieldID, $this->_fields); } public function removeField($fieldID) { if (array_key_exists($fieldID, $this->_fields)) { unset($this->_fields[$fieldID]); } } public function getFields() { return $this->_fields; } public function setFieldPerms($fieldID, $perms) { if (array_key_exists($fieldID, $this->_fields)) { $this->_fields[$fieldID]['perms'] .= $perms; } } public function getFieldPerms($fieldID) { if (array_key_exists($fieldID, $this->_fields)) { $perms = V::get('perms', '', $this->_fields[$fieldID]); if ($perms) { return implode(',', array_unique(str_split($perms))); } } return ''; } public function hasFieldPerm($fieldID, $perm) { if (array_key_exists($fieldID, $this->_fields)) { if (false !== strpos($this->_fields[$fieldID]['perms'], $perm)) { return true; } return false; } return false; } public function getFieldIdByName($fieldName) { $fieldID = 0; if (empty($fieldName)) { return; } foreach ($this->_fields as $kID => $vField) { if ($vField['name'] == $fieldName) { $fieldID = $kID; } } return $fieldID; } public function hasSuperAccessPerms() { foreach ($this->_fields as $kFldID => $vFld) { if ($this->hasFieldPerm($kFldID, 'S')) { return true; } else if ($this->hasFieldPerm($kFldID, 'V')) { return true; } } return false; } public function hasPermSuperWrite() { foreach ($this->_fields as $kFldID => $vFld) { if ($this->hasFieldPerm($kFldID, 'S')) { return true; } } return false; } /** * */ public function canWriteRecord($record) { $dbgArr = array(); $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : ''; $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : ''; $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : ''; $dbgArr['user_groups'] = User::getLdapGroupsNames(); if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'
dbgArr (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($dbgArr);echo'';}
if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo 'true - is record owner
';} return true; } if ($dbgArr['record_write']) { if (in_array($dbgArr['record_write'], $dbgArr['user_groups'])) { if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo 'true - has group write
';} return true; } } else { if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo 'true - group write not set
';} return true; } return false; } public function canReadRecord($record) { $dbgArr = array(); $dbgArr['record_owner'] = (isset($record->L_APPOITMENT_USER))? $record->L_APPOITMENT_USER : ''; $dbgArr['record_write'] = (isset($record->A_ADM_COMPANY))? $record->A_ADM_COMPANY : ''; $dbgArr['record_read'] = (isset($record->A_CLASSIFIED))? $record->A_CLASSIFIED : ''; $dbgArr['user_groups'] = User::getLdapGroupsNames(); if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo'record('.$record->ID.') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($dbgArr);echo'';}
if ($dbgArr['record_owner'] && $dbgArr['record_owner'] == User::getLogin()) {
if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo 'true - is record owner
';} return true; } if ($dbgArr['record_read']) { if (in_array($dbgArr['record_read'], $dbgArr['user_groups'])) { if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo 'true - has group read
';} return true; } } else { if(V::get('DBG_ACL', '', $_REQUEST) > 2){echo 'true - group read not set
';} return true; } return false; } /** * @param $taskPerm - 'C', 'W', 'R' */ public function isAllowed($fieldID, $taskPerm, $record = null) { if (!in_array($taskPerm, array('C', 'W', 'R'))) { return false; } $adminFields = array(); $adminFields[] = 'ID'; $adminFields[] = 'A_RECORD_CREATE_DATE'; $adminFields[] = 'A_RECORD_CREATE_AUTHOR'; $adminFields[] = 'A_RECORD_UPDATE_DATE'; $adminFields[] = 'A_RECORD_UPDATE_AUTHOR'; $fieldName = $this->_fields[$fieldID]['name']; if ($taskPerm == 'R' && in_array($fieldName, $adminFields)) { return true; } // check perm: allow 'RS', 'WS' - can R/W field even if cant read record // check 'O' - can read field even if cant read field but can read record if(V::get('DBG_ACL', '', $_REQUEST) > 1){ echo' (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r(array('Field'=>$fieldID.'('.$fieldName.')'
,'taskPerm'=>$taskPerm
,'canReadRecord'=>'"'.$this->canReadRecord($record).'"'
,'hasFieldPerm(O) || canWriteRecord'=>'"'.$this->hasFieldPerm($fieldID, 'O').'" || "'.$this->canReadRecord($record).'"'
,'hasFieldPerm(S)'=>'"'.$this->hasFieldPerm($fieldID, 'S').'"'
,'hasFieldPerm(V)'=>'"'.$this->hasFieldPerm($fieldID, 'V').'"'
));echo''; }
if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
if ($taskPerm == 'R' && $this->hasFieldPerm($fieldID, 'V')) {
return true;
} else if ($taskPerm == 'R'
&& $record
&& $this->hasFieldPerm($fieldID, 'O')
&& ($this->canReadRecord($record) || $this->canWriteRecord($record))
) {
return true;// 'WO' or 'CO'
}
return false;
}
// check 'R' - require can read record, or V - Super View
if ($taskPerm == 'R') {
if ($this->canReadRecord($record) || $this->hasFieldPerm($fieldID, 'V')) {
return true;
} else {
return false;
}
}
// 'C' and 'W' require colType
$colType = $this->getFieldTypeById($fieldID);
if (!$colType) {
return false;
}
if ($taskPerm == 'W') {
if ($record) {
if(V::get('DBG_ACL', '', $_REQUEST) > 1){echo '(Field: '.$fieldID.', canWriteRecord: ' . $this->canWriteRecord($record) . ' || (hasFieldPerm(S): ' . $this->hasFieldPerm($fieldID, 'S') . ' && hasFieldPerm(W): ' . $this->hasFieldPerm($fieldID, 'W') . '))';}
return ($this->canWriteRecord($record)|| $this->hasFieldPerm($fieldID, 'S'));
}
}
return true;
}
/**
* @param $taskPerm - 'C', 'W'
*/
public function showFormItem($taskPerm, $fieldID, $fName, $fValue, $params = array(), $record = null) {
$out = '';
if (!$this->isAllowed($fieldID, $taskPerm, $record)) {
if ($taskPerm == 'R') {
$out .= 'Brak uprawnień do odczytu';
}
else if ($taskPerm == 'W') {
$out .= 'Brak uprawnień do zapisu';
} else {
$out .= 'Brak uprawnień do tego pola (' . $taskPerm . ')';
}
return $out;
}
$colName = $this->_fields[$fieldID]['name'];
if ($colName == 'ID') {
return $out;
}
$colType = $this->getFieldTypeById($fieldID);
if (!$colType) {
$out .= 'Error - unknown type';
return $out;
}
Lib::loadClass('Typespecial');
$typeSpecial = Typespecial::getInstance($fieldID, $colName);
$html = new stdClass();
$html->_params = array();
$html->tag = 'input';
$html->cnt = '';
$html->attrs = array();
$html->attrs['id'] = $fName;
$html->attrs['name'] = $fName;
$html->attrs['type'] = 'text';
$html->attrs['value'] = htmlspecialchars($fValue);
if (isset($params['tabindex'])) {
$html->attrs['tabindex'] = $params['tabindex'];
}
if (!$this->hasFieldPerm($fieldID, $taskPerm)) {
$html->attrs['disabled'] = 'disabled';
}
$maxGrid = V::get('maxGrid', 10, $params);
if (substr($colType['type'], 0, 3) == 'int'
|| substr($colType['type'], 0, 7) == 'tinyint'
|| substr($colType['type'], 0, 8) == 'smallint'
|| substr($colType['type'], 0, 6) == 'bigint'
) {
//$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 4));
$html->attrs['type'] = 'number';
$html->attrs['class'][] = 'input-small';
}
else if (substr($colType['type'], 0, 6) == 'double') {
$html->attrs['type'] = 'text';
$html->attrs['class'][] = 'input-small';
}
else if (substr($colType['type'], 0, 7) == 'decimal') {
$html->attrs['type'] = 'text';
$html->attrs['class'][] = 'input-small';
}
else if (substr($colType['type'], 0, 7) == 'varchar'
|| substr($colType['type'], 0, 4) == 'char'
) {
//$h->Type_value = (int)str_replace(array(' ','(',')'), '', substr($h->Type, 8));
$html->attrs['type'] = 'text';
$maxLength = (int)str_replace(array(' ','(',')'), '', substr($colType['type'], strpos($colType['type'], '(') + 1, -1));
if ($maxLength > 0) {
$html->attrs['maxlength'] = $maxLength;
}
$valLength = strlen($fValue);
if (isset($params['widthClass'])) {
if ($params['widthClass'] == 'inside-modal') {
$html->attrs['style'] = 'width:98%;';
} else {
$html->attrs['style'] = 'width:98%;';
}
} else {
/*
if ($maxLength < 11) {
$html->attrs['class'][] = 'span2';
} else if ($maxLength < 31) {
$html->attrs['class'][] = 'span5';
} else if ($maxLength < 51) {
$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
} else if ($maxLength < 101) {
$html->attrs['class'][] = (10 <= $maxGrid)? 'span10' : "span{$maxGrid}";
} else {
$html->attrs['class'][] = (12 <= $maxGrid)? 'span12' : "span{$maxGrid}";
}
*/
}
}
else if (substr($colType['type'], 0, 4) == 'date') {
$testDatePicker = true;
if ($testDatePicker) {
$html->attrs['type'] = 'text';
$html->_params[] = 'date';
if (substr($colType['type'], 0, 8) == 'datetime') {
$html->attrs['class'][] = 'se_type-datetime';// datetimepicker';
$html->attrs['data-format'] = 'yyyy-MM-dd hh:mm';
$html->attrs['maxlength'] = 19;
} else {
$html->attrs['class'][] = 'se_type-date';// datetimepicker';
$html->attrs['maxlength'] = 10;
}
if (substr($html->attrs['value'], 0, 10) == '0000-00-00') {
$html->attrs['value'] = '';
}
} else {
$html->attrs['type'] = 'date';
}
}
else if ($colType['type'] == 'time') {
$testDatePicker = true;
if ($testDatePicker) {
$html->attrs['type'] = 'text';
$html->_params[] = 'time';
$html->attrs['class'][] = 'se_type-time';// datetimepicker';
$html->attrs['data-format'] = 'hh:mm:ss';
$html->attrs['maxlength'] = 8;
if (substr($html->attrs['value'], 0, 8) == '00:00:00') {
$html->attrs['value'] = '';
}
} else {
$html->attrs['type'] = 'time';
}
}
else if (substr($colType['type'], 0, 4) == 'enum') {
unset($html->attrs['type']);
unset($html->attrs['value']);
$html->tag = 'select';
$values = explode(',', str_replace(array('(',')',"'",'"'), '', substr($colType['type'], 5)));
$selValue = $fValue;
if (empty($selValue) && $selValue !== '0' && !empty($colType['default'])) {
if ($taskPerm == 'C') {
$selValue = $colType['default'];
} else if ($taskPerm == 'W' && $this->isAllowed($fieldID, 'R', $record)) {
$selValue = $colType['default'];
}
}
$html->cnt .= '';
if (!empty($selValue) && !in_array($selValue, $values)) {
$html->cnt .= '';
}
foreach ($values as $val) {
$sel = ($selValue == $val)? ' selected="selected"' : '';
$html->cnt .= '';
}
}
else if (substr($colType['type'], 0, 4) == 'text'
|| substr($colType['type'], 0, 8) == 'tinytext'
|| substr($colType['type'], 0, 10) == 'mediumtext'
|| substr($colType['type'], 0, 8) == 'longtext'
) {
$html->tag = 'textarea';
$html->cnt = htmlspecialchars($fValue);
if (isset($params['widthClass'])) {
if ($params['widthClass'] == 'inside-modal') {
$html->attrs['style'] = 'width:98%;';
} else {
$html->attrs['style'] = 'width:98%;';
}
} else {
//$html->attrs['class'][] = (8 <= $maxGrid)? 'span8' : "span{$maxGrid}";
}
$html->attrs['rows'] = '3';
unset($html->attrs['type']);
unset($html->attrs['value']);
}
else if ('polygon' == $colType['type']) { return '...'; }// Wielokąt
else if ('multipolygon' == $colType['type']) { return '...'; }// Zbiór wielokątów
else if ('linestring' == $colType['type']) { return '...'; }// Krzywa z interpolacji liniowej pomiędzy punktami
else if ('point' == $colType['type']) { return '...'; }// Punkt w przestrzeni 2-wymiarowej
else if ('geometry' == $colType['type']) { return '...'; }// Typy, które mogą przechowywać geometrię dowolnego typu
else if ('multipoint' == $colType['type']) { return '...'; }// Zbiór punktów
else if ('multilinestring' == $colType['type']) { return '...'; }// Zbiór krzywych z interpolacji liniowej pomiędzy punktami
else if ('geometrycollection' == $colType['type']) { return '...'; }// Zbiór obiektów geometrycznych dowolnego typu
else {
return 'unknown Type "'.$colType['type'].'"';
}
$html->attrs['class'][] = 'form-control';
$attrsOut = array();
foreach ($html->attrs as $k => $v) {
if (is_array($v)) $v = implode(' ', $v);
$attrsOut[] = "{$k}=\"{$v}\"";
}
if (in_array($html->tag, array('select', 'textarea'))) {
$out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . '>';
$out .= $html->cnt;
$out .= '' . $html->tag . '>';
} else {
$out .= '<' . $html->tag . '' . (($attrsOut)? ' ' . implode(' ', $attrsOut) : '') . ' />';
}
if (in_array('date', $html->_params)) {
$out = '