Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CRM-20988,Don't display option of Export, Delete etc to users who lack that permission #10791

Merged
merged 2 commits into from
Aug 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions CRM/Batch/BAO/Batch.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ public static function getBatchList(&$params) {

switch ($batchStatusByName[$values['status_id']]) {
case 'Open':
case 'Reopened':
CRM_Utils_Array::remove($newLinks, 'reopen', 'download');
break;

Expand All @@ -300,6 +301,15 @@ public static function getBatchList(&$params) {
case 'Exported':
CRM_Utils_Array::remove($newLinks, 'close', 'edit', 'reopen', 'export');
}
if (!CRM_Batch_BAO_Batch::checkBatchPermission('edit', $values['created_id'])) {
CRM_Utils_Array::remove($newLinks, 'close', 'edit', 'export');
}
if (!CRM_Batch_BAO_Batch::checkBatchPermission('export', $values['created_id'])) {
CRM_Utils_Array::remove($newLinks, 'export', 'download');
}
if (!CRM_Batch_BAO_Batch::checkBatchPermission('delete', $values['created_id'])) {
CRM_Utils_Array::remove($newLinks, 'delete');
}
}
if (!empty($values['type_id'])) {
$values['batch_type'] = $batchTypes[$values['type_id']];
Expand Down Expand Up @@ -381,6 +391,15 @@ public static function whereClause($params) {
"created_id.sort_name",
"created_id",
);
if (!CRM_Core_Permission::check("view all manual batches")) {
if (CRM_Core_Permission::check("view own manual batches")) {
$loggedInContactId = CRM_Core_Session::singleton()->get('userID');
$params['created_id'] = $loggedInContactId;
}
else {
$params['created_id'] = 0;
}
}
foreach ($return as $field) {
if (!isset($params[$field])) {
continue;
Expand Down Expand Up @@ -775,4 +794,32 @@ public static function getBatchStatuses($batchIds) {
return $batches;
}

/**
* Function to check permission for batch.
*
* @param string $action
* @param int $batchCreatedId
* batch created by contact id
*
* @return bool
*/
public static function checkBatchPermission($action, $batchCreatedId = NULL) {
if (in_array($action, array('reopen', 'close'))) {
$action = 'edit';
}
if (CRM_Core_Permission::check("{$action} all manual batches")) {
return TRUE;
}
if (CRM_Core_Permission::check("{$action} own manual batches")) {
$loggedInContactId = CRM_Core_Session::singleton()->get('userID');
if ($batchCreatedId == $loggedInContactId) {
return TRUE;
}
elseif (CRM_Utils_System::isNull($batchCreatedId)) {
return TRUE;
}
}
return FALSE;
}

}
10 changes: 7 additions & 3 deletions CRM/Financial/Form/BatchTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function preProcess() {
$validStatus = TRUE;
}
$this->assign('validStatus', $validStatus);

$this->_values = civicrm_api3('Batch', 'getSingle', array('id' => self::$_entityID));
$batchTitle = CRM_Core_DAO::getFieldValue('CRM_Batch_BAO_Batch', self::$_entityID, 'title');
CRM_Utils_System::setTitle(ts('Accounting Batch - %1', array(1 => $batchTitle)));

Expand Down Expand Up @@ -100,8 +100,12 @@ public function buildQuickForm() {
}

parent::buildQuickForm();
$this->add('submit', 'close_batch', ts('Close Batch'));
$this->add('submit', 'export_batch', ts('Close & Export Batch'));
if (CRM_Batch_BAO_Batch::checkBatchPermission('edit', $this->_values['created_id'])) {
$this->add('submit', 'close_batch', ts('Close Batch'));
if (CRM_Batch_BAO_Batch::checkBatchPermission('export', $this->_values['created_id'])) {
$this->add('submit', 'export_batch', ts('Close & Export Batch'));
}
}

// text for sort_name
$this->addElement('text',
Expand Down
5 changes: 5 additions & 0 deletions CRM/Financial/Form/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ public function buildQuickForm() {
'delete' => ts('Delete'),
);

foreach ($batchAction as $action => $ignore) {
if (!CRM_Batch_BAO_Batch::checkBatchPermission($action)) {
unset($batchAction[$action]);
}
}
$this->add('select',
'batch_update',
ts('Task'),
Expand Down