Skip to content

Commit

Permalink
Added option to set the captcha form on a resource form
Browse files Browse the repository at this point in the history
  • Loading branch information
LorenzoJokhan committed Sep 1, 2023
1 parent 9c45225 commit 389684e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/cms/lib/modules/resource-form-widgets/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.exports = {
{
name: 'submitting',
label: 'Submitting',
fields: ['buttonTextSubmit', 'buttonTextSave', 'buttonTextConcept']
fields: ['buttonTextSubmit', 'buttonTextSave', 'buttonTextConcept', 'useCaptcha']
},
{
name: 'agreed',
Expand Down
17 changes: 17 additions & 0 deletions packages/cms/lib/modules/resource-form-widgets/lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,23 @@ const fields = [
type: 'string',
label: 'Text for button to save',
},

{
name: 'useCaptcha',
type: 'boolean',
label: 'Use captcha on resource form when made available to all users (only when viewable by everyone)',
choices: [
{
value: true,
label: "Yes"
},
{
value: false,
label: "No"
},
],
def: true
},
{
name: 'buttonTextConcept',
type: 'string',
Expand Down
16 changes: 16 additions & 0 deletions packages/cms/lib/modules/resource-form-widgets/lib/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,22 @@ module.exports = async function(self, options) {
delete data.extraData;
}

if(req.body && req.body.areYouABot) {
const captchData = req.session.captcha;

console.log({captchData});
const isCaptchaValid = captchData && captchData.text && captchData.text === req.body.areYouABot;

if (!isCaptchaValid) {
return res.status(403).json({
'msg' : 'The captcha code is not correct, try again or refresh the captcha.'
});
}

// clean up key before we send it to the api
delete req.body.areYouABot;
}

const options = {
method: req.body.resourceId ? 'PUT' : 'POST',
uri: req.body.resourceId ? `${postUrl}/${req.body.resourceId}` : postUrl,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div id="captcha">
<h2>{{ data.widget.labelCaptcha if data.widget.labelCaptcha else 'Captcha' }}</h2>
<div class="form-info" style="height: auto;"> <div class="form-field">

<img loading="lazy" src="{{data.siteUrl}}/modules/openstad-captcha/captcha" class="captcha-img" width="200" />
<br />
<a href="#" class="captcha-refresh">
<small class="underline">
refresh
</small>
</a>
<br />
<label>
{% if data.widget.captchaLabel %}
{{data.widget.captchaLabel}}
{% else %}
{{ __('Please enter the words you see into the field below ') }}
{% endif %}
</label>
<br />
<input type="text" class="input-field" required name="areYouABot" />
</div>

</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,10 @@ <h2>{{ data.widget.labelPhone if data.widget.labelPhone else 'Telefoonnummer (op
</div>
{% endif %}

{% if data.widget.useCaptcha and data.widget.formVisibility == 'always' %}
{% include 'includes/fields/captcha.html' %}
{% endif %}

{% if data.hasModeratorRights and data.widget.displayBudget %}
<div id="budget">
<h2>Budget</h2>
Expand Down

1 comment on commit 389684e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Published new image: openstad/frontend:feature-add-captcha-to-resource-form-if-set-to-everyone-389684e

Please sign in to comment.