Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
235 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
database/migrations/2020_11_16_00004_create_ticket_references_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
use Illuminate\Database\Migrations\Migration; | ||
use Illuminate\Database\Schema\Blueprint; | ||
use Illuminate\Support\Facades\Schema; | ||
|
||
class CreateTicketReferencesTable extends Migration | ||
{ | ||
/** | ||
* Run the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function up() | ||
{ | ||
Schema::create(config('laravel-tickets.database.ticket-references-table'), function (Blueprint $table) { | ||
$table->id(); | ||
$table->unsignedBigInteger('ticket_id'); | ||
$table->morphs('referenceable'); | ||
$table->timestamps(); | ||
|
||
if (! config('laravel-tickets.models.uuid')) { | ||
$table->foreign('ticket_id') | ||
->on(config('laravel-tickets.database.tickets-table'))->references('id'); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Reverse the migrations. | ||
* | ||
* @return void | ||
*/ | ||
public function down() | ||
{ | ||
Schema::dropIfExists(config('laravel-tickets.database.ticket-references-table')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
|
||
namespace RexlManu\LaravelTickets\Interfaces; | ||
|
||
|
||
interface TicketReference | ||
{ | ||
|
||
/** | ||
* Check if a user has access to this reference | ||
* | ||
* @return boolean | ||
*/ | ||
function hasReferenceAccess() : bool; | ||
|
||
/** | ||
* Combine type and id to a string for forms | ||
* | ||
* @return string | ||
*/ | ||
function toReference() : string; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
|
||
namespace RexlManu\LaravelTickets\Models; | ||
|
||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use RexlManu\LaravelTickets\Traits\HasConfigModel; | ||
|
||
class TicketReference extends Model | ||
{ | ||
|
||
use HasConfigModel; | ||
|
||
public function getTable() | ||
{ | ||
return config('laravel-tickets.database.ticket-references-table'); | ||
} | ||
|
||
public function ticket() | ||
{ | ||
return $this->belongsTo(Ticket::class); | ||
} | ||
|
||
public function referenceable() | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
|
||
namespace RexlManu\LaravelTickets\Rule; | ||
|
||
|
||
use Illuminate\Contracts\Validation\Rule; | ||
use Illuminate\Database\Eloquent\Relations\Relation; | ||
use RexlManu\LaravelTickets\Interfaces\TicketReference; | ||
|
||
class TicketReferenceRule implements Rule | ||
{ | ||
|
||
/** | ||
* Determine if the ticket reference is valid | ||
* Checks | ||
* if value contains type and id | ||
* if the model exists | ||
* if the model is a instance of @param string $attribute | ||
* | ||
* @param mixed $value | ||
* | ||
* @return bool if the value is valid | ||
* @link TicketReference | ||
* if the user has rights to the model | ||
* | ||
*/ | ||
public function passes($attribute, $value) | ||
{ | ||
if (! str_contains($value, ',')) return false; | ||
|
||
$values = explode(',', $value); | ||
if (count($values) !== 2) return false; | ||
|
||
$type = $values[ 0 ]; | ||
if (! class_exists($type)) return false; | ||
$model = resolve($type)->find($values[ 1 ]); | ||
if (empty($model) | ||
|| ! $model instanceof TicketReference | ||
|| ! $model->hasReferenceAccess()) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Get the validation error message. | ||
* | ||
* @return string | ||
*/ | ||
public function message() | ||
{ | ||
return trans('The reference is not valid'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
|
||
namespace RexlManu\LaravelTickets\Traits; | ||
|
||
|
||
trait HasTicketReference | ||
{ | ||
|
||
public function toReference() : string | ||
{ | ||
$type = get_class($this); | ||
return "$type,$this->id"; | ||
} | ||
|
||
} |