- Handles the @Security annotation. Rules are defined at DI level.
This commit is contained in:
commit
fe04bddf77
|
@ -0,0 +1,21 @@
|
||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2019 Dave Mc Nicoll
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -0,0 +1,26 @@
|
||||||
|
{
|
||||||
|
"name": "mcnd/notes-security",
|
||||||
|
"description": "Security annotation at class or method level.",
|
||||||
|
"type": "library",
|
||||||
|
"license": "MIT",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Dave Mc Nicoll",
|
||||||
|
"email": "mcndave@gmail.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"mcnd/notes": "master-dev",
|
||||||
|
},
|
||||||
|
"repositories": [
|
||||||
|
{
|
||||||
|
"type": "vcs",
|
||||||
|
"url": "https://github.com/mcNdave/notes.git"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Notes\\Route\\": "src/"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,8 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Notes\Security\Annotation;
|
||||||
|
|
||||||
|
class Security implements \Notes\Annotation {
|
||||||
|
|
||||||
|
public bool $locked;
|
||||||
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
<?php declare(strict_types = 1);
|
||||||
|
|
||||||
|
namespace Notes\Security;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
|
||||||
|
use Notes\ObjectReflection,
|
||||||
|
Notes\ObjectResolver;
|
||||||
|
|
||||||
|
class SecurityHandler {
|
||||||
|
|
||||||
|
protected ResponseInterface $response;
|
||||||
|
|
||||||
|
public function __construct(ResponseInterface $response) {
|
||||||
|
$this->response = $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function verify(string $className, string $methodName) : ? ResponseInterface {
|
||||||
|
# Should generate an equivalent of Ulmus's object reflection here !
|
||||||
|
if ( $this->getClassAnnotations($className, $methodName)->locked ) {
|
||||||
|
return $this->response;
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getClassAnnotations(string $className, string $methodName) : \Notes\Annotation
|
||||||
|
{
|
||||||
|
$objectResolver = new ObjectResolver($className, true, true, false, true);
|
||||||
|
|
||||||
|
if ( null !== ( $method = $objectResolver->getAnnotationListFromClassname( Annotation\Security::class ) ) ) {
|
||||||
|
if ( $method[$methodName] ?? false ) {
|
||||||
|
return $method[$methodName];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( null !== ( $object = $objectResolver->getAnnotationFromClassname( Annotation\Security::class ) ) ) {
|
||||||
|
return $object;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue