-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from nswdpc/v0-2
Merge hide response form email change
- Loading branch information
Showing
10 changed files
with
553 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
Name: nswdpc_recaptchav3_userforms | ||
--- | ||
SilverStripe\UserForms\Control\UserDefinedFormController: | ||
extensions: | ||
- 'NSWDPC\SpamProtection\UserDefinedFormControllerExtension' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace NSWDPC\SpamProtection; | ||
|
||
use SilverStripe\Core\Extension; | ||
use SilverStripe\ORM\ArrayList; | ||
use SilverStripe\Control\Email\Email; | ||
use SilverStripe\UserForms\Model\Recipient\EmailRecipient; | ||
|
||
/** | ||
* Extension to handle email modification | ||
* @author James | ||
*/ | ||
class UserDefinedFormControllerExtension extends Extension { | ||
|
||
/** | ||
* Modify email data to take into account whether the reCAPTCHA value | ||
* is to be included in the list of fields added to the email for all | ||
* recipients | ||
*/ | ||
public function updateEmailData(&$emailData, $attachments) { | ||
|
||
if(!isset($emailData['Fields']) || !($emailData['Fields'] instanceof ArrayList)) { | ||
// invalid field data | ||
return; | ||
} | ||
|
||
foreach($emailData['Fields'] as $field) { | ||
if( ($field instanceof SubmittedRecaptchaV3Field) && (!$field->getIncludeValueInEmails()) ) { | ||
$emailData['Fields']->remove( $field ); | ||
} | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
namespace NSWDPC\SpamProtection; | ||
|
||
use SilverStripe\UserForms\Model\Submission\SubmittedFormField; | ||
|
||
/** | ||
* SubmittedRecaptchaV3Field | ||
* Used to allow determination of whether a value is a recaptcha score | ||
* @author James | ||
*/ | ||
class SubmittedRecaptchaV3Field extends SubmittedFormField | ||
{ | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
protected $includeValueInEmails = false; | ||
|
||
/** | ||
* Setter | ||
*/ | ||
public function setIncludeValueInEmails(bool $include) { | ||
$this->includeValueInEmails = $include; | ||
return $this; | ||
} | ||
|
||
/** | ||
* Getter | ||
*/ | ||
public function getIncludeValueInEmails() : bool { | ||
return $this->includeValueInEmails; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
namespace NSWDPC\SpamProtection; | ||
|
||
use Silverstripe\Core\Config\Config; | ||
use SilverStripe\Dev\BuildTask; | ||
use SilverStripe\Core\Injector\Injector; | ||
use SilverStripe\ORM\DB; | ||
|
||
/** | ||
* Update the IncludeInEmails value for historical field values | ||
* | ||
* This is a manual migration you can optionally run if you wish to retain | ||
* Recaptchav3 verification values in emails | ||
* | ||
* Provide a before value to update fields created before that datetime eg. 2022-01-01 | ||
* | ||
* This task will be removed or disabled in future releases | ||
* | ||
* @author James | ||
*/ | ||
class IncludeInEmailsTask extends BuildTask | ||
{ | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected $title = "reCAPTCHAv3 include in emails task for historical fields"; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected $description = 'Retain the reCAPTCHAv3 verification values in emails sent. If you do not need the values in emails, do not run this task.'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $segment = 'RecaptchaV3IncludeInEmailsTask'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
public function run($request) { | ||
|
||
$before = $request->getVar('before'); | ||
$publish = $request->getVar('publish') == 1; | ||
$commit = $request->getVar('commit') == 1; | ||
|
||
if(!$commit) { | ||
DB::alteration_message("Pass commit=1 to make changes", "info"); | ||
} | ||
if(!$publish) { | ||
DB::alteration_message("Pass publish=1 to publish changes", "info"); | ||
} | ||
|
||
if(!$before) { | ||
DB::alteration_message("You must provide a date/time as the 'before' param, to update all fields before that date/time. The value is anything understood by DateTime", "error"); | ||
return; | ||
} | ||
|
||
try { | ||
$dt = new \DateTime($before); | ||
$beforeFormatted = $dt->format('Y-m-d'); | ||
} catch(\Exception $e) { | ||
DB::alteration_message("Could not understand the before value '{$before}'", "error"); | ||
return; | ||
} | ||
|
||
$fields = EditableRecaptchaV3Field::get()->filter([ | ||
'Created:LessThan' => $beforeFormatted | ||
]); | ||
|
||
if($fields->count() == 0) { | ||
DB::alteration_message("No fields found to change before {$beforeFormatted}", "noop"); | ||
return; | ||
} | ||
|
||
foreach($fields as $field) { | ||
try { | ||
if($commit) { | ||
$field->IncludeInEmails = 1; | ||
$field->write(); | ||
DB::alteration_message("Changed field #{$field->ID} '{$field->Title}', created:{$field->Created}", "changed"); | ||
if($publish) { | ||
$field->doPublish(); | ||
DB::alteration_message("Published field #{$field->ID} '{$field->Title}', created:{$field->Created}", "changed"); | ||
} | ||
} else { | ||
DB::alteration_message("Would have changed field #{$field->ID} '{$field->Title}', created:{$field->Created}", "info"); | ||
} | ||
} catch (\Exception $e) { | ||
DB::alteration_message("Failed to change field #{$field->ID} '{$field->Title}', created:{$field->Created}. Error:{$e->getMessage()}", "error"); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.