Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
08c0ffacab | ||
|
|
6ba66dcd37 | ||
|
|
81fa3d49c7 | ||
|
|
34a3351ed5 | ||
|
|
dd680c8a9a | ||
|
|
c92baf6cda | ||
|
|
86afea9b4e | ||
|
|
4568427c66 | ||
|
|
19c79d0374 | ||
|
|
0cdc2dc3d8 |
No files matched your search
@@ -99,6 +99,8 @@ class AnnotationReader
|
|||||||
case $reflect instanceof ReflectionClass :
|
case $reflect instanceof ReflectionClass :
|
||||||
return $reflect->name;
|
return $reflect->name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return "#UNKNOWN#";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getObjectNamespace(Reflector $reflect) : string
|
protected function getObjectNamespace(Reflector $reflect) : string
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ use RuntimeException, DirectoryIterator, Generator;
|
|||||||
|
|
||||||
class Breadcrumb extends RouteFetcher {
|
class Breadcrumb extends RouteFetcher {
|
||||||
|
|
||||||
|
public bool $ignoreSoleRoute = true;
|
||||||
|
|
||||||
public function getRouteTree(\Notes\Route\Attribute\Method\Route $route) : array
|
public function getRouteTree(\Notes\Route\Attribute\Method\Route $route) : array
|
||||||
{
|
{
|
||||||
$tree = [];
|
$tree = [];
|
||||||
@@ -36,7 +38,11 @@ class Breadcrumb extends RouteFetcher {
|
|||||||
else {
|
else {
|
||||||
$route = null;
|
$route = null;
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
|
if ($this->ignoreSoleRoute && count($tree) === 1) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
return array_reverse($tree);
|
return array_reverse($tree);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -87,7 +87,7 @@ class CommandFetcher {
|
|||||||
|
|
||||||
$class = $this->generateClassname($basename, $namespace);
|
$class = $this->generateClassname($basename, $namespace);
|
||||||
|
|
||||||
$objectResolver = new ObjectResolver($class, true, false, false);
|
$objectResolver = new ObjectResolver($class, $this->cache);
|
||||||
|
|
||||||
$attributes = $objectResolver->getAttributeListFromClassname( $this->annotations['object'], false ) ;
|
$attributes = $objectResolver->getAttributeListFromClassname( $this->annotations['object'], false ) ;
|
||||||
|
|
||||||
|
|||||||
@@ -56,4 +56,27 @@ abstract class Reflected
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAttributes(null|string|array $attributeType = null): array
|
||||||
|
{
|
||||||
|
if ($attributeType) {
|
||||||
|
$list = [];
|
||||||
|
|
||||||
|
foreach($this->attributes as $attribute) {
|
||||||
|
foreach((array) $attributeType as $type) {
|
||||||
|
if ($attribute->object instanceof $type) {
|
||||||
|
$list[] = $attribute;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_reverse($list);
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_reverse($this->attributes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getAttribute(string|array $attributeType): ?object
|
||||||
|
{
|
||||||
|
return $this->getAttributes($attributeType)[0] ?? null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -14,10 +14,14 @@ class ReflectedClass implements ReflectedInterface
|
|||||||
public array $traits = [],
|
public array $traits = [],
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
public function getClassName() : string
|
||||||
|
{
|
||||||
|
return end(explode('\\', $this->name));
|
||||||
|
}
|
||||||
|
|
||||||
public function getProperties(bool $deep = true) : array
|
public function getProperties(bool $deep = true) : array
|
||||||
{
|
{
|
||||||
return $deep ? array_replace(
|
return $deep ? array_replace(
|
||||||
# $this->parent ? $this->parent->getProperties(true) : [],
|
|
||||||
$this->parent ? $this->parent->getProperties(true) : [],
|
$this->parent ? $this->parent->getProperties(true) : [],
|
||||||
$this->properties
|
$this->properties
|
||||||
) : $this->properties;
|
) : $this->properties;
|
||||||
@@ -35,14 +39,33 @@ class ReflectedClass implements ReflectedInterface
|
|||||||
) : $this->methods;
|
) : $this->methods;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAttributes(bool $deep = true) : array
|
public function getAttributes(bool $deep = true, ? string $attributeType = null) : array
|
||||||
{
|
{
|
||||||
return $deep ? array_merge(
|
return array_reverse($this->getOrderedAttributes($deep, $attributeType));
|
||||||
$this->parent ? $this->parent->getAttributes(true) : [],
|
}
|
||||||
array_merge(...array_map(fn($e) => $e->getAttributes(true), $this->interfaces)),
|
|
||||||
array_merge(...array_map(fn($e) => $e->getAttributes(true), $this->traits)),
|
protected function getOrderedAttributes(bool $deep = true, ? string $attributeType = null) : array
|
||||||
|
{
|
||||||
|
$attributes = $deep ? array_merge(
|
||||||
|
$this->parent ? $this->parent->getOrderedAttributes(true) : [],
|
||||||
|
array_merge(...array_map(fn($e) => $e->getOrderedAttributes(true), $this->interfaces)),
|
||||||
|
array_merge(...array_map(fn($e) => $e->getOrderedAttributes(true), $this->traits)),
|
||||||
$this->attributes
|
$this->attributes
|
||||||
) : $this->attributes;
|
) : $this->attributes;
|
||||||
|
|
||||||
|
if ($attributeType) {
|
||||||
|
$list = [];
|
||||||
|
|
||||||
|
foreach($attributes as $attribute) {
|
||||||
|
if ($attribute->object instanceof $attributeType) {
|
||||||
|
$list[] = $attribute;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $list;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $attributes;
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get Attributes based on Native attributes's target
|
# Get Attributes based on Native attributes's target
|
||||||
@@ -146,4 +169,15 @@ class ReflectedClass implements ReflectedInterface
|
|||||||
|
|
||||||
return $list;
|
return $list;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getAttribute(string $attributeType): ?object
|
||||||
|
{
|
||||||
|
foreach($this->getAttributes(true, $attributeType) as $attribute) {
|
||||||
|
if ($attribute->object instanceof $attributeType) {
|
||||||
|
return $attribute;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,8 +15,4 @@ class ReflectedMethod extends Reflected implements ReflectedInterface
|
|||||||
public array $parameters = [],
|
public array $parameters = [],
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function getAttributes(): array
|
|
||||||
{
|
|
||||||
return $this->attributes;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -14,12 +14,6 @@ class ReflectedProperty extends Reflected implements ReflectedInterface, \ArrayA
|
|||||||
public array $attributes = [],
|
public array $attributes = [],
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function getAttributes(): array
|
|
||||||
{
|
|
||||||
return $this->attributes;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
## BACKWARD COMPATIBILITY ONLY To be removed really soon !
|
## BACKWARD COMPATIBILITY ONLY To be removed really soon !
|
||||||
public function offsetExists(mixed $offset): bool
|
public function offsetExists(mixed $offset): bool
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -31,7 +31,12 @@ class ObjectReflection {
|
|||||||
|
|
||||||
public static function fromClass(ReflectionClass|string $class, ? CacheInterface $cache = null) : self
|
public static function fromClass(ReflectionClass|string $class, ? CacheInterface $cache = null) : self
|
||||||
{
|
{
|
||||||
return new static($class, null, $cache);
|
return new static($class, $cache);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getClassReflection() : ReflectionClass
|
||||||
|
{
|
||||||
|
return $this->classReflection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function reflectClass() : ReflectedClass
|
public function reflectClass() : ReflectedClass
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ class RouteFetcher {
|
|||||||
|
|
||||||
$objects = $objectResolver->reflectedClass->getClassAttributeListFromClassname($attributes['object'], true, false);
|
$objects = $objectResolver->reflectedClass->getClassAttributeListFromClassname($attributes['object'], true, false);
|
||||||
|
|
||||||
foreach(array_reverse($objects) as $object) {
|
foreach($objects as $object) {
|
||||||
if ($object->method ?? false ) {
|
if ($object->method ?? false ) {
|
||||||
$methods ??= (array) $object->method;
|
$methods ??= (array) $object->method;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,5 +7,6 @@ class Taxus implements \Notes\Attribute {
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
public null|string|\BackedEnum $privilege = null,
|
public null|string|\BackedEnum $privilege = null,
|
||||||
public null|string $module = null,
|
public null|string $module = null,
|
||||||
|
public string|array $method = "*",
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
@@ -2,11 +2,13 @@
|
|||||||
|
|
||||||
namespace Notes\Security;
|
namespace Notes\Security;
|
||||||
|
|
||||||
|
use Notes\Common\ReflectedAttribute;
|
||||||
use Taxus\Taxus;
|
use Taxus\Taxus;
|
||||||
|
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
use Notes\ObjectResolver;
|
use Notes\ObjectResolver;
|
||||||
|
use Taxus\TaxusGrantEnum;
|
||||||
|
|
||||||
class SecurityHandler {
|
class SecurityHandler {
|
||||||
|
|
||||||
@@ -27,10 +29,12 @@ class SecurityHandler {
|
|||||||
return $this->isLocked($className, $methodName) ? $this->redirectResponse : null;
|
return $this->isLocked($className, $methodName) ? $this->redirectResponse : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# @TODO Must HANDLE REALM !
|
||||||
public function isLocked(string $className, string $methodName) : bool
|
public function isLocked(string $className, string $methodName) : bool
|
||||||
{
|
{
|
||||||
if ( $security = $this->getClassAttributes(Attribute\Security::class, $className, $methodName) ) {
|
# Searching method first and fallbacking on object if none found
|
||||||
return array_pop($security)->locked;
|
if ( $list = $this->findAttributes(Attribute\Security::class, $className, $methodName) ) {
|
||||||
|
return array_shift($list)->locked;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -38,15 +42,16 @@ class SecurityHandler {
|
|||||||
|
|
||||||
public function taxus(string $className, string $methodName, object $user = null) : ? ResponseInterface
|
public function taxus(string $className, string $methodName, object $user = null) : ? ResponseInterface
|
||||||
{
|
{
|
||||||
if ($taxus = $this->getClassAttributes(Attribute\Taxus::class, $className, $methodName)) {
|
$fromObject = $this->findAttributes(Attribute\Taxus::class, $className);
|
||||||
if ($this->unauthorizeResponse) {
|
$fromMethod = $this->findAttributes(Attribute\Taxus::class, $className, $methodName);
|
||||||
foreach ($taxus as $item) {
|
|
||||||
if (!isset($item->privilege) || $this->taxus->granted($item->privilege, $user, $item)) {
|
if ($fromMethod || $fromObject) {
|
||||||
|
if ( $this->taxusGrantPermission($fromMethod, $user) || $this->taxusGrantPermission($fromObject, $user) ) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return call_user_func_array($this->unauthorizeResponse, [ $user, $taxus, $className, $methodName ]);
|
if ($this->unauthorizeResponse) {
|
||||||
|
return call_user_func_array($this->unauthorizeResponse, [ $user, ['method' => $fromMethod, 'object' => $fromObject ], $className, $methodName ]);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
throw new \ErrorException("Unauthorized response given.");
|
throw new \ErrorException("Unauthorized response given.");
|
||||||
@@ -56,22 +61,41 @@ class SecurityHandler {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getClassAttributes(string $annotationClass, string $className, string $methodName)/* : \Notes\Attribute|array */
|
protected function findAttributes(string $attribute, string $class, ? string $method = null) : array
|
||||||
{
|
{
|
||||||
$objectResolver = new ObjectResolver($className);
|
$objectResolver = new ObjectResolver($class);
|
||||||
|
|
||||||
try {
|
if ($method) {
|
||||||
$method = $objectResolver->reflectedClass->getClassAttributeList( $annotationClass, true, false );
|
$fromMethod = $objectResolver->reflectedClass->getMethods( false );
|
||||||
|
|
||||||
# var_dump($method);
|
if (isset($fromMethod[$method])) {
|
||||||
}
|
$attributeList = $fromMethod[$method]->getAttributes($attribute);
|
||||||
catch(\Exception $e) { }
|
|
||||||
|
|
||||||
if ( $method[$methodName] ?? false ) {
|
if ($attributeList) {
|
||||||
return $method[$methodName];
|
return array_map(fn(ReflectedAttribute $ref) => $ref->object, $attributeList);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return array_filter($method, fn($e) => is_object($e));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$attributeList = $objectResolver->reflectedClass->getAttributes(false, $attribute) ?: $objectResolver->reflectedClass->getAttributes(true, $attribute);
|
||||||
|
|
||||||
|
if ($attributeList) {
|
||||||
|
return array_map(fn(ReflectedAttribute $ref) => $ref->object, $attributeList);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function taxusGrantPermission(array $attributeList, object $user = null) : bool
|
||||||
|
{
|
||||||
|
foreach ($attributeList as $item) {
|
||||||
|
if ( $grant = $this->taxus->granted($item->privilege, $user, $item) ) {
|
||||||
|
if (is_bool($grant) ? $grant : $grant === TaxusGrantEnum::Authorized) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user