Ticket 244 et ompagnie : permet un soft delete de user et projects
This commit is contained in:
parent
901c550990
commit
8f753cb349
9 changed files with 119 additions and 8 deletions
|
|
@ -10,6 +10,7 @@ use App\Http\Requests\UpdateCrudModel;
|
|||
use App\Http\Resources\CrudResource;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Illuminate\Support\Facades\Session;
|
||||
|
|
@ -166,7 +167,10 @@ class CrudController extends Controller
|
|||
|
||||
public function destroy(Model $model)
|
||||
{
|
||||
if ($restrict = $model->gotHasManyRelationWithRestrictOnDelete()) {
|
||||
if (
|
||||
!in_array(SoftDeletes::class, class_uses_recursive($model), true) and
|
||||
$restrict = $model->gotHasManyRelationWithRestrictOnDelete()
|
||||
) {
|
||||
return redirect()->back()
|
||||
->with('error', trans(
|
||||
':model n\'as pas pu être supprimé car il est encore associé avec des :associated',
|
||||
|
|
|
|||
|
|
@ -39,7 +39,7 @@ class TicketController extends Controller
|
|||
{
|
||||
/** @var \App\Models\User */
|
||||
$user = auth()->user();
|
||||
$tickets = Ticket::with('project');
|
||||
$tickets = Ticket::with('project')->has('project');
|
||||
if (!$user->role->equals(Role::admin())) {
|
||||
$tickets = $tickets->whereHas('project', function ($q) use ($user) {
|
||||
$q->whereHas('users', function ($q) use ($user) {
|
||||
|
|
|
|||
|
|
@ -60,6 +60,9 @@ class CrudRequest extends FormRequest
|
|||
if ($props[$k]['type']->equals(CrudType::belongsToMany())) {
|
||||
$propsRules["$k.*"][0] = 'exists:' .
|
||||
(new $props[$k]['belongsToMany']())->getTable() . ',id';
|
||||
if (!isset($this->{$k})) {
|
||||
$this->merge([$k => []]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ use App\Enums\Role;
|
|||
use App\Traits\IsCrudModel;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
|
|
@ -15,6 +16,7 @@ class Project extends Model
|
|||
{
|
||||
use HasFactory;
|
||||
use IsCrudModel;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $crudLabelColumn = 'name';
|
||||
|
||||
|
|
@ -46,6 +48,7 @@ class Project extends Model
|
|||
'type' => CrudType::belongsToMany(),
|
||||
'belongsToMany' => User::class,
|
||||
'belongsToManyLabel' => 'name',
|
||||
'nullable' => true,
|
||||
'readonly' => function () {
|
||||
return auth()->user()->role->equals(Role::admin()) ? false : true;
|
||||
},
|
||||
|
|
@ -65,7 +68,7 @@ class Project extends Model
|
|||
static::created(function (Project $project) {
|
||||
$path = Project::ticketAssetsFullPath($project->slug_name);
|
||||
if (!File::exists($path)) {
|
||||
File::makeDirectory($path);
|
||||
File::makeDirectory($path, 0755, true);
|
||||
}
|
||||
});
|
||||
static::updating(function (Project $project) {
|
||||
|
|
@ -75,7 +78,7 @@ class Project extends Model
|
|||
if (File::exists($oldPath)) {
|
||||
File::move($oldPath, $newPath);
|
||||
} else {
|
||||
File::makeDirectory($newPath);
|
||||
File::makeDirectory($newPath, 0755, true);
|
||||
}
|
||||
});
|
||||
static::deleted(function (Project $project) {
|
||||
|
|
|
|||
|
|
@ -7,8 +7,11 @@ use App\Enums\CrudType;
|
|||
use App\Enums\Role;
|
||||
use App\Traits\IsCrudModel;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Str;
|
||||
use Kwaadpepper\Enum\Traits\CastsEnums;
|
||||
|
||||
class User extends Authenticatable
|
||||
|
|
@ -17,6 +20,7 @@ class User extends Authenticatable
|
|||
use Notifiable;
|
||||
use CastsEnums;
|
||||
use IsCrudModel;
|
||||
use SoftDeletes;
|
||||
|
||||
protected $enumCasts = [
|
||||
'role' => Role::class
|
||||
|
|
@ -81,7 +85,19 @@ class User extends Authenticatable
|
|||
'required' => true,
|
||||
'nullable' => false,
|
||||
'validate' => [
|
||||
'max:255'
|
||||
'max:255',
|
||||
// @phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed
|
||||
function ($attribute, $value, $fail) {
|
||||
$q = static::query();
|
||||
if (request()->user) {
|
||||
$q = $q->where('id', '!=', $this->id);
|
||||
}
|
||||
if ($q->where('email', $value)->exists()) {
|
||||
$fail(__(':attribute est déjà utilisé', [
|
||||
'attribute' => $attribute
|
||||
]));
|
||||
}
|
||||
}
|
||||
],
|
||||
'actions' => [
|
||||
CrudAction::viewAny(),
|
||||
|
|
@ -123,6 +139,23 @@ class User extends Authenticatable
|
|||
parent::__construct($attributes);
|
||||
}
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::creating(function (User $user) {
|
||||
if (\is_null($user->password)) {
|
||||
$user->password = Str::random(10);
|
||||
}
|
||||
if (Hash::needsRehash($user->password)) {
|
||||
$user->password = Hash::make($user->password);
|
||||
}
|
||||
});
|
||||
static::updating(function (User $user) {
|
||||
if (Hash::needsRehash($user->password)) {
|
||||
$user->password = Hash::make($user->password);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function tickets()
|
||||
{
|
||||
return $this->hasMany(Ticket::class, 'created_by');
|
||||
|
|
|
|||
|
|
@ -3,6 +3,7 @@
|
|||
namespace App\Providers;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\Ticket;
|
||||
use App\Models\User;
|
||||
use Illuminate\Cache\RateLimiting\Limit;
|
||||
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
||||
|
|
@ -51,6 +52,9 @@ class RouteServiceProvider extends ServiceProvider
|
|||
});
|
||||
Route::model('user', User::class);
|
||||
Route::model('project', Project::class);
|
||||
Route::bind('ticket', function ($value) {
|
||||
return Ticket::where('id', $value)->has('project')->firstOrFail();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -168,11 +168,11 @@ trait IsCrudModel
|
|||
return collect(static::$editableProperties)->mapWithKeys(function ($item, $key) {
|
||||
$item = \array_merge(self::$schemaCrud, $item);
|
||||
$defaultRules = [];
|
||||
if (!$item['nullable']) {
|
||||
if (!$item['nullable'] === true) {
|
||||
$defaultRules[] = 'required';
|
||||
} elseif ($item['required']) {
|
||||
} elseif ($item['required'] === true) {
|
||||
$defaultRules[] = 'required';
|
||||
} elseif ($item['readonly']) {
|
||||
} elseif ($item['readonly'] === true) {
|
||||
$defaultRules[] = 'prohibited';
|
||||
}
|
||||
switch ($item['type']) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddSoftDeleteOnUsers extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('users', function (Blueprint $table) {
|
||||
$table->dropSoftDeletes();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class AddSoftDeleteOnProjects extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::table('projects', function (Blueprint $table) {
|
||||
$table->softDeletes();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::table('projects', function (Blueprint $table) {
|
||||
$table->dropSoftDeletes();
|
||||
});
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue