ACL.php 10.1 KB
Newer Older
totten's avatar
totten committed
1 2 3
<?php
/*
 +--------------------------------------------------------------------+
yashodha's avatar
yashodha committed
4
 | CiviCRM version 4.6                                                |
totten's avatar
totten committed
5
 +--------------------------------------------------------------------+
kurund's avatar
kurund committed
6
 | Copyright CiviCRM LLC (c) 2004-2014                                |
totten's avatar
totten committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 +--------------------------------------------------------------------+
 | This file is a part of CiviCRM.                                    |
 |                                                                    |
 | CiviCRM is free software; you can copy, modify, and distribute it  |
 | under the terms of the GNU Affero General Public License           |
 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception.   |
 |                                                                    |
 | CiviCRM is distributed in the hope that it will be useful, but     |
 | WITHOUT ANY WARRANTY; without even the implied warranty of         |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               |
 | See the GNU Affero General Public License for more details.        |
 |                                                                    |
 | You should have received a copy of the GNU Affero General Public   |
 | License and the CiviCRM Licensing Exception along                  |
 | with this program; if not, contact CiviCRM LLC                     |
 | at info[AT]civicrm[DOT]org. If you have questions about the        |
 | GNU Affero General Public License or the licensing of CiviCRM,     |
 | see the CiviCRM license FAQ at http://civicrm.org/licensing        |
 +--------------------------------------------------------------------+
26
 */
totten's avatar
totten committed
27 28 29 30

/**
 *
 * @package CRM
kurund's avatar
kurund committed
31
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
32 33 34 35 36 37 38
 * $Id$
 *
 */

/**
 *
 * @package CRM
kurund's avatar
kurund committed
39
 * @copyright CiviCRM LLC (c) 2004-2014
totten's avatar
totten committed
40 41 42 43 44 45
 * $Id$
 *
 */
class CRM_ACL_Form_ACL extends CRM_Admin_Form {

  /**
46
   * Set default values for the form.
totten's avatar
totten committed
47 48
   *
   *
colemanw's avatar
colemanw committed
49
   * @return void
totten's avatar
totten committed
50
   */
51
  public function setDefaultValues() {
totten's avatar
totten committed
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    $defaults = parent::setDefaultValues();

    if ($this->_action & CRM_Core_Action::ADD) {
      $defaults['object_type'] = 1;
    }

    $showHide = new CRM_Core_ShowHideBlocks();

    if (isset($defaults['object_table'])) {
      switch ($defaults['object_table']) {
        case 'civicrm_saved_search':
          $defaults['group_id'] = $defaults['object_id'];
          $defaults['object_type'] = 1;
          $showHide->addShow("id-group-acl");
          $showHide->addHide("id-profile-acl");
          $showHide->addHide("id-custom-acl");
          $showHide->addHide("id-event-acl");
          break;

        case 'civicrm_uf_group':
          $defaults['uf_group_id'] = $defaults['object_id'];
          $defaults['object_type'] = 2;
          $showHide->addHide("id-group-acl");
          $showHide->addShow("id-profile-acl");
          $showHide->addHide("id-custom-acl");
          $showHide->addHide("id-event-acl");
          break;

        case 'civicrm_custom_group':
          $defaults['custom_group_id'] = $defaults['object_id'];
          $defaults['object_type'] = 3;
          $showHide->addHide("id-group-acl");
          $showHide->addHide("id-profile-acl");
          $showHide->addShow("id-custom-acl");
          $showHide->addHide("id-event-acl");
          break;

        case 'civicrm_event':
          $defaults['event_id'] = $defaults['object_id'];
          $defaults['object_type'] = 4;
          $showHide->addHide("id-group-acl");
          $showHide->addHide("id-profile-acl");
          $showHide->addHide("id-custom-acl");
          $showHide->addShow("id-event-acl");
          break;
      }
    }
    else {
      $showHide->addHide("id-group-acl");
      $showHide->addHide("id-profile-acl");
      $showHide->addHide("id-custom-acl");
      $showHide->addHide("id-event-acl");
    }

    // Don't assign showHide elements to template in DELETE mode (fields to be shown and hidden don't exist)
    if (!($this->_action & CRM_Core_Action::DELETE)) {
      $showHide->addToTemplate();
    }

    return $defaults;
  }

  /**
115
   * Build the form object
totten's avatar
totten committed
116
   *
colemanw's avatar
colemanw committed
117
   * @return void
totten's avatar
totten committed
118 119 120 121
   */
  public function buildQuickForm() {
    parent::buildQuickForm();

122 123
    $this->setPageTitle(ts('ACL'));

totten's avatar
totten committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
    if ($this->_action & CRM_Core_Action::DELETE) {
      return;
    }

    $attributes = CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL');

    $this->add('text', 'name', ts('Description'), CRM_Core_DAO::getAttribute('CRM_ACL_DAO_ACL', 'name'), TRUE);

    $operations = array('' => ts('- select -')) + CRM_ACL_BAO_ACL::operation();
    $this->add('select',
      'operation',
      ts('Operation'),
      $operations, TRUE
    );

139 140
    $objTypes = array(
      '1' => ts('A group of contacts'),
totten's avatar
totten committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
      '2' => ts('A profile'),
      '3' => ts('A set of custom data fields'),
    );

    if (CRM_Core_Permission::access('CiviEvent')) {
      $objTypes['4'] = ts('Events');
    }

    $extra = array('onclick' => "showObjectSelect();");
    $this->addRadio('object_type',
      ts('Type of Data'),
      $objTypes,
      $extra,
      '&nbsp;', TRUE
    );

    $label = ts('Role');
158
    $role = array(
totten's avatar
totten committed
159 160 161
        '-1' => ts('- select role -'),
        '0' => ts('Everyone'),
      ) + CRM_Core_OptionGroup::values('acl_role');
totten's avatar
totten committed
162 163
    $this->add('select', 'entity_id', $label, $role, TRUE);

164
    $group = array(
totten's avatar
totten committed
165 166 167
        '-1' => ts('- select -'),
        '0' => ts('All Groups'),
      ) + CRM_Core_PseudoConstant::group();
totten's avatar
totten committed
168

169
    $customGroup = array(
totten's avatar
totten committed
170 171 172
        '-1' => ts('- select -'),
        '0' => ts('All Custom Groups'),
      ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
totten's avatar
totten committed
173

174
    $ufGroup = array(
totten's avatar
totten committed
175 176 177
        '-1' => ts('- select -'),
        '0' => ts('All Profiles'),
      ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
totten's avatar
totten committed
178

179
    $event = array(
totten's avatar
totten committed
180 181 182
        '-1' => ts('- select -'),
        '0' => ts('All Events'),
      ) + CRM_Event_PseudoConstant::event(NULL, FALSE, "( is_template IS NULL OR is_template != 1 )");
totten's avatar
totten committed
183 184 185 186 187 188 189 190 191 192 193

    $this->add('select', 'group_id', ts('Group'), $group);
    $this->add('select', 'custom_group_id', ts('Custom Data'), $customGroup);
    $this->add('select', 'uf_group_id', ts('Profile'), $ufGroup);
    $this->add('select', 'event_id', ts('Event'), $event);

    $this->add('checkbox', 'is_active', ts('Enabled?'));

    $this->addFormRule(array('CRM_ACL_Form_ACL', 'formRule'));
  }

194
  /**
195
   * @param array $params
196 197 198
   *
   * @return bool
   */
199
  public static function formRule($params) {
totten's avatar
totten committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
    $showHide = new CRM_Core_ShowHideBlocks();

    // Make sure role is not -1
    if ($params['entity_id'] == -1) {
      $errors['entity_id'] = ts('Please assign this permission to a Role.');
    }

    $validOperations = array('View', 'Edit');
    $operationMessage = ts("Only 'View' and 'Edit' operations are valid for this type of data");

    // Figure out which type of object we're permissioning on and make sure user has selected a value.
    switch ($params['object_type']) {
      case 1:
        if ($params['group_id'] == -1) {
          $errors['group_id'] = ts('Please select a Group (or ALL Groups).');
          $showHide->addShow("id-group-acl");
          $showHide->addHide("id-profile-acl");
          $showHide->addHide("id-custom-acl");
          $showHide->addHide("id-event-acl");
        }
        if (!in_array($params['operation'], $validOperations)) {
          $errors['operation'] = $operationMessage;
        }
        break;

      case 2:
        if ($params['uf_group_id'] == -1) {
          $errors['uf_group_id'] = ts('Please select a Profile (or ALL Profiles).');
          $showHide->addShow("id-profile-acl");
          $showHide->addHide("id-group-acl");
          $showHide->addHide("id-custom-acl");
          $showHide->addHide("id-event-acl");
        }
        break;

      case 3:
        if ($params['custom_group_id'] == -1) {
          $errors['custom_group_id'] = ts('Please select a set of Custom Data (or ALL Custom Data).');
          $showHide->addShow("id-custom-acl");
          $showHide->addHide("id-group-acl");
          $showHide->addHide("id-profile-acl");
          $showHide->addHide("id-event-acl");
        }
        if (!in_array($params['operation'], $validOperations)) {
          $errors['operation'] = $operationMessage;
        }
        break;

      case 4:
        if ($params['event_id'] == -1) {
          $errors['event_id'] = ts('Please select an Event (or ALL Events).');
          $showHide->addShow("id-event-acl");
          $showHide->addHide("id-custom-acl");
          $showHide->addHide("id-group-acl");
          $showHide->addHide("id-profile-acl");
        }
        if (!in_array($params['operation'], $validOperations)) {
          $errors['operation'] = $operationMessage;
        }
        break;
    }

    $showHide->addToTemplate();

    return empty($errors) ? TRUE : $errors;
  }

  /**
268
   * Process the form submission
totten's avatar
totten committed
269 270
   *
   *
colemanw's avatar
colemanw committed
271
   * @return void
totten's avatar
totten committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316
   */
  public function postProcess() {
    // note this also resets any ACL cache
    CRM_Core_BAO_Cache::deleteGroup('contact fields');

    if ($this->_action & CRM_Core_Action::DELETE) {
      CRM_ACL_BAO_ACL::del($this->_id);
      CRM_Core_Session::setStatus(ts('Selected ACL has been deleted.'), ts('Record Deleted'), 'success');
    }
    else {
      $params = $this->controller->exportValues($this->_name);
      $params['is_active'] = CRM_Utils_Array::value('is_active', $params, FALSE);
      $params['deny'] = 0;
      $params['entity_table'] = 'civicrm_acl_role';

      // Figure out which type of object we're permissioning on and set object_table and object_id.
      switch ($params['object_type']) {
        case 1:
          $params['object_table'] = 'civicrm_saved_search';
          $params['object_id'] = $params['group_id'];
          break;

        case 2:
          $params['object_table'] = 'civicrm_uf_group';
          $params['object_id'] = $params['uf_group_id'];
          break;

        case 3:
          $params['object_table'] = 'civicrm_custom_group';
          $params['object_id'] = $params['custom_group_id'];
          break;

        case 4:
          $params['object_table'] = 'civicrm_event';
          $params['object_id'] = $params['event_id'];
          break;
      }

      if ($this->_id) {
        $params['id'] = $this->_id;
      }

      CRM_ACL_BAO_ACL::create($params);
    }
  }
317

totten's avatar
totten committed
318
}