@@ -679,4 +679,32 @@ private function defineConstants()
679
679
defined ('YII_ENV ' ) or define ('YII_ENV ' , 'test ' );
680
680
defined ('YII_ENABLE_ERROR_HANDLER ' ) or define ('YII_ENABLE_ERROR_HANDLER ' , false );
681
681
}
682
+
683
+ /**
684
+ * Sets a cookie and, if validation is enabled, signs it.
685
+ * @param string $name The name of the cookie
686
+ * @param string $value The value of the cookie
687
+ * @param array $params Additional cookie params like `domain`, `path`, `expires` and `secure`.
688
+ */
689
+ public function setCookie ($ name , $ val , array $ params = [])
690
+ {
691
+ // Sign the cookie.
692
+ if ($ this ->app ->request ->enableCookieValidation ) {
693
+ $ val = $ this ->app ->security ->hashData (serialize ([$ name , $ val ]), $ this ->app ->request ->cookieValidationKey );
694
+ }
695
+ parent ::setCookie ($ name , $ val , $ params );
696
+ }
697
+
698
+ /**
699
+ * This function creates the CSRF Cookie.
700
+ * @param string $val The value of the CSRF token
701
+ * @return string[] Returns an array containing the name of the CSRF param and the masked CSRF token.
702
+ */
703
+ public function createAndSetCsrfCookie ($ val )
704
+ {
705
+ $ masked = $ this ->app ->security ->maskToken ($ val );
706
+ $ name = $ this ->app ->request ->csrfParam ;
707
+ $ this ->setCookie ($ name , $ val );
708
+ return [$ name , $ masked ];
709
+ }
682
710
}
0 commit comments