This repository was archived by the owner on Apr 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdminController.php
More file actions
132 lines (117 loc) · 5.05 KB
/
Copy pathAdminController.php
File metadata and controls
132 lines (117 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* Copyright Zikula - https://ziku.la/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Zikula\Module\CoreManagerModule\Controller;
use Github\Exception\RuntimeException as GitHubRuntimeException;
use Github\HttpClient\Message\ResponseMediator;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Zikula\Core\Controller\AbstractController;
use Zikula\Module\CoreManagerModule\Entity\CoreReleaseEntity;
use Zikula\Module\CoreManagerModule\Form\Type\ConfigType;
use Zikula\Module\CoreManagerModule\Form\Type\ConfirmReloadType;
use Zikula\Module\CoreManagerModule\Manager\ReleaseManager;
use Zikula\ThemeModule\Engine\Annotation\Theme;
/**
* @Route("/admin")
*
* UI operations executable by admins only.
*/
class AdminController extends AbstractController
{
/**
* @Route("")
* @Theme("admin")
*
* @return Response
* @throws AccessDeniedException
*/
public function index(Request $request)
{
if (!$this->hasPermission($this->name.'::', '::', ACCESS_ADMIN)) {
throw new AccessDeniedException();
}
// Rate limit check
$client = $this->container->get('zikula_core_manager_module.client_helper')->getGitHubClient();
$response = $client->getHttpClient()->get('rate_limit');
$rate = ResponseMediator::getContent($response);
$rate = $rate['rate'];
$now = new \DateTime('now');
$reset = \DateTime::createFromFormat('U', $rate['reset'], new \DateTimeZone('UTC'));
$rate['minutesUntilReset'] = $now->diff($reset)->format('%i');
$form = $this->createForm(ConfigType::class, $this->getVars());
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->setVars($form->getData());
$this->addFlash('info', $this->__('Settings updated!'));
$client = $this->container->get('zikula_core_manager_module.client_helper')->getGitHubClient(false);
if ($client === false) {
$this->setVar('github_token', null);
$this->addFlash('error', 'GitHub token is invalid, authorization failed!');
}
}
$hasPushAccess = false;
try {
$hasPushAccess = $this->container->get('zikula_core_manager_module.client_helper')->hasGitHubClientPushAccess($client);
} catch (GitHubRuntimeException $exception) {
// authentication required / wrong credentials, so no push access
}
return $this->render('@ZikulaCoreManagerModule/Admin/modifyconfig.html.twig', [
'form' => $form->createView(),
'rate' => $rate,
'hasPushAccess' => $hasPushAccess,
]);
}
/**
* @Route("/releases/toggle-state/{id}")
* @ParamConverter(class="ZikulaCoreManagerModule:CoreReleaseEntity")
*/
public function toggleReleaseState(CoreReleaseEntity $release)
{
if (!$this->hasPermission($this->name.'::', '::', ACCESS_MODERATE)) {
throw new AccessDeniedException();
}
if ($release->getState() === CoreReleaseEntity::STATE_OUTDATED) {
$release->setState(CoreReleaseEntity::STATE_SUPPORTED);
} else if ($release->getState() === CoreReleaseEntity::STATE_SUPPORTED) {
$release->setState(CoreReleaseEntity::STATE_OUTDATED);
} else {
throw new NotFoundHttpException('Cannot change release state - must be outdated or supported to change it!');
}
$this->getDoctrine()->getManager()->merge($release);
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('zikulacoremanagermodule_user_viewcorereleases');
}
/**
* @Route("/releases/reload")
* @Theme("admin")
*/
public function reloadCoreReleases(Request $request)
{
if (!$this->hasPermission($this->name.'::', '::', ACCESS_MODERATE)) {
throw new AccessDeniedException();
}
$form = $this->createForm(ConfirmReloadType::class, [], [
'translator' => $this->getTranslator()
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
/** @var ReleaseManager $releaseManager */
$releaseManager = $this->get('zikula_core_manager_module.releasemanager');
$releaseManager->reloadReleases($form->get('createnews')->getData());
$this->addFlash('info', $this->__('Reloaded all core releases from GitHub.'));
return $this->redirectToRoute('zikulacoremanagermodule_user_viewcorereleases');
}
return $this->render('@ZikulaCoreManagerModule/Admin/reloadreleases.html.twig', [
'form' => $form->createView()
]);
}
}