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;
|
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]
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function index(Request $request)
|
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
|
|
|
|
2017-02-01 18:50:28 -08:00
|
|
|
$this->authorize('view', Group::class);
|
2021-06-10 13:15:52 -07:00
|
|
|
$allowed_columns = ['id', 'name', 'created_at', 'users_count'];
|
2017-02-01 18:50:28 -08:00
|
|
|
|
2024-02-28 00:15:59 -08:00
|
|
|
$groups = Group::select('id', 'name', 'permissions', 'created_at', 'updated_at', 'created_by')->with('admin')->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'));
|
|
|
|
}
|
|
|
|
|
2023-04-12 11:28:46 -07:00
|
|
|
// Make sure the offset and limit are actually integers and do not exceed system limits
|
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');
|
2019-09-03 14:02:08 -07:00
|
|
|
|
2017-02-01 18:50:28 -08:00
|
|
|
$order = $request->input('order') === 'asc' ? 'asc' : 'desc';
|
|
|
|
$sort = in_array($request->input('sort'), $allowed_columns) ? $request->input('sort') : 'created_at';
|
|
|
|
$groups->orderBy($sort, $order);
|
|
|
|
|
|
|
|
$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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function store(Request $request)
|
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
2017-02-01 18:50:28 -08:00
|
|
|
$group = new Group;
|
2023-03-18 11:53:20 -07:00
|
|
|
|
|
|
|
$group->name = $request->input('name');
|
2024-02-28 00:15:59 -08:00
|
|
|
$group->created_by = Auth::user()->id;
|
2023-10-10 17:51:29 -07:00
|
|
|
$group->permissions = json_encode($request->input('permissions')); // Todo - some JSON validation stuff here
|
2017-02-01 18:50:28 -08:00
|
|
|
|
|
|
|
if ($group->save()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $group, trans('admin/groups/message.create.success')));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
2023-03-18 11:53:20 -07:00
|
|
|
$this->authorize('superadmin');
|
2017-02-01 18:50:28 -08:00
|
|
|
$group = Group::findOrFail($id);
|
2021-06-10 13:15:52 -07:00
|
|
|
|
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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function update(Request $request, $id)
|
|
|
|
{
|
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()) {
|
|
|
|
return response()->json(Helper::formatStandardApiResponse('success', $group, trans('admin/groups/message.update.success')));
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
* @return \Illuminate\Http\Response
|
|
|
|
*/
|
|
|
|
public function destroy($id)
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|