diff --git a/app/Http/Controllers/Auth/RegisterController.php b/app/Http/Controllers/Auth/RegisterController.php index 566fa518..439f3430 100644 --- a/app/Http/Controllers/Auth/RegisterController.php +++ b/app/Http/Controllers/Auth/RegisterController.php @@ -35,7 +35,8 @@ public function register(Request $request) { DB::transaction(function () use (&$user, $request): void { $user = (new UserCreateJob( $request->input('email'), - $request->input('password') + $request->input('password'), + $request->input('accepted_policies') ))->handle(); (UserVerificationCreateTokenAndSendJob::newForAccountCreation($user))->handle(); }); diff --git a/app/Jobs/UserCreateJob.php b/app/Jobs/UserCreateJob.php index 958bfc13..2b9b20a8 100644 --- a/app/Jobs/UserCreateJob.php +++ b/app/Jobs/UserCreateJob.php @@ -3,6 +3,10 @@ namespace App\Jobs; use App\User; +use App\UserTermsOfUseAcceptance; +use App\Policy; +use App\PolicyAcceptance; +use App\TermsOfUseVersion use Illuminate\Support\Facades\Hash; class UserCreateJob extends Job { @@ -12,11 +16,12 @@ class UserCreateJob extends Job { private $verified; - public function __construct($email, $password, $verified = false) { + public function __construct($email, $password, $acceptedPolicyIds, $verified = false) { // TODO maybe pass in an unsaved eloquent model? // // but that would make CLI job creation hard $this->email = $email; $this->password = $password; + $this->acceptedPolicyIds = $acceptedPolicyIds; $this->verified = false; } @@ -30,6 +35,34 @@ public function handle() { 'verified' => $this->verified, ]); + // accept latest Terms of Use Policy automatically + $latestToU = TermsOfUseVersion::latestActiveVersion(); + if ($latestToU) { + UserTermsOfUseAcceptance::create([ + 'user_id' => $user->id, + 'tou_version' => $latestToU->version, + 'tou_accepted_at' => now(), + ]); + } else { + Log::warning("No active Terms of Use version found when creating user {$user->email} (ID {$user->id})."); + } + + if (is_array($this->acceptedPolicyIds)) { + foreach($this->acceptedPolicyIds as $acceptedPolicyId) { + $policy = Policy::find($acceptedPolicyId); + + if ($policy) { + PolicyAcceptance::create([ + 'user_id' => $user->id, + 'policy_id' => $policy->id, + 'accepted_at' => now(), + ]); + } else { + Log::warning("Policy ID '{$acceptedPolicyId}' not found when creating user {$user->email} (ID {$user->id})."); + } + } + } + return $user; } diff --git a/tests/Jobs/UserAcceptedPoliciesTest.php b/tests/Jobs/UserAcceptedPoliciesTest.php new file mode 100644 index 00000000..d2421c9b --- /dev/null +++ b/tests/Jobs/UserAcceptedPoliciesTest.php @@ -0,0 +1,70 @@ +handle(); + + $this->assertEquals( + PolicyAcceptance::all()->count(), + 0 + ); + } + + public function testUserRegistrationAcceptsOne(): void { + $termsOfUse = new Policy; + $termsOfUse->policy_type = 'terms-of-use'; + $termsOfUse->active_from = CarbonImmutable::now(); + $termsOfUse->content_vue_file = 'termsOfUse.vue'; + $termsOfUse->save(); + + $email = 'test+' . uniqid('', true) . '@example.com'; + $user = (new UserCreateJob($email, 'thisisapassword123', [$termsOfUse->id], true))->handle(); + + $this->assertDatabaseHas('policy_acceptances', [ + 'user_id' => $user->id, + 'policy_id' => $termsOfUse->id, + ]); + } + + public function testUserRegistrationAcceptsTwo(): void { + $termsOfUse = new Policy; + $termsOfUse->policy_type = 'terms-of-use'; + $termsOfUse->active_from = CarbonImmutable::now(); + $termsOfUse->content_vue_file = 'termsOfUse.vue'; + $termsOfUse->save(); + + $hostingPolicy = new Policy; + $hostingPolicy->policy_type = 'hosting-policy'; + $hostingPolicy->active_from = CarbonImmutable::now(); + $hostingPolicy->content_vue_file = 'hostingPolicy.vue'; + $hostingPolicy->save(); + + $email = 'test+' . uniqid('', true) . '@example.com'; + $user = (new UserCreateJob($email, 'thisisapassword123', [$termsOfUse->id, $hostingPolicy->id], true))->handle(); + + $this->assertDatabaseHas('policy_acceptances', [ + 'user_id' => $user->id, + 'policy_id' => $termsOfUse->id, + ]); + + $this->assertDatabaseHas('policy_acceptances', [ + 'user_id' => $user->id, + 'policy_id' => $hostingPolicy->id, + ]); + } +} diff --git a/tests/Jobs/UserTermsOfUseAcceptanceTest.php b/tests/Jobs/UserTermsOfUseAcceptanceTest.php new file mode 100644 index 00000000..28cde503 --- /dev/null +++ b/tests/Jobs/UserTermsOfUseAcceptanceTest.php @@ -0,0 +1,32 @@ +handle(); + $email = 'test+' . uniqid('', true) . '@example.com'; + $user = (new UserCreateJob($email, 'thisisapassword123', null, true))->handle(); + + $this->assertDatabaseHas('tou_acceptances', [ + 'user_id' => $user->id, + 'tou_version' => TermsOfUseVersion::latestActiveVersion()->version, + ]); + + $rows = UserTermsOfUseAcceptance::where('user_id', $user->id)->get(); + $this->assertCount(1, $rows); + $acceptance = $rows->first(); + + $this->assertSame(TermsOfUseVersion::latestActiveVersion()->version, $acceptance->tou_version); + $this->assertNotNull($acceptance->tou_accepted_at); + } +}