Skip to content

Commit 084000e

Browse files
committed
Integratedd CSRF protection added
1 parent bc9ef93 commit 084000e

File tree

20 files changed

+321
-17
lines changed

20 files changed

+321
-17
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Jet\MVC_View;
4+
use Jet\Form_Renderer_Field_Input;
5+
6+
/**
7+
* @var MVC_View $this
8+
* @var Form_Renderer_Field_Input $r
9+
*/
10+
$r = $this->getRaw( 'renderer' );
11+
12+
$field = $r->getField();
13+
if( $field->getLastErrorCode() ) {
14+
$r->addCustomCssClass( 'is-invalid' );
15+
}
16+
?>
17+
<input type="hidden" name="<?= $field->getTagNameValue() ?>" id="<?= $field->getId() ?>" value="<?= $field->getValue() ?>"/>

_installer/views/form/start.phtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ if(!$r->getBaseCssClasses()) {
1818
<form <?=$r->renderTagAttributes()?>>
1919
<?php if( !$form->getIsReadonly() ): ?>
2020
<input type="hidden" name="<?=$form->getSentKey()?>" value="<?=$form->getName();?>">
21-
<?php endif; ?>
21+
<?php if(($csrf_field=$form->getCSRFTokenField())):
22+
echo $csrf_field;
23+
endif;
24+
endif; ?>

_tools/studio/application/Classes/AccessControl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ public static function getLoginForm(): Form
127127
$password_field,
128128
]
129129
);
130+
$form->enableCSRFProtection();
130131

131132
$form->getField( 'username' )->setIsRequired( true );
132133
/**

_tools/studio/application/dictionaries/en_US/login.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
'password' => '',
88
'Logout' => '',
99
'Invalid username or password!' => '',
10+
'Please enter username and password' => '',
1011
];
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
use Jet\MVC_View;
4+
use Jet\Form_Renderer_Field_Input;
5+
6+
/**
7+
* @var MVC_View $this
8+
* @var Form_Renderer_Field_Input $r
9+
*/
10+
$r = $this->getRaw( 'renderer' );
11+
12+
$field = $r->getField();
13+
if( $field->getLastErrorCode() ) {
14+
$r->addCustomCssClass( 'is-invalid' );
15+
}
16+
?>
17+
<input type="hidden" name="<?= $field->getTagNameValue() ?>" id="<?= $field->getId() ?>" value="<?= $field->getValue() ?>"/>

_tools/studio/application/views/form/start.phtml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@ if(!$r->getBaseCssClasses()) {
1818
<form <?=$r->renderTagAttributes()?>>
1919
<?php if( !$form->getIsReadonly() ): ?>
2020
<input type="hidden" name="<?=$form->getSentKey()?>" value="<?=$form->getName();?>">
21-
<?php endif; ?>
21+
<?php if(($csrf_field=$form->getCSRFTokenField())):
22+
echo $csrf_field;
23+
endif;
24+
endif; ?>

application/Modules/Login/Admin/Main.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public function getLoginForm() : Form
5252
$password_field,
5353
]
5454
);
55+
$form->enableCSRFProtection();
5556

5657
$form->getField( 'username' )->setIsRequired( true );
5758
/**

application/Modules/Login/Web/Main.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function getLoginForm() : Form
5353
$password_field,
5454
]
5555
);
56+
$form->enableCSRFProtection();
5657

5758
$form->getField( 'username' )->setIsRequired( true );
5859
/**

application/Modules/Test/Forms/Controller/Main.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,20 @@ function( array $files ) {
384384

385385

386386
$forms = [];
387+
388+
$common_form = new Form(
389+
'common_form', [
390+
$input_field,
391+
$validated_input_field,
392+
]
393+
);
394+
$common_form->enableCSRFProtection();
387395

388396
$forms['common_form'] = [
389397
'title' => Tr::_( 'Common form' ),
390-
'form' => new Form(
391-
'common_form', [
392-
$input_field,
393-
$validated_input_field,
394-
]
395-
),
398+
'form' => $common_form,
396399
];
400+
397401

398402
$forms['numbers_form'] = [
399403
'title' => Tr::_( 'Number form' ),

application/Modules/Test/Forms/views/test-forms/form.phtml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22

33
use Jet\Form;
4+
use Jet\Form_Field;
45
use Jet\Form_Renderer;
56
use Jet\Tr;
67
use Jet\MVC_View;
@@ -22,7 +23,10 @@ $form->renderer()->setDefaultFieldWidth([
2223
?>
2324
<?= $form->start() ?>
2425

25-
<?php foreach( $form->getFields() as $field ) echo $field; ?>
26+
<?php foreach( $form->getFields() as $field ):
27+
echo $field;
28+
endforeach;
29+
?>
2630

2731
<?php if( !$form->getIsReadonly() ): ?>
2832
<div class="row">

0 commit comments

Comments
 (0)