diff --git a/config/packages/translation.yaml b/config/packages/translation.yaml index d87c6cf..aaf506f 100644 --- a/config/packages/translation.yaml +++ b/config/packages/translation.yaml @@ -8,7 +8,7 @@ framework: crowdin: dsn: '%env(CROWDIN_DSN)%' domains: ['messages'] - locales: ['en', 'nl', 'leet', 'lol'] + locales: ['en', 'nl', 'leet'] # loco: # dsn: '%env(LOCO_DSN)%' # lokalise: diff --git a/config/routes.yaml b/config/routes.yaml index 9ee50f2..a09b8cc 100644 --- a/config/routes.yaml +++ b/config/routes.yaml @@ -34,6 +34,13 @@ login: path: '/{_locale}/login' controller: App\Controller\UserController::loginAction +webhook: + path: '/hook/{id}/{token}' + controller: App\Controller\JobController::hookAction + requirements: + id: \d+ + token: '[A-Za-z0-9]+' + job_index: path: '/{_locale}/job' controller: App\Controller\JobController::defaultAction diff --git a/migrations/Version1003.php b/migrations/Version1003.php new file mode 100644 index 0000000..5257441 --- /dev/null +++ b/migrations/Version1003.php @@ -0,0 +1,51 @@ +connection->executeQuery('SELECT * FROM job')->fetchAllAssociative(); + foreach($allJobs as $job) { + $data = json_decode($job['data'], true); + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $randomString = ''; + $length = 32; + + for ($i = 0; $i < $length; $i++) { + $index = rand(0, strlen($characters) - 1); + $randomString .= $characters[$index]; + } + + $data['hooktoken'] = $randomString; + $this->addSql('UPDATE job SET data = "' . addSlashes(json_encode($data)) . '" WHERE id = ' . $job['id']); + } + } + + public function down(Schema $schema): void + { + $allJobs = $this->connection->executeQuery('SELECT * FROM job')->fetchAllAssociative(); + foreach($allJobs as $job) { + $data = json_decode($job['data'], true); + unset($data['hooktoken']); + $this->addSql('UPDATE job SET data = "' . addSlashes(json_encode($data)) . '" WHERE id = ' . $job['id']); + } + } +} diff --git a/src/Controller/JobController.php b/src/Controller/JobController.php index d78341b..09aef0d 100644 --- a/src/Controller/JobController.php +++ b/src/Controller/JobController.php @@ -111,4 +111,15 @@ class JobController extends AbstractController } return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST); } + + public function hookAction(Request $request, ManagerRegistry $doctrine, int $id, string $token) + { + $jobRepo = $doctrine->getRepository(Job::class); + $job = $jobRepo->find($id); + if(!empty($job->getToken()) && $job->getToken() == $token && $job->getRunning() != 1) { + return new JsonResponse($jobRepo->run($job, false, time())); + } + + return new JsonResponse(['success'=>false, 'message' => 'Your request is invalid'], Response::HTTP_BAD_REQUEST); + } } \ No newline at end of file diff --git a/src/Entity/Job.php b/src/Entity/Job.php index 50f8cd6..8c5fb95 100644 --- a/src/Entity/Job.php +++ b/src/Entity/Job.php @@ -259,4 +259,29 @@ class Job return $this; } + public function getToken(): string + { + return $this->getData('hooktoken') ?? ''; + } + + public function deleteToken(): Job + { + $this->removeData('hooktoken'); + return $this; + } + + public function addToken(): Job + { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $randomString = ''; + $length = 32; + + for ($i = 0; $i < $length; $i++) { + $index = rand(0, strlen($characters) - 1); + $randomString .= $characters[$index]; + } + + $this->setData('hooktoken', $randomString); + return $this; + } } \ No newline at end of file diff --git a/templates/job/add.html.twig b/templates/job/add.html.twig index 9b9bd47..b2329f9 100644 --- a/templates/job/add.html.twig +++ b/templates/job/add.html.twig @@ -2,252 +2,252 @@ {% block title %}{{ "job.add.title" | trans }}{% endblock %} {% block content %}