Skip to content

A free open source IT asset/license management system

Added created_by to groups table

snipe0bd27d61b016
diff --git a/app/Http/Controllers/Api/GroupsController.php b/app/Http/Controllers/Api/GroupsController.php
index 8548af0ba5..1d663c777a 100644
--- a/app/Http/Controllers/Api/GroupsController.php
+++ b/app/Http/Controllers/Api/GroupsController.php
@@ -25,7 +25,7 @@ class GroupsController extends Controller
$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'));
@@ -63,6 +63,7 @@ class GroupsController extends Controller
$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()) {
diff --git a/app/Http/Controllers/GroupsController.php b/app/Http/Controllers/GroupsController.php
index b98156824a..544ebb34ed 100755
--- a/app/Http/Controllers/GroupsController.php
+++ b/app/Http/Controllers/GroupsController.php
@@ -5,6 +5,7 @@ namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\Group;
use Illuminate\Http\Request;
+use Auth;
/**
* This controller handles all actions related to User Groups for
@@ -63,6 +64,7 @@ class GroupsController extends Controller
$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'));
diff --git a/app/Http/Transformers/GroupsTransformer.php b/app/Http/Transformers/GroupsTransformer.php
index 81755afa43..bf7e2bfd70 100644
--- a/app/Http/Transformers/GroupsTransformer.php
+++ b/app/Http/Transformers/GroupsTransformer.php
@@ -26,6 +26,7 @@ class GroupsTransformer
'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'),
];
diff --git a/app/Models/Group.php b/app/Models/Group.php
index c0de8c263d..5e0db1c91e 100755
--- a/app/Models/Group.php
+++ b/app/Models/Group.php
@@ -58,6 +58,18 @@ class Group extends SnipeModel
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
*
diff --git a/database/migrations/2024_02_28_080016_add_created_by_to_permission_groups.php b/database/migrations/2024_02_28_080016_add_created_by_to_permission_groups.php
new file mode 100644
index 0000000000..5b437a0e3d
--- /dev/null
+++ b/database/migrations/2024_02_28_080016_add_created_by_to_permission_groups.php
@@ -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');
+ }
+ });
+ }
+}
diff --git a/resources/views/groups/index.blade.php b/resources/views/groups/index.blade.php
index 4b2a2f0e32..49d7d768b9 100755
--- a/resources/views/groups/index.blade.php
+++ b/resources/views/groups/index.blade.php
@@ -45,7 +45,9 @@
<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>