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.
This commit is contained in:
committed by
James Brooks
parent
bf769e1470
commit
ad0954eb20
@@ -16,6 +16,7 @@ use CachetHQ\Cachet\Bus\Commands\ComponentGroup\RemoveComponentGroupCommand;
|
||||
use CachetHQ\Cachet\Bus\Commands\ComponentGroup\UpdateComponentGroupCommand;
|
||||
use CachetHQ\Cachet\Models\ComponentGroup;
|
||||
use GrahamCampbell\Binput\Facades\Binput;
|
||||
use Illuminate\Contracts\Auth\Guard;
|
||||
use Illuminate\Database\QueryException;
|
||||
use Illuminate\Support\Facades\Request;
|
||||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
@@ -29,6 +30,23 @@ use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
||||
*/
|
||||
class ComponentGroupController extends AbstractApiController
|
||||
{
|
||||
/**
|
||||
* The user session object.
|
||||
*
|
||||
* @var \Illuminate\Contracts\Auth\Guard
|
||||
*/
|
||||
protected $guard;
|
||||
|
||||
/**
|
||||
* Creates a new component group controller instance.
|
||||
*
|
||||
* @param \Illuminate\Contracts\Auth\Guard $guard
|
||||
*/
|
||||
public function __construct(Guard $guard)
|
||||
{
|
||||
$this->guard = $guard;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all groups.
|
||||
*
|
||||
@@ -37,6 +55,9 @@ class ComponentGroupController extends AbstractApiController
|
||||
public function getGroups()
|
||||
{
|
||||
$groups = ComponentGroup::query();
|
||||
if (!$this->guard->check()) {
|
||||
$groups = ComponentGroup::visible();
|
||||
}
|
||||
|
||||
$groups->search(Binput::except(['sort', 'order', 'per_page']));
|
||||
|
||||
@@ -74,7 +95,8 @@ class ComponentGroupController extends AbstractApiController
|
||||
$group = dispatch(new AddComponentGroupCommand(
|
||||
Binput::get('name'),
|
||||
Binput::get('order', 0),
|
||||
Binput::get('collapsed', 0)
|
||||
Binput::get('collapsed', 0),
|
||||
Binput::get('visible', ComponentGroup::VISIBLE_AUTHENTICATED)
|
||||
));
|
||||
} catch (QueryException $e) {
|
||||
throw new BadRequestHttpException();
|
||||
@@ -97,7 +119,8 @@ class ComponentGroupController extends AbstractApiController
|
||||
$group,
|
||||
Binput::get('name'),
|
||||
Binput::get('order'),
|
||||
Binput::get('collapsed')
|
||||
Binput::get('collapsed'),
|
||||
Binput::get('visible')
|
||||
));
|
||||
} catch (QueryException $e) {
|
||||
throw new BadRequestHttpException();
|
||||
|
||||
@@ -277,7 +277,8 @@ class ComponentController extends Controller
|
||||
$group = dispatch(new AddComponentGroupCommand(
|
||||
Binput::get('name'),
|
||||
Binput::get('order', 0),
|
||||
Binput::get('collapsed')
|
||||
Binput::get('collapsed'),
|
||||
Binput::get('visible')
|
||||
));
|
||||
} catch (ValidationException $e) {
|
||||
return Redirect::route('dashboard.components.groups.add')
|
||||
@@ -304,7 +305,8 @@ class ComponentController extends Controller
|
||||
$group,
|
||||
Binput::get('name'),
|
||||
$group->order,
|
||||
Binput::get('collapsed')
|
||||
Binput::get('collapsed'),
|
||||
Binput::get('visible')
|
||||
));
|
||||
} catch (ValidationException $e) {
|
||||
return Redirect::route('dashboard.components.groups.edit', ['id' => $group->id])
|
||||
|
||||
@@ -17,6 +17,7 @@ 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;
|
||||
@@ -52,16 +53,25 @@ class DashboardController extends Controller
|
||||
*/
|
||||
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)
|
||||
public function __construct(Feed $feed, Guard $guard)
|
||||
{
|
||||
$this->feed = $feed;
|
||||
$this->guard = $guard;
|
||||
$this->startDate = new Date();
|
||||
$this->dateTimeZone = Config::get('cachet.timezone');
|
||||
}
|
||||
@@ -86,9 +96,9 @@ class DashboardController extends Controller
|
||||
$components = Component::orderBy('order')->get();
|
||||
$incidents = $this->getIncidents();
|
||||
$subscribers = $this->getSubscribers();
|
||||
$usedComponentGroups = Component::enabled()->where('group_id', '>', 0)->groupBy('group_id')->pluck('group_id');
|
||||
$componentGroups = ComponentGroup::whereIn('id', $usedComponentGroups)->orderBy('order')->get();
|
||||
$ungroupedComponents = Component::enabled()->where('group_id', 0)->orderBy('order')->orderBy('created_at')->get();
|
||||
|
||||
$componentGroups = $this->getVisibleGroupedComponents();
|
||||
$ungroupedComponents = Component::ungrouped()->get();
|
||||
|
||||
$welcomeUser = !Auth::user()->welcomed;
|
||||
if ($welcomeUser) {
|
||||
@@ -174,4 +184,22 @@ class DashboardController extends Controller
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user