Skip to content

Commit

Permalink
Add weights to membership type links
Browse files Browse the repository at this point in the history
This includes moving the permissioning of those links to the financialacls extension
(since there was already an affected test in that extension too).
  • Loading branch information
eileenmcnaughton committed Sep 2, 2023
1 parent 9d477b4 commit ce6cca3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 13 deletions.
14 changes: 6 additions & 8 deletions CRM/Member/Page/MembershipType.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,26 @@ public function &links() {
'url' => 'civicrm/admin/member/membershipType/add',
'qs' => 'action=update&id=%%id%%&reset=1',
'title' => ts('Edit Membership Type'),
'weight' => CRM_Core_Action::getWeight(CRM_Core_Action::UPDATE),
],
CRM_Core_Action::DISABLE => [
'name' => ts('Disable'),
'ref' => 'crm-enable-disable',
'title' => ts('Disable Membership Type'),
'weight' => CRM_Core_Action::getWeight(CRM_Core_Action::DISABLE),
],
CRM_Core_Action::ENABLE => [
'name' => ts('Enable'),
'ref' => 'crm-enable-disable',
'title' => ts('Enable Membership Type'),
'weight' => CRM_Core_Action::getWeight(CRM_Core_Action::ENABLE),
],
CRM_Core_Action::DELETE => [
'name' => ts('Delete'),
'url' => 'civicrm/admin/member/membershipType/add',
'qs' => 'action=delete&id=%%id%%',
'title' => ts('Delete Membership Type'),
'weight' => CRM_Core_Action::getWeight(CRM_Core_Action::DELETE),
],
];
}
Expand All @@ -86,9 +90,9 @@ public function run() {
/**
* Browse all membership types.
*
* @return void
* @throws \CRM_Core_Exception
*/
public function browse() {
public function browse(): void {
// Ensure an action is assigned, even null - since this page is overloaded for other uses
// we need to avoid e-notices.
$this->assign('action');
Expand Down Expand Up @@ -137,12 +141,6 @@ public function browse() {
);
}
}
if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('edit contributions of type ' . CRM_Contribute_PseudoConstant::financialType($type['financial_type_id']))) {
unset($links[CRM_Core_Action::UPDATE], $links[CRM_Core_Action::ENABLE], $links[CRM_Core_Action::DISABLE]);
}
if (CRM_Financial_BAO_FinancialType::isACLFinancialTypeStatus() && !CRM_Core_Permission::check('delete contributions of type ' . CRM_Contribute_PseudoConstant::financialType($type['financial_type_id']))) {
unset($links[CRM_Core_Action::DELETE]);
}
// form all action links
$action = array_sum(array_keys($this->links()));

Expand Down
7 changes: 5 additions & 2 deletions CRM/PCP/BAO/PCP.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public static function getPcpDashboardInfo($contactId) {
$params = [1 => [$contactId, 'Integer']];
$pcpInfoDao = CRM_Core_DAO::executeQuery($query, $params);

$links = self::pcpLinks();
$hide = $mask = array_sum(array_keys($links['all']));
$approved = CRM_Core_PseudoConstant::getKey('CRM_PCP_BAO_PCP', 'status_id', 'Approved');
$contactPCPPages = [];
$pcpInfo = [];

while ($pcpInfoDao->fetch()) {
$links = self::pcpLinks($pcpInfoDao->id);
$hide = $mask = array_sum(array_keys($links['all']));
$mask = $hide;
if ($links) {
$replace = [
Expand Down Expand Up @@ -274,6 +274,9 @@ public static function honorRoll($pcpId) {
* (reference) of action links
*/
public static function &pcpLinks($pcpId = NULL) {
if (!$pcpId) {
CRM_Core_Error::deprecatedWarning('pcpId should be provided to render links');
}
if (!(self::$_pcpLinks)) {
$deleteExtra = ts('Are you sure you want to delete this Personal Campaign Page?') . '\n' . ts('This action cannot be undone.');

Expand Down
2 changes: 1 addition & 1 deletion CRM/Utils/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ public static function postCommit($op, $objectName, $objectId, $objectRef = NULL
* The unique identifier for the object.
* @param array $links
* (optional) the links array (introduced in v3.2).
* @param int $mask
* @param int|null $mask
* (optional) the bitmask to show/hide links.
* @param array $values
* (optional) the values to fill the links.
Expand Down
19 changes: 19 additions & 0 deletions ext/financialacls/financialacls.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,22 @@ function financialacls_civicrm_alterMenu(array &$menu): void {
}
$menu['civicrm/admin/financial/financialType']['access_arguments'] = [['administer CiviCRM Financial Types']];
}

function financialacls_civicrm_links(string $op, ?string $objectName, ?int $objectID, array &$links, ?int &$mask, array &$values) {
if ($objectName === 'MembershipType') {
$financialType = CRM_Core_PseudoConstant::getName('CRM_Member_BAO_MembershipType', 'financial_type_id', CRM_Member_BAO_MembershipType::getMembershipType($objectID)['financial_type_id']);
$hasEditPermission = CRM_Core_Permission::check('edit contributions of type ' . $financialType);
$hasDeletePermission = CRM_Core_Permission::check('delete contributions of type ' . $financialType);
if (!$hasDeletePermission || !$hasEditPermission) {
foreach ($links as $index => $link) {
if (!$hasEditPermission && in_array($link['name'], ['Edit', 'Enable', 'Disable'], TRUE)) {
unset($links[$index]);
}
if (!$hasDeletePermission && $link['name'] === 'Delete') {
unset($links[$index]);
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,31 @@ public function testMembershipTypePage(): void {
$assigned = \CRM_Core_Smarty::singleton()->get_template_vars();
$this->assertArrayNotHasKey($types['Forbidden']['id'], $assigned['rows']);
$this->assertArrayHasKey($types['Go for it']['id'], $assigned['rows']);
$links = $assigned['rows'][$types['Go for it']['id']]['action'];
$this->assertStringContainsString("title='Edit Membership Type' ", $links);
$this->assertStringContainsString("title='Disable Membership Type' ", $links);
$this->assertStringContainsString("title='Delete Membership Type' ", $links);

// Now check that the edit & delete links are removed if we remove those permissions.
$permissions = \CRM_Core_Config::singleton()->userPermissionClass->permissions;
foreach ($permissions as $index => $permission) {
if (in_array($permission, ['edit contributions of type Donation', 'delete contributions of type Donation'], TRUE)) {
unset($permissions[$index]);
}
}
$this->setPermissions($permissions);
$page->browse();
$assigned = \CRM_Core_Smarty::singleton()->get_template_vars();
$this->assertEquals('<span></span>', $assigned['rows'][$types['Go for it']['id']]['action']);
}

/**
* Set up a membership scenario where the user can access one type but not the other.
*
* @return \Civi\Api4\Generic\Result
* @throws \CRM_Core_Exception
* @throws \Civi\API\Exception\UnauthorizedException
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/
protected function setUpMembershipTypesACLLimited(): Result {
$types = MembershipType::save(FALSE)
Expand Down

0 comments on commit ce6cca3

Please sign in to comment.