2019-05-09 06:54:39 +00:00
< ? php
namespace Database\Validator ;
use Database\Document ;
use Utopia\Validator ;
class Authorization extends Validator
{
/**
* @ var array
*/
2019-09-06 17:04:26 +00:00
protected static $roles = [ '*' ];
2019-05-09 06:54:39 +00:00
/**
* @ var Document
*/
protected $document = null ;
/**
* @ var string
*/
protected $action = '' ;
/**
* @ var string
*/
2019-09-17 09:55:31 +00:00
protected $message = 'Authorization Error' ;
2019-05-09 06:54:39 +00:00
/**
* Structure constructor .
*
* @ param Document $document
2019-09-06 17:04:26 +00:00
* @ param string $action
2019-05-09 06:54:39 +00:00
*/
public function __construct ( Document $document , $action )
{
$this -> document = $document ;
$this -> action = $action ;
}
/**
2019-09-06 17:04:26 +00:00
* Get Description .
2019-05-09 06:54:39 +00:00
*
* Returns validator description
*
* @ return string
*/
public function getDescription ()
{
return $this -> message ;
}
/**
2019-09-06 17:04:26 +00:00
* Is valid .
2019-05-09 06:54:39 +00:00
*
* Returns true if valid or false if not .
*
2019-09-06 17:04:26 +00:00
* @ param array $permissions
*
2019-05-09 06:54:39 +00:00
* @ return bool
*/
public function isValid ( $permissions )
{
2019-09-06 17:04:26 +00:00
if ( ! self :: $status ) {
2019-05-09 06:54:39 +00:00
return true ;
}
2019-09-06 17:04:26 +00:00
if ( ! isset ( $permissions [ $this -> action ])) {
$this -> message = 'Missing action key: "' . $this -> action . '"' ;
2019-05-09 06:54:39 +00:00
return false ;
}
2019-10-20 18:28:07 +00:00
if ( is_array ( $permissions [ $this -> action ]) && empty ( $permissions [ $this -> action ])) {
return true ;
}
2019-05-09 06:54:39 +00:00
$permission = null ;
foreach ( $permissions [ $this -> action ] as $permission ) {
2019-09-06 17:04:26 +00:00
$permission = str_replace ( ':{self}' , ':' . $this -> document -> getUid (), $permission );
2019-05-09 06:54:39 +00:00
2019-09-06 17:04:26 +00:00
if ( in_array ( $permission , self :: getRoles ())) {
2019-05-09 06:54:39 +00:00
return true ;
}
}
2019-10-20 18:28:07 +00:00
$this -> message = 'User is missing ' . $this -> action . ' for "' . $permission . '" permission. Only this scopes "' . json_encode ( self :: getRoles ()) . '" is given and only this are allowed "' . json_encode ( $permissions [ $this -> action ]) . '".' ;
2019-05-09 06:54:39 +00:00
return false ;
}
/**
* @ param string $role
*/
2019-09-06 17:04:26 +00:00
public static function setRole ( $role )
2019-05-09 06:54:39 +00:00
{
self :: $roles [] = $role ;
}
/**
* @ return array
*/
2019-09-06 17:04:26 +00:00
public static function getRoles ()
2019-05-09 06:54:39 +00:00
{
return self :: $roles ;
}
/**
* @ var bool
*/
2019-09-06 17:04:26 +00:00
public static $status = true ;
2019-05-09 06:54:39 +00:00
/**
*
*/
2019-09-06 17:04:26 +00:00
public static function enable ()
2019-05-09 06:54:39 +00:00
{
self :: $status = true ;
}
/**
*
*/
2019-09-06 17:04:26 +00:00
public static function disable ()
2019-05-09 06:54:39 +00:00
{
self :: $status = false ;
}
2019-09-06 17:04:26 +00:00
}