Verified Commit ed6d08f8 authored by Korko's avatar Korko
Browse files

Hash draw id and participant in organizer panel

parent 0e64a390
......@@ -15,32 +15,30 @@ class Draw extends Model
protected static $hashConnection = 'draw';
/**
* The attributes that are mass assignable.
* The attributes that should be cast.
*
* @var array
*/
protected $fillable = ['mail_title', 'mail_body', 'expires_at'];
protected $casts = [
'mail_title' => EncryptedString::class,
'mail_body' => EncryptedString::class,
];
/**
* The accessors to append to the model's array form.
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $append = ['hash'];
protected $dates = [
'expires_at',
];
/**
* The attributes that should be cast.
* The attributes that are mass assignable.
*
* @var array
*/
protected $casts = [
'mail_title' => EncryptedString::class,
'mail_body' => EncryptedString::class,
];
protected $fillable = ['mail_title', 'mail_body', 'expires_at'];
public function save(array $options = [])
{
......
......@@ -14,9 +14,9 @@ trait HashId
*/
protected $append = ['hash'];
protected static function getHashConnection()
protected function getHashConnection()
{
return property_exists(new static, 'hashConnection') ? static::$hashConnection : null;
return property_exists($this, 'hashConnection') ? $this->hashConnection : null;
}
public function getHashAttribute()
......@@ -24,12 +24,24 @@ trait HashId
return Hashids::connection($this->getHashConnection())->encode($this->id);
}
public static function findByHashOrFail($hash)
public function scopeFindByHashOrFail($query, $hash)
{
if (! $hash || ! ($ids = Hashids::connection(static::getHashConnection())->decode($hash))) {
if (! $hash || ! ($ids = Hashids::connection($this->getHashConnection())->decode($hash))) {
throw (new ModelNotFoundException())->setModel(__CLASS__);
}
return static::findOrFail($ids[0]);
return $query->findOrFail($ids[0]);
}
/**
* Retrieve the model for a bound value.
*
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value, $field = null)
{
return $this->findByHashOrFail($value);
}
}
......@@ -15,14 +15,14 @@ class DearSantaController extends Controller
public function view(Participant $participant)
{
return view('dearSanta', [
'santa' => $participant->hash,
'participant' => $participant->hash,
]);
}
public function fetch(Participant $participant)
{
return response()->json([
'santa' => [
'participant' => [
'id' => $participant->hash,
'name' => $participant->name,
],
......@@ -61,7 +61,7 @@ class DearSantaController extends Controller
'id', 'mail_body', 'mail',
]),
]) :
redirect('/dearsanta/'.$participant->id)->with('message', $message);
redirect('/dearsanta/'.$participant->hash)->with('message', $message);
}
protected function sendMail(DearSanta $dearSanta)
......
......@@ -27,7 +27,7 @@ class TargetDrawn extends TrackedMailable
$this->content = $this->parseKeywords($santa->draw->mail_body, $santa);
$this->dearSantaLink = route('dearsanta', ['santa' => $santa->hash]).'#'.base64_encode(Crypt::getKey());
$this->dearSantaLink = route('dearsanta', ['participant' => $santa->hash]).'#'.base64_encode(Crypt::getKey());
$this->track($santa->mail);
}
......
......@@ -13,16 +13,6 @@ class Participant extends Model
protected static $hashConnection = 'santa';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'draw_id', 'target_id', 'mail_id'];
// Fake property
public $exclusions;
/**
* The attributes that should be cast.
*
......@@ -33,6 +23,16 @@ class Participant extends Model
'email' => EncryptedString::class,
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'draw_id', 'target_id', 'mail_id'];
// Fake property
public $exclusions;
public function draw()
{
return $this->belongsTo(Draw::class);
......@@ -58,7 +58,7 @@ class Participant extends Model
return $this->belongsTo(Mail::class, 'mail_id');
}
public static function getFromDearSantaUrl($url)
public function getFromDearSantaUrl($url)
{
$route = app('router')
->getRoutes()
......
......@@ -25,13 +25,7 @@ class RouteServiceProvider extends ServiceProvider
*/
public function boot()
{
Route::bind('santa', function ($value) {
return Participant::findByHashOrFail($value);
});
Route::bind('draw', function ($value) {
return Draw::findByHashOrFail($value);
});
//
parent::boot();
}
......
@extends('templates/fetcher', ['styles' => '/css/dearSanta.css', 'fetchUrl' => route('dearsanta.fetch', ['santa' => $santa])])
@extends('templates/fetcher', ['styles' => '/css/dearSanta.css', 'fetchUrl' => route('dearsanta.fetch', ['participant' => $participant])])
@section('script')
@parent
......
......@@ -18,21 +18,21 @@ Route::post('/', 'RandomFormController@handle'); //->middleware('decrypt.input')
Route::get('/faq', 'RandomFormController@faq')->name('faq');
Route::pattern('santa', '[0-9a-zA-Z]{'.config('hashids.connections.santa.length').'}');
Route::pattern('participant', '[0-9a-zA-Z]{'.config('hashids.connections.santa.length').'}');
Route::get('/dearsanta/{santa}', 'DearSantaController@view')->name('dearsanta');
Route::get('/dearsanta/{participant}', 'DearSantaController@view')->name('dearsanta');
Route::middleware(['decrypt.key'])->group(function () {
Route::post('/dearsanta/{santa}', 'DearSantaController@fetch')->name('dearsanta.fetch');
Route::post('/dearsanta/{santa}/send', 'DearSantaController@handle')->name('dearsanta.contact');
Route::post('/dearsanta/{participant}', 'DearSantaController@fetch')->name('dearsanta.fetch');
Route::post('/dearsanta/{participant}/send', 'DearSantaController@handle')->name('dearsanta.contact');
Route::post('/dearsanta/{participant}/fetchState', 'DearSantaController@fetchState')->name('dearsanta.fetchState');
});
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');
Route::post('/org/{draw}/fetchState', 'OrganizerController@fetchState')->name('organizerPanel.fetchState');
Route::post('/org/{draw}/{participant}/changeEmail', 'OrganizerController@changeEmail')->name('organizerPanel.changeEmail');
Route::post('/org/{draw}/{participant}/resendEmail', 'OrganizerController@resendEmail')->name('organizerPanel.resendEmail');
});
Route::post('/org/{draw}/fetchState', 'OrganizerController@fetchState')->name('organizerPanel.fetchState');
......@@ -69,7 +69,7 @@ class RequestDearSantaTest extends RequestCase
$santa = $participants[$santaId];
// Check data stored are decryptable
$pathTheorical = parse_url(route('dearsanta', ['santa' => '%s']), PHP_URL_PATH);
$pathTheorical = parse_url(route('dearsanta', ['participant' => '%s']), PHP_URL_PATH);
$data = sscanf($path, $pathTheorical);
$id = Hashids::decode($data[0])[0];
$santaTheorical = Participant::find($id);
......@@ -78,7 +78,7 @@ class RequestDearSantaTest extends RequestCase
$this->assertEquals($santa['email'], $santaTheorical->santa->email);
// Try to contact santa
$response = $this->ajaxPost(route('dearsanta.contact', ['santa' => $data[0]]), [
$response = $this->ajaxPost(route('dearsanta.contact', ['participant' => $data[0]]), [
'key' => base64_encode(Crypt::getKey()),
'content' => 'test dearsanta mail content',
]);
......
......@@ -37,7 +37,7 @@ class RequestOrganizerTest extends RequestCase
// Check data can be changed
$path = route('organizerPanel.resendEmail', [
'draw' => $draw->hash,
'participant' => $participant,
'participant' => $participant->hash,
]);
$response = $this->ajaxPost($path, [
'key' => base64_encode(Crypt::getKey()),
......@@ -62,7 +62,7 @@ class RequestOrganizerTest extends RequestCase
// Check data can be changed
$path = route('organizerPanel.changeEmail', [
'draw' => $draw->hash,
'participant' => $participant,
'participant' => $participant->hash,
]);
$response = $this->ajaxPost($path, [
'key' => base64_encode(Crypt::getKey()),
......
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