back and forth on config - if should come from configure or from component config - stupid lol unit testing different tokens for turnstile lends to keeping that out of component config
CI / testsuite (mysql, 8.2, ) (push) Successful in 3m6s
CI / testsuite (mysql, 8.4, ) (push) Successful in 4m20s
CI / testsuite (pgsql, 8.2, ) (push) Successful in 3m10s
CI / testsuite (pgsql, 8.4, ) (push) Successful in 4m21s
CI / testsuite (sqlite, 8.2, ) (push) Successful in 3m7s
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Successful in 3m4s
CI / testsuite (sqlite, 8.4, ) (push) Successful in 4m38s
CI / Coding Standard & Static Analysis (push) Successful in 2m20s

This commit is contained in:
bs
2026-11-14 18:38:09 -08:00
parent 32afb9ce64
commit 35fab648f7
5 changed files with 37 additions and 28 deletions
+4 -3
View File
@@ -4,10 +4,10 @@
return [
'ContactUs' => [
'fields' => [
'subject' => true,
'subject' => false,
'email' => true,
'captcha' => true,
'turnstile' => true,
'captcha' => false,
'turnstile' => false,
],
'clientIpHeader' => null,
'email' => [
@@ -20,6 +20,7 @@ return [
'bcc' => '', // email string or array of emails
],
],
'model' => 'CakeContactUs.ContactUsFormSubmissions',
'addIdToRedirect' => true,
'redirectUrl' => [
'prefix' => 'Admin',
+26 -23
View File
@@ -24,14 +24,17 @@ class ContactUsComponent extends Component {
/**
* @var \CakeContactUs\Model\Table\ContactUsFormSubmissionsTable|\Cake\ORM\Table
*/
protected ContactUsFormSubmissionsTable|Table $ContactUsFormSubmissions;
protected ContactUsFormSubmissionsTable|Table $table;
/**
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [];
protected array $_defaultConfig = [
'actions' => [],
'model' => 'CakeContactUs.ContactUsFormSubmissions',
];
/**
* @var array|string[]
@@ -76,21 +79,22 @@ class ContactUsComponent extends Component {
*
*/
public function initialize(array $config): void {
$config = array_merge($config, Configure::read('ContactUs'));
$this->setConfig($config);
parent::initialize($config); // TODO: Change the autogenerated stub
$this->table = TableRegistry::getTableLocator()->get($this->getConfig('model'));
}
$this->ContactUsFormSubmissions = TableRegistry::getTableLocator()->get('CakeContactUs.ContactUsFormSubmissions');
$requireCaptcha = Configure::readOrFail('ContactUs.fields.captcha');
$requireTurnstile = Configure::readOrFail('ContactUs.fields.turnstile');
$this->setConfig('redirectUrl', Configure::read('ContactUs.redirectUrl', '/'));
$this->setConfig('addIdToRedirect', Configure::read('ContactUs.addIdToRedirect', false));
$this->setConfig('requireEmail', Configure::readOrFail('ContactUs.fields.email'));
$this->setConfig('requireCaptcha', $requireCaptcha);
if ($requireCaptcha) {
/**
* @param \Cake\Event\EventInterface $event
*
* @return void
*/
public function beforeRender(EventInterface $event): void {
if (Configure::read('ContactUs.fields.captcha')) {
$this->getController()->viewBuilder()->addHelpers(['Captcha.Captcha']);
}
if ($requireTurnstile) {
$this->setConfig('turnstile', Configure::readOrFail('Turnstile'));
if (Configure::read('ContactUs.fields.turnstile')) {
$this->getController()->viewBuilder()->addHelper('CakeContactUs.Turnstile', Configure::readOrFail('Turnstile'));
}
}
@@ -100,10 +104,10 @@ class ContactUsComponent extends Component {
*/
public function newContactUsForm() {
if ($this->getConfig('requireCaptcha') && PHP_SAPI !== 'cli') {
$this->ContactUsFormSubmissions->addBehavior('Captcha.Captcha');
$this->table->addBehavior('Captcha.Captcha');
}
return $this->ContactUsFormSubmissions->newEmptyEntity();
return $this->table->newEmptyEntity();
}
/**
@@ -117,7 +121,7 @@ class ContactUsComponent extends Component {
}
$postData['client_ip'] = $this->_getIp();
$postData['submitted_at'] = DateTime::now();
$contactUsFormSubmission = $this->ContactUsFormSubmissions->patchEntity($contactUsFormSubmission, $postData);
$contactUsFormSubmission = $this->table->patchEntity($contactUsFormSubmission, $postData);
$this->getController()->set(['contactUsFormSubmission' => $contactUsFormSubmission]);
$event = $this->getController()->dispatchEvent(CakeContactUsPlugin::EVENT_BEFORE_CONTACT_US_FORM_SAVED, [
@@ -180,10 +184,9 @@ class ContactUsComponent extends Component {
return $result;
}
$this->Flash->success($message);
$redirectUrl = $this->getConfig('redirectUrl');
if ($this->getConfig('addIdToRedirect')) {
$redirectUrl[] = $contactUsFormSubmissionSaved->get($this->ContactUsFormSubmissions->getPrimaryKey());
$redirectUrl[] = $contactUsFormSubmissionSaved->get($this->table->getPrimaryKey());
}
return $this->getController()->redirect($redirectUrl);
@@ -193,11 +196,11 @@ class ContactUsComponent extends Component {
* @return void
*/
public function beforeFormSaved(EventInterface $event) {
if (!$this->getConfig('turnstile')) {
if (!Configure::read('ContactUs.fields.turnstile')) {
return;
}
$siteKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SITE_KEY) ?: $this->getConfig('turnstile.siteKey');
$secretKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SECRET_KEY) ?: $this->getConfig('turnstile.secretKey');
$siteKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SITE_KEY) ?: Configure::readOrFail('Turnstile.siteKey');
$secretKey = getenv(CakeContactUsPlugin::CAKE_CONTACT_US_CLOUDFLARE_TURNSTILE_SECRET_KEY) ?: Configure::readOrFail('Turnstile.secretKey');
if (!$siteKey || !$secretKey) {
$event->stopPropagation();
$event->setResult([
@@ -247,7 +250,7 @@ class ContactUsComponent extends Component {
* @return \Cake\Datasource\EntityInterface|\Cake\Http\Response
*/
protected function _finishSave(EntityInterface $contactUsFormSubmission) {
$contactUsFormSubmissionSaved = $this->ContactUsFormSubmissions->save($contactUsFormSubmission);
$contactUsFormSubmissionSaved = $this->table->save($contactUsFormSubmission);
if ($contactUsFormSubmissionSaved) {
return $this->_afterFormSaved($contactUsFormSubmissionSaved);
@@ -268,7 +271,7 @@ class ContactUsComponent extends Component {
// }
$url = 'https://challenges.cloudflare.com/turnstile/v0/siteverify';
$data = [
'secret' => $this->getConfig('turnstile.secretKey'),
'secret' => Configure::readOrFail('Turnstile.secretKey'),
'response' => $token,
];
@@ -18,7 +18,9 @@ class ContactUsFormSubmissionsController extends AppController {
*/
public function initialize(): void {
parent::initialize(); // TODO: Change the autogenerated stub
$this->loadComponent('CakeContactUs.ContactUs');
$this->loadComponent('CakeContactUs.ContactUs', [
'actions' => ['contact'],
]);
}
/**
@@ -182,7 +182,6 @@ class ContactUsFormSubmissionsControllerTest extends TestCase {
'cf-turnstile-response' => 'XXXX.DUMMY.TOKEN.XXXX',
];
$this->post($url, $data);
// dd($this->_response);
$this->assertResponseCode(200);
$cntAfter = $this->ContactUsFormSubmissions->find()->count();
$this->assertEquals($cntBefore, $cntAfter);
+4
View File
@@ -65,6 +65,9 @@ Configure::write('ContactUs', [
'email' => true,
'turnstile' => false,
],
'model' => 'CakeContactUs.ContactUsFormSubmissions',
'addIdToRedirect' => false,
'redirectUrl' => '/',
'email' => [
'mailerClass' => 'CakeContactUs.ContactUsFormSubmissions',
'confirmation' => false, // true or false
@@ -73,6 +76,7 @@ Configure::write('ContactUs', [
'to' => 'test@example.com',
],
],
// 'turnstile' => [
// 'siteKey' => '1x00000000000000000000AA',
// 'secretKey' => '1x0000000000000000000000000000000AA',