Fix table names are globally unique instead of per user

This commit is contained in:
2018-08-13 00:10:10 +02:00
parent c76fc21820
commit 7f48d14ce3
2 changed files with 9 additions and 4 deletions
+7 -3
View File
@@ -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\"",
]);
}
+2 -1
View File
@@ -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();
}
}