REST API hook help

Discussion in 'aMember Pro v.4' started by xindexer, Dec 11, 2012.

  1. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    I have installed the REST API and have been able to make it work for the default settings.

    I have also been able to successfully hack the base code to start customizing the procedures; however, I'm stuck on trying to implement a hook.

    Conceptually I want to do this (i think)

    1. Use the hook to set up a new api call by adding a controller
    2. Use the controller to execute the authentication then the custom code

    in my plugin file I have this function:

    Code:
    function onGetApiControllers(Am_Event $event) {
            return $event->getList->addController('test', 'test', array('by-login-pass', 'by-login', 'by-email'), 'Check User Access Test', 'api');
    }
    Where do I add the controller file? I was able add a "TestController.php" file in the /applaction/api/controller/ folder and it worked just fine.

    But where would I put the file for the plugin?

    An example would be very helpful.

    Thanks
  2. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Actually this doesn't matter where you have that file, jsut make sure it is included before being used.
    So for example you even can include TextController class inside your plugin file.
    Or a better choice create separate folder to store plugin files:
    /amember/application/default/plugins/misc/pluginname/
    put your TestController.php into that folder, then from your plugin hook include that file:
    PHP:
    function onGetApiControllers(Am_Event $event) {
           include_once(
    'TestController.php'); 
           return 
    $event->getList->addController('test''test', array('by-login-pass''by-login''by-email'), 'Check User Access Test''api');
    }
  3. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    Still not getting anywhere on this. Here's what I have but it's not working

    PHP:
    class Am_Plugin_Seo extends Am_Plugin {
     
        function 
    onGetApiControllers(Am_Event $event) {
            return 
    $event->getList->addController('test-access''test-access', array('by-login-pass''by-login''by-email'),
                
    'Test User Access''api');
        }
     
    }
     
    class 
    Api_TestAccessController extends Am_Controller_Api
    {
        protected function 
    checkUser(User $user null$errCode null$errMsg null)
        {
            if (
    $user)
            {
                
    $accessRecords $user->getActiveProductsExpiration();
                
    $ret = array(
                    
    'ok' => true,
                    
    'name' => $user->getName(),
                );
                foreach (
    $accessRecords as $pid => $expires)
                {
                    
    $ret['subscriptions'][$pid] = $expires;
                }
            } else {
                if (empty(
    $errCode)) $errCode = -1;
                if (empty(
    $errMsg)) $errMsg "Failure";
                
    $ret = array(
                    
    'ok' => false,
                    
    'code' => $errCode,
                    
    'msg'  => $errMsg,
                );
            }
            
    $this->ajaxResponse($ret);
        }
     
        
    /**
        * Check access by username/password
        */
        
    function byLoginPassAction()
        {
            
    $code null;
            
    $user $this->getDi()->userTable->getAuthenticatedRow($this->_getParam('login'), $this->_getParam('pass'), $code);
            
    $res = new Am_Auth_Result($code);
            
    $this->checkUser($user$res->getCode(), $res->getMessage());
        }
        
    /**
        * Check access by username
        */
        
    function byLoginAction()
        {
            
    $user $this->getDi()->userTable->findFirstByLogin($this->_getParam('login'));
            
    $this->checkUser($user);
        }
        
    /**
        * Check access by email address
        */
        
    function byEmailAction()
        {
            
    $user $this->getDi()->userTable->findFirstByEmail($this->_getParam('email'));
            
    $this->checkUser($user);
        }
    }
     
    ?>
    What am I doing wrong?
  4. xindexer

    xindexer Member

    Joined:
    Apr 17, 2011
    Messages:
    52
    When i put this line in the bootstrap file it works fine

    PHP:
    $this->addController('test-access'test-access', array('reply'), 'Test User Access', 'api');
    but when I use this - it doesn't work
    PHP:
    class Am_Plugin_Seo extends Am_Plugin {
        function 
    onGetApiControllers(Am_Event $event) {
            return 
    $event->getList()->addController('test-access''test-access', array('reply'), 'Test User Access''api');
        }
    }
    I have tried multiple iterations here - and am tired of spending hours on this one thing

    PHP:
    class Am_Plugin_Seo extends Am_Plugin {
        function 
    onGetApiControllers(Am_Event $event) {
            return 
    $event->getList->addController('test-access''test-access', array('reply'), 'Test User Access''api');
        }
    }

    Where is the problem?

    Heres the test-access class

    PHP:
    class Api_TestAccessController extends Am_Controller_Api
    {
        protected function 
    replyAction() {
            
    $this->ajaxResponse('test');
        }
    }
  5. alexander

    alexander Administrator Staff Member

    Joined:
    Jan 8, 2003
    Messages:
    6,279
    Sorry for this issue.
    There is a typo in Am_Event class.
    Change
    tyis line:
    const GET_API_CONTROLLERS = 'getApiPermissionsList';
    to
    const GET_API_CONTROLLERS = 'getApiControllers';

Share This Page