124 lines
2.9 KiB
PHP
124 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace App\Policies;
|
|
|
|
use App\Album;
|
|
use App\Group;
|
|
use App\Helpers\PermissionsHelper;
|
|
use App\Permission;
|
|
use App\User;
|
|
use Illuminate\Auth\Access\HandlesAuthorization;
|
|
|
|
class AlbumPolicy
|
|
{
|
|
use HandlesAuthorization;
|
|
|
|
/**
|
|
* Create a new policy instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
//
|
|
}
|
|
|
|
public function before($user, $ability)
|
|
{
|
|
if ($user->is_admin)
|
|
{
|
|
// Admins can do anything
|
|
return true;
|
|
}
|
|
}
|
|
|
|
public function changePermissions(User $user, Album $album)
|
|
{
|
|
// Only the album's owner (or an admin, matched by the before() rule) can change permissions
|
|
return $user->id == $album->user_id;
|
|
}
|
|
|
|
public function changePhotoMetadata(User $user, Album $album)
|
|
{
|
|
if ($user->id == $album->user_id)
|
|
{
|
|
// The album's owner and can do everything
|
|
return true;
|
|
}
|
|
|
|
return $this->userHasPermission($user, $album, 'change-photo-metadata');
|
|
}
|
|
|
|
public function delete(User $user, Album $album)
|
|
{
|
|
if ($user->id == $album->user_id)
|
|
{
|
|
// The album's owner and can do everything
|
|
return true;
|
|
}
|
|
|
|
return $this->userHasPermission($user, $album, 'delete');
|
|
}
|
|
|
|
public function deletePhotos(User $user, Album $album)
|
|
{
|
|
if ($user->id == $album->user_id)
|
|
{
|
|
// The album's owner and can do everything
|
|
return true;
|
|
}
|
|
|
|
return $this->userHasPermission($user, $album, 'delete-photos');
|
|
}
|
|
|
|
public function edit(User $user, Album $album)
|
|
{
|
|
if ($user->id == $album->user_id)
|
|
{
|
|
// The album's owner and can do everything
|
|
return true;
|
|
}
|
|
|
|
return $this->userHasPermission($user, $album, 'edit');
|
|
}
|
|
|
|
public function manipulatePhotos(User $user, Album $album)
|
|
{
|
|
if ($user->id == $album->user_id)
|
|
{
|
|
// The album's owner and can do everything
|
|
return true;
|
|
}
|
|
|
|
return $this->userHasPermission($user, $album, 'manipulate-photos');
|
|
}
|
|
|
|
public function uploadPhotos(User $user, Album $album)
|
|
{
|
|
if ($user->id == $album->user_id)
|
|
{
|
|
// The album's owner and can do everything
|
|
return true;
|
|
}
|
|
|
|
return $this->userHasPermission($user, $album, 'upload-photos');
|
|
}
|
|
|
|
public function view(User $user, Album $album)
|
|
{
|
|
if ($user->id == $album->user_id)
|
|
{
|
|
// The album's owner and can do everything
|
|
return true;
|
|
}
|
|
|
|
return $this->userHasPermission($user, $album, 'view');
|
|
}
|
|
|
|
private function userHasPermission(User $user, Album $album, $permission)
|
|
{
|
|
$helper = new PermissionsHelper();
|
|
return $helper->userCan_Album($album, $user, $permission);
|
|
}
|
|
}
|