2017-02-01 18:50:28 -08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
|
|
|
|
use App\Helpers\Helper;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Http\Controllers\Controller;
|
2017-02-01 18:50:28 -08:00
|
|
|
use App\Http\Transformers\GroupsTransformer;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Group;
|
|
|
|
use Illuminate\Http\Request;
|
2024-07-04 23:07:20 -07:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2017-02-01 18:50:28 -08:00
|
|
|
|
2023-03-18 11:53:20 -07:00
|
|
|
|
2017-02-01 18:50:28 -08:00
|
|
|
class GroupsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Display a listing of the resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function index(Request $request) : JsonResponse | array
|
2017-02-01 18:50:28 -08:00
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
|
|
|
|
2017-02-01 18:50:28 -08:00
|
|
|
$this->authorize('view', Group::class);
|
|
|
|
|
2024-09-19 09:20:56 -07:00
|
|
|
$groups = Group::select('id', 'name', 'permissions', 'created_at', 'updated_at', 'created_by')->with('adminuser')->withCount('users as users_count');
|
2017-02-01 18:50:28 -08:00
|
|
|
|
2019-05-23 17:39:50 -07:00
|
|
|
if ($request->filled('search')) {
|
2017-02-01 18:50:28 -08:00
|
|
|
$groups = $groups->TextSearch($request->input('search'));
|
|
|
|
}
|
|
|
|
|
2022-06-28 19:59:45 -07:00
|
|
|
if ($request->filled('name')) {
|
|
|
|
$groups->where('name', '=', $request->input('name'));
|
|
|
|
}
|
|
|
|
|
2024-09-19 09:20:56 -07:00
|
|
|
|
2023-10-14 12:39:52 -07:00
|
|
|
$offset = ($request->input('offset') > $groups->count()) ? $groups->count() : app('api_offset_value');
|
2023-04-16 08:46:39 -07:00
|
|
|
$limit = app('api_limit_value');
|
2017-02-01 18:50:28 -08:00
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
2024-09-19 09:20:56 -07:00
|
|
|
|
|
|
|
switch ($request->input('sort')) {
|
|
|
|
case 'created_by':
|
|
|
|
$groups = $groups->OrderByCreatedBy($order);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// This array is what determines which fields should be allowed to be sorted on ON the table itself.
|
|
|
|
// These must match a column on the consumables table directly.
|
|
|
|
$allowed_columns = [
|
|
|
|
'id',
|
|
|
|
'name',
|
|
|
|
'created_at',
|
|
|
|
'users_count',
|
|
|
|
];
|
|
|
|
|
|
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
|
|
|
|
$groups = $groups->orderBy($sort, $order);
|
|
|
|
break;
|
|
|
|
}
|
2017-02-01 18:50:28 -08:00
|
|
|
|
|
|
|
$total = $groups->count();
|
|
|
|
$groups = $groups->skip($offset)->take($limit)->get();
|
2021-06-10 13:15:52 -07:00
|
|
|
|
2017-02-01 18:50:28 -08:00
|
|
|
return (new GroupsTransformer)->transformGroups($groups, $total);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store a newly created resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function store(Request $request) : JsonResponse
|
2017-02-01 18:50:28 -08:00
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
2017-02-01 18:50:28 -08:00
|
|
|
$group = new Group;
|
2024-05-28 07:21:28 -07:00
|
|
|
// Get all the available permissions
|
|
|
|
$permissions = config('permissions');
|
|
|
|
$groupPermissions = Helper::selectedPermissionsArray($permissions, $permissions);
|
2023-03-18 11:53:20 -07:00
|
|
|
|
|
|
|
$group->name = $request->input('name');
|
2024-07-04 12:48:35 -07:00
|
|
|
$group->created_by = auth()->id();
|
2024-05-28 13:03:09 -07:00
|
|
|
$group->permissions = json_encode($request->input('permissions', $groupPermissions));
|
2017-02-01 18:50:28 -08:00
|
|
|
|
|
|
|
if ($group->save()) {
|
2024-05-28 07:21:28 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', (new GroupsTransformer)->transformGroup($group), trans('admin/groups/message.success.create')));
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $group->getErrors()));
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display the specified resource.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param int $id
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function show($id) : array
|
2017-02-01 18:50:28 -08:00
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
2017-02-01 18:50:28 -08:00
|
|
|
$group = Group::findOrFail($id);
|
2017-09-27 15:18:29 -07:00
|
|
|
return (new GroupsTransformer)->transformGroup($group);
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the specified resource in storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param \Illuminate\Http\Request $request
|
|
|
|
* @param int $id
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function update(Request $request, $id) : JsonResponse
|
2017-02-01 18:50:28 -08:00
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
2017-02-01 18:50:28 -08:00
|
|
|
$group = Group::findOrFail($id);
|
2023-03-18 11:53:20 -07:00
|
|
|
|
|
|
|
$group->name = $request->input('name');
|
|
|
|
$group->permissions = $request->input('permissions'); // Todo - some JSON validation stuff here
|
2017-02-01 18:50:28 -08:00
|
|
|
|
|
|
|
if ($group->save()) {
|
2024-05-28 07:21:28 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', (new GroupsTransformer)->transformGroup($group), trans('admin/groups/message.success.update')));
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('error', null, $group->getErrors()));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the specified resource from storage.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
|
|
|
* @since [v4.0]
|
|
|
|
* @param int $id
|
|
|
|
*/
|
2024-07-04 23:07:20 -07:00
|
|
|
public function destroy($id) : JsonResponse
|
2017-02-01 18:50:28 -08:00
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
2017-02-01 18:50:28 -08:00
|
|
|
$group = Group::findOrFail($id);
|
|
|
|
$group->delete();
|
|
|
|
|
2021-06-10 13:15:52 -07:00
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/groups/message.delete.success')));
|
2017-02-01 18:50:28 -08:00
|
|
|
}
|
|
|
|
}
|