Skip to content

Commit

Permalink
Fix UsersController::create() and PagesController::renameFIle() n…
Browse files Browse the repository at this point in the history
…ot being validated
  • Loading branch information
giuscris committed Nov 1, 2024
1 parent d939d27 commit d667d7e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 16 deletions.
8 changes: 7 additions & 1 deletion formwork/src/Panel/Controllers/PagesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ public function renameFile(RouteParams $routeParams): Response

$page = $this->site->findPage($routeParams->get('page'));

$fields = $this->modal('renameFile')->fields();

$fields->setValues($this->request->input())->validate();

$data = $fields->everyItem()->value();

if ($page === null) {
$this->panel->notify($this->translate('panel.pages.page.cannotRenameFile.pageNotFound'), 'error');
return $this->redirectToReferer(default: $this->generateRoute('panel.pages'), base: $this->panel->panelRoot());
Expand All @@ -420,7 +426,7 @@ public function renameFile(RouteParams $routeParams): Response
return $this->redirect($this->generateRoute('panel.pages.edit', ['page' => $routeParams->get('page')]));
}

$name = Str::slug(FileSystem::name($this->request->input()->get('filename')));
$name = Str::slug(FileSystem::name($data->get('filename')));
$extension = FileSystem::extension($routeParams->get('filename'));

$newName = $name . '.' . $extension;
Expand Down
28 changes: 14 additions & 14 deletions formwork/src/Panel/Controllers/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,33 +50,33 @@ public function create(): Response
return $this->forward(ErrorsController::class, 'forbidden');
}

$requestData = $this->request->input();

$fields = $this->modal('newUser')->fields();

// Ensure no required data is missing
try {
$fields->setValues($requestData)->validate();
$fields->setValues($this->request->input())->validate();
} catch (ValidationException) {
$this->panel->notify($this->translate('panel.users.user.cannotCreate.varMissing'), 'error');
return $this->redirect($this->generateRoute('panel.users'));
}

$data = $fields->everyItem()->value();

$username = $data->get('username');

// Ensure there isn't a user with the same username
if ($this->site->users()->has($requestData->get('username'))) {
if ($this->site->users()->has($username)) {
$this->panel->notify($this->translate('panel.users.user.cannotCreate.alreadyExists'), 'error');
return $this->redirect($this->generateRoute('panel.users'));
}

$userData = [
'username' => $requestData->get('username'),
'fullname' => $requestData->get('fullname'),
'hash' => Password::hash($requestData->get('password')),
'email' => $requestData->get('email'),
'language' => $requestData->get('language'),
];

Yaml::encodeToFile($userData, FileSystem::joinPaths($this->config->get('system.users.paths.accounts'), $requestData->get('username') . '.yaml'));
Yaml::encodeToFile([
'username' => $username,
'fullname' => $data->get('fullname'),
'hash' => Password::hash($data->get('password')),
'email' => $data->get('email'),
'language' => $data->get('language'),
], FileSystem::joinPaths($this->config->get('system.users.paths.accounts'), $username . '.yaml'));

$this->panel->notify($this->translate('panel.users.user.created'), 'success');
return $this->redirect($this->generateRoute('panel.users'));
Expand Down Expand Up @@ -218,7 +218,7 @@ public function images(RouteParams $routeParams): Response
$path = FileSystem::joinPaths($this->config->get('system.users.paths.images'), $routeParams->get('image'));

if (FileSystem::isFile($path)) {
return new FileResponse($path);
return new FileResponse($path, headers: ['Cache-Control' => 'max-age=31536000, private']);
}

throw new FileNotFoundException('Cannot find asset');
Expand Down
2 changes: 1 addition & 1 deletion panel/modals/newUser.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
title: '{{panel.users.newUser}}'

action: '/users/new/'
action: /users/new/

fields:
fullname:
Expand Down

0 comments on commit d667d7e

Please sign in to comment.