protector
[ class tree: protector ] [ index: protector ] [ all elements ]

Source for file LibertyProtector.php

Documentation is available at LibertyProtector.php

  1. <?php
  2. /**
  3.  * protector package limits content based on user role
  4.  *
  5.  * @copyright (c) 2004-15 bitweaver.org
  6.  * @package protector
  7.  */
  8.  
  9. /**
  10.  * required setup
  11.  */
  12. require_onceLIBERTY_PKG_PATH.'LibertyBase.php' );
  13.  
  14. /**
  15.  * Protector class to illustrate best practices when creating a new bitweaver package that
  16.  * builds on core bitweaver functionality, such as the Liberty CMS engine
  17.  *
  18.  * @package protector
  19.  */
  20. class LibertyProtector extends LibertyBase {
  21.     /**
  22.     * During initialisation, be sure to call our base constructors
  23.     **/
  24.  
  25.     function __construct$pContentId=NULL {
  26.         $this->mContentId $pContentId;
  27.         parent::__construct();
  28.     }
  29.  
  30.     /**
  31.     * Update the liberty_content_role_map table with corrected role_id(s).
  32.     * 
  33.     * In -1 for anonymouse is not stored, switching content to anonymouse will clear array
  34.     *  
  35.     * @param object $pParamHash 
  36.     */
  37.     function storeProtection&$pParamHash {
  38.         global $gBitSystem;
  39.         if@BitBase::verifyId$pParamHash['protector']['role_id') ) {
  40.             $this->mDb->query"DELETE FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?"array$pParamHash['content_id') );
  41.             if$gBitSystem->isFeatureActive'protector_single_role' ) ) {
  42.                 if$pParamHash['protector']['role_id'!= -)
  43.                     $this->mDb->query"INSERT INTO `".BIT_DB_PREFIX."liberty_content_role_map` ( `role_id`, `content_id` ) VALUES ( ?, ? )"array$pParamHash['protector']['role_id']$pParamHash['content_id') );
  44.             else {
  45.                 foreach$pParamHash['protector']['role_id'AS $roleId {
  46.                     if$roleId != -)
  47.                     $this->mDb->query"INSERT INTO `".BIT_DB_PREFIX."liberty_content_role_map` ( `role_id`, `content_id` ) VALUES ( ?, ? )"array$roleId$pParamHash['content_id') );
  48.                 }
  49.             }
  50.         }
  51.         returncount$this->mErrors == );
  52.     }
  53.  
  54.     /**
  55.     * Delete entry(ies) from liberty_content_role_map table with content_id.
  56.     * 
  57.     * @param object $pContent 
  58.     */
  59.     function expunge$ContentId=NULL {
  60.         $ret FALSE;
  61.         if@BitBase::verifyId$ContentId ) ) {
  62.             $this->mDb->query"DELETE FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?"array$ContentId ) );
  63.         }
  64.         return $ret;
  65.     }
  66.  
  67.     /**
  68.     * @return liberty_content_role_map for selected content_id
  69.     *  Ret -1 for anonymouse if alternatives are not stored
  70.     ***/
  71.     function getProtectionList$ContentId=NULL {
  72.         global $gBitSystem;
  73.         $ret array'-1' <= $ContentId );
  74.         ifisset$ContentId ) ) {
  75.             $ret $this->mDb->GetAssoc"SELECT `role_id`, `content_id` FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?"array$ContentId ) );
  76.         }
  77.         return $ret;
  78.     }
  79. }
  80.  
  81. /**
  82. * function to provide list of filtered content
  83. **/
  84.  
  85. function protector_content_list({
  86.     global $gBitUser;
  87.     $roles array_keys($gBitUser->mRoles);
  88.     $ret array(
  89.         'join_sql' => " LEFT JOIN `".BIT_DB_PREFIX."liberty_content_role_map` lcrm ON ( lc.`content_id`=lcrm.`content_id` ) LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` purm ON ( purm.`user_id`=".$gBitUser->mUserId." ) AND ( purm.`role_id`=lcrm.`role_id` ) ",
  90.         'where_sql' => " AND (lcrm.`content_id` IS NULL OR lcrm.`role_id` IN("implode(','array_fill(0count($roles)'?')) ." ) OR purm.`user_id`=?) ",
  91.         'bind_vars' => array_merge$rolesarray$gBitUser->mUserId ) ),
  92.     );
  93.     return $ret;
  94. }
  95.  
  96. /**
  97.  * function to load a filtered content element
  98.  * 
  99.  * @param object $pContent 
  100.  */
  101. function protector_content_load&$pContent NULL {
  102.     global $gBitUser;
  103.  
  104.     $roles array_keys($gBitUser->mRoles);
  105.     protector_content_verify_access$pContent$roles );
  106.     $ret array(
  107.         'join_sql' => " LEFT JOIN `".BIT_DB_PREFIX."liberty_content_role_map` lcrm ON ( lc.`content_id`=lcrm.`content_id` ) LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` purm ON ( purm.`role_id`=lcrm.`role_id` ) ",
  108.         'where_sql' => " AND (lcrm.`content_id` IS NULL OR lcrm.`role_id` IN("implode(','array_fill(0count($roles)'?')) ." ) OR purm.`user_id`=?) ",
  109.         'bind_vars' => array$gBitUser->mUserId ),
  110.     );
  111.     $ret['bind_vars'array_merge$roles$ret['bind_vars');
  112.     return $ret;
  113. }
  114.  
  115. /**
  116. * function to store a filtered content element
  117. @param object $pObject 
  118. @param array $pParamHash 
  119. ***/
  120. function protector_content_store&$pObject&$pParamHash {
  121.     global $gBitSystem$gProtector;
  122.     $errors NULL;
  123.     // If a content access system is active, let's call it
  124.     if$gBitSystem->isPackageActive'protector' ) ) {
  125.         if!$gProtector->storeProtection$pParamHash ) ) {
  126.             $errors['protector'$gProtector->mErrors['security'];
  127.         }
  128.     }
  129.     return$errors );
  130. }
  131.  
  132. /**
  133. * function to store a filtered comment element
  134. @param object $pContent 
  135. @param array $pParamHash 
  136. ***/
  137. function protector_comment_store&$pContent&$pParamHash {
  138.     global $gBitSystem$gProtector;
  139.     $errors NULL;
  140.     // If a content access system is active, let's call it
  141.     if$gBitSystem->isPackageActive'protector' ) ) {
  142.         ifisset$pParamHash['comments_parent_id') ) {
  143.             $pParamHash['protector']['role_id'$pContent->mDb->GetOne"SELECT `role_id` FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?"array$pParamHash['comments_parent_id') );
  144.         }
  145.         if!$gProtector->storeProtection$pParamHash ) ) {
  146.             $errors['protector'$gProtector->mErrors['security'];
  147.         }
  148.     }
  149.     return$errors );
  150. }
  151.  
  152. /**
  153. * function to delete a filtered content element
  154. @param object $pContent 
  155. @param array $pParamHash 
  156. ***/
  157. function protector_content_expunge&$pContent NULL {
  158.         if@BitBase::verifyId$pContent->mContentId ) ) {
  159.             $pContent->mDb->query"DELETE FROM `".BIT_DB_PREFIX."liberty_content_role_map` WHERE `content_id`=?"array$pContent->mContentId ) );
  160.         }
  161. }
  162.  
  163. /**
  164. * function to display a filtered content element
  165. @param object $pContent 
  166. @param array $pParamHash 
  167. ***/
  168. function protector_content_display&$pContent&$pParamHash {
  169.     global $gBitSystem$gBitSmarty;
  170.     $pContent->hasUserPermission$pParamHash['perm_name');
  171. }
  172.  
  173. /**
  174. * function to verify access to a filtered content element
  175. @param object $pContent 
  176. @param array $pHash 
  177. ***/
  178. function protector_content_verify_access&$pContent&$pHash {
  179.     global $gBitUser$gBitSystem;
  180.  
  181.     $error NULL;
  182.     if!$pContent->verifyId$pContent->mContentId ) ) {
  183. // vd($pContent);
  184. // need to get ContentId if not set
  185.     }
  186.     if$pContent->verifyId$pContent->mContentId ) ) {
  187.         $query "SELECT lc.`content_id`, lcrm.`role_id` as `is_protected`
  188.             FROM `".BIT_DB_PREFIX."liberty_content` lc 
  189.             LEFT JOIN `".BIT_DB_PREFIX."liberty_content_role_map` lcrm ON ( lc.`content_id`=lcrm.`content_id` ) LEFT OUTER JOIN `".BIT_DB_PREFIX."users_roles_map` urm ON ( urm.`user_id`=".$gBitUser->mUserId." ) AND ( urm.`role_id`=lcrm.`role_id` ) 
  190.             WHERE lc.`content_id` = ?";
  191.         $ret $pContent->mDb->getRow$queryarray$pContent->mContentId ) );
  192.         if$ret and is_numeric($ret['is_protected']and !in_array$ret['is_protected']$pHash ) ) {
  193.             $gBitSystem->fatalErrortra'You do not have permission to access this '.$pContent->getContentTypeName() )'error.tpl'tra'Permission denied.' ) );
  194.         }
  195.     }
  196.     return $error;
  197. }
  198.  
  199. /**
  200. * function to edit a filtered content element
  201. @param object $pContent 
  202. ***/
  203. function protector_content_edit&$pContent {
  204.     global $gProtector$gBitUser$gBitSmarty;
  205.     $roles $gBitUser->getRoles();
  206.     $roles[-1]['role_name'"~~ System Default ~~";
  207.     ksort$roles );
  208.     foreacharray_keys$roles as $roleId {
  209.         if$roleId != -{
  210.             $protectorRolesId[$roleId$roles[$roleId]['role_name'];
  211.         else {
  212.             $protectorRolesId[$roleId"~~ System Default ~~";
  213.         }
  214.     }
  215.     if $pContent->mContentId {
  216.         $serviceHash['protector']['role'$gProtector->getProtectionList$pContent->mContentId );
  217.     else {
  218.         if isset$pContent->mInfo['parent_id') ) {
  219.             $serviceHash['protector']['role'$gProtector->getProtectionList$pContent->mInfo['parent_id');
  220.         }
  221.     }    
  222.     if isset$serviceHash['protector']['role') ) $prot array_keys$serviceHash['protector']['role')}
  223.     $serviceHash['protector']['role_id'empty$prot[0? -$prot[0);
  224.     $gBitSmarty->assign_by_ref'serviceHash'$serviceHash );
  225.     $gBitSmarty->assign_by_ref'protectorRolesId'$protectorRolesId );
  226.     $gBitSmarty->assign_by_ref'protectorRoles'$roles );
  227. }
  228.  
  229. global $gProtector;
  230. $gProtector new LibertyProtector();
  231.  
  232. ?>

Documentation generated on Wed, 29 Jul 2015 13:56:56 +0000 by phpDocumentor 1.5.0-lsces