shared dashboards
This commit is contained in:
52
app/Jobs/Common/CreateDashboard.php
Normal file
52
app/Jobs/Common/CreateDashboard.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Common;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Common\Dashboard;
|
||||
|
||||
class CreateDashboard extends Job
|
||||
{
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($request)
|
||||
{
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Item
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->request['enabled'] = $this->request['enabled'] ?? 1;
|
||||
|
||||
$this->dashboard = Dashboard::create($this->request->all());
|
||||
|
||||
$this->attachToUser();
|
||||
|
||||
return $this->dashboard;
|
||||
}
|
||||
|
||||
protected function attachToUser()
|
||||
{
|
||||
if ($this->request->has('users')) {
|
||||
$user = $this->request->get('users');
|
||||
} else {
|
||||
$user = user();
|
||||
}
|
||||
|
||||
if (empty($user)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->dashboard->users()->attach($user);
|
||||
}
|
||||
}
|
||||
64
app/Jobs/Common/DeleteDashboard.php
Normal file
64
app/Jobs/Common/DeleteDashboard.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Common;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Traits\Users;
|
||||
use Artisan;
|
||||
|
||||
class DeleteDashboard extends Job
|
||||
{
|
||||
use Users;
|
||||
|
||||
protected $dashboard;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $dashboard
|
||||
*/
|
||||
public function __construct($dashboard)
|
||||
{
|
||||
$this->dashboard = $dashboard;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->authorize();
|
||||
|
||||
$this->deleteRelationships($this->dashboard, ['widgets']);
|
||||
|
||||
$this->dashboard->delete();
|
||||
|
||||
Artisan::call('cache:clear');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this action is applicable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
// Can't delete your last dashboard
|
||||
if (user()->dashboards()->enabled()->count() == 1) {
|
||||
$message = trans('dashboards.error.delete_last');
|
||||
|
||||
throw new \Exception($message);
|
||||
}
|
||||
|
||||
// Check if user can access dashboard
|
||||
if (!$this->isUserDashboard($this->dashboard->id)) {
|
||||
$message = trans('dashboards.error.not_user_dashboard');
|
||||
|
||||
throw new \Exception($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
70
app/Jobs/Common/UpdateDashboard.php
Normal file
70
app/Jobs/Common/UpdateDashboard.php
Normal file
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace App\Jobs\Common;
|
||||
|
||||
use App\Abstracts\Job;
|
||||
use App\Models\Common\Dashboard;
|
||||
use App\Traits\Users;
|
||||
|
||||
class UpdateDashboard extends Job
|
||||
{
|
||||
use Users;
|
||||
|
||||
protected $dashboard;
|
||||
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* Create a new job instance.
|
||||
*
|
||||
* @param $dashboard
|
||||
* @param $request
|
||||
*/
|
||||
public function __construct($dashboard, $request)
|
||||
{
|
||||
$this->dashboard = $dashboard;
|
||||
$this->request = $this->getRequestInstance($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the job.
|
||||
*
|
||||
* @return Dashboard
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
$this->authorize();
|
||||
|
||||
$this->dashboard->update($this->request->all());
|
||||
|
||||
if ($this->request->has('users')) {
|
||||
$this->dashboard->users()->sync($this->request->get('users'));
|
||||
}
|
||||
|
||||
return $this->dashboard;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if this action is applicable.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
$user = user();
|
||||
|
||||
// Can't delete your last dashboard
|
||||
if ($this->request->has('users') && !in_array($user->id, (array) $this->request->get('users')) && ($user->dashboards()->enabled()->count() == 1)) {
|
||||
$message = trans('dashboards.error.delete_last');
|
||||
|
||||
throw new \Exception($message);
|
||||
}
|
||||
|
||||
// Check if user can access dashboard
|
||||
if (!$this->isUserDashboard($this->dashboard->id)) {
|
||||
$message = trans('dashboards.error.not_user_dashboard');
|
||||
|
||||
throw new \Exception($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user