Skip to content

Commit

Permalink
Add "marketing agreement" to registration, user profile and admin panel
Browse files Browse the repository at this point in the history
  • Loading branch information
danon committed Sep 20, 2023
1 parent 3216baa commit ffc4c36
Show file tree
Hide file tree
Showing 14 changed files with 187 additions and 203 deletions.
9 changes: 9 additions & 0 deletions app/Domain/Html.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Coyote\Domain;

class Html
{
public function __construct(public string $content)
{
}
}
28 changes: 28 additions & 0 deletions app/Domain/User/UserSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Coyote\Domain\User;

use Coyote\Domain\Html;

class UserSettings
{
public function marketingAgreement(): Html
{
$gdpr = '<a href="mailto:[email protected]">[email protected]</a>';
$_4programmers = '<a href="https://4programmers.net/">4programmers.net</a>';

return new Html("Wyrażam zgodę na otrzymywanie, na podany przeze mnie adres e-mail, informacji handlowych kierowanych do mnie
przez 4programmers.net (tj. Makana sp. z o.o., z siedzibą przy ul. Krupniczej 13, 50-075 Wrocław). Informacje
handlowe dotyczyć będą produktów, usług i działalności realizowanej przez 4programmers.net i jej kontrahentów.
Rozumiem, że zgodę mogę wycofać w dowolnym momencie, jednak nie będzie to miało wpływu na przetwarzanie, którego
dokonano przed jej wycofaniem. Przedmiotowa zgoda może zostać wycofana poprzez odznaczenie jej w ustawieniach mojego
konta albo poprzez wysłanie stosownej wiadomości na adres e-mail: $gdpr lub adres siedziby $_4programmers.");
}

public function termsAndPrivacyPolicyAgreement(): Html
{
$terms = '<a href="/Regulamin">regulamin</a>';
$privacyPolicy = '<a href="/Polityka_prywatności">politykę prywatności</a>';

return new Html("Akceptuję $terms oraz $privacyPolicy.<b>*</b>");
}
}
69 changes: 27 additions & 42 deletions app/Http/Forms/Auth/RegisterForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

namespace Coyote\Http\Forms\Auth;

use Coyote\Domain\User\UserSettings;
use Coyote\Services\FormBuilder\Form;
use Coyote\Services\FormBuilder\ValidatesWhenSubmitted;
use Coyote\View\TwigHtml;
use Illuminate\Contracts\Validation\Validator;

class RegisterForm extends Form implements ValidatesWhenSubmitted
{
const RECAPTCHA_URL = 'https://www.google.com/recaptcha/api/siteverify';

/**
* @var string
*/
/** @var string */
protected $theme = self::THEME_INLINE;

public function buildForm()
public function buildForm(): void
{
$this
->setAttr(['id' => 'js-register-form'])
->add('name', 'text', [
'rules' => 'required|min:2|max:28|username|user_unique',
'label' => 'Nazwa użytkownika',
'attr' => [
'autofocus' => 'autofocus'
]
'attr' => ['autofocus' => 'autofocus']
])
->add('password', 'password', [
'rules' => 'required|confirmed|min:3',
Expand All @@ -36,73 +33,61 @@ public function buildForm()
->add('email', 'text', [
'rules' => 'required|email|max:255|email_unique',
'label' => 'E-mail',
'help' => 'Nie wysyłamy reklam. Twój e-mail nie zostanie nikomu udostępniony.'
'help' => 'Nie wysyłamy reklam. Twój e-mail nie zostanie nikomu udostępniony.'
])
->add('terms', 'checkbox', [
'rules' => 'accepted',
'label' => new TwigHtml((new UserSettings)->termsAndPrivacyPolicyAgreement())
])
->add('marketing_agreement', 'checkbox', [
'rules' => 'boolean',
'label' => new TwigHtml((new UserSettings)->marketingAgreement())
])
->add('terms', 'checkbox', ['rules' => 'accepted'])
->add('email_confirmation', 'honeypot')
->add('submit', 'submit', [
'label' => 'Utwórz konto',
'attr' => [
'class' => 'g-recaptcha btn btn-primary',
'data-sitekey' => config('services.recaptcha.key'),
'data-callback' => 'onSubmit',
'attr' => [
'class' => 'g-recaptcha btn btn-primary',
'data-sitekey' => config('services.recaptcha.key'),
'data-callback' => 'onSubmit',
'data-submit-state' => 'Rejestracja...'
]
]);
}

/**
* @return array
*/
public function rules()
public function rules(): array
{
return parent::rules() + ['human' => 'required'];
}

/**
* @return \Illuminate\Contracts\Validation\Validator
*/
protected function getValidatorInstance()
protected function getValidatorInstance(): Validator
{
$validator = parent::getValidatorInstance();

if (empty(config('services.recaptcha.secret'))) {
return $validator;
}

$validator->after(function ($validator) {
if (empty($this->request->input('g-recaptcha-response'))) {
$validator->errors()->add('name', trans('validation.recaptcha'));
logger()->debug('Empty captcha');

return false;
}

$response = json_decode($this->makeRequest($this->request->input('g-recaptcha-response')), true);
logger()->debug($response);

if (!$response['success']) {
$validator->errors()->add('name', trans('validation.recaptcha'));

return false;
}
});

return $validator;
}

/**
* @param string $recaptcha
* @return bool|string
*/
private function makeRequest(string $recaptcha)
private function makeRequest(string $recaptcha): string
{
$data = [
'secret' => config('services.recaptcha.secret'),
'remoteip' => $this->request->ip(),
'response' => $recaptcha
];

return file_get_contents(self::RECAPTCHA_URL . '?' . http_build_query($data));
return \file_get_contents('https://www.google.com/recaptcha/api/siteverify' . '?' . http_build_query([
'secret' => config('services.recaptcha.secret'),
'remoteip' => $this->request->ip(),
'response' => $recaptcha
]));
}
}
50 changes: 12 additions & 38 deletions app/Http/Forms/User/AdminForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,17 @@

namespace Coyote\Http\Forms\User;

use Coyote\Repositories\Contracts\GroupRepositoryInterface as GroupRepository;
use Coyote\Repositories\Contracts\GroupRepositoryInterface;
use Coyote\Services\Geocoder\GeocoderInterface;
use Illuminate\Contracts\Auth\Access\Gate;

class AdminForm extends SettingsForm
{
/**
* @var GroupRepository
*/
protected $group;

/**
* @var Gate
*/
protected $gate;

/**
* @param GeocoderInterface $geocoder
* @param GroupRepository $group
* @param Gate $gate
*/
public function __construct(GeocoderInterface $geocoder, GroupRepository $group, Gate $gate)
public function __construct(GeocoderInterface $geocoder, private GroupRepositoryInterface $group)
{
parent::__construct($geocoder);

$this->group = $group;
$this->gate = $gate;
}

public function buildForm()
public function buildForm():void
{
$this->add('name', 'text', [
'label' => 'Nazwa użytkownika',
Expand All @@ -41,14 +22,6 @@ public function buildForm()
parent::buildForm();

$this
// ->add('skills', 'collection', [
// 'label' => 'Umiejętności',
// 'child_attr' => [
// 'type' => 'child_form',
// 'class' => SkillsForm::class,
// 'value' => $this->data
// ]
// ])
->addAfter('group_id', 'is_confirm', 'checkbox', [
'label' => 'Potwierdzony adres e-mail',
'rules' => 'bool'
Expand All @@ -57,20 +30,21 @@ public function buildForm()
'label' => 'Sponsor',
'rules' => 'bool'
])
->addAfter('group_id', 'marketing_agreement', 'checkbox', [
'label' => 'Zgoda marketingowa',
'rules' => 'bool'
])
->addAfter('group_id', 'is_active', 'checkbox', [
'label' => 'Konto aktywne',
'rules' => 'bool'
])
->addAfter('allow_sticky_header', 'delete_photo', 'checkbox', [
'label' => 'Usuń zdjęcie'
])
->add('groups', 'choice', [
'label' => 'Grupy użytkownika',
'choices' => $this->group->pluck('name', 'id'),
'property' => 'id'
]);

$groups = $this->group->pluck('name', 'id');

$this->add('groups', 'choice', [
'label' => 'Grupy użytkownika',
'choices' => $groups,
'property' => 'id'
]);
}
}
80 changes: 33 additions & 47 deletions app/Http/Forms/User/SettingsForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,45 @@

namespace Coyote\Http\Forms\User;

use Coyote\Domain\User\UserSettings;
use Coyote\Services\FormBuilder\Form;
use Coyote\Services\FormBuilder\FormEvents;
use Coyote\Services\Geocoder\GeocoderInterface;
use Coyote\User;
use Coyote\View\TwigHtml;

class SettingsForm extends Form
{
/**
* @var GeocoderInterface
*/
protected $geocoder;

/**
* @param GeocoderInterface $geocoder
*/
public function __construct(GeocoderInterface $geocoder)
public function __construct(private GeocoderInterface $geocoder)
{
parent::__construct();

$this->geocoder = $geocoder;
$this->bindFormEvents();
}

public function buildForm()
private function bindFormEvents(): void
{
/** @var \Coyote\User $user */
$this->addEventListener(FormEvents::POST_SUBMIT, function (Form $form) {
$form->add('latitude', 'hidden', ['value' => null]);
$form->add('longitude', 'hidden', ['value' => null]);
if ($form->get('location')->getValue()) {
$location = $this->geocoder->geocode($form->get('location')->getValue());
$form->get('latitude')->setValue($location->latitude);
$form->get('longitude')->setValue($location->longitude);
}
});
$this->addEventListener(FormEvents::POST_SUBMIT, function (Form $form) {
$github = $form->get('github')->getValue();
if ($github) {
if (filter_var($github, FILTER_VALIDATE_URL) === false) {
$form->get('github')->setValue('https://github.com/' . $github);
}
}
});
}

public function buildForm(): void
{
/** @var User $user */
$user = $this->getData();

// id uzytkownika, ktorego ustawienia wlasnie edytujemy
Expand Down Expand Up @@ -74,7 +87,10 @@ public function buildForm()
->add('allow_sticky_header', 'checkbox', [
'rules' => 'boolean',
'label' => 'Przyklejony pasek menu',
'help' => 'Pasek menu będzie zawsze widoczny podczas przewijania okna.'
])
->add('marketing_agreement', 'checkbox', [
'rules' => 'boolean',
'label' => new TwigHtml((new UserSettings)->marketingAgreement())
])
->add('firm', 'text', [
'rules' => 'nullable|string|max:100',
Expand All @@ -83,9 +99,7 @@ public function buildForm()
->add('position', 'text', [
'rules' => 'nullable|string|max:100',
'label' => 'Stanowisko',
'attr' => [
'placeholder' => 'Np. Junior Java Developer'
]
'attr' => ['placeholder' => 'Np. Junior Java Developer']
])
->add('github', 'text', [
'rules' => 'nullable|string|max:200',
Expand All @@ -96,9 +110,7 @@ public function buildForm()
'rules' => 'nullable|string|max:500',
'label' => 'O sobie',
'help' => 'W tym polu możesz zamieścić krótką informację o sobie, czym się zajmujesz, co cię interesuje. Ta informacja zostanie wyświetlona na Twoim profilu.',
'attr' => [
'rows' => 3
]
'attr' => ['rows' => 3]
])
->add('birthyear', 'select', [
'rules' => 'nullable|integer|between:1950,' . (date('Y') - 1),
Expand Down Expand Up @@ -127,33 +139,7 @@ public function buildForm()
'attr' => ['rows' => 3]
])
->add('submit', 'submit', [
'label' => 'Zapisz',
'attr' => ['data-submit-state' => 'Wysyłanie...']
'label' => 'Zapisz'
]);
}

protected function bindFormEvents()
{
$this->addEventListener(FormEvents::POST_SUBMIT, function (Form $form) {
$form->add('latitude', 'hidden', ['value' => null]);
$form->add('longitude', 'hidden', ['value' => null]);

if ($form->get('location')->getValue()) {
$location = $this->geocoder->geocode($form->get('location')->getValue());

$form->get('latitude')->setValue($location->latitude);
$form->get('longitude')->setValue($location->longitude);
}
});

$this->addEventListener(FormEvents::POST_SUBMIT, function (Form $form) {
$github = $form->get('github')->getValue();

if ($github) {
if (filter_var($github, FILTER_VALIDATE_URL) === false) {
$form->get('github')->setValue('https://github.com/' . $github);
}
}
});
}
}
4 changes: 3 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* @property int $allow_smilies
* @property int $allow_sig
* @property int $allow_sticky_header
* @property int $marketing_agreement
* @property int $birthyear
* @property int $reputation
* @property string $name
Expand Down Expand Up @@ -112,7 +113,8 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
'allow_smilies',
'allow_sig',
'allow_subscribe',
'allow_sticky_header'
'allow_sticky_header',
'marketing_agreement',
];

/**
Expand Down
Loading

0 comments on commit ffc4c36

Please sign in to comment.