From 0bd27d61b01641e2e3da42f896ed99fa531db596 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 28 Feb 2024 08:15:59 +0000 Subject: [PATCH 1/2] Added created_by to groups table Signed-off-by: snipe --- app/Http/Controllers/Api/GroupsController.php | 3 +- app/Http/Controllers/GroupsController.php | 2 ++ app/Http/Transformers/GroupsTransformer.php | 1 + app/Models/Group.php | 12 +++++++ ...16_add_created_by_to_permission_groups.php | 34 +++++++++++++++++++ resources/views/groups/index.blade.php | 2 ++ 6 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2024_02_28_080016_add_created_by_to_permission_groups.php 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 + * @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 @@ +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 @@ {{ trans('admin/groups/table.name') }} {{ trans('admin/groups/table.users') }} {{ trans('general.created_at') }} + {{ trans('general.created_by') }} {{ trans('table.actions') }} + From a059a42799b330a9863a7d6d4d64f0a9c1f768d8 Mon Sep 17 00:00:00 2001 From: snipe Date: Wed, 28 Feb 2024 11:14:48 +0000 Subject: [PATCH 2/2] Added use Auth to API controller Signed-off-by: snipe --- app/Http/Controllers/Api/GroupsController.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Http/Controllers/Api/GroupsController.php b/app/Http/Controllers/Api/GroupsController.php index 1d663c777a..5a441e41ce 100644 --- a/app/Http/Controllers/Api/GroupsController.php +++ b/app/Http/Controllers/Api/GroupsController.php @@ -7,6 +7,7 @@ use App\Http\Controllers\Controller; use App\Http\Transformers\GroupsTransformer; use App\Models\Group; use Illuminate\Http\Request; +use Auth; class GroupsController extends Controller