2016-09-01 16:23:39 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Providers;
|
|
|
|
|
2016-09-09 16:59:13 +01:00
|
|
|
use App\Album;
|
2016-09-11 07:19:11 +01:00
|
|
|
use App\Facade\UserConfig;
|
2017-04-15 09:41:15 +01:00
|
|
|
use App\Group;
|
|
|
|
use App\Permission;
|
2016-09-11 07:19:11 +01:00
|
|
|
use App\Photo;
|
2017-03-21 22:10:36 +00:00
|
|
|
use App\Policies\AlbumPolicy;
|
2017-04-16 09:00:57 +01:00
|
|
|
use App\Policies\PhotoPolicy;
|
2017-04-15 09:41:15 +01:00
|
|
|
use App\User;
|
|
|
|
use function GuzzleHttp\Psr7\mimetype_from_extension;
|
2016-09-01 16:23:39 +01:00
|
|
|
use Illuminate\Support\Facades\Gate;
|
|
|
|
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
|
|
|
|
|
|
|
|
class AuthServiceProvider extends ServiceProvider
|
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
/**
|
|
|
|
* @var Permission[]
|
|
|
|
*/
|
|
|
|
private $permissions;
|
|
|
|
|
2016-09-01 16:23:39 +01:00
|
|
|
/**
|
|
|
|
* The policy mappings for the application.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $policies = [
|
2017-04-16 09:00:57 +01:00
|
|
|
Album::class => AlbumPolicy::class,
|
|
|
|
Photo::class => PhotoPolicy::class
|
2016-09-01 16:23:39 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register any authentication / authorization services.
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function boot()
|
|
|
|
{
|
|
|
|
$this->registerPolicies();
|
|
|
|
|
2017-04-15 09:41:15 +01:00
|
|
|
Gate::define('admin:access', function ($user)
|
2016-09-11 07:19:11 +01:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
return $this->userHasAdminPermission($user, 'access');
|
2016-09-01 16:23:39 +01:00
|
|
|
});
|
2017-04-15 09:41:15 +01:00
|
|
|
Gate::define('admin:configure', function ($user)
|
2017-03-21 21:48:55 +00:00
|
|
|
{
|
2017-04-15 09:41:15 +01:00
|
|
|
return $this->userHasAdminPermission($user, 'configure');
|
2017-03-21 21:48:55 +00:00
|
|
|
});
|
2017-04-15 09:41:15 +01:00
|
|
|
Gate::define('admin:manage-albums', function ($user)
|
|
|
|
{
|
|
|
|
return $this->userHasAdminPermission($user, 'manage-albums');
|
|
|
|
});
|
2017-04-15 09:58:33 +01:00
|
|
|
Gate::define('admin:manage-groups', function ($user)
|
|
|
|
{
|
|
|
|
return $this->userHasAdminPermission($user, 'manage-groups');
|
|
|
|
});
|
|
|
|
Gate::define('admin:manage-storage', function ($user)
|
|
|
|
{
|
|
|
|
return $this->userHasAdminPermission($user, 'manage-storage');
|
|
|
|
});
|
|
|
|
Gate::define('admin:manage-users', function ($user)
|
|
|
|
{
|
|
|
|
return $this->userHasAdminPermission($user, 'manage-users');
|
|
|
|
});
|
2017-04-15 09:41:15 +01:00
|
|
|
|
2016-09-11 07:19:11 +01:00
|
|
|
Gate::define('photo.download_original', function ($user, Photo $photo)
|
|
|
|
{
|
|
|
|
if (!UserConfig::get('restrict_original_download'))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($user->id == $photo->user_id);
|
|
|
|
});
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|
2017-04-15 09:41:15 +01:00
|
|
|
|
|
|
|
private function userHasAdminPermission(User $user, $permissionDescription)
|
|
|
|
{
|
|
|
|
if ($user->is_admin)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @var Group $group */
|
|
|
|
foreach ($user->groups as $group)
|
|
|
|
{
|
|
|
|
if ($group->hasAdminPermission($group, $this->getAdminPermission($permissionDescription)))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getAdminPermission($description)
|
|
|
|
{
|
|
|
|
if (is_null($this->permissions))
|
|
|
|
{
|
|
|
|
$this->permissions = Permission::where('section', 'admin')->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($this->permissions as $permission)
|
|
|
|
{
|
|
|
|
if (strtolower($permission->description) == strtolower($description))
|
|
|
|
{
|
|
|
|
return $permission;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2016-09-01 16:23:39 +01:00
|
|
|
}
|