Files
cachet-docker/app/Http/Controllers/Dashboard/DashboardController.php
Marius Palade ad0954eb20 Add visibility to component groups (#2027)
Implement visibility for the components groups. Closes #1892

Add functional test that asserts a guest can only see public items.

* Fix tests not running due to hitting the Setup page.

The missing `boostrap/cachet/testing.php` file is now generated the first time tests are ran.

* Add a functional test that asserts logged in users can see all items.

Add constants for possible values for the visible column/field of the ComponentGroup model.
Code review changes.

* Add API tests for component group visibility feature.

* Implement the visibility hidden option for a component group. Fixes #1892.

Add migration for the created_by column, in component_groups table.
Add methods to the ComponentGroup and User models to be able to work with the created_by column.
Hidden component groups are no longer displayed on the index page for loggedin users.
Add functional test for the dashboard page.
Save owner on create/edit component group.
Update the API tests for Component group visibility feature.

* Replace auth() usage with app(Guard::class).

* Apply StyleCI fixes.

* Drop the hidden visibility feature and fix all tests.

Some code review fixes too.

* Rename public to visible since it's a reserved keyword. Apply StyleCI fixes and correct typo.

* Code review changes.

* Tidy up component and component groups gathering.

* Code review changes and StyleCI fixes.

* Code review changes.

* Remove extra whitespace

* Remove useless method.
2016-10-02 13:57:32 +01:00

206 lines
5.9 KiB
PHP

<?php
/*
* This file is part of Cachet.
*
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Http\Controllers\Dashboard;
use CachetHQ\Cachet\Bus\Commands\User\WelcomeUserCommand;
use CachetHQ\Cachet\Integrations\Contracts\Feed;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\ComponentGroup;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Models\Subscriber;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\View;
use Jenssegers\Date\Date;
/**
* This is the dashboard controller class.
*
* @author James Brooks <james@alt-three.com>
*/
class DashboardController extends Controller
{
/**
* Start date.
*
* @var \Jenssegers\Date\Date
*/
protected $startDate;
/**
* The timezone the status page is running in.
*
* @var string
*/
protected $timeZone;
/**
* The feed integration.
*
* @var \CachetHQ\Cachet\Integrations\Feed
*/
protected $feed;
/**
* The user session object.
*
* @var \Illuminate\Contracts\Auth\Guard
*/
protected $guard;
/**
* Creates a new dashboard controller instance.
*
* @param \CachetHQ\Cachet\Integrations\Feed $feed
* @param \Illuminate\Contracts\Auth\Guard $guard
*
* @return void
*/
public function __construct(Feed $feed, Guard $guard)
{
$this->feed = $feed;
$this->guard = $guard;
$this->startDate = new Date();
$this->dateTimeZone = Config::get('cachet.timezone');
}
/**
* Redirect /admin to /dashboard.
*
* @return \Illuminate\Http\RedirectResponse
*/
public function redirectAdmin()
{
return Redirect::route('dashboard.index');
}
/**
* Shows the dashboard view.
*
* @return \Illuminate\View\View
*/
public function showDashboard()
{
$components = Component::orderBy('order')->get();
$incidents = $this->getIncidents();
$subscribers = $this->getSubscribers();
$componentGroups = $this->getVisibleGroupedComponents();
$ungroupedComponents = Component::ungrouped()->get();
$welcomeUser = !Auth::user()->welcomed;
if ($welcomeUser) {
dispatch(new WelcomeUserCommand(Auth::user()));
}
$entries = null;
if ($feed = $this->feed->latest()) {
$entries = array_slice($feed->channel->item, 0, 5);
}
return View::make('dashboard.index')
->withPageTitle(trans('dashboard.dashboard'))
->withComponents($components)
->withIncidents($incidents)
->withSubscribers($subscribers)
->withEntries($entries)
->withComponentGroups($componentGroups)
->withUngroupedComponents($ungroupedComponents)
->withWelcomeUser($welcomeUser);
}
/**
* Fetches all of the incidents over the last 30 days.
*
* @return \Illuminate\Support\Collection
*/
protected function getIncidents()
{
$allIncidents = Incident::notScheduled()->whereBetween('created_at', [
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
$this->startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function (Incident $incident) {
return (new Date($incident->created_at))
->setTimezone($this->dateTimeZone)->toDateString();
});
// Add in days that have no incidents
foreach (range(0, 30) as $i) {
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
if (!isset($allIncidents[$date->toDateString()])) {
$allIncidents[$date->toDateString()] = [];
}
}
// Sort the array so it takes into account the added days
$allIncidents = $allIncidents->sortBy(function ($value, $key) {
return strtotime($key);
}, SORT_REGULAR, false);
return $allIncidents;
}
/**
* Fetches all of the subscribers over the last 30 days.
*
* @return \Illuminate\Support\Collection
*/
protected function getSubscribers()
{
$allSubscribers = Subscriber::whereBetween('created_at', [
$this->startDate->copy()->subDays(30)->format('Y-m-d').' 00:00:00',
$this->startDate->format('Y-m-d').' 23:59:59',
])->orderBy('created_at', 'desc')->get()->groupBy(function (Subscriber $incident) {
return (new Date($incident->created_at))
->setTimezone($this->dateTimeZone)->toDateString();
});
// Add in days that have no incidents
foreach (range(0, 30) as $i) {
$date = (new Date($this->startDate))->setTimezone($this->dateTimeZone)->subDays($i);
if (!isset($allSubscribers[$date->toDateString()])) {
$allSubscribers[$date->toDateString()] = [];
}
}
// Sort the array so it takes into account the added days
$allSubscribers = $allSubscribers->sortBy(function ($value, $key) {
return strtotime($key);
}, SORT_REGULAR, false);
return $allSubscribers;
}
/**
* Get visible grouped components.
*
* @return \Illuminate\Support\Collection
*/
protected function getVisibleGroupedComponents()
{
$componentGroupsBuilder = ComponentGroup::query();
if (!$this->guard->check()) {
$componentGroupsBuilder = ComponentGroup::visible();
}
$usedComponentGroups = Component::grouped()->pluck('group_id');
return $componentGroupsBuilder->used($usedComponentGroups)
->get();
}
}