Verified Commit 2ec71167 authored by Korko's avatar Korko
Browse files

Hash Organizer draw id and move hash to model

parent 107c3712
Pipeline #290661 failed with stage
in 1 minute and 57 seconds
......@@ -2,13 +2,11 @@
namespace App\Console\Commands;
use App\Draw;
use App\Participant;
use Arr;
use Crypt;
use DrawHandler;
use Hashids;
use Illuminate\Console\Command;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class FixOrganizer extends Command
{
......@@ -45,7 +43,7 @@ class FixOrganizer extends Command
{
$this->setCryptKeyFromUrl($this->argument('url'));
$draw = Draw::getDrawFromDearSantaUrl($this->argument('url'));
$draw = Participant::getFromDearSantaUrl($this->argument('url'))->draw;
$draw->organizer->email = $this->argument('email');
$draw->organizer->save();
......
......@@ -10,6 +10,10 @@ use Illuminate\Database\Eloquent\Model;
class Draw extends Model
{
use HashId;
protected static $hashConnection = 'draw';
/**
* The attributes that are mass assignable.
*
......@@ -17,6 +21,13 @@ class Draw extends Model
*/
protected $fillable = ['mail_title', 'mail_body', 'expires_at'];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $append = ['hash'];
protected $dates = [
'expires_at',
];
......@@ -52,21 +63,4 @@ class Draw extends Model
{
return $this->participants->first();
}
public function getDrawFromDearSantaUrl($url)
{
$route = app('router')
->getRoutes()
->getByName('dearsanta');
$request = app('request')
->create($this->argument('url'));
$hash = $route->bind($request)->santa;
if (!$hash || ! ($ids = Hashids::decode($hash))) {
throw (new ModelNotFoundException())->setModel(Participant::class);
}
return Participant::findOrFail($id[0])->draw;
}
}
<?php
namespace App;
use Hashids;
use Illuminate\Database\Eloquent\ModelNotFoundException;
trait HashId
{
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $append = ['hash'];
protected static function getHashConnection()
{
return property_exists(new static, 'hashConnection') ? static::$hashConnection : null;
}
public function getHashAttribute()
{
return Hashids::connection($this->getHashConnection())->encode($this->id);
}
public static function findByHashOrFail($hash)
{
if (!$hash || ! ($ids = Hashids::connection(static::getHashConnection())->decode($hash))) {
throw (new ModelNotFoundException())->setModel(__CLASS__);
}
return static::findOrFail($ids[0]);
}
}
......@@ -8,7 +8,6 @@ use App\Jobs\SendMail;
use App\Mail as MailModel;
use App\Mail\DearSanta as DearSantaEmail;
use App\Participant;
use Hashids;
use Metrics;
class DearSantaController extends Controller
......@@ -16,7 +15,7 @@ class DearSantaController extends Controller
public function view(Participant $participant)
{
return view('dearSanta', [
'santa' => Hashids::encode($participant->id),
'santa' => $participant->hash,
]);
}
......@@ -24,7 +23,7 @@ class DearSantaController extends Controller
{
return response()->json([
'santa' => [
'id' => Hashids::encode($participant->id),
'id' => $participant->hash,
'name' => $participant->name,
],
'draw' => $participant->draw->mail_title,
......
......@@ -16,14 +16,14 @@ class OrganizerController extends Controller
public function view(Draw $draw)
{
return view('organizer', [
'draw' => $draw->id,
'draw' => $draw->hash,
]);
}
public function fetch(Draw $draw)
{
return response()->json([
'draw' => $draw->id,
'draw' => $draw->hash,
'participants' => $draw->participants->mapWithKeys(function ($participant) {
return [$participant->id => $participant->only([
'id', 'name', 'email', 'mail',
......
<?php
namespace App\Http\Middleware;
use Hashids;
use Illuminate\Foundation\Http\Middleware\TransformsRequest as Middleware;
use Illuminate\Support\Arr;
class Unhash extends Middleware
{
/**
* Transform the given value.
*
* @param string $key
* @param mixed $value
*
* @return mixed
*/
protected function transform($key, $value)
{
if (! array_key_exists($key, config('hashids.connections'))) {
return $value;
}
return Arr::get(Hashids::connection($key)->decode($value), 0, $value);
}
}
......@@ -3,7 +3,6 @@
namespace App\Jobs;
use App\Mail as MailModel;
use Hashids;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
......@@ -32,9 +31,7 @@ class ParseBounces implements ShouldQueue
if ($hash !== null) {
try {
[$id] = Hashids::connection('bounce')->decode($hash);
$mail = MailModel::findOrFail($id);
$mail = MailModel::findByHashOrFail($hash);
$mail->delivery_status = MailModel::ERROR;
$mail->save();
} catch (Exception $e) {
......
......@@ -7,6 +7,10 @@ use Illuminate\Database\Eloquent\Model;
class Mail extends Model
{
use HashId;
protected static $hashConnection = 'bounce';
/**
* The attributes that are mass assignable.
*
......
......@@ -28,7 +28,7 @@ class OrganizerRecap extends Mailable
$this->expirationDate = $draw->expires_at->locale(App::getLocale())->isoFormat('LL');
$this->panelLink = route('organizerPanel', ['draw' => $draw->id]).'#'.base64_encode(Crypt::getKey());
$this->panelLink = route('organizerPanel', ['draw' => $draw->hash]).'#'.base64_encode(Crypt::getKey());
}
/**
......
......@@ -4,7 +4,6 @@ namespace App\Mail;
use App\Participant;
use Crypt;
use Hashids;
use Illuminate\Bus\Queueable;
class TargetDrawn extends TrackedMailable
......@@ -28,7 +27,7 @@ class TargetDrawn extends TrackedMailable
$this->content = $this->parseKeywords($santa->draw->mail_body, $santa);
$this->dearSantaLink = route('dearsanta', ['santa' => Hashids::encode($santa->id)]).'#'.base64_encode(Crypt::getKey());
$this->dearSantaLink = route('dearsanta', ['santa' => $santa->hash]).'#'.base64_encode(Crypt::getKey());
$this->track($santa->mail);
}
......
......@@ -3,7 +3,6 @@
namespace App\Mail;
use App\Mail as MailModel;
use Hashids;
class TrackedMailable extends Mailable
{
......@@ -40,7 +39,7 @@ class TrackedMailable extends Mailable
public function send($mailer)
{
$this->withSwiftMessage(function ($message) {
$hash = Hashids::connection('bounce')->encode($this->mailId);
$hash = MailModel::find($this->mailId)->hash;
$message->getHeaders()
->addPathHeader('Return-Path', str_replace('*', $hash, config('mail.return_path')));
......
......@@ -9,6 +9,9 @@ use Illuminate\Notifications\Notifiable;
class Participant extends Model
{
use Notifiable;
use HashId;
protected static $hashConnection = 'santa';
/**
* The attributes that are mass assignable.
......@@ -54,4 +57,18 @@ class Participant extends Model
{
return $this->belongsTo(Mail::class, 'mail_id');
}
public static function getFromDearSantaUrl($url)
{
$route = app('router')
->getRoutes()
->getByName('dearsanta');
$request = app('request')
->create($this->argument('url'));
$hash = $route->bind($request)->santa;
return static::findByHashOrFail($hash);
}
}
......@@ -2,8 +2,8 @@
namespace App\Providers;
use App\Draw;
use App\Participant;
use Hashids;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Route;
......@@ -26,10 +26,11 @@ class RouteServiceProvider extends ServiceProvider
public function boot()
{
Route::bind('santa', function ($value) {
if (($ids = Hashids::decode($value)) && ($santa = Participant::find($ids[0]))) {
return $santa;
}
abort(404);
return Participant::findByHashOrFail($value);
});
Route::bind('draw', function ($value) {
return Draw::findByHashOrFail($value);
});
parent::boot();
......
......@@ -24,7 +24,7 @@ return [
|
*/
'default' => 'main',
'default' => 'santa',
/*
|--------------------------------------------------------------------------
......@@ -38,8 +38,13 @@ return [
*/
'connections' => [
'main' => [
'salt' => env('HASHIDS_SALT_MAIN', 'secretsanta'),
'santa' => [
'salt' => env('HASHIDS_SALT_SANTA', 'secretsanta-santa'),
'length' => '5',
],
'draw' => [
'salt' => env('HASHIDS_SALT_DRAW', 'secretsanta-draw'),
'length' => '5',
],
......
......@@ -18,7 +18,7 @@ Route::post('/', 'RandomFormController@handle'); //->middleware('decrypt.input')
Route::get('/faq', 'RandomFormController@faq')->name('faq');
Route::pattern('santa', '[0-9a-zA-Z]{'.config('hashids.connections')[config('hashids.default')]['length'].'}');
Route::pattern('santa', '[0-9a-zA-Z]{'.config('hashids.connections')['santa']['length'].'}');
Route::get('/dearsanta/{santa}', 'DearSantaController@view')->name('dearsanta');
Route::middleware(['decrypt.key'])->group(function () {
......@@ -27,6 +27,8 @@ Route::middleware(['decrypt.key'])->group(function () {
});
Route::post('/dearsanta/{santa}/fetchState', 'DearSantaController@fetchState')->name('dearsanta.fetchState');
Route::pattern('draw', '[0-9a-zA-Z]{'.config('hashids.connections')['draw']['length'].'}');
Route::get('/org/{draw}', 'OrganizerController@view')->name('organizerPanel');
Route::middleware(['decrypt.key'])->group(function () {
Route::post('/org/{draw}', 'OrganizerController@fetch')->name('organizerPanel.fetch');
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment