Skip to content

A free open source IT asset/license management system

Fixed #18148 and #17451 - return int for user_count, fixed validation

snipeddb031f09123
diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php
index adbe4bc35c..92008bddef 100644
--- a/app/Http/Controllers/Api/DepartmentsController.php
+++ b/app/Http/Controllers/Api/DepartmentsController.php
@@ -27,18 +27,19 @@ class DepartmentsController extends Controller
$allowed_columns = ['id', 'name', 'image', 'users_count', 'notes'];
$departments = Department::select(
- 'departments.id',
- 'departments.name',
- 'departments.phone',
- 'departments.fax',
- 'departments.location_id',
- 'departments.company_id',
- 'departments.manager_id',
- 'departments.created_at',
- 'departments.updated_at',
- 'departments.image',
- 'departments.notes',
- )->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');
+ [
+ 'departments.id',
+ 'departments.name',
+ 'departments.phone',
+ 'departments.fax',
+ 'departments.location_id',
+ 'departments.company_id',
+ 'departments.manager_id',
+ 'departments.created_at',
+ 'departments.updated_at',
+ 'departments.image',
+ 'departments.notes'
+ ])->with('users')->with('location')->with('manager')->with('company')->withCount('users as users_count');
if ($request->filled('search')) {
$departments = $departments->TextSearch($request->input('search'));
@@ -105,7 +106,7 @@ class DepartmentsController extends Controller
$department->manager_id = ($request->filled('manager_id') ? $request->input('manager_id') : null);
if ($department->save()) {
- return response()->json(Helper::formatStandardApiResponse('success', $department, trans('admin/departments/message.create.success')));
+ return response()->json(Helper::formatStandardApiResponse('success', (new DepartmentsTransformer)->transformDepartment($department), trans('admin/departments/message.create.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, $department->getErrors()));
@@ -121,7 +122,7 @@ class DepartmentsController extends Controller
public function show($id) : array
{
$this->authorize('view', Department::class);
- $department = Department::findOrFail($id);
+ $department = Department::withCount('users as users_count')->findOrFail($id);
return (new DepartmentsTransformer)->transformDepartment($department);
}
diff --git a/app/Http/Transformers/DepartmentsTransformer.php b/app/Http/Transformers/DepartmentsTransformer.php
index e072585a12..267413e104 100644
--- a/app/Http/Transformers/DepartmentsTransformer.php
+++ b/app/Http/Transformers/DepartmentsTransformer.php
@@ -43,7 +43,7 @@ class DepartmentsTransformer
'id' => (int) $department->location->id,
'name' => e($department->location->name),
] : null,
- 'users_count' => e($department->users_count),
+ 'users_count' => (int) ($department->users_count),
'notes' => Helper::parseEscapedMarkedownInline($department->notes),
'created_at' => Helper::getFormattedDateObject($department->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($department->updated_at, 'datetime'),
diff --git a/app/Models/Department.php b/app/Models/Department.php
index 4fc72e474f..fda461bdad 100644
--- a/app/Models/Department.php
+++ b/app/Models/Department.php
@@ -31,7 +31,7 @@ class Department extends SnipeModel
];
protected $rules = [
- 'name' => 'required|max:255|is_unique_department',
+ 'name' => 'required|max:255|is_unique_across_company_and_location:departments,name',
'location_id' => 'numeric|nullable|exists:locations,id',
'company_id' => 'numeric|nullable|exists:companies,id',
'manager_id' => 'numeric|nullable|exists:users,id',
diff --git a/app/Providers/ValidationServiceProvider.php b/app/Providers/ValidationServiceProvider.php
index 13ee6c5729..26bc9ada07 100644
--- a/app/Providers/ValidationServiceProvider.php
+++ b/app/Providers/ValidationServiceProvider.php
@@ -283,41 +283,36 @@ class ValidationServiceProvider extends ServiceProvider
}
});
- Validator::extend('is_unique_department', function ($attribute, $value, $parameters, $validator) {
+ /**
+ * Check that the 'name' field is unique in the table while within both company_id and location_id
+ * This is only used by Departments right now, but could be used elsewhere in the future.
+ */
+ Validator::extend('is_unique_across_company_and_location', function ($attribute, $value, $parameters, $validator) {
$data = $validator->getData();
+ $table = array_get($parameters, 0);
- if (
- array_key_exists('location_id', $data) && $data['location_id'] !== null &&
- array_key_exists('company_id', $data) && $data['company_id'] !== null
- ) {
- //for updating existing departments
- if(array_key_exists('id', $data) && $data['id'] !== null){
- $count = Department::where('name', $data['name'])
- ->where('location_id', $data['location_id'])
- ->where('company_id', $data['company_id'])
- ->whereNotNull('company_id')
- ->whereNotNull('location_id')
- ->where('id', '!=', $data['id'])
- ->count('name');
-
- return $count < 1;
- }else // for entering in new departments
- {
- $count = Department::where('name', $data['name'])
- ->where('location_id', $data['location_id'])
- ->where('company_id', $data['company_id'])
- ->whereNotNull('company_id')
- ->whereNotNull('location_id')
- ->count('name');
+ $count = DB::table($table)->select($attribute)
+ ->where($attribute, $value)
+ ->whereNull('deleted_at');
- return $count < 1;
+ if (array_key_exists('id', $data) && $data['id'] !== null) {
+ $count = $count->where('id', '!=', $data['id']);
}
- }
- else {
- return true;
- }
+
+ if (array_key_exists('location_id', $data) && $data['location_id'] !== null) {
+ $count = $count->where('location_id', $data['location_id']);
+ }
+
+ if (array_key_exists('company_id', $data) && $data['company_id'] !== null) {
+ $count = $count->where('company_id', $data['company_id']);
+ }
+
+ $count = $count->count('name');
+ return $count < 1;
+
});
+
Validator::extend('not_array', function ($attribute, $value, $parameters, $validator) {
return !is_array($value);
});
diff --git a/resources/lang/en-US/validation.php b/resources/lang/en-US/validation.php
index f80dd70163..2d4af64fb4 100644
--- a/resources/lang/en-US/validation.php
+++ b/resources/lang/en-US/validation.php
@@ -174,7 +174,7 @@ return [
'ulid' => 'The :attribute field must be a valid ULID.',
'uuid' => 'The :attribute field must be a valid UUID.',
'fmcs_location' => 'Full multiple company support and location scoping is enabled in the Admin Settings, and the selected location and selected company are not compatible.',
-
+ 'is_unique_across_company_and_location' => 'The :attribute must be unique within the selected company and location.',
/*
|--------------------------------------------------------------------------