Skip to content
This repository has been archived by the owner on Aug 5, 2018. It is now read-only.

Commit

Permalink
implemented another listener
Browse files Browse the repository at this point in the history
Unfortunately Kdyby\Events behavior is not good at all. It's necessary to register presenter or components into container, but it's against generated factories behavior. It's quite a big security hole, because if you forgot to register these classes into container, event will NOT be called... :-(
  • Loading branch information
mrtnzlml committed Jul 11, 2014
1 parent e69283d commit 65e7329
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 38 deletions.
4 changes: 3 additions & 1 deletion app/components/PostForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
class PostForm extends UI\Control {

public $onSave = [];
//public $onBeforeRestrictedFunctionality = [];

/** @var \App\Posts */
private $posts;
Expand Down Expand Up @@ -59,6 +60,7 @@ protected function createComponentPostForm() {
}

public function postFormSucceeded($form) {
//$this->onBeforeRestrictedFunctionality($this); //FIXME: must be registered in config, but it's against generated factories
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat opravdu **ne**můžete!', 'danger');
$this->redirect('this');
Expand Down Expand Up @@ -96,4 +98,4 @@ private function editable() {
return $this->presenter->user->isAllowed('Admin', App\Authorizator::EDIT) ? TRUE : FALSE;
}

}
}
4 changes: 3 additions & 1 deletion app/components/UserEditForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
class UserEditForm extends UI\Control {

public $onSave = [];
//public $onBeforeRestrictedFunctionality = [];

private $users;
private $account;
Expand Down Expand Up @@ -61,6 +62,7 @@ protected function createComponentForm() {
}

public function formSucceeded(UI\Form $form) {
//$this->onBeforeRestrictedFunctionality($this); //FIXME: must be registered in config, but it's against generated factories
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat opravdu **ne**můžete!', 'danger');
$this->redirect('this');
Expand Down Expand Up @@ -88,4 +90,4 @@ private function editable() {
return $this->presenter->user->isAllowed('Admin', App\Authorizator::EDIT) ? TRUE : FALSE;
}

}
}
10 changes: 9 additions & 1 deletion app/config/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,18 @@ services:
- App\Tags(@doctrine.dao(Entity\Tag))
- App\Users(@doctrine.dao(Entity\User))
#- App\Xmlrpc

- PostFormFactory
- UserEditFormFactory

- PostsListener
#It should be here because of Kdyby\Events (?) --- security problem (if you forgot it) :-(
- App\AdminPresenter
-
class: PostsListener
tags: [kdyby.subscriber]
-
class: RestrictListener
tags: [kdyby.subscriber]

extensions:
console: Kdyby\Console\DI\ConsoleExtension
Expand Down
29 changes: 29 additions & 0 deletions app/listeners/RestrictListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

class RestrictListener extends Nette\Object implements Kdyby\Events\Subscriber {

public function getSubscribedEvents() {
return array(
'App\AdminPresenter::onBeforeRestrictedFunctionality' => 'adminEdit',
'Cntrl\PostForm::onBeforeRestrictedFunctionality' => 'controlEdit',
'Cntrl\UserEditForm::onBeforeRestrictedFunctionality' => 'controlEdit',
);
}

public function adminEdit(Nette\Application\UI\Presenter $presenter) {
if (!$presenter->user->isAllowed('Admin', App\Authorizator::EDIT)) {
$presenter->flashMessage('Myslím to vážně, editovat opravdu **ne**můžete!', 'danger');
$presenter->redirect('this');
return;
}
}

public function controlEdit(Nette\Application\UI\Control $control) {
if (!$control->presenter->user->isAllowed('Admin', App\Authorizator::EDIT)) {
$control->presenter->flashMessage('Myslím to vážně, editovat opravdu **ne**můžete!', 'danger');
$control->presenter->redirect('this');
return;
}
}

}
44 changes: 9 additions & 35 deletions app/presenters/AdminPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

class AdminPresenter extends BasePresenter {

public $onBeforeRestrictedFunctionality = [];

/** @var Pictures @inject */
public $pictures;
/** @var Tags @inject */
Expand Down Expand Up @@ -117,11 +119,7 @@ protected function createComponentColor() {
* @param $id
*/
public function colorSucceeded($button, $id) {
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat opravdu **ne**můžete!', 'danger');
$this->redirect('this');
return;
}
$this->onBeforeRestrictedFunctionality($this);
$vals = $button->getForm()->getValues();
$newColor = preg_replace('<#>', '', $vals['color' . $id]);
if (ctype_xdigit($newColor) && (strlen($newColor) == 6 || strlen($newColor) == 3)) {
Expand Down Expand Up @@ -159,11 +157,7 @@ public function handleUpdate($title, $content, $tags) {
}

public function handleDelete($id) {
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat (ani mazat) opravdu **ne**můžete!', 'danger');
$this->redirect('this');
return;
}
$this->onBeforeRestrictedFunctionality($this);
try {
$this->posts->delete($this->posts->findOneBy(array('id' => $id)));
$this->flashMessage('Článek byl úspěšně smazán.', 'success');
Expand All @@ -174,11 +168,7 @@ public function handleDelete($id) {
}

public function handleDeleteTag($tag_id) {
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat (ani mazat) opravdu **ne**můžete!', 'danger');
$this->redirect('this');
return;
}
$this->onBeforeRestrictedFunctionality($this);
try {
$this->tags->delete($this->tags->findOneBy(array('id' => $tag_id)));
$this->flashMessage('Tag byl úspěšně smazán.', 'success');
Expand All @@ -189,11 +179,7 @@ public function handleDeleteTag($tag_id) {
}

public function handleDeleteUser($user_id) {
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat (ani mazat) opravdu **ne**můžete!', 'danger');
$this->redirect('this');
return;
}
$this->onBeforeRestrictedFunctionality($this);
try {
$this->users->delete($this->users->findOneBy(['id' => $user_id]));
$this->flashMessage('Uživatel byl úspěšně smazán.', 'success');
Expand All @@ -204,11 +190,7 @@ public function handleDeleteUser($user_id) {
}

public function handleRegenerate($tag_id) {
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat opravdu **ne**můžete!', 'danger');
$this->redirect('this');
return;
}
$this->onBeforeRestrictedFunctionality($this);
try {
$tag = $this->tags->findOneBy(array('id' => $tag_id));
$tag->color = substr(md5(rand()), 0, 6); //Short and sweet
Expand All @@ -221,7 +203,7 @@ public function handleRegenerate($tag_id) {
}

public function handleUploadPicture() {
ob_start();
//ob_start();
$uploader = new \UploadHandler();
$uploader->allowedExtensions = array("jpeg", "jpg", "png", "gif", "iso");
$uploader->chunksFolder = __DIR__ . '/../../www/chunks';
Expand Down Expand Up @@ -249,11 +231,7 @@ public function handleUploadPicture() {
}

public function handleDeletePicture($id) {
if (!$this->editable()) {
$this->presenter->flashMessage('Myslím to vážně, editovat (ani mazat) opravdu **ne**můžete!', 'danger');
$this->redirect('this');
return;
}
$this->onBeforeRestrictedFunctionality($this);
$picture = $this->pictures->findOneBy(['id' => $id]);
@unlink(__DIR__ . '/../../www/uploads/' . $picture->uuid . DIRECTORY_SEPARATOR . $picture->name);
@rmdir(__DIR__ . '/../../www/uploads/' . $picture->uuid);
Expand All @@ -262,8 +240,4 @@ public function handleDeletePicture($id) {
$this->redirect('this');
}

private function editable() {
return $this->user->isAllowed('Admin', App\Authorizator::EDIT) ? TRUE : FALSE;
}

}

0 comments on commit 65e7329

Please sign in to comment.