Skip to content

Commit

Permalink
Fix issue where role null never allowed a category to be created
Browse files Browse the repository at this point in the history
  • Loading branch information
rowasc committed Jun 17, 2020
1 parent 6891e3c commit 48edfb6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
7 changes: 3 additions & 4 deletions v4/Http/Controllers/CategoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function store(Request $request)
return response()->json($category->errors, 422);
}

$category = DB::transaction(function() use ($id, $input, $request, $category) {
$category = DB::transaction(function () use ($id, $input, $request, $category) {
$category = Category::create(
array_merge(
$input,
Expand Down Expand Up @@ -160,7 +160,7 @@ public function update(int $id, Request $request)
if (!$category->validate($input)) {
return response()->json($category->errors, 422);
}
$category = DB::transaction(function() use ($id, $input, $request, $category) {
$category = DB::transaction(function () use ($id, $input, $request, $category) {
$category->update($request->input());
$this->updateTranslations($request->input('translations'), $category->id, 'category');
return $category;
Expand Down Expand Up @@ -209,7 +209,7 @@ public function delete(int $id, Request $request)
{
$category = Category::find($id);
$this->authorize('delete', $category);
$success = DB::transaction(function() use ($id, $request, $category) {
$success = DB::transaction(function () use ($id, $request, $category) {
$category->translations()->delete();
$success = $category->delete();
return $success;
Expand All @@ -227,6 +227,5 @@ public function delete(int $id, Request $request)
500
);
}

}//end delete()
}//end class
21 changes: 12 additions & 9 deletions v4/Models/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Validator;
use Ushahidi\Core\Entity\Permission;
use Illuminate\Support\Facades\Input;

class Category extends Model
{
public $errors;
Expand Down Expand Up @@ -201,16 +202,15 @@ public function getRules()
'numeric'
],
'role' => [
Rule::exists('roles', 'name'),
function($attribute, $value, $fail) {
$has_parent = Input::get('parent_id'); // Retrieve status
function ($attribute, $value, $fail) {
$has_parent = Input::get('parent_id'); // Retrieve status

$parent = $has_parent ? Category::find(Input::get('parent_id')) : null;
// ... and check if the role matches its parent
if ($parent && $parent->role != $value) {
return $fail(trans('validation.child_parent_role_match'));
}
}
$parent = $has_parent ? Category::find(Input::get('parent_id')) : null;
// ... and check if the role matches its parent
if ($parent && $parent->role != $value) {
return $fail(trans('validation.child_parent_role_match'));
}
}
]
];
}//end validationMessages()
Expand Down Expand Up @@ -367,6 +367,9 @@ public static function makeSlug($value)
public function validate($data)
{
$v = Validator::make($data, $this->getRules(), self::validationMessages());
$v->sometimes('role', 'exists:roles,name', function ($input) {
return !!$input;
});
// check for failure
if (!$v->fails()) {
return true;
Expand Down

0 comments on commit 48edfb6

Please sign in to comment.