Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
1aa836ff
Commit
1aa836ff
authored
Sep 17, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use meta tags to pass CSRF token.
parent
240b42aa
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
21 additions
and
21 deletions
+21
-21
yii.js
framework/yii/assets/yii.js
+13
-6
View.php
framework/yii/base/View.php
+8
-0
YiiAsset.php
framework/yii/web/YiiAsset.php
+0
-15
No files found.
framework/yii/assets/yii.js
View file @
1aa836ff
...
...
@@ -43,12 +43,19 @@
*/
yii
=
(
function
(
$
)
{
var
pub
=
{
// version of Yii framework
version
:
'2.0'
,
// CSRF token name and value. If this is set and a form is created and submitted using JavaScript
// via POST, the CSRF token should be submitted too to pass CSRF validation.
csrfVar
:
undefined
,
csrfToken
:
undefined
,
/**
* @return string|undefined the CSRF variable name. Undefined is returned is CSRF validation is not enabled.
*/
getCsrfVar
:
function
()
{
return
$
(
'meta[name=csrf-var]'
).
attr
(
'content'
);
},
/**
* @return string|undefined the CSRF token. Undefined is returned is CSRF validation is not enabled.
*/
getCsrfToken
:
function
()
{
return
$
(
'meta[name=csrf-token]'
).
attr
(
'content'
);
},
initModule
:
function
(
module
)
{
if
(
module
.
isActive
===
undefined
||
module
.
isActive
)
{
...
...
framework/yii/base/View.php
View file @
1aa836ff
...
...
@@ -12,6 +12,7 @@ use yii\helpers\FileHelper;
use
yii\helpers\Html
;
use
yii\web\JqueryAsset
;
use
yii\web\AssetBundle
;
use
yii\web\Request
;
use
yii\widgets\Block
;
use
yii\widgets\ContentDecorator
;
use
yii\widgets\FragmentCache
;
...
...
@@ -708,6 +709,13 @@ class View extends Component
if
(
!
empty
(
$this
->
metaTags
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
metaTags
);
}
$request
=
Yii
::
$app
->
getRequest
();
if
(
$request
instanceof
Request
&&
$request
->
enableCsrfValidation
)
{
$lines
[]
=
Html
::
tag
(
'meta'
,
''
,
array
(
'name'
=>
'csrf-var'
,
'content'
=>
$request
->
csrfVar
));
$lines
[]
=
Html
::
tag
(
'meta'
,
''
,
array
(
'name'
=>
'csrf-token'
,
'content'
=>
$request
->
getCsrfToken
()));
}
if
(
!
empty
(
$this
->
linkTags
))
{
$lines
[]
=
implode
(
"
\n
"
,
$this
->
linkTags
);
}
...
...
framework/yii/web/YiiAsset.php
View file @
1aa836ff
...
...
@@ -23,19 +23,4 @@ class YiiAsset extends AssetBundle
public
$depends
=
array
(
'yii\web\JqueryAsset'
,
);
/**
* @inheritdoc
*/
public
function
registerAssets
(
$view
)
{
parent
::
registerAssets
(
$view
);
$js
[]
=
"yii.version='"
.
Yii
::
getVersion
()
.
"';"
;
$request
=
Yii
::
$app
->
getRequest
();
if
(
$request
instanceof
Request
&&
$request
->
enableCsrfValidation
)
{
$js
[]
=
"yii.csrfVar='
{
$request
->
csrfVar
}
';"
;
$js
[]
=
"yii.csrfToken='
{
$request
->
csrfToken
}
';"
;
}
$view
->
registerJs
(
implode
(
"
\n
"
,
$js
),
View
::
POS_END
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment