Enable ZC to see netmail in their zone, and point owners to see their own netmail

This commit is contained in:
Deon George 2023-12-18 13:00:04 +11:00
parent 6fb7d165ae
commit 1ded66990c
3 changed files with 66 additions and 2 deletions

View File

@ -6,6 +6,7 @@ use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Collection;
use Laravel\Sanctum\HasApiTokens;
use App\Traits\{ScopeActive,UserSwitch};
@ -91,6 +92,31 @@ class User extends Authenticatable implements MustVerifyEmail
*/
public function isZC(): bool
{
return $this->systems->pluck('addresses')->flatten()->where('role',Address::NODE_ZC)->count() > 0;
return $this->zc()->count() > 0;
}
/**
* Return the zones that this user is ZC for
*
* @return Collection
*/
public function points(): Collection
{
$result = collect();
foreach($this->systems->pluck('addresses')->flatten()->where('role','>',Address::NODE_HC) as $ao)
$result = $result->merge($ao->children());
return $result;
}
/**
* Return the zones that this user is ZC for
*
* @return Collection
*/
public function zc(): Collection
{
return $this->systems->pluck('addresses')->flatten()->where('role',Address::NODE_ZC);
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Policies;
use Illuminate\Auth\Access\HandlesAuthorization;
use App\Models\{Netmail,User};
class NetmailPolicy
{
use HandlesAuthorization;
/**
* This determines whether a logged-in user can view a netmail
*
* @param User $user
* @param Netmail $netmail
* @return bool
*/
public function view(User $user, Netmail $netmail): bool
{
$zones = $user->zc()->pluck('zone')
//->merge($user->rc()->pluck('zone'))
//->merge($user->nc()->pluck('zone'))
//->merge($user->hub()->pluck('zone'))
->merge($user->points()->pluck('zone'));
// Site Admins can always view
return (
$user->isAdmin()
|| ($zones->contains($netmail->fftn->zone))
|| ($zones->contains($netmail->tftn->zone))
);
}
}

View File

@ -134,10 +134,13 @@ Route::middleware(['auth','verified','activeuser'])->group(function () {
->where('o','[0-9]+');
});
Route::middleware(['auth','can:view,o'])->group(function () {
Route::get('netmail/view/{o}',[NetmailController::class,'view']);
});
Route::middleware(['auth','can:admin'])->group(function () {
Route::match(['get','post'],'address/merge/{id}',[SystemController::class,'address_merge']);
Route::get('echomail/view/{o}',[EchomailController::class,'view']);
Route::get('netmail/view/{o}',[NetmailController::class,'view']);
Route::match(['get','post'],'setup',[HomeController::class,'setup']);
Route::view('user','user.home');