diff --git a/Documentation/Conception.md b/Documentation/Conception.md index 68b4cd9..177be45 100644 --- a/Documentation/Conception.md +++ b/Documentation/Conception.md @@ -64,7 +64,7 @@ et de reporter le plus d'erreurs possibles lorsqu'une requĂȘte ne valide pas le public function doPostAction(array $form): HttpResponse { $failures = []; $req = HttpRequest::from($form, $failures, [ - 'email' => [Validators::email(), Validators::isLenBetween(6, 64)] + 'email' => [DefaultValidators::email(), DefaultValidators::isLenBetween(6, 64)] ]); if (!empty($failures)) { //ou $req == null diff --git a/Documentation/mvc/editor.puml b/Documentation/mvc/editor.puml index d684e5b..3e68d33 100644 --- a/Documentation/mvc/editor.puml +++ b/Documentation/mvc/editor.puml @@ -35,4 +35,10 @@ class TacticInfoGateway{ TacticInfoGateway *--"- con" Connexion +class TacticValidator{ + + validateAccess(tacticId:int, tactic:?TacticInfo, ownerId:int): ?ValidationFail {static} +} + +EditorController ..> TacticValidator + @enduml \ No newline at end of file diff --git a/Documentation/validation.puml b/Documentation/validation.puml index 4595a5a..64ac1a5 100644 --- a/Documentation/validation.puml +++ b/Documentation/validation.puml @@ -53,7 +53,7 @@ class Validation { Validation ..> Validator -class Validators { +class DefaultValidators { + nonEmpty(): Validator + shorterThan(limit: int): Validator + userString(maxLen: int): Validator @@ -68,7 +68,7 @@ class Validators { + isURL(): Validator } -Validators ..> Validator +DefaultValidators ..> Validator diff --git a/src/Api/Controller/APIAuthController.php b/src/Api/Controller/APIAuthController.php index fc0eef6..8e6291c 100644 --- a/src/Api/Controller/APIAuthController.php +++ b/src/Api/Controller/APIAuthController.php @@ -8,7 +8,7 @@ use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; use IQBall\Core\Model\AuthModel; -use IQBall\Core\Validation\Validators; +use IQBall\Core\Validation\DefaultValidators; class APIAuthController { private AuthModel $model; @@ -27,8 +27,8 @@ class APIAuthController { */ public function authorize(): HttpResponse { return Control::runChecked([ - "email" => [Validators::email(), Validators::lenBetween(5, 256)], - "password" => [Validators::lenBetween(6, 256)], + "email" => [DefaultValidators::email(), DefaultValidators::lenBetween(5, 256)], + "password" => [DefaultValidators::lenBetween(6, 256)], ], function (HttpRequest $req) { $failures = []; $account = $this->model->login($req["email"], $req["password"], $failures); diff --git a/src/Api/Controller/APITacticController.php b/src/Api/Controller/APITacticController.php index a116add..2156538 100644 --- a/src/Api/Controller/APITacticController.php +++ b/src/Api/Controller/APITacticController.php @@ -10,7 +10,7 @@ use IQBall\Core\Http\HttpResponse; use IQBall\Core\Http\JsonHttpResponse; use IQBall\Core\Model\TacticModel; use IQBall\Core\Validation\FieldValidationFail; -use IQBall\Core\Validation\Validators; +use IQBall\Core\Validation\DefaultValidators; /** * API endpoint related to tactics @@ -33,7 +33,7 @@ class APITacticController { */ public function updateName(int $tactic_id, Account $account): HttpResponse { return Control::runChecked([ - "name" => [Validators::lenBetween(1, 50), Validators::nameWithSpaces()], + "name" => [DefaultValidators::lenBetween(1, 50), DefaultValidators::nameWithSpaces()], ], function (HttpRequest $request) use ($tactic_id, $account) { $failures = $this->model->updateName($tactic_id, $request["name"], $account->getUser()->getId()); diff --git a/src/App/Control.php b/src/App/Control.php index 5c2fe0f..b8148bb 100644 --- a/src/App/Control.php +++ b/src/App/Control.php @@ -11,7 +11,7 @@ use IQBall\Core\Validation\Validator; class Control { /** * Runs given callback, if the request's json validates the given schema. - * @param array $schema an array of `fieldName => Validators` which represents the request object schema + * @param array $schema an array of `fieldName => DefaultValidators` which represents the request object schema * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * THe callback must accept an HttpRequest, and return an HttpResponse object. * @return HttpResponse @@ -30,7 +30,7 @@ class Control { /** * Runs given callback, if the given request data array validates the given schema. * @param array $data the request's data array. - * @param array $schema an array of `fieldName => Validators` which represents the request object schema + * @param array $schema an array of `fieldName => DefaultValidators` which represents the request object schema * @param callable(HttpRequest): HttpResponse $run the callback to run if the request is valid according to the given schema. * THe callback must accept an HttpRequest, and return an HttpResponse object. * @return HttpResponse diff --git a/src/App/Controller/AuthController.php b/src/App/Controller/AuthController.php index cd89d11..4d9eebe 100644 --- a/src/App/Controller/AuthController.php +++ b/src/App/Controller/AuthController.php @@ -7,7 +7,7 @@ use IQBall\App\ViewHttpResponse; use IQBall\Core\Http\HttpRequest; use IQBall\Core\Http\HttpResponse; use IQBall\Core\Model\AuthModel; -use IQBall\Core\Validation\Validators; +use IQBall\Core\Validation\DefaultValidators; class AuthController { private AuthModel $model; @@ -32,10 +32,10 @@ class AuthController { public function register(array $request, MutableSessionHandle $session): HttpResponse { $fails = []; $request = HttpRequest::from($request, $fails, [ - "username" => [Validators::name(), Validators::lenBetween(2, 32)], - "password" => [Validators::lenBetween(6, 256)], - "confirmpassword" => [Validators::lenBetween(6, 256)], - "email" => [Validators::email(), Validators::lenBetween(5, 256)], + "username" => [DefaultValidators::name(), DefaultValidators::lenBetween(2, 32)], + "password" => [DefaultValidators::lenBetween(6, 256)], + "confirmpassword" => [DefaultValidators::lenBetween(6, 256)], + "email" => [DefaultValidators::email(), DefaultValidators::lenBetween(5, 256)], ]); if (!empty($fails)) { return ViewHttpResponse::twig("display_register.html.twig", ['fails' => $fails]); diff --git a/src/App/Controller/TeamController.php b/src/App/Controller/TeamController.php index 4ab3fd7..048d182 100644 --- a/src/App/Controller/TeamController.php +++ b/src/App/Controller/TeamController.php @@ -11,7 +11,7 @@ use IQBall\Core\Http\HttpResponse; use IQBall\Core\Model\TeamModel; use IQBall\Core\Validation\FieldValidationFail; use IQBall\Core\Validation\ValidationFail; -use IQBall\Core\Validation\Validators; +use IQBall\Core\Validation\DefaultValidators; class TeamController { private TeamModel $model; @@ -48,10 +48,10 @@ class TeamController { public function submitTeam(array $request, SessionHandle $session): HttpResponse { $failures = []; $request = HttpRequest::from($request, $failures, [ - "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], - "main_color" => [Validators::hexColor()], - "second_color" => [Validators::hexColor()], - "picture" => [Validators::isURL()], + "name" => [DefaultValidators::lenBetween(1, 32), DefaultValidators::nameWithSpaces()], + "main_color" => [DefaultValidators::hexColor()], + "second_color" => [DefaultValidators::hexColor()], + "picture" => [DefaultValidators::isURL()], ]); if (!empty($failures)) { $badFields = []; @@ -84,7 +84,7 @@ class TeamController { public function listTeamByName(array $request, SessionHandle $session): HttpResponse { $errors = []; $request = HttpRequest::from($request, $errors, [ - "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], + "name" => [DefaultValidators::lenBetween(1, 32), DefaultValidators::nameWithSpaces()], ]); if (!empty($errors) && $errors[0] instanceof FieldValidationFail) { @@ -166,7 +166,7 @@ class TeamController { ], HttpCodes::FORBIDDEN); } $request = HttpRequest::from($request, $errors, [ - "email" => [Validators::email(), Validators::lenBetween(5, 256)], + "email" => [DefaultValidators::email(), DefaultValidators::lenBetween(5, 256)], ]); if(!empty($errors)) { return ViewHttpResponse::twig('add_member.html.twig', ['badEmail' => true,'idTeam' => $idTeam]); @@ -226,10 +226,10 @@ class TeamController { } $failures = []; $request = HttpRequest::from($request, $failures, [ - "name" => [Validators::lenBetween(1, 32), Validators::nameWithSpaces()], - "main_color" => [Validators::hexColor()], - "second_color" => [Validators::hexColor()], - "picture" => [Validators::isURL()], + "name" => [DefaultValidators::lenBetween(1, 32), DefaultValidators::nameWithSpaces()], + "main_color" => [DefaultValidators::hexColor()], + "second_color" => [DefaultValidators::hexColor()], + "picture" => [DefaultValidators::isURL()], ]); if (!empty($failures)) { $badFields = []; diff --git a/src/Core/Validation/Validators.php b/src/Core/Validation/DefaultValidators.php similarity index 99% rename from src/Core/Validation/Validators.php rename to src/Core/Validation/DefaultValidators.php index 52bc08c..b6ffc38 100644 --- a/src/Core/Validation/Validators.php +++ b/src/Core/Validation/DefaultValidators.php @@ -5,7 +5,7 @@ namespace IQBall\Core\Validation; /** * A collection of standard validators */ -class Validators { +class DefaultValidators { /** * @return Validator a validator that validates a given regex */