Difference between revisions of "How to enable access to helpdesk only if user have special product"

From aMember Pro Documentation
Jump to: navigation, search
(Created page with "Create /amember/application/configs/site.php with this code: <source> <?php function helpdeskEnabled(){ $user = Am_Di::getInstance()->auth->getUser(); ...")
 
 
(4 intermediate revisions by one user not shown)
Line 1: Line 1:
Create /amember/application/configs/site.php with this code:  
+
Add this code to [[site.php file]]:
 
<source>
 
<source>
<?php
+
function helpdeskEnabled(){
        function helpdeskEnabled(){
+
    $user = Am_Di::getInstance()->auth->getUser();
            $user = Am_Di::getInstance()->auth->getUser();
+
    if(!$user) return true; // give user chance to log in first
            if(!$user) return false;
+
    foreach($user->getActiveProducts() as $p){
            foreach($user->getActiveProducts() as $p){
+
        $data = $p->getBillingPlanData();
                $data = $p->getBillingPlanData();
+
        if($data['helpdesk_enabled']) return true;
                if($data['helpdesk_enabled']) return true;
+
    }
            }
+
    return false;
            return false;
+
}
        }
+
  
 +
Am_Di::getInstance()->billingPlanTable->customFields()
 +
    ->add(new Am_CustomFieldSelect('helpdesk_enabled', "Enable access to helpdesk", '', '', array('options' => array(0=>'No', 1=>'Yes'))));
  
        Am_Di::getInstance()->billingPlanTable->customFields()
+
Am_Di::getInstance()->hook->add('userMenu',  function (Am_Event $event){
            ->add(new Am_CustomFieldSelect('helpdesk_enabled', "Enable access to helpdesk", '', '', array('options' => array(0=>'No', 1=>'Yes'))));
+
    $menu = $event->getMenu();
 +
    if(!helpdeskEnabled()) $menu->removePage($menu->findOneBy('id', 'helpdesk'))
 +
});
  
        Am_Di::getInstance()->hook->add('userMenu', 'onUserMenu');
+
Am_Di::getInstance()->hook->add(Am_Event::INIT_CONTROLLER_PAGES, function (Am_Event $event){
        function onUserMenu($event){
+
    $c = $event->getController();
            $menu = $event->getMenu();
+
    if(!is_a($c, 'Helpdesk_IndexController')) return;
            if(!helpdeskEnabled()) $menu->removePage($menu->findOneBy('id', 'helpdesk'));
+
    if(!helpdeskEnabled()) throw new Am_Exception_InputError('Access denied!');
        }
+
  
        Am_Di::getInstance()->hook->add(Am_Event::INIT_CONTROLLER_PAGES, 'onControllerPages');
+
});
        function onControllerPages($event){
+
            $c = $event->getController();
+
            if(!is_a($c, 'Helpdesk_IndexController')) return;
+
            if(!helpdeskEnabled()) throw new Am_Exception_InputError('Access denied!');
+
           
+
        }
+
?>
+
 
</source>
 
</source>
 
By default access to helpdesk will be disabled for all users.  You can enable it for selected products only(aMember CP -> Manage Products -> Edit Product -> Enable access to helpdesk)
 
By default access to helpdesk will be disabled for all users.  You can enable it for selected products only(aMember CP -> Manage Products -> Edit Product -> Enable access to helpdesk)

Latest revision as of 06:21, 20 January 2017

Add this code to site.php file:

function helpdeskEnabled(){
    $user = Am_Di::getInstance()->auth->getUser();
    if(!$user) return true; // give user chance to log in first 
    foreach($user->getActiveProducts() as $p){
        $data = $p->getBillingPlanData();
        if($data['helpdesk_enabled']) return true;
    }
    return false;
}
 
Am_Di::getInstance()->billingPlanTable->customFields()
    ->add(new Am_CustomFieldSelect('helpdesk_enabled', "Enable access to helpdesk", '', '', array('options' => array(0=>'No', 1=>'Yes'))));
 
Am_Di::getInstance()->hook->add('userMenu',  function (Am_Event $event){
    $menu = $event->getMenu();
    if(!helpdeskEnabled()) $menu->removePage($menu->findOneBy('id', 'helpdesk'));  
});
 
Am_Di::getInstance()->hook->add(Am_Event::INIT_CONTROLLER_PAGES,  function (Am_Event $event){
    $c = $event->getController();
    if(!is_a($c, 'Helpdesk_IndexController')) return;
    if(!helpdeskEnabled()) throw new Am_Exception_InputError('Access denied!');
 
});

By default access to helpdesk will be disabled for all users. You can enable it for selected products only(aMember CP -> Manage Products -> Edit Product -> Enable access to helpdesk)