Merge branch 'trunk'

This commit is contained in:
Olivier Legat 2022-07-12 11:26:54 +02:00
commit bbb1b0517c
13 changed files with 151 additions and 17 deletions

View file

@ -1,3 +1,12 @@
### 01.07.2022 -> 1.2.2
- Fixed CSP directives
- Fixed multiples bugs
- Fixed View sur crédits
- Fixed file create
- Changed estimation can be 0.125 days
- Fixed files view
- Added ticket link on credit log
### 09.06.2022 -> 1.2.1
- CSP rules enforce
- Bug fixes CSP issues

View file

@ -38,12 +38,24 @@ class AccountController extends Controller
*/
public function indexQuery(Builder $query): Builder
{
/** @var \App\Models\Brand|null $brand */
if (!($brand = request()->brand)) {
throw new \ErrorException('Cannot get brand');
/** @var \App\Models\User */
$user = auth()->user();
if (!$user->isAdmin()) {
$userBrands = $user->brands();
return $query->whereHas('brand', function ($q) use ($userBrands) {
$q->whereIn('brand_id', $userBrands->pluck('id'));
});
}
return $query->where('brand_id', '=', $brand->id);
/** @var \App\Models\Brand|null $brand */
if (!($brand = request()->brand)) {
abort(404, 'Cannot get brand');
}
return $query->whereHas('brand', function ($q) use ($brand) {
$q->where('id', $brand->id);
});
}
/**

View file

@ -36,13 +36,14 @@ class CreditLogController extends Controller
{
$query->orderBy('created_at', 'DESC');
/** @var \App\Models\Credit\Account|null $account */
if (!($account = request()->account)) {
throw new \ErrorException('Cannot get account');
}
/** @var \App\Models\User */
$user = auth()->user();
/** @var \App\Models\Credit\Account $account */
$account = request()->account;
return $query->whereHas('account', function ($q) use ($account) {
$q->whereIn('id', $account->pluck('id'));
$q->where('id', '=', $account->id);
});
}

View file

@ -49,7 +49,11 @@ class User extends Authenticatable
*/
protected $crudValueColumn = 'id';
// stores unhashed pwd
/**
* stores unhashed pwd
*
* @var string|null
*/
public $tmpPass = null;
/**
@ -249,6 +253,16 @@ class User extends Authenticatable
// * METHODS
/**
* get User brands
*
* @return \Illuminate\Database\Eloquent\Collection<\App\Models\Brand>
*/
public function brands()
{
return $this->sites->pluck('brand')->filter();
}
/**
* Send User creation confirm mail
*

View file

@ -32,7 +32,7 @@ class AccountPolicy extends RootPolicy
'update' => URR::make('denyEveryOne'),
'create' => URR::make('userIsAdmin', $user),
'delete' => URR::make('userIsAdmin', $user),
'credit' => URR::make('userIsAdmin', $user)
'credit' => URR::make('allowEveryOne')
]
];
}

View file

@ -3,7 +3,7 @@
namespace App\Policies;
use App\Models\User;
use App\Policies\Rules\UserRoleRules as URR;
use App\Policies\Rules\CreditAccountLogRules;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Database\Eloquent\Model;
use Kwaadpepper\CrudPolicies\Policies\RootPolicy;
@ -27,7 +27,7 @@ class CreditLogPolicy extends RootPolicy
*/
$this->policies = [
'creditlog' => [
'viewAny' => URR::make('allowEveryOne')
'viewAny' => CreditAccountLogRules::make('nonAdminUserHasBrands', $user, $model)
]
];
}

View file

@ -0,0 +1,24 @@
<?php
namespace App\Policies\Rules;
use App\Models\User;
use Kwaadpepper\CrudPolicies\Policies\Rules\StaticRules;
/**
* User Policiy Static Rules
* ! Rules has to return boolean value
*/
abstract class CreditAccountLogRules extends StaticRules
{
/**
* Account belongs to users brand
*
* @param \App\Models\User $user
* @return boolean
*/
protected static function nonAdminUserHasBrands(User $user): bool
{
return $user->isAdmin() or $user->brands()->count() > 0;
}
}

View file

@ -4,6 +4,7 @@ namespace App\Providers;
use App\Models\Brand;
use App\Models\Credit\Account;
use App\Models\Credit\CreditLog;
use App\Models\File;
use App\Models\Label;
use App\Models\Log;
@ -15,6 +16,7 @@ use App\Models\User;
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
use Illuminate\Http\Request;
use Illuminate\Routing\Route as RoutingRoute;
use Illuminate\Support\Facades\RateLimiter;
use Illuminate\Support\Facades\Route;
@ -45,7 +47,43 @@ class RouteServiceProvider extends ServiceProvider
->namespace($this->namespace)
->group(base_path('routes/web.php'));
});
Route::model('account', Account::class);
Route::bind('account', function ($value, RoutingRoute $route) {
/** @var \App\Models\User|null */
if (!($user = \auth()->user())) {
abort(401);
}
$this->assertClientHasBrands($user);
// * If route has brand parameter
/** @var \App\Models\Brand|null */
if ($brand = $route->parameter('brand')) {
return Account::where('id', \intval($value))->whereHas('brand', function ($q) use ($brand) {
$q->where('id', $brand->id);
})->firstOrFail();
}
return Account::where('id', \intval($value))->firstOrFail();
});
Route::bind('brand', function ($value) {
/** @var \App\Models\User|null */
if (!($user = \auth()->user())) {
abort(401);
}
$this->assertClientHasBrands($user);
return Account::where('id', \intval($value))->firstOrFail();
});
Route::model('credit_log', function ($value) {
/** @var \App\Models\User|null */
if (!($user = \auth()->user())) {
abort(401);
}
$this->assertClientHasBrands($user);
return CreditLog::where('id', \intval($value))->firstOrFail();
});
Route::model('brand', Brand::class);
Route::model('label', Label::class);
Route::model('marker', Marker::class);
@ -74,6 +112,30 @@ class RouteServiceProvider extends ServiceProvider
});
}
/**
* Assert client user has brands
* or return to tickets.index with warning
*
* @param \App\Models\User $user
* @return void
*/
private function assertClientHasBrands(User $user): void
{
if (!$user->isAdmin()) {
$userBrandsAccounts = $user->brands()->each(function (Brand $brand) {
$brand->load('accounts');
})->pluck('accounts')->flatten();
if (!$userBrandsAccounts->count()) {
\redirect()->route('ticket.index')->withErrors(['warning' => trans(
// phpcs:ignore Generic.Files.LineLength.TooLong
'Pour utiliser cette fonctionnalité vous devez appartenir à une marque, merci d\'en faire la demande à :mail',
['mail' => collect(config('mail.contactsList.serviceweb'))->implode(' ')]
)])->throwResponse();
}
}
}
/**
* * Register CSP report URI
*

View file

@ -1,6 +1,6 @@
{
"name": "ldcweb/supportweb",
"version": "1.2.1",
"version": "1.2.2",
"type": "project",
"description": "The Laravel Framework.",
"keywords": [

View file

@ -30,6 +30,7 @@
</form>
</div>
</div>
@if(request()->brand)
<div class="row">
<div class="col-1">
<span class="fw-bold h4">{{ __('Marque')}}</span>
@ -38,6 +39,7 @@
<b class="h4">{{ optional(request()->brand)->name }}</b>
</div>
</div>
@endif
<table class="table text-center">
<thead class="thead-dark">
@include('crud-policies::crud.modules.sorter')

View file

@ -30,6 +30,16 @@
</form>
</div>
</div>
@if(request()->brand)
<div class="row">
<div class="col-1">
<span class="fw-bold h4">{{ __('Marque')}}</span>
</div>
<div class="col">
<b class="h4">{{ optional(request()->brand)->name }}</b>
</div>
</div>
@endif
<div class="row">
<div class="col-1">
<span class="fw-bold h4">{{ __('Compte')}}</span>

View file

@ -47,7 +47,7 @@
@if(!Auth::user()->isAdmin())
@can('viewAny', App\Models\Credit\Account::class)
<li class="nav-item">
<a class="nav-link @if(strpos(request()->route()->getName(), 'users') === 0)active @endif" href="{{ route('credit_accounts.index') }}">{{ __('models.classes.accounts') }}</a>
<a class="nav-link @if(strpos(request()->route()->getName(), 'credit_accounts') === 0)active @endif" href="{{ route('credit_accounts.index') }}">{{ __('models.classes.accounts') }}</a>
</li>
@endcan
@endif

View file

@ -11,7 +11,7 @@ $userPr = Auth::user()->sites ?? collect([]);
<div class="col">
<div class="card">
<div class="card-header">
<h2 class="d-inline">{{ __('Gestion des tickets') }} @if(request()->st) (Archivés) @endif</h2>
<h2 class="d-inline">{{ __('Gestion des tickets') }} @if(Session::get('tickets.st') == App\Enums\TicketStatus::archived()->value) (Archivés) @endif</h2>
<div class="float-end">
<div class="row">
<div class="col">