Advertisement
TheFan1968

ZF3: Simple AccessControlClass

Sep 20th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.67 KB | None | 0 0
  1. <?php
  2. namespace Kvberlin\AccessControl;
  3.  
  4. use Application\UserModel\UserRepoInterface;
  5. use Zend\Session\Container;
  6. use Zend\Mvc\Controller\AbstractActionController;
  7. // use Zend\Debug\Debug;
  8.  
  9.  
  10. class AccessControl implements AccessControlInterface
  11. {
  12.  
  13.     private $user;
  14.     private $acl;
  15.    
  16.     /**
  17.      *
  18.      * {@inheritDoc}
  19.      * @see \Kvberlin\AccessControl\AccessControlInterface::getLogin()
  20.      */
  21.     public function getLogin(string $name, string $password){
  22.        
  23.         $login_user = $this->user->getUserByName($name);
  24. //         Debug::dump($login_user);
  25.         if($login_user->getName()):
  26.             if(md5($password) == $login_user->getPassword()):
  27.                 $container = new Container('application');
  28.                 $container->username      = $login_user->getName();
  29.                 $container->login         = true;
  30.                 $container->loginkey      = sha1($login_user->getName().$login_user->getPassword());
  31.             else:
  32.                 //TODO Passwort falsch
  33.                 return false;
  34.             endif;
  35.         else:
  36.             //TODO Username existiert nicht
  37.             return false;
  38.         endif;    
  39.        return true;
  40.     }
  41.  
  42.     /**
  43.      *
  44.      * {@inheritDoc}
  45.      * @see \Kvberlin\AccessControl\AccessControlInterface::setUserRepo()
  46.      */
  47.     public function setUserRepo(UserRepoInterface $repo){
  48.         $this->user = $repo;
  49.     }
  50.  
  51.     /**
  52.      *
  53.      * {@inheritDoc}
  54.      * @see \Kvberlin\AccessControl\AccessControlInterface::checkLoginStatus()
  55.      */
  56.     public function checkLoginStatus(string $loginkey){
  57.         $container  = new Container('application');
  58.         $user       = $this->user->getUserByName($container->username);
  59.         $key        = sha1($user->getName().$user->getPassword());
  60.         if($loginkey === $key):
  61.             return true;
  62.         else:
  63.             $this->getLogout();
  64.             return false;
  65.         endif;
  66.     }
  67.  
  68.     /**
  69.      *
  70.      * {@inheritDoc}
  71.      * @see \Kvberlin\AccessControl\AccessControlInterface::getLogout()
  72.      */
  73.     public function getLogout(){
  74.         $container = new Container('application');
  75.         $container->init          = 0;
  76.         $container->username      = null;
  77.         $container->login         = false;
  78.         $container->offsetUnset('loginkey');
  79.         session_destroy();
  80.         return true;
  81.     }
  82.    
  83.     /**
  84.      * {@inheritDoc}
  85.      * @see \Kvberlin\AccessControl\AccessControlInterface::setAclList()
  86.      */
  87.     public function setAclList(AccessControlList $acl){
  88.         $this->acl = $acl;
  89.     }
  90.     /**
  91.      * {@inheritDoc}
  92.      * @see \Kvberlin\AccessControl\AccessControlInterface::isAllowed()
  93.      */
  94.     public function isAllowed(string $resource=null, string $privilege){
  95.        
  96.         return $this->acl->isAllowed($this->getUserRole(),$resource,$privilege);
  97.     }
  98.     /**
  99.      * {@inheritDoc}
  100.      * @see \Kvberlin\AccessControl\AccessControlInterface::getUserRole()
  101.      */
  102.     public function getUserRole(){
  103.         $user  = $this->user->getUserByName($this->getApplicationContainer()->username);
  104.         return $user->getRole();
  105.     }
  106.  
  107.     /**
  108.      * {@inheritDoc}
  109.      * @see \Kvberlin\AccessControl\AccessControlInterface::getApplicationContainer()
  110.      */
  111.     public function getApplicationContainer(){
  112.         return new Container('application');
  113.     }
  114.  
  115.  
  116.     public function checkAccess(string $resource, string $privilege, AbstractActionController $controller){
  117.         if($this->isAllowed($resource,$privilege)):
  118.             return true;
  119.         else:
  120.             $controller->redirect()->toRoute('application',['action' => 'access']);
  121.         endif;
  122.     }
  123.  
  124.  
  125.  
  126.  
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement