[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/vtlib/Vtiger/ -> Access.php (source)

   1  <?php
   2  /*+***********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.0
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   *************************************************************************************/
  10  include_once ('include/utils/UserInfoUtil.php');
  11  include_once ('vtlib/Vtiger/Utils.php');
  12  include_once ('vtlib/Vtiger/Profile.php');
  13  
  14  /**
  15   * Provides API to control Access like Sharing, Tools etc. for vtiger CRM Module
  16   * @package vtlib
  17   */
  18  class Vtiger_Access {
  19  
  20      /**
  21       * Helper function to log messages
  22       * @param String Message to log
  23       * @param Boolean true appends linebreak, false to avoid it
  24       * @access private
  25       */
  26  	static function log($message, $delim=true) {
  27          Vtiger_Utils::Log($message, $delim);
  28      }
  29  
  30      /**
  31       * Get unique id for sharing access record.
  32       * @access private
  33       */
  34  	static function __getDefaultSharingAccessId() {
  35          global $adb;
  36          return $adb->getUniqueID('vtiger_def_org_share');
  37      }
  38  
  39      /**
  40       * Recalculate sharing access rules.
  41       * @internal This function could take up lot of resource while execution
  42       * @access private
  43       */
  44  	static function syncSharingAccess() {
  45          self::log("Recalculating sharing rules ... ", false);
  46          RecalculateSharingRules();
  47          self::log("DONE");
  48      }
  49  
  50      /**
  51       * Enable or Disable sharing access control to module
  52       * @param Vtiger_Module Instance of the module to use
  53       * @param Boolean true to enable sharing access, false disable sharing access
  54       * @access private
  55       */
  56  	static function allowSharing($moduleInstance, $enable=true) {
  57          global $adb;
  58          $ownedby = $enable? 0 : 1;
  59          $adb->pquery("UPDATE vtiger_tab set ownedby=? WHERE tabid=?", Array($ownedby, $moduleInstance->id));
  60          self::log(($enable? "Enabled" : "Disabled") . " sharing access control ... DONE");
  61      }
  62  
  63      /**
  64       * Initialize sharing access.
  65       * @param Vtiger_Module Instance of the module to use
  66       * @access private
  67       * @internal This method is called from Vtiger_Module during creation.
  68       */
  69  	static function initSharing($moduleInstance) {
  70          global $adb;
  71  
  72          $result = $adb->query("SELECT share_action_id from vtiger_org_share_action_mapping WHERE share_action_name in
  73              ('Public: Read Only', 'Public: Read, Create/Edit', 'Public: Read, Create/Edit, Delete', 'Private')");
  74  
  75          for($index = 0; $index < $adb->num_rows($result); ++$index) {
  76              $actionid = $adb->query_result($result, $index, 'share_action_id');
  77              $adb->pquery("INSERT INTO vtiger_org_share_action2tab(share_action_id,tabid) VALUES(?,?)", Array($actionid, $moduleInstance->id));
  78          }
  79          self::log("Setting up sharing access options ... DONE");
  80      }
  81  
  82      /**
  83       * Delete sharing access setup for module
  84       * @param Vtiger_Module Instance of module to use
  85       * @access private
  86       * @internal This method is called from Vtiger_Module during deletion.
  87       */
  88  	static function deleteSharing($moduleInstance) {
  89          global $adb;
  90          $adb->pquery("DELETE FROM vtiger_org_share_action2tab WHERE tabid=?", Array($moduleInstance->id));
  91          self::log("Deleting sharing access ... DONE");
  92      }
  93  
  94      /**
  95       * Set default sharing for a module
  96       * @param Vtiger_Module Instance of the module
  97       * @param String Permission text should be one of ['Public_ReadWriteDelete', 'Public_ReadOnly', 'Public_ReadWrite', 'Private']
  98       * @access private
  99       */
 100  	static function setDefaultSharing($moduleInstance, $permission_text='Public_ReadWriteDelete') {
 101          global $adb;
 102  
 103          $permission_text = strtolower($permission_text);
 104  
 105          if($permission_text == 'public_readonly')             $permission = 0;
 106          else if($permission_text == 'public_readwrite')       $permission = 1;
 107          else if($permission_text == 'public_readwritedelete') $permission = 2;
 108          else if($permission_text == 'private')                $permission = 3;
 109          else $permission = 2; // public_readwritedelete is default
 110  
 111          $editstatus = 0; // 0 or 1
 112  
 113          $result = $adb->pquery("SELECT * FROM vtiger_def_org_share WHERE tabid=?", Array($moduleInstance->id));
 114          if($adb->num_rows($result)) {
 115              $ruleid = $adb->query_result($result, 0, 'ruleid');
 116              $adb->pquery("UPDATE vtiger_def_org_share SET permission=? WHERE ruleid=?", Array($permission, $ruleid));
 117          } else {
 118              $ruleid = self::__getDefaultSharingAccessId();
 119              $adb->pquery("INSERT INTO vtiger_def_org_share (ruleid,tabid,permission,editstatus) VALUES(?,?,?,?)",
 120                  Array($ruleid,$moduleInstance->id,$permission,$editstatus));
 121          }
 122  
 123          self::syncSharingAccess();
 124      }
 125  
 126      /**
 127       * Enable tool for module.
 128       * @param Vtiger_Module Instance of module to use
 129       * @param String Tool (action name) like Import, Export, Merge
 130       * @param Boolean true to enable tool, false to disable
 131       * @param Integer (optional) profile id to use, false applies to all profile.
 132       * @access private
 133       */
 134  	static function updateTool($moduleInstance, $toolAction, $flag, $profileid=false) {
 135          global $adb;
 136  
 137          $result = $adb->pquery("SELECT actionid FROM vtiger_actionmapping WHERE actionname=?", Array($toolAction));
 138          if($adb->num_rows($result)) {
 139              $actionid = $adb->query_result($result, 0, 'actionid');
 140              $permission = ($flag == true)? '0' : '1';
 141  
 142              $profileids = Array();
 143              if($profileid) {
 144                  $profileids[] = $profileid;
 145              } else {
 146                  $profileids = Vtiger_Profile::getAllIds();
 147              }
 148  
 149              self::log( ($flag? 'Enabling':'Disabling') . " $toolAction for Profile [", false);
 150  
 151              foreach($profileids as $useprofileid) {
 152                  $result = $adb->pquery("SELECT permission FROM vtiger_profile2utility WHERE profileid=? AND tabid=? AND activityid=?",
 153                      Array($useprofileid, $moduleInstance->id, $actionid));
 154                  if($adb->num_rows($result)) {
 155                      $curpermission = $adb->query_result($result, 0, 'permission');
 156                      if($curpermission != $permission) {
 157                          $adb->pquery("UPDATE vtiger_profile2utility set permission=? WHERE profileid=? AND tabid=? AND activityid=?",
 158                              Array($permission, $useprofileid, $moduleInstance->id, $actionid));
 159                      }
 160                  } else {
 161                      $adb->pquery("INSERT INTO vtiger_profile2utility (profileid, tabid, activityid, permission) VALUES(?,?,?,?)",
 162                             Array($useprofileid, $moduleInstance->id, $actionid, $permission));
 163                  }
 164  
 165                  self::log("$useprofileid,", false);
 166              }
 167              self::log("] ... DONE");
 168          }
 169      }
 170  
 171      /**
 172       * Delete tool (actions) of the module
 173       * @param Vtiger_Module Instance of module to use
 174       */
 175  	static function deleteTools($moduleInstance) {
 176          global $adb;
 177          $adb->pquery("DELETE FROM vtiger_profile2utility WHERE tabid=?", Array($moduleInstance->id));
 178          self::log("Deleting tools ... DONE");
 179      }
 180  }
 181  ?>


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1