-
Notifications
You must be signed in to change notification settings - Fork 143
Expand file tree
/
Copy pathSecretController.php
More file actions
138 lines (109 loc) · 3.8 KB
/
SecretController.php
File metadata and controls
138 lines (109 loc) · 3.8 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
133
134
135
136
137
138
<?php
declare(strict_types=1);
namespace PHPCensor\Controller;
use DateTime;
use PHPCensor\Form;
use PHPCensor\Helper\Lang;
use PHPCensor\Model\Secret;
use PHPCensor\Store\SecretStore;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\RedirectResponse;
use PHPCensor\Model\User;
use PHPCensor\WebController;
use PHPCensor\Form\Element\Csrf;
/**
* @package PHP Censor
* @subpackage Application
*
* @author Dmitry Khomutov <[email protected]>
*/
class SecretController extends WebController
{
public string $layoutName = 'layout';
protected SecretStore $secretStore;
public function init(): void
{
parent::init();
$this->secretStore = $this->storeRegistry->get('Secret');
}
public function index(): void
{
$this->requireAdmin();
$secrets = [];
$secretList = $this->secretStore->getWhere([], 100, 0, ['name' => 'ASC']);
foreach ($secretList['items'] as $secret) {
$thisSecret = [
'name' => $secret->getName(),
'id' => $secret->getId(),
];
$secrets[] = $thisSecret;
}
$this->layout->title = Lang::get('secrets');
$this->view->secrets = $secrets;
$this->view->user = $this->getUser();
}
/**
* @return Response
*
* @throws \PHPCensor\Common\Exception\InvalidArgumentException
* @throws \PHPCensor\Common\Exception\RuntimeException
* @throws \PHPCensor\Exception\HttpException
*/
public function edit(?int $secretId = null)
{
$this->requireAdmin();
if (!\is_null($secretId)) {
$secret = $this->secretStore->getById($secretId);
} else {
$secret = new Secret($this->storeRegistry);
}
if ($this->request->getMethod() === 'POST') {
$secret->setName($this->getParam('name'));
$secret->setValue($this->getParam('value'));
if (\is_null($secretId)) {
/** @var User $user */
$user = $this->getUser();
$secret->setCreateDate(new DateTime());
$secret->setUserId($user->getId());
}
$this->secretStore->save($secret);
$response = new RedirectResponse(APP_URL . 'secret');
return $response;
}
$form = new Form();
$form->setMethod('POST');
$form->setAction(APP_URL . 'secret/edit' . (!\is_null($secretId) ? '/' . $secretId : ''));
$form->addField(new Csrf($this->session, 'secret_form'));
$field = Form\Element\Text::create('name', Lang::get('secret_name'), true);
$field
->setClass('form-control')
->setContainerClass('form-group')
->setPattern(Secret::SECRET_NAME_PATTERN)
->setValue($secret->getName());
$form->addField($field);
$field = Form\Element\TextArea::create('value', Lang::get('secret_value'), true);
$field
->setClass('form-control')
->setContainerClass('form-group')
->setRows(8)
->setValue($secret->getValue());
$form->addField($field);
$submit = new Form\Element\Submit();
$submit->setClass('btn btn-success');
$submit->setValue(Lang::get('secret_save'));
$form->addField($submit);
$this->view->form = $form;
}
/**
* @throws \PHPCensor\Common\Exception\Exception
* @throws \PHPCensor\Common\Exception\InvalidArgumentException
* @throws \PHPCensor\Exception\HttpException
*/
public function delete(int $secretId): Response
{
$this->requireAdmin();
$group = $this->secretStore->getById($secretId);
$this->secretStore->delete($group);
return new RedirectResponse(APP_URL . 'secret');
}
}