Skip to content

Commit

Permalink
Implemented requested changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Feb 1, 2024
1 parent 018bc97 commit 14ef876
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 51 deletions.
4 changes: 4 additions & 0 deletions app/Http/Controllers/User/EmailValidationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ class EmailValidationController extends Controller
{
public function validateEmail(Request $request, User $user)
{
if (!(auth()->check() && auth()->user()->id == $user->id)) {
return response()->redirectTo(route('settings.subscription'))->withError(__('emails/validation.error'));
}

$token = $request->get('token');

/** @var UserValidation $validation */
Expand Down
9 changes: 4 additions & 5 deletions app/Jobs/Emails/Subscriptions/EmailValidationJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ class EmailValidationJob implements ShouldQueue
use Queueable;
use SerializesModels;

/** @var int user id */
protected $user;
protected $token;
protected int $user;
protected string $token;

/**
*/
Expand All @@ -41,12 +40,12 @@ public function handle()
if (empty($user)) {
return;
}

$url = route('validation.email', ['user' => $user, 'token' => $this->token]);
// Send an email to the user
Mail::to($user->email)
->locale($user->locale)
->send(
new ValidationEmail($user, $this->token)
new ValidationEmail($user, $url)
);
}
}
11 changes: 4 additions & 7 deletions app/Mail/Subscription/User/ValidationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ class ValidationEmail extends Mailable
use Queueable;
use SerializesModels;

/**
* @var User
*/
public $user;
public $token;
public User $user;
public string $url;


public $date;
Expand All @@ -26,10 +23,10 @@ class ValidationEmail extends Mailable
*
* @return void
*/
public function __construct(User $user, string $token)
public function __construct(User $user, string $url)
{
$this->user = $user;
$this->token = $token;
$this->url = $url;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions app/Models/Relations/UserRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Models\UserApp;
use App\Models\UserFlag;
use App\Models\Users\Tutorial;
use App\Models\UserValidation;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\HasMany;

Expand Down Expand Up @@ -199,4 +200,9 @@ public function upvotes(): HasMany
{
return $this->hasMany(FeatureVote::class);
}

public function userValidation(): HasMany
{
return $this->hasOne(UserValidation::class, 'user_id', 'id');
}
}
7 changes: 7 additions & 0 deletions app/Models/Scopes/UserScope.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
<?php

namespace App\Models\Scopes;
use Illuminate\Database\Eloquent\Builder;

/**
* Trait UserScope
* @package App\Models\Scopes
*/
trait UserScope
{
/**
*/
public function scopeValid(Builder $query, bool $valid = true): Builder
{
return $query->where(['is_valid' => $valid]);
}
}
43 changes: 43 additions & 0 deletions app/Services/Users/EmailValidationService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Services\Users;

use App\Traits\UserAware;
use App\Models\UserFlag;
use App\Models\UserValidation;
use App\Jobs\Emails\Subscriptions\EmailValidationJob;
use Illuminate\Support\Str;

class EmailValidationService
{
use UserAware;

public function requiresEmail(): void
{
$token = UserValidation::where('user_id', $this->user->id)->first();
if ($token && $token->is_valid) {
return;
}
//Check for existing token
$flag = UserFlag::where('user_id', $this->user->id)->where('flag', UserFlag::FLAG_EMAIL)->first();

if (!$flag) {
$flag = new UserFlag();
$flag->user_id = $this->user->id;
$flag->flag = UserFlag::FLAG_EMAIL;
$flag->save();
}

if (!$token) {
$token = new UserValidation();
$token->token = Str::uuid();
$token->user_id = $this->user->id;
$token->is_valid = false;
$token->save();
}

EmailValidationJob::dispatch($this->user, $token->token);

return;
}
}
34 changes: 1 addition & 33 deletions app/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@
use App\Models\Scopes\UserScope;
use App\Models\UserLog;
use App\Models\UserSetting;
use App\Models\UserFlag;
use App\Models\UserValidation;
use App\Models\Relations\UserRelations;
use Carbon\Carbon;
use App\Jobs\Emails\Subscriptions\EmailValidationJob;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -437,7 +434,7 @@ public function isFrauding(): bool
return false;
}

$validation = UserValidation::where('user_id', $this->id)->where('is_valid', true)->first();
$validation = $this->userValidation->valid()->first();
if ($validation) {
return false;
}
Expand All @@ -462,35 +459,6 @@ public function isFrauding(): bool
->count() >= 2;
}

public function requiresEmail(): self
{
$token = UserValidation::where('user_id', $this->id)->first();
if ($token && $token->is_valid) {
return $this;
}
//Check for existing token
$flag = UserFlag::where('user_id', $this->id)->where('flag', UserFlag::FLAG_EMAIL)->first();

if (!$flag) {
$flag = new UserFlag();
$flag->user_id = $this->id;
$flag->flag = UserFlag::FLAG_EMAIL;
$flag->save();
}

if (!$token) {
$token = new UserValidation();
$token->token = Str::uuid();
$token->user_id = $this->id;
$token->is_valid = false;
$token->save();
}

EmailValidationJob::dispatch($this, $token->token);

return $this;
}

/**
* List of campaigns the user is the only admin of. This is used for the automatic purge warning emails
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public function up(): void
$table->id();
$table->uuid('token');
$table->unsignedInteger('user_id');
$table->boolean('is_valid');
$table->boolean('is_valid')->default(false);
$table->timestamps();

$table->foreign('user_id')->references('id')->on('users')->cascadeOnDelete();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@extends('emails.base', [
'utmSource' => 'subscription',
'utmSource' => 'validation',
'utmCampaign' => 'failed-charge'
])

Expand All @@ -12,8 +12,8 @@
</p>

<p>This is an automatic notification.</p>
<p>To validate the email for your Kanka account click <a href="{{ 'https://app.kanka.io/users/' . $user->id . '/validation?token=' . $token }}">here</a>. This link will expire in 24 hours.</p>
<p>If the above link doesnt work, open the following URL in your web browser {{ 'https://app.kanka.io/users/' . $user->id . '/validation?token=' . $token }}</p>
<p>To validate the email for your Kanka account click <a href="{{ $url }}">here</a>. This link will expire in 24 hours.</p>
<p>If the above link doesnt work, open the following URL in your web browser {{ $url }}</p>
<p>
{{ __('emails/subscriptions/upcoming.closing') }}<br />
The Kanka Team
Expand Down
6 changes: 4 additions & 2 deletions resources/views/settings/subscription/change.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@

<x-grid type="1/1">
@if (!$user->isFrauding())
@inject('emailService', 'App\Services\Users\EmailValidationService')
@php
$user->requiresEmail();
/** @var \App\Services\Users\EmailValidationService $emailService */
$emailService->user($user)->requiresEmail();
@endphp
<x-alert type="warning">
{{ __('emails/validation.modal') }}
Expand Down Expand Up @@ -79,7 +81,7 @@
</li>
<li role="presentation">
<a href="#giropay" aria-controls="settings" role="tab" data-toggle="tab">
Giropay
giropay
</a>
</li>
@endif
Expand Down

0 comments on commit 14ef876

Please sign in to comment.