Skip to content

Commit

Permalink
Merge pull request #14344 from snipe/features/added_created_by_to_groups
Browse files Browse the repository at this point in the history
Added created_by to groups table
  • Loading branch information
snipe authored Feb 28, 2024
2 parents fcb2bf7 + a059a42 commit 1e66985
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
4 changes: 3 additions & 1 deletion app/Http/Controllers/Api/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Transformers\GroupsTransformer;
use App\Models\Group;
use Illuminate\Http\Request;
use Auth;


class GroupsController extends Controller
Expand All @@ -25,7 +26,7 @@ public function index(Request $request)
$this->authorize('view', Group::class);
$allowed_columns = ['id', 'name', 'created_at', 'users_count'];

$groups = Group::select('id', 'name', 'permissions', 'created_at', 'updated_at')->withCount('users as users_count');
$groups = Group::select('id', 'name', 'permissions', 'created_at', 'updated_at', 'created_by')->with('admin')->withCount('users as users_count');

if ($request->filled('search')) {
$groups = $groups->TextSearch($request->input('search'));
Expand Down Expand Up @@ -63,6 +64,7 @@ public function store(Request $request)
$group = new Group;

$group->name = $request->input('name');
$group->created_by = Auth::user()->id;
$group->permissions = json_encode($request->input('permissions')); // Todo - some JSON validation stuff here

if ($group->save()) {
Expand Down
2 changes: 2 additions & 0 deletions app/Http/Controllers/GroupsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Helpers\Helper;
use App\Models\Group;
use Illuminate\Http\Request;
use Auth;

/**
* This controller handles all actions related to User Groups for
Expand Down Expand Up @@ -63,6 +64,7 @@ public function store(Request $request)
$group = new Group();
$group->name = $request->input('name');
$group->permissions = json_encode($request->input('permission'));
$group->created_by = Auth::user()->id;

if ($group->save()) {
return redirect()->route('groups.index')->with('success', trans('admin/groups/message.success.create'));
Expand Down
1 change: 1 addition & 0 deletions app/Http/Transformers/GroupsTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function transformGroup(Group $group)
'name' => e($group->name),
'permissions' => json_decode($group->permissions),
'users_count' => (int) $group->users_count,
'created_by' => ($group->admin) ? e($group->admin->present()->fullName) : null,
'created_at' => Helper::getFormattedDateObject($group->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($group->updated_at, 'datetime'),
];
Expand Down
12 changes: 12 additions & 0 deletions app/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public function users()
return $this->belongsToMany(\App\Models\User::class, 'users_groups');
}

/**
* Get the user that created the group
*
* @author A. Gianotto <[email protected]>
* @since [v6.3.0]
* @return \Illuminate\Database\Eloquent\Relations\Relation
*/
public function admin()
{
return $this->belongsTo(\App\Models\User::class, 'created_by');
}

/**
* Decode JSON permissions into array
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddCreatedByToPermissionGroups extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('permission_groups', function (Blueprint $table) {
$table->integer('created_by')->nullable()->default(null);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('permission_groups', function (Blueprint $table) {
if (Schema::hasColumn('permission_groups', 'created_by')) {
$table->dropColumn('created_by');
}
});
}
}
2 changes: 2 additions & 0 deletions resources/views/groups/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class="table table-striped snipe-table"
<th data-switchable="true" data-sortable="true" data-field="name" data-formatter="groupsAdminLinkFormatter" data-visible="true">{{ trans('admin/groups/table.name') }}</th>
<th data-switchable="true" data-sortable="true" data-field="users_count" data-visible="true"><i class="fas fa-user" aria-hidden="true"></i><span class="sr-only">{{ trans('admin/groups/table.users') }}</span></th>
<th data-switchable="true" data-sortable="true" data-field="created_at" data-visible="true" data-formatter="dateDisplayFormatter">{{ trans('general.created_at') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="created_by" data-formatter="usersLinkFormatter">{{ trans('general.created_by') }}</th>
<th data-switchable="false" data-searchable="false" data-sortable="false" data-field="actions" data-formatter="groupsActionsFormatter">{{ trans('table.actions') }}</th>

</tr>
</thead>
</table>
Expand Down

0 comments on commit 1e66985

Please sign in to comment.