Files
cachet-docker/app/Http/Controllers/Api/ComponentGroupController.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

146 lines
3.8 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\Api;
use CachetHQ\Cachet\Bus\Commands\ComponentGroup\AddComponentGroupCommand;
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;
/**
* This is the component group controller.
*
* @author James Brooks <james@alt-three.com>
* @author Graham Campbell <graham@alt-three.com>
* @author Joe Cohen <joe@alt-three.com>
*/
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.
*
* @return \Illuminate\Http\JsonResponse
*/
public function getGroups()
{
$groups = ComponentGroup::query();
if (!$this->guard->check()) {
$groups = ComponentGroup::visible();
}
$groups->search(Binput::except(['sort', 'order', 'per_page']));
if ($sortBy = Binput::get('sort')) {
$direction = Binput::has('order') && Binput::get('order') == 'desc';
$groups->sort($sortBy, $direction);
}
$groups = $groups->paginate(Binput::get('per_page', 20));
return $this->paginator($groups, Request::instance());
}
/**
* Get a single group.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup $group
*
* @return \Illuminate\Http\JsonResponse
*/
public function getGroup(ComponentGroup $group)
{
return $this->item($group);
}
/**
* Create a new component group.
*
* @return \Illuminate\Http\JsonResponse
*/
public function postGroups()
{
try {
$group = dispatch(new AddComponentGroupCommand(
Binput::get('name'),
Binput::get('order', 0),
Binput::get('collapsed', 0),
Binput::get('visible', ComponentGroup::VISIBLE_AUTHENTICATED)
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
return $this->item($group);
}
/**
* Update an existing group.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup $group
*
* @return \Illuminate\Http\JsonResponse
*/
public function putGroup(ComponentGroup $group)
{
try {
$group = dispatch(new UpdateComponentGroupCommand(
$group,
Binput::get('name'),
Binput::get('order'),
Binput::get('collapsed'),
Binput::get('visible')
));
} catch (QueryException $e) {
throw new BadRequestHttpException();
}
return $this->item($group);
}
/**
* Delete an existing group.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup $group
*
* @return \Illuminate\Http\JsonResponse
*/
public function deleteGroup(ComponentGroup $group)
{
dispatch(new RemoveComponentGroupCommand($group));
return $this->noContent();
}
}