shared dashboards

This commit is contained in:
denisdulici
2020-01-07 17:15:00 +03:00
parent dcab115207
commit 2436898f04
33 changed files with 806 additions and 457 deletions

View File

@@ -58,7 +58,7 @@ abstract class Controller extends BaseController
$controller .= Str::kebab($arr[0]);
// Skip ACL
$skip = ['common-dashboard', 'portal-dashboard'];
$skip = ['portal-dashboard'];
if (in_array($controller, $skip)) {
return;
}

View File

@@ -0,0 +1,70 @@
<?php
namespace App\BulkActions\Common;
use App\Abstracts\BulkAction;
use App\Jobs\Common\DeleteDashboard;
use App\Jobs\Common\UpdateDashboard;
use App\Models\Common\Dashboard;
class Dashboards extends BulkAction
{
public $model = Dashboard::class;
public $actions = [
'enable' => [
'name' => 'general.enable',
'message' => 'bulk_actions.message.enable',
'permission' => 'update-common-dashboards',
],
'disable' => [
'name' => 'general.disable',
'message' => 'bulk_actions.message.disable',
'permission' => 'update-common-dashboards',
],
'delete' => [
'name' => 'general.delete',
'message' => 'bulk_actions.message.delete',
'permission' => 'delete-common-dashboards',
],
];
public function enable($request)
{
$dashboards = $this->getSelectedRecords($request);
foreach ($dashboards as $dashboard) {
try {
$this->dispatch(new UpdateDashboard($dashboard, $request->merge(['enabled' => 1])));
} catch (\Exception $e) {
flash($e->getMessage())->error();
}
}
}
public function disable($request)
{
$dashboards = $this->getSelectedRecords($request);
foreach ($dashboards as $dashboard) {
try {
$this->dispatch(new UpdateDashboard($dashboard, $request->merge(['enabled' => 0])));
} catch (\Exception $e) {
flash($e->getMessage())->error();
}
}
}
public function destroy($request)
{
$dashboards = $this->getSelectedRecords($request);
foreach ($dashboards as $dashboard) {
try {
$this->dispatch(new DeleteDashboard($dashboard));
} catch (\Exception $e) {
flash($e->getMessage())->error();
}
}
}
}

View File

@@ -1,117 +0,0 @@
<?php
namespace App\Http\Controllers\Common;
use App\Abstracts\Http\Controller;
use App\Http\Requests\Common\Dashboard as Request;
use App\Models\Common\Dashboard as Model;
use App\Models\Common\Widget;
use App\Traits\DateTime;
use App\Utilities\Widgets as WidgetUtility;
class Dashboard extends Controller
{
use DateTime;
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$dashboard_id = session('dashboard_id', 0);
// Change Dashboard
if (request()->get('dashboard_id', 0)) {
$dashboard_id = request()->get('dashboard_id');
session(['dashboard_id' => $dashboard_id]);
}
$dashboards = Model::where('user_id', user()->id)->enabled()->get();
if (!$dashboard_id) {
$dashboard_id = $dashboards->pluck('id')->first();
}
// Dashboard
$dashboard = Model::find($dashboard_id);
// Widgets
$widgets = Widget::where('dashboard_id', $dashboard->id)->orderBy('sort', 'asc')->get()->filter(function ($widget) {
return WidgetUtility::canRead($widget->class);
});
$financial_start = $this->getFinancialStart()->format('Y-m-d');
return view('common.dashboard.index', compact('dashboards', 'dashboard', 'widgets', 'financial_start'));
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return Response
*/
public function store(Request $request)
{
$request['enabled'] = 1;
$request['user_id'] = user()->id;
$dashboard = Model::create($request->input());
$response['data'] = $dashboard;
$response['redirect'] = route('dashboard');
return response()->json($response);
}
/**
* Show the form for editing the specified resource.
*
* @param Model $dashboard
*
* @return Response
*/
public function edit(Model $dashboard)
{
return response()->json($dashboard);
}
/**
* Update the specified resource in storage.
*
* @param Model $dashboard
* @param $request
* @return Response
*/
public function update(Model $dashboard, Request $request)
{
$request['enabled'] = 1;
$dashboard->update($request->input());
$response['data'] = $dashboard;
$response['redirect'] = route('dashboard');
return response()->json($response);
}
/**
* Remove the specified resource from storage.
*
* @param Model $dashboard
*
* @return Response
*/
public function destroy(Model $dashboard)
{
$dashboard->delete();
session(['dashboard_id' => user()->dashboards()->pluck('id')->first()]);
$response['redirect'] = route('dashboard');
return response()->json($response);
}
}

View File

@@ -0,0 +1,244 @@
<?php
namespace App\Http\Controllers\Common;
use App\Abstracts\Http\Controller;
use App\Http\Requests\Common\Dashboard as Request;
use App\Jobs\Common\CreateDashboard;
use App\Jobs\Common\DeleteDashboard;
use App\Jobs\Common\UpdateDashboard;
use App\Models\Common\Company;
use App\Models\Common\Dashboard;
use App\Models\Common\Widget;
use App\Traits\DateTime;
use App\Traits\Users;
use App\Utilities\Widgets;
class Dashboards extends Controller
{
use DateTime, Users;
/**
* Instantiate a new controller instance.
*/
public function __construct()
{
// Add CRUD permission check
$this->middleware('permission:create-common-dashboards')->only(['create', 'store', 'duplicate', 'import']);
$this->middleware('permission:read-common-dashboards')->only(['index', 'edit', 'export']);
$this->middleware('permission:update-common-dashboards')->only(['update', 'enable', 'disable', 'share']);
$this->middleware('permission:delete-common-dashboards')->only('destroy');
}
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$dashboards = user()->dashboards()->collect();
return view('common.dashboards.index', compact('dashboards'));
}
/**
* Show the form for viewing the specified resource.
*
* @return Response
*/
public function show()
{
$dashboard_id = session('dashboard_id', 0);
// Change Dashboard
if (request()->get('dashboard_id', 0)) {
$dashboard_id = request()->get('dashboard_id');
session(['dashboard_id' => $dashboard_id]);
}
$dashboards = user()->dashboards()->enabled()->get();
if (!$dashboard_id) {
$dashboard_id = $dashboards->pluck('id')->first();
}
// Dashboard
$dashboard = Dashboard::find($dashboard_id);
// Widgets
$widgets = Widget::where('dashboard_id', $dashboard->id)->orderBy('sort', 'asc')->get()->filter(function ($widget) {
return Widgets::canRead($widget->class);
});
$financial_start = $this->getFinancialStart()->format('Y-m-d');
return view('common.dashboards.show', compact('dashboards', 'dashboard', 'widgets', 'financial_start'));
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
$users = Company::find(session('company_id'))->users()->get()->sortBy('name');
return view('common.dashboards.create', compact('users'));
}
/**
* Store a newly created resource in storage.
*
* @param $request
* @return Response
*/
public function store(Request $request)
{
$response = $this->ajaxDispatch(new CreateDashboard($request));
if ($response['success']) {
$response['redirect'] = route('dashboard');
$message = trans('messages.success.added', ['type' => trans_choice('general.dashboards', 1)]);
flash($message)->success();
} else {
$response['redirect'] = route('dashboard');
$message = $response['message'];
flash($message)->error();
}
return response()->json($response);
}
/**
* Show the form for editing the specified resource.
*
* @param Dashboard $dashboard
*
* @return Response
*/
public function edit(Dashboard $dashboard)
{
if (!$this->isUserDashboard($dashboard->id)) {
return redirect()->route('dashboards.index');
}
$users = Company::find(session('company_id'))->users()->get()->sortBy('name');
return view('common.dashboards.edit', compact('dashboard', 'users'));
}
/**
* Update the specified resource in storage.
*
* @param Dashboard $dashboard
* @param $request
* @return Response
*/
public function update(Dashboard $dashboard, Request $request)
{
$response = $this->ajaxDispatch(new UpdateDashboard($dashboard, $request));
if ($response['success']) {
$response['redirect'] = route('dashboards.index');
$message = trans('messages.success.updated', ['type' => trans_choice('general.dashboards', 1)]);
flash($message)->success();
} else {
$response['redirect'] = route('dashboards.edit', $dashboard->id);
$message = $response['message'];
flash($message)->error();
}
return response()->json($response);
}
/**
* Enable the specified resource.
*
* @param Dashboard $dashboard
*
* @return Response
*/
public function enable(Dashboard $dashboard)
{
$response = $this->ajaxDispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 1])));
if ($response['success']) {
$response['message'] = trans('messages.success.enabled', ['type' => trans_choice('general.dashboards', 1)]);
}
return response()->json($response);
}
/**
* Disable the specified resource.
*
* @param Dashboard $dashboard
*
* @return Response
*/
public function disable(Dashboard $dashboard)
{
$response = $this->ajaxDispatch(new UpdateDashboard($dashboard, request()->merge(['enabled' => 0])));
if ($response['success']) {
$response['message'] = trans('messages.success.disabled', ['type' => trans_choice('general.dashboards', 1)]);
}
return response()->json($response);
}
/**
* Remove the specified resource from storage.
*
* @param Dashboard $dashboard
*
* @return Response
*/
public function destroy(Dashboard $dashboard)
{
$response = $this->ajaxDispatch(new DeleteDashboard($dashboard));
$response['redirect'] = route('dashboard');
if ($response['success']) {
$message = trans('messages.success.deleted', ['type' => $dashboard->name]);
flash($message)->success();
session(['dashboard_id' => user()->dashboards()->pluck('id')->first()]);
} else {
$message = $response['message'];
flash($message)->error();
}
return response()->json($response);
}
/**
* Change the active dashboard.
*
* @param Dashboard $dashboard
*
* @return Response
*/
public function switch(Dashboard $dashboard)
{
if ($this->isUserDashboard($dashboard->id)) {
session(['dashboard_id' => $dashboard->id]);
}
return redirect()->route('dashboard');
}
}

View File

@@ -29,10 +29,6 @@ class CreateUser extends Job
{
$user = User::create($this->request->input());
if ($this->request->has('permissions')) {
$user->permissions()->attach($this->request->get('permissions'));
}
// Upload picture
if ($this->request->file('picture')) {
$media = $this->getMedia($this->request->file('picture'), 'users');
@@ -40,10 +36,16 @@ class CreateUser extends Job
$user->attachMedia($media, 'picture');
}
// Attach roles
if ($this->request->has('dashboards')) {
$user->dashboards()->attach($this->request->get('dashboards'));
}
if ($this->request->has('permissions')) {
$user->permissions()->attach($this->request->get('permissions'));
}
$user->roles()->attach($this->request->get('roles'));
// Attach companies
$user->companies()->attach($this->request->get('companies'));
Artisan::call('cache:clear');

View File

@@ -28,8 +28,6 @@ class DeleteUser extends Job
{
$this->authorize();
$this->deleteRelationships($this->user, ['widgets', 'dashboards']);
$this->user->delete();
Artisan::call('cache:clear');

View 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);
}
}

View 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);
}
}
}

View 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);
}
}
}

View File

@@ -3,7 +3,6 @@
namespace App\Listeners\Menu;
use App\Events\Menu\AdminCreated as Event;
use App\Models\Common\Dashboard;
class AddAdminItems
{
@@ -21,7 +20,7 @@ class AddAdminItems
$attr = ['icon' => ''];
// Dashboard
$dashboards = Dashboard::ofUser($user->id)->get();
$dashboards = user()->dashboards()->enabled()->get();
if ($dashboards->count() > 1) {
$menu->dropdown(trim(trans_choice('general.dashboards', 2)), function ($sub) use ($user, $attr, $dashboards) {

View File

@@ -695,6 +695,7 @@ class Version200 extends Listener
{
$this->attachPermissions([
'admin' => [
'common-dashboards' => 'c,r,u,d',
'common-reports' => 'c,r,u,d',
'common-search' => 'r',
'common-widgets' => 'c,r,u,d',
@@ -720,9 +721,10 @@ class Version200 extends Listener
'widgets-total-profit' => 'r',
],
'manager' => [
'common-dashboards' => 'c,r,u,d',
'common-reports' => 'c,r,u,d',
'common-search' => 'r',
'common-widgets' => 'r',
'common-widgets' => 'c,r,u,d',
'offline-payments-settings' => 'r,u,d',
'paypal-standard-settings' => 'r,u',
'settings-company' => 'r',
@@ -885,6 +887,7 @@ class Version200 extends Listener
'app/Http/Controllers/Api/Incomes/Revenues.php',
'app/Http/Controllers/ApiController.php',
'app/Http/Controllers/Controller.php',
'app/Http/Controllers/Common/Dashboard.php',
'app/Http/Controllers/Modals/BillPayments.php',
'app/Http/Controllers/Modals/InvoicePayments.php',
'app/Http/Controllers/modules/Token.php',
@@ -1021,6 +1024,7 @@ class Version200 extends Listener
'public/js/highchart',
'public/js/lightbox',
'public/js/moment',
'resources/views/common/dashboard',
'resources/views/customers',
'resources/views/expenses',
'resources/views/incomes',

View File

@@ -59,12 +59,7 @@ class User extends Authenticatable
public function dashboards()
{
return $this->hasMany('App\Models\Common\Dashboard');
}
public function widgets()
{
return $this->hasManyThrough('App\Models\Common\Widget', 'App\Models\Common\Dashboard');
return $this->morphToMany('App\Models\Common\Dashboard', 'user', 'user_dashboards', 'user_id', 'dashboard_id');
}
/**

View File

@@ -16,7 +16,7 @@ class Dashboard extends Model
*
* @var array
*/
protected $fillable = ['company_id', 'user_id', 'name', 'enabled'];
protected $fillable = ['company_id', 'name', 'enabled'];
/**
* Sortable columns.
@@ -25,18 +25,13 @@ class Dashboard extends Model
*/
public $sortable = ['name', 'enabled'];
public function user()
public function users()
{
return $this->belongsTo('App\Models\Auth\User', 'user_id', 'id');
return $this->morphedByMany('App\Models\Auth\User', 'user', 'user_dashboards', 'dashboard_id', 'user_id');
}
public function widgets()
{
return $this->hasMany('App\Models\Common\Widget')->orderBy('sort', 'asc');
}
public function scopeOfUser($query, $user_id)
{
return $query->where('user_id', $user_id);
}
}

View File

@@ -2,8 +2,6 @@
namespace App\Traits;
use App\Models\Auth\User;
trait Users
{
/**
@@ -39,4 +37,28 @@ trait Users
return false;
}
/**
* Check user dashboard assignment
*
* @param $id
*
* @return boolean
*/
public function isUserDashboard($id)
{
$user = user();
if (empty($user)) {
return false;
}
$dashboards = $user->dashboards()->pluck('id')->toArray();
if (in_array($id, $dashboards)) {
return true;
}
return false;
}
}