Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/add captcha to resource form if set to everyone #427

Open
wants to merge 10 commits into
base: development
Choose a base branch
from
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 @@ -96,7 +96,7 @@ module.exports = {
{
name: 'submitting',
label: 'Submitting',
fields: ['buttonTextSubmit', 'buttonTextSave', 'buttonTextConcept']
fields: ['buttonTextSubmit', 'buttonTextSave', 'buttonTextConcept', 'useCaptcha', 'captchaTitle', 'captchaMessage', 'captchaRefreshText']
},
{
name: 'agreed',
Expand Down
32 changes: 32 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 @@ -898,6 +898,38 @@ 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: 'captchaTitle',
type: 'string',
label: 'Title above the captcha',
},
{
name: 'captchaMessage',
type: 'string',
label: 'Instruction text for the captcha',
},
{
name: 'captchaRefreshText',
type: 'string',
label: 'Refresh text',
},
{
name: 'buttonTextConcept',
type: 'string',
Expand Down
16 changes: 15 additions & 1 deletion packages/cms/lib/modules/resource-form-widgets/lib/submit.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,21 @@ module.exports = async function (self, options) {
delete data.extraData;
}

const options = {
if(req.body && req.body.areYouABot) {
const captchaData = req.session.captcha;
const isCaptchaValid = captchaData && captchaData.text && captchaData.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}`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ <h2>
{% endfor %}
</div>
{% endfor %}

{% if data.widget.useCaptcha and data.widget.formVisibility == 'always' %}
{% include 'includes/fields/captcha.html' %}
{% endif %}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div id="captcha">
<h2>{{ data.widget.captchaTitle if data.widget.captchaTitle 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">
{% if data.widget.captchaRefreshText %}
{{data.widget.captchaRefreshText}}
{% else %}
{{ __('refresh') }}
{% endif %}
</small>
</a>
<br />
<label>
{% if data.widget.captchaMessage %}
{{data.widget.captchaMessage}}
{% 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 @@ -180,4 +180,8 @@ <h2>Budget</h2>
{% include 'includes/dynamic-form-fields.html' %}
{% endif %}

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

{% include 'includes/form-footer.html' %}
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.widget.displayBudget %}
<div id="budget">
<h2>{{data.widget.titleBudget or 'Budget'}}</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ <h4>
</div>
</div>


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

<br/>
<br/>

Expand Down