Commit b77ccf25 authored by mattwire's avatar mattwire
Browse files

Allow to restrict 'From Addresses' for mailings by group.

parent 4a07bd0a
......@@ -4,7 +4,7 @@ use CRM_Groupadmin_ExtensionUtil as E;
class CRM_Groupadmin_BAO_GroupAdminContact extends CRM_Groupadmin_DAO_GroupAdminContact {
/**
* @param $contactID
* @param int $contactID
*
* @return int[]
*/
......@@ -54,6 +54,8 @@ class CRM_Groupadmin_BAO_GroupAdminContact extends CRM_Groupadmin_DAO_GroupAdmin
}
/**
* Check if the current user is admin for groupID.
*
* @param int $groupID
*
* @return bool
......@@ -67,4 +69,25 @@ class CRM_Groupadmin_BAO_GroupAdminContact extends CRM_Groupadmin_DAO_GroupAdmin
return (!empty($groups));
}
/**
* Returns a list of groups that the contact is admin for (defaults to current user if no contactID set).
*
* @param int $contactID
*
* @return mixed
* @throws \CiviCRM_API3_Exception
*/
public static function getAdminGroupsForUser($contactID = NULL) {
if (empty($contactID)) {
$contactID = CRM_Core_Session::getLoggedInContactID();
}
$groups = civicrm_api3('GroupAdminContact', 'get', [
'contact_id' => CRM_Core_Session::getLoggedInContactID(),
]);
foreach ($groups as $groupDetail) {
$groupsList[$groupDetail['group_id']] = $groupDetail['group_id'];
}
return $groupsList;
}
}
......@@ -16,7 +16,12 @@ class CRM_Groupadmin_Form_Edit extends CRM_Core_Form {
protected $groupID;
public function buildQuickForm() {
if ($this->isSubmitted()) {
return;
}
$this->groupID = CRM_Utils_Request::retrieveValue('gid', 'Positive', NULL, TRUE);
$this->add('hidden', 'group_id');
$this->setTitle('Edit Group Administrators');
$this->assign('groupTitle', civicrm_api3('Group', 'getvalue', ['id' => $this->groupID, 'return' => 'title']));
......@@ -46,6 +51,19 @@ class CRM_Groupadmin_Form_Edit extends CRM_Core_Form {
$this->addEntityRef('contact_id', ts('Add Group Admin Contact'), ['create' => FALSE], FALSE);
$fromAddresses = civicrm_api3('OptionValue', 'get', [
'is_active' => 1,
'option_group_id' => "from_email_address",
'domain_id' => CRM_Core_Config::domainID(),
['options' => ['limit' => 0]]
])['values'];
$fromAddressOptions[0] = E::ts(' - All - ');
foreach ($fromAddresses as $optionValueID => $fromAddressDetail) {
$fromAddressOptions[$optionValueID] = htmlentities($fromAddressDetail['label']);
}
$this->add('select','from_address', E::ts('Allowed From Addresses for this group'), $fromAddressOptions, FALSE, ['multiple' => TRUE]);
$this->addButtons([
[
'type' => 'submit',
......@@ -55,4 +73,30 @@ class CRM_Groupadmin_Form_Edit extends CRM_Core_Form {
]);
}
public function setDefaultValues() {
$defaultFromAddress = json_decode(\Civi::settings()->get('groupadmin_fromaddresses'), TRUE);
if (isset($defaultFromAddress[$this->groupID])) {
$defaults['from_address'] = $defaultFromAddress[$this->groupID];
}
else {
$defaults['from_address'] = [0];
}
$defaults['group_id'] = $this->groupID;
return $defaults;
}
public function postProcess() {
$submitValues = $this->getSubmitValues();
if (empty($submitValues['group_id'])) {
throw new CRM_Core_Exception('Missing group ID');
}
$this->groupID = $submitValues['group_id'];
if (!empty($submitValues['from_address'])) {
$defaultFromAddress = json_decode(\Civi::settings()->get('groupadmin_fromaddresses'), TRUE);
$defaultFromAddress[$this->groupID] = $submitValues['from_address'];
\Civi::settings()->set('groupadmin_fromaddresses', json_encode($defaultFromAddress));
}
}
}
<?php
/*
+--------------------------------------------------------------------+
| Copyright CiviCRM LLC. All rights reserved. |
| |
| This work is published under the GNU AGPLv3 license with some |
| permitted exceptions and without any warranty. For full license |
| and copyright information, see https://civicrm.org/licensing |
+--------------------------------------------------------------------+
*/
class CRM_Groupadmin_FromAddress {
/**
* Filter the email "from addresses" based on the "Group Admin" setting for groups.
* There is no group ID so it is filtered by ALL groups the user has admin for.
*/
public static function filterFromAddresses() {
$settings = \Civi::resources()->getSettings();
if (!isset($settings['crmMailing']['fromAddress'])) {
return;
}
else {
$settings = $settings['crmMailing']['fromAddress'];
}
$groupsList = CRM_Groupadmin_BAO_GroupAdminContact::getAdminGroupsForUser();
$defaultFromAddress = json_decode(\Civi::settings()->get('groupadmin_fromaddresses'), TRUE);
$fromAddressIDs = [];
foreach ($groupsList as $groupID) {
if (isset($defaultFromAddress[$groupID])) {
if (in_array(0, $defaultFromAddress[$groupID])) {
// All addresses allowed.
return;
}
$fromAddressIDs = array_merge($fromAddressIDs, $defaultFromAddress[$groupID]);
}
}
foreach ($settings as $index => $fromAddress) {
if (!in_array($fromAddress['id'], $fromAddressIDs)) {
unset($settings[$index]);
}
}
\Civi::resources()->addSetting(['crmMailing' => ['fromAddress' => $settings]]);
}
}
......@@ -36,6 +36,12 @@ It defaults to a top-level "Group Admins" menu item but you can move it once cre
Grant the *access GroupAdmin menu* permission to anyone who should see this menu (it will automatically filter to
show only the groups which the user has access to).
## Restrict "From Addresses" for mailings
You can select a set of allowed "from addresses" for each group. When a group admin opens the mailing composer they will
only see from addresses from the groups that they are admins for.
![groupadmin from address selection](images/groupadminedit_fromaddress.png)
## Support and Maintenance
This extension is supported and maintained by:
......
......@@ -9,6 +9,10 @@ Releases use the following numbering system:
* **[BC]**: Items marked with [BC] indicate a breaking change that will require updates to your code if you are using that code in your extension.
## Release 0.4
* Allow to restrict "From Addresses" for mailings by group.
## Release 0.3
* Add group admin menu that shows groups that the logged in user has permissions for. Links are configurable based on group type.
......
......@@ -214,9 +214,11 @@ function groupadmin_civicrm_navigationMenu(&$menu) {
function groupadmin_civicrm_buildForm($formName, &$form) {
$navMenu = new CRM_Groupadmin_NavigationMenu();
$navMenu->createOrUpdateGroupAdminMenu();
CRM_Groupadmin_FromAddress::filterFromAddresses();
}
function groupadmin_civicrm_pageRun(&$page) {
$navMenu = new CRM_Groupadmin_NavigationMenu();
$navMenu->createOrUpdateGroupAdminMenu();
CRM_Groupadmin_FromAddress::filterFromAddresses();
}
......@@ -14,8 +14,8 @@
<url desc="Support">https://mjw.pt/support/groupadmin</url>
<url desc="Licensing">http://www.gnu.org/licenses/agpl-3.0.html</url>
</urls>
<releaseDate>2020-08-22</releaseDate>
<version>0.3</version>
<releaseDate>2020-08-23</releaseDate>
<version>0.4</version>
<develStage>beta</develStage>
<compatibility>
<ver>5.28</ver>
......
......@@ -24,6 +24,11 @@
<div class="content">{$form.contact_id.html}</div>
</div>
<div class="crm-section">
<div class="label">{$form.from_address.label}</div>
<div class="content">{$form.from_address.html}</div>
</div>
<div class="crm-submit-buttons">
{include file="CRM/common/formButtons.tpl" location="bottom"}
</div>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment