Advertisement
Guest User

Untitled

a guest
Mar 8th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.01 KB | None | 0 0
  1. <?php
  2.  
  3. class Api_Controller_Default extends Core_Controller_Default {
  4.  
  5.     /**
  6.      * @var string
  7.      */
  8.     public $namespace = "api";
  9.  
  10.     /**
  11.      * @var Api_Model_User
  12.      */
  13.     public $user = null;
  14.  
  15.     /**
  16.      * @var array
  17.      */
  18.     public $secured_actions = [];
  19.  
  20.     /**
  21.      * @return $this
  22.      */
  23.     public function init() {
  24.    
  25.         parent::init();
  26.  
  27.         # Test AUTH
  28.        if (!preg_match("/admin_api_account_autologin/", $this->getFullActionName("_"))) {
  29.            
  30.             $username = $this->getRequest()->getServer("PHP_AUTH_USER");
  31.             $password = $this->getRequest()->getServer("PHP_AUTH_PW");
  32.  
  33.             $this->user = (new Api_Model_User())
  34.                 ->find($username, 'username');
  35.  
  36.             if ($_SERVER['REMOTE_ADDR'] === $_SERVER['SERVER_ADDR'] && __getConfig('allow_local_api') === true) {
  37.                 // Special case grant auth
  38.                 return $this;
  39.             } else {
  40.                 if (!$this->user->getId() || !$this->user->authenticate($password)) {
  41.                     return $this->forward('notauthorized');
  42.                 }
  43.             }
  44.         }
  45.  
  46.         # Test ACL
  47.        if(in_array($this->getRequest()->getActionName(), $this->secured_actions)) {
  48.             return $this->hasAccess();
  49.         }
  50.  
  51.         return $this;
  52.  
  53.     }
  54.  
  55.     /**
  56.      * @param null $key
  57.      * @return $this|void
  58.      */
  59.     public function hasAccess($key = null) {
  60.         if(empty($key)) {
  61.             $key = sprintf("%s.%s", $this->namespace, $this->getRequest()->getActionName());
  62.         }
  63.  
  64.         if(!$this->user->hasAccess($key)) {
  65.             return $this->forward("notauthorized");
  66.         }
  67.  
  68.         return $this;
  69.     }
  70.  
  71.     /**
  72.      *
  73.      */
  74.     public function notauthorizedAction() {
  75.         $data = array(
  76.             "error" => 1,
  77.             "message" => $this->_("Authentication failed. Please, check the username and/or the password")
  78.         );
  79.         $this->_sendHtml($data);
  80.     }
  81.  
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement