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\Http\Request;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
use MightyPork\Exceptions\NotApplicableException; use MightyPork\Exceptions\NotApplicableException;
use MightyPork\Exceptions\SimpleValidationException;
use MightyPork\Utils\Utils; use MightyPork\Utils\Utils;
class TableController extends Controller class TableController extends Controller
@ -131,7 +132,6 @@ class TableController extends Controller
'name' => [ 'name' => [
'required', 'required',
VALI_NAME, VALI_NAME,
Rule::unique('tables')->ignoreModel($tableModel),
], ],
'title' => ['required', VALI_LINE], 'title' => ['required', VALI_LINE],
'description' => ['nullable', VALI_TEXT], 'description' => ['nullable', VALI_TEXT],
@ -139,6 +139,11 @@ class TableController extends Controller
'origin' => ['nullable', VALI_TEXT], '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->fill($input->all());
$tableModel->save(); $tableModel->save();
@ -156,7 +161,6 @@ class TableController extends Controller
'name' => [ 'name' => [
'required', 'required',
VALI_NAME, VALI_NAME,
Rule::unique('tables'),
], ],
'title' => ['required', VALI_LINE], 'title' => ['required', VALI_LINE],
'description' => ['nullable', VALI_TEXT], 'description' => ['nullable', VALI_TEXT],
@ -169,7 +173,7 @@ class TableController extends Controller
// Check if table name is unique for user // Check if table name is unique for user
if ($u->hasTable($input->name)) { if ($u->hasTable($input->name)) {
return $this->backWithErrors([ 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 * Check if this user has a table with the givenname
* *
* @param string $name * @param string $name
* @return bool
*/ */
public function hasTable(string $name) public function hasTable(string $name)
{ {
$this->tables()->where('name', $name)->exists(); return $this->tables()->where('name', $name)->exists();
} }
} }

Loading…
Cancel
Save