Fix table names are globally unique instead of per user

master
Ondřej Hruška 6 years ago
parent c76fc21820
commit 7f48d14ce3
Signed by: MightyPork
GPG Key ID: 2C5FD5035250423D
  1. 10
      app/Http/Controllers/TableController.php
  2. 3
      app/Models/User.php

@ -18,6 +18,7 @@ use DB;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
use MightyPork\Exceptions\NotApplicableException;
use MightyPork\Exceptions\SimpleValidationException;
use MightyPork\Utils\Utils;
class TableController extends Controller
@ -131,7 +132,6 @@ class TableController extends Controller
'name' => [
'required',
VALI_NAME,
Rule::unique('tables')->ignoreModel($tableModel),
],
'title' => ['required', VALI_LINE],
'description' => ['nullable', VALI_TEXT],
@ -139,6 +139,11 @@ class TableController extends Controller
'origin' => ['nullable', VALI_TEXT],
]);
$otherTableNames = $user->tables()->get(['name'])->pluck('name')->diff([$table])->all();
if (in_array($input->name, $otherTableNames)) {
return $this->backWithErrors(['name' => "You already have a table called \"$input->name\""]);
}
$tableModel->fill($input->all());
$tableModel->save();
@ -156,7 +161,6 @@ class TableController extends Controller
'name' => [
'required',
VALI_NAME,
Rule::unique('tables'),
],
'title' => ['required', VALI_LINE],
'description' => ['nullable', VALI_TEXT],
@ -169,7 +173,7 @@ class TableController extends Controller
// Check if table name is unique for user
if ($u->hasTable($input->name)) {
return $this->backWithErrors([
'name' => "A table called \"$input->name\" already exists in your account.",
'name' => "You already have a table called \"$input->name\"",
]);
}

@ -231,9 +231,10 @@ class User extends BaseModel implements
* Check if this user has a table with the givenname
*
* @param string $name
* @return bool
*/
public function hasTable(string $name)
{
$this->tables()->where('name', $name)->exists();
return $this->tables()->where('name', $name)->exists();
}
}

Loading…
Cancel
Save