ACL.php 7.93 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
 * $Id$
 *
 */
class CRM_ACL_Page_ACL extends CRM_Core_Page_Basic {

45 46
  public $useLivePageJS = TRUE;

totten's avatar
totten committed
47 48 49 50 51 52 53 54 55 56
  /**
   * The action links that we need to display for the browse screen
   *
   * @var array
   */
  static $_links = NULL;

  /**
   * Get BAO Name
   *
57 58
   * @return string
   *   Classname of BAO.
totten's avatar
totten committed
59
   */
60
  public function getBAOName() {
totten's avatar
totten committed
61 62 63 64 65 66
    return 'CRM_ACL_BAO_ACL';
  }

  /**
   * Get action Links
   *
67 68
   * @return array
   *   (reference) of action links
totten's avatar
totten committed
69
   */
70
  public function &links() {
totten's avatar
totten committed
71 72 73 74 75 76 77 78 79 80
    if (!(self::$_links)) {
      self::$_links = array(
        CRM_Core_Action::UPDATE => array(
          'name' => ts('Edit'),
          'url' => 'civicrm/acl',
          'qs' => 'reset=1&action=update&id=%%id%%',
          'title' => ts('Edit ACL'),
        ),
        CRM_Core_Action::DISABLE => array(
          'name' => ts('Disable'),
81
          'ref' => 'crm-enable-disable',
totten's avatar
totten committed
82 83 84 85
          'title' => ts('Disable ACL'),
        ),
        CRM_Core_Action::ENABLE => array(
          'name' => ts('Enable'),
86
          'ref' => 'crm-enable-disable',
totten's avatar
totten committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
          'title' => ts('Enable ACL'),
        ),
        CRM_Core_Action::DELETE => array(
          'name' => ts('Delete'),
          'url' => 'civicrm/acl',
          'qs' => 'reset=1&action=delete&id=%%id%%',
          'title' => ts('Delete ACL'),
        ),
      );
    }
    return self::$_links;
  }

  /**
   * Run the page.
   *
   * This method is called after the page is created. It checks for the
   * type of action and executes that action.
   * Finally it calls the parent's run method.
   *
   * @return void
   */
109
  public function run() {
totten's avatar
totten committed
110 111 112 113 114 115 116 117 118 119 120 121 122
    // get the requested action
    $action = CRM_Utils_Request::retrieve('action', 'String',
      // default to 'browse'
      $this, FALSE, 'browse'
    );

    // assign vars to templates
    $this->assign('action', $action);
    $id = CRM_Utils_Request::retrieve('id', 'Positive',
      $this, FALSE, 0
    );

    // set breadcrumb to append to admin/access
totten's avatar
totten committed
123 124 125
    $breadCrumb = array(
      array(
        'title' => ts('Access Control'),
totten's avatar
totten committed
126 127 128
        'url' => CRM_Utils_System::url('civicrm/admin/access',
          'reset=1'
        ),
totten's avatar
totten committed
129 130
      )
    );
totten's avatar
totten committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    CRM_Utils_System::appendBreadCrumb($breadCrumb);
    // what action to take ?
    if ($action & (CRM_Core_Action::ADD | CRM_Core_Action::DELETE)) {
      $this->edit($action, $id);
    }

    if ($action & (CRM_Core_Action::UPDATE)) {
      $this->edit($action, $id);

      if (isset($id)) {
        $aclName = CRM_Core_DAO::getFieldValue('CRM_ACL_DAO_ACL', $id);
        CRM_Utils_System::setTitle(ts('Edit ACL -  %1', array(1 => $aclName)));
      }
    }

    // finally browse the acl's
colemanw's avatar
colemanw committed
147 148 149
    if ($action & CRM_Core_Action::BROWSE) {
      $this->browse();
    }
totten's avatar
totten committed
150 151 152 153 154 155 156 157 158 159

    // parent run
    return parent::run();
  }

  /**
   * Browse all acls
   *
   * @return void
   */
160
  public function browse() {
totten's avatar
totten committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174
    // get all acl's sorted by weight
    $acl = array();
    $query = "
  SELECT *
    FROM civicrm_acl
   WHERE ( object_table IN ( 'civicrm_saved_search', 'civicrm_uf_group', 'civicrm_custom_group', 'civicrm_event' ) )
ORDER BY entity_id
";
    $dao = CRM_Core_DAO::executeQuery($query,
      CRM_Core_DAO::$_nullArray
    );

    $roles = CRM_Core_OptionGroup::values('acl_role');

totten's avatar
totten committed
175
    $group = array(
totten's avatar
totten committed
176 177 178
        '-1' => ts('- select -'),
        '0' => ts('All Groups'),
      ) + CRM_Core_PseudoConstant::group();
totten's avatar
totten committed
179
    $customGroup = array(
totten's avatar
totten committed
180 181 182
        '-1' => ts('- select -'),
        '0' => ts('All Custom Groups'),
      ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_CustomField', 'custom_group_id');
totten's avatar
totten committed
183
    $ufGroup = array(
totten's avatar
totten committed
184 185 186
        '-1' => ts('- select -'),
        '0' => ts('All Profiles'),
      ) + CRM_Core_PseudoConstant::get('CRM_Core_DAO_UFField', 'uf_group_id');
totten's avatar
totten committed
187

totten's avatar
totten committed
188
    $event = array(
totten's avatar
totten committed
189 190 191
        '-1' => ts('- select -'),
        '0' => ts('All Events'),
      ) + CRM_Event_PseudoConstant::event();
totten's avatar
totten committed
192 193 194 195 196 197 198 199 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

    while ($dao->fetch()) {
      $acl[$dao->id] = array();
      $acl[$dao->id]['name'] = $dao->name;
      $acl[$dao->id]['operation'] = $dao->operation;
      $acl[$dao->id]['entity_id'] = $dao->entity_id;
      $acl[$dao->id]['entity_table'] = $dao->entity_table;
      $acl[$dao->id]['object_table'] = $dao->object_table;
      $acl[$dao->id]['object_id'] = $dao->object_id;
      $acl[$dao->id]['is_active'] = $dao->is_active;

      if ($acl[$dao->id]['entity_id']) {
        $acl[$dao->id]['entity'] = $roles[$acl[$dao->id]['entity_id']];
      }
      else {
        $acl[$dao->id]['entity'] = ts('Everyone');
      }

      switch ($acl[$dao->id]['object_table']) {
        case 'civicrm_saved_search':
          $acl[$dao->id]['object'] = $group[$acl[$dao->id]['object_id']];
          $acl[$dao->id]['object_name'] = ts('Group');
          break;

        case 'civicrm_uf_group':
          $acl[$dao->id]['object'] = $ufGroup[$acl[$dao->id]['object_id']];
          $acl[$dao->id]['object_name'] = ts('Profile');
          break;

        case 'civicrm_custom_group':
          $acl[$dao->id]['object'] = $customGroup[$acl[$dao->id]['object_id']];
          $acl[$dao->id]['object_name'] = ts('Custom Group');
          break;

        case 'civicrm_event':
          $acl[$dao->id]['object'] = $event[$acl[$dao->id]['object_id']];
          $acl[$dao->id]['object_name'] = ts('Event');
          break;
      }

      // form all action links
      $action = array_sum(array_keys($this->links()));

      if ($dao->is_active) {
        $action -= CRM_Core_Action::ENABLE;
      }
      else {
        $action -= CRM_Core_Action::DISABLE;
      }

242 243 244 245 246 247 248 249 250
      $acl[$dao->id]['action'] = CRM_Core_Action::formLink(
        self::links(),
        $action,
        array('id' => $dao->id),
        ts('more'),
        FALSE,
        'ACL.manage.action',
        'ACL',
        $dao->id
totten's avatar
totten committed
251 252 253 254 255 256 257 258
      );
    }
    $this->assign('rows', $acl);
  }

  /**
   * Get name of edit form
   *
259 260
   * @return string
   *   Classname of edit form.
totten's avatar
totten committed
261
   */
262
  public function editForm() {
totten's avatar
totten committed
263 264 265 266 267 268
    return 'CRM_ACL_Form_ACL';
  }

  /**
   * Get edit form name
   *
269 270
   * @return string
   *   name of this page.
totten's avatar
totten committed
271
   */
272
  public function editName() {
totten's avatar
totten committed
273 274 275 276 277 278
    return 'ACL';
  }

  /**
   * Get user context.
   *
279 280
   * @param null $mode
   *
281 282
   * @return string
   *   user context.
totten's avatar
totten committed
283
   */
284
  public function userContext($mode = NULL) {
totten's avatar
totten committed
285 286
    return 'civicrm/acl';
  }
287

totten's avatar
totten committed
288
}