2016-03-25 01:18:05 -07:00
|
|
|
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2016-04-28 20:57:02 -07:00
|
|
|
use App\Helpers\Helper;
|
2019-03-13 20:12:03 -07:00
|
|
|
use App\Models\Group;
|
2019-12-11 11:26:58 -08:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-10 22:05:49 -08:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
|
2016-04-07 13:21:09 -07:00
|
|
|
/**
|
|
|
|
* This controller handles all actions related to User Groups for
|
|
|
|
* the Snipe-IT Asset Management application.
|
|
|
|
*
|
|
|
|
* @version v1.0
|
|
|
|
*/
|
2016-03-25 01:18:05 -07:00
|
|
|
class GroupsController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
2016-04-07 13:21:09 -07:00
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the user group listing, which is generated in getDatatable.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net]
|
|
|
|
* @see GroupsController::getDatatable() method that generates the JSON response
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2017-06-08 20:24:26 -07:00
|
|
|
public function index()
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// Show the page
|
2018-12-12 18:16:39 -08:00
|
|
|
return view('groups/index');
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:21:09 -07:00
|
|
|
* Returns a view that displays a form to create a new User Group.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net]
|
|
|
|
* @see GroupsController::postCreate()
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2019-12-11 11:26:58 -08:00
|
|
|
public function create(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-03-25 19:26:22 -07:00
|
|
|
$group = new Group;
|
2016-03-25 01:18:05 -07:00
|
|
|
// Get all the available permissions
|
|
|
|
$permissions = config('permissions');
|
2017-12-05 18:17:11 -08:00
|
|
|
$groupPermissions = Helper::selectedPermissionsArray($permissions, $permissions);
|
2019-12-11 11:26:58 -08:00
|
|
|
$selectedPermissions = $request->old('permissions', $groupPermissions);
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
// Show the page
|
2017-06-09 16:44:03 -07:00
|
|
|
return view('groups/edit', compact('permissions', 'selectedPermissions', 'groupPermissions'))->with('group', $group);
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:21:09 -07:00
|
|
|
* Validates and stores the new User Group data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net]
|
|
|
|
* @see GroupsController::getCreate()
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2019-12-11 11:09:54 -08:00
|
|
|
public function store(Request $request)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
// create a new group instance
|
2016-03-25 19:26:22 -07:00
|
|
|
$group = new Group();
|
2019-12-11 11:09:54 -08:00
|
|
|
$group->name = $request->input('name');
|
|
|
|
$group->permissions = json_encode($request->input('permission'));
|
2016-03-25 01:18:05 -07:00
|
|
|
|
|
|
|
if ($group->save()) {
|
2017-02-01 18:50:28 -08:00
|
|
|
return redirect()->route("groups.index")->with('success', trans('admin/groups/message.success.create'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2019-01-24 15:21:38 -08:00
|
|
|
return redirect()->back()->withInput()->withErrors($group->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:21:09 -07:00
|
|
|
* Returns a view that presents a form to edit a User Group.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net]
|
|
|
|
* @see GroupsController::postEdit()
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
|
|
|
*/
|
2017-12-05 16:49:26 -08:00
|
|
|
public function edit($id)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-03-25 19:26:22 -07:00
|
|
|
$group = Group::find($id);
|
2017-10-17 21:43:57 -07:00
|
|
|
|
|
|
|
if ($group) {
|
|
|
|
$permissions = config('permissions');
|
|
|
|
$groupPermissions = $group->decodePermissions();
|
|
|
|
$selected_array = Helper::selectedPermissionsArray($permissions, $groupPermissions);
|
|
|
|
return view('groups.edit', compact('group', 'permissions', 'selected_array', 'groupPermissions'));
|
|
|
|
}
|
|
|
|
|
2017-12-05 16:49:26 -08:00
|
|
|
return redirect()->route('groups.index')->with('error', trans('admin/groups/message.group_not_found'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-04-07 13:21:09 -07:00
|
|
|
* Validates and stores the updated User Group data.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net]
|
|
|
|
* @see GroupsController::getEdit()
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.0]
|
2016-12-19 22:00:50 -08:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2019-12-11 11:09:54 -08:00
|
|
|
public function update(Request $request, $id = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
2016-03-25 19:26:22 -07:00
|
|
|
if (!$group = Group::find($id)) {
|
2019-01-24 14:56:47 -08:00
|
|
|
return redirect()->route('groups.index')->with('error', trans('admin/groups/message.group_not_found', compact('id')));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2019-12-11 11:09:54 -08:00
|
|
|
$group->name = $request->input('name');
|
|
|
|
$group->permissions = json_encode($request->input('permission'));
|
2016-04-28 14:03:54 -07:00
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
if (!config('app.lock_passwords')) {
|
|
|
|
if ($group->save()) {
|
2017-06-08 20:24:26 -07:00
|
|
|
return redirect()->route('groups.index')->with('success', trans('admin/groups/message.success.update'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-04-28 21:06:41 -07:00
|
|
|
return redirect()->back()->withInput()->withErrors($group->getErrors());
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-06-08 20:24:26 -07:00
|
|
|
return redirect()->route('groups.index')->with('error', trans('general.feature_disabled'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-24 19:35:26 -07:00
|
|
|
* Validates and deletes the User Group.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net]
|
|
|
|
* @see GroupsController::getEdit()
|
|
|
|
* @param int $id
|
|
|
|
* @since [v1.0]
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
* @throws \Exception
|
2016-12-19 22:00:50 -08:00
|
|
|
*/
|
2017-06-08 20:24:26 -07:00
|
|
|
public function destroy($id = null)
|
2016-03-25 01:18:05 -07:00
|
|
|
{
|
|
|
|
if (!config('app.lock_passwords')) {
|
2016-12-19 22:00:50 -08:00
|
|
|
if (!$group = Group::find($id)) {
|
2019-01-24 15:21:38 -08:00
|
|
|
return redirect()->route('groups.index')->with('error', trans('admin/groups/message.group_not_found', compact('id')));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2016-12-19 22:00:50 -08:00
|
|
|
$group->delete();
|
|
|
|
// Redirect to the group management page
|
2017-06-08 20:24:26 -07:00
|
|
|
return redirect()->route('groups.index')->with('success', trans('admin/groups/message.success.delete'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
2017-06-08 20:24:26 -07:00
|
|
|
return redirect()->route('groups.index')->with('error', trans('general.feature_disabled'));
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|
|
|
|
|
2017-10-17 21:43:57 -07:00
|
|
|
/**
|
|
|
|
* Returns a view that invokes the ajax tables which actually contains
|
|
|
|
* the content for the group detail page.
|
|
|
|
*
|
|
|
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
2018-07-24 19:35:26 -07:00
|
|
|
* @param $id
|
2017-10-17 21:43:57 -07:00
|
|
|
* @return \Illuminate\Contracts\View\View
|
2018-07-24 19:35:26 -07:00
|
|
|
* @since [v4.0.11]
|
2017-10-17 21:43:57 -07:00
|
|
|
*/
|
|
|
|
public function show($id)
|
|
|
|
{
|
|
|
|
$group = Group::find($id);
|
|
|
|
|
|
|
|
if ($group) {
|
|
|
|
return view('groups/view', compact('group'));
|
|
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('groups.index')->with('error', trans('admin/groups/message.group_not_found', compact('id')));
|
|
|
|
}
|
|
|
|
|
2016-03-25 01:18:05 -07:00
|
|
|
}
|