diff --git a/config/canvas.php b/config/canvas.php index 8027e0632..9e9021c78 100644 --- a/config/canvas.php +++ b/config/canvas.php @@ -2,6 +2,19 @@ return [ + /* + |-------------------------------------------------------------------------- + | Canvas Database Connection + |-------------------------------------------------------------------------- + | + | This is the database connection you want Canvas to use while storing & + | reading your content. By default Canvas uses the default one. However, you can change that + | to anything you want. + | + */ + + 'database_connection' => env('CANVAS_DB_CONNECTION', 'mysql'), + /* |-------------------------------------------------------------------------- | Base Domain diff --git a/database/migrations/2020_09_21_000000_create_canvas_tables.php b/database/migrations/2020_09_21_000000_create_canvas_tables.php index 221cace44..8b823a6d4 100644 --- a/database/migrations/2020_09_21_000000_create_canvas_tables.php +++ b/database/migrations/2020_09_21_000000_create_canvas_tables.php @@ -62,7 +62,7 @@ */ public function up(): void { - Schema::create(self::USERS_TABLE, function (Blueprint $table) { + Schema::connection(config('canvas.database_connection'))->create(self::USERS_TABLE, function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('name'); $table->string('email')->unique(); @@ -84,7 +84,7 @@ public function up(): void $table->softDeletes(); }); - Schema::create(self::TAGS_TABLE, function (Blueprint $table) { + Schema::connection(config('canvas.database_connection'))->create(self::TAGS_TABLE, function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('slug'); $table->string('name'); @@ -99,7 +99,7 @@ public function up(): void $table->unique(['slug', 'user_id']); }); - Schema::create(self::TOPICS_TABLE, function (Blueprint $table) { + Schema::connection(config('canvas.database_connection'))->create(self::TOPICS_TABLE, function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('slug'); $table->string('name'); @@ -114,7 +114,7 @@ public function up(): void $table->unique(['slug', 'user_id']); }); - Schema::create(self::POSTS_TABLE, function (Blueprint $table) { + Schema::connection(config('canvas.database_connection'))->create(self::POSTS_TABLE, function (Blueprint $table) { $table->uuid('id')->primary(); $table->string('slug'); $table->string('title'); @@ -132,14 +132,14 @@ public function up(): void $table->unique(['slug', 'user_id']); }); - Schema::create(self::POSTS_TAGS_TABLE, function (Blueprint $table) { + Schema::connection(config('canvas.database_connection'))->create(self::POSTS_TAGS_TABLE, function (Blueprint $table) { $table->foreignUuid('post_id')->index()->references('id')->on(self::POSTS_TABLE); $table->foreignUuid('tag_id')->index()->references('id')->on(self::TAGS_TABLE); $table->unique(['post_id', 'tag_id']); }); - Schema::create(self::VIEWS_TABLE, function (Blueprint $table) { + Schema::connection(config('canvas.database_connection'))->create(self::VIEWS_TABLE, function (Blueprint $table) { $table->increments('id'); $table->foreignUuid('post_id')->index()->references('id')->on(self::POSTS_TABLE); $table->string('ip')->nullable(); @@ -150,7 +150,7 @@ public function up(): void $table->index('created_at'); }); - Schema::create(self::VISITS_TABLE, function (Blueprint $table) { + Schema::connection(config('canvas.database_connection'))->create(self::VISITS_TABLE, function (Blueprint $table) { $table->increments('id'); $table->foreignUuid('post_id')->references('id')->on(self::POSTS_TABLE); $table->string('ip')->nullable(); @@ -169,12 +169,12 @@ public function up(): void */ public function down(): void { - Schema::dropIfExists(self::USERS_TABLE); - Schema::dropIfExists(self::TAGS_TABLE); - Schema::dropIfExists(self::TOPICS_TABLE); - Schema::dropIfExists(self::POSTS_TABLE); - Schema::dropIfExists(self::POSTS_TAGS_TABLE); - Schema::dropIfExists(self::VIEWS_TABLE); - Schema::dropIfExists(self::VISITS_TABLE); + Schema::connection(config('canvas.database_connection'))->dropIfExists(self::USERS_TABLE); + Schema::connection(config('canvas.database_connection'))->dropIfExists(self::TAGS_TABLE); + Schema::connection(config('canvas.database_connection'))->dropIfExists(self::TOPICS_TABLE); + Schema::connection(config('canvas.database_connection'))->dropIfExists(self::POSTS_TABLE); + Schema::connection(config('canvas.database_connection'))->dropIfExists(self::POSTS_TAGS_TABLE); + Schema::connection(config('canvas.database_connection'))->dropIfExists(self::VIEWS_TABLE); + Schema::connection(config('canvas.database_connection'))->dropIfExists(self::VISITS_TABLE); } }; diff --git a/src/Console/MigrateCommand.php b/src/Console/MigrateCommand.php index 4eefb74b2..83382439d 100644 --- a/src/Console/MigrateCommand.php +++ b/src/Console/MigrateCommand.php @@ -30,6 +30,7 @@ class MigrateCommand extends Command public function handle() { $this->callSilent('migrate', [ + '--database' => config('canvas.database_connection'), '--path' => 'vendor/austintoddj/canvas/database/migrations', '--force' => $this->option('force') ?? true, ]); diff --git a/src/Http/Requests/AuthenticatedSessionRequest.php b/src/Http/Requests/AuthenticatedSessionRequest.php index fe96cef3b..ef58c19ad 100644 --- a/src/Http/Requests/AuthenticatedSessionRequest.php +++ b/src/Http/Requests/AuthenticatedSessionRequest.php @@ -29,8 +29,10 @@ public function rules() { $this->redirect = route('canvas.login.view'); + $connection = config('canvas.database_connection'); + return [ - 'email' => 'required|email:filter|exists:canvas_users', + 'email' => "required|email:filter|exists:{$connection}.canvas_users", 'password' => 'required', ]; } diff --git a/src/Http/Requests/NewPasswordRequest.php b/src/Http/Requests/NewPasswordRequest.php index 7d19bd0b2..3fd8aaa14 100644 --- a/src/Http/Requests/NewPasswordRequest.php +++ b/src/Http/Requests/NewPasswordRequest.php @@ -25,9 +25,11 @@ public function authorize() */ public function rules() { + $connection = config('canvas.database_connection'); + return [ 'token' => 'required', - 'email' => 'required|email:filter|exists:canvas_users', + 'email' => "required|email:filter|exists:{$connection}.canvas_users", 'password' => 'required|confirmed|min:8', ]; } diff --git a/src/Http/Requests/PasswordResetLinkRequest.php b/src/Http/Requests/PasswordResetLinkRequest.php index 2a07154d2..ced02e44d 100644 --- a/src/Http/Requests/PasswordResetLinkRequest.php +++ b/src/Http/Requests/PasswordResetLinkRequest.php @@ -25,8 +25,10 @@ public function authorize() */ public function rules() { + $connection = config('canvas.database_connection'); + return [ - 'email' => 'required|email:filter|exists:canvas_users', + 'email' => "required|email:filter|exists:{$connection}.canvas_users", ]; } diff --git a/src/Http/Requests/StorePostRequest.php b/src/Http/Requests/StorePostRequest.php index 8010ffb55..bd7075bf1 100644 --- a/src/Http/Requests/StorePostRequest.php +++ b/src/Http/Requests/StorePostRequest.php @@ -34,11 +34,13 @@ public function authorize() */ public function rules() { + $connection = config('canvas.database_connection'); + return [ 'slug' => [ 'required', 'alpha_dash', - Rule::unique('canvas_posts')->where(function (Builder $query) { + Rule::unique("{$connection}.canvas_posts")->where(function (Builder $query) { return $query->where('slug', request('slug'))->where('user_id', request()->user('canvas')->id); })->ignore($this->route('id'))->whereNull('deleted_at'), ], diff --git a/src/Http/Requests/StoreTagRequest.php b/src/Http/Requests/StoreTagRequest.php index 20891850c..519584ed0 100644 --- a/src/Http/Requests/StoreTagRequest.php +++ b/src/Http/Requests/StoreTagRequest.php @@ -27,12 +27,14 @@ public function authorize() */ public function rules() { + $connection = config('canvas.database_connection'); + return [ 'name' => 'required|string', 'slug' => [ 'required', 'alpha_dash', - Rule::unique('canvas_tags')->where(function (Builder $query) { + Rule::unique("{$connection}.canvas_tags")->where(function (Builder $query) { return $query->where('slug', request('slug'))->where('user_id', request()->user('canvas')->id); })->ignore($this->route('id'))->whereNull('deleted_at'), ], diff --git a/src/Http/Requests/StoreTopicRequest.php b/src/Http/Requests/StoreTopicRequest.php index 61fac79d7..62ac0c6f7 100644 --- a/src/Http/Requests/StoreTopicRequest.php +++ b/src/Http/Requests/StoreTopicRequest.php @@ -27,12 +27,14 @@ public function authorize() */ public function rules() { + $connection = config('canvas.database_connection'); + return [ 'name' => 'required|string', 'slug' => [ 'required', 'alpha_dash', - Rule::unique('canvas_topics')->where(function (Builder $query) { + Rule::unique("{$connection}.canvas_topics")->where(function (Builder $query) { return $query->where('slug', request('slug'))->where('user_id', request()->user('canvas')->id); })->ignore($this->route('id'))->whereNull('deleted_at'), ], diff --git a/src/Http/Requests/StoreUserRequest.php b/src/Http/Requests/StoreUserRequest.php index 62c6feaa0..c9a66fb32 100644 --- a/src/Http/Requests/StoreUserRequest.php +++ b/src/Http/Requests/StoreUserRequest.php @@ -27,19 +27,21 @@ public function authorize() */ public function rules() { + $connection = config('canvas.database_connection'); + return [ 'name' => 'required|string', 'email' => [ 'required', 'email:filter', - Rule::unique('canvas_users')->where(function (Builder $query) { + Rule::unique("{$connection}.canvas_users")->where(function (Builder $query) { return $query->where('email', request('email')); })->ignore($this->route('id'))->whereNull('deleted_at'), ], 'username' => [ 'nullable', 'alpha_dash', - Rule::unique('canvas_users')->where(function (Builder $query) { + Rule::unique("{$connection}.canvas_users")->where(function (Builder $query) { return $query->where('username', request('username')); })->ignore($this->route('id'))->whereNull('deleted_at'), ], diff --git a/src/Models/AbstractCanvasModel.php b/src/Models/AbstractCanvasModel.php new file mode 100644 index 000000000..5432ec89c --- /dev/null +++ b/src/Models/AbstractCanvasModel.php @@ -0,0 +1,20 @@ +getKeyName(); + } + + /** + * Get the unique identifier for the user. + * + * @return mixed + */ + public function getAuthIdentifier() + { + return $this->{$this->getAuthIdentifierName()}; + } + + /** + * Get the password for the user. + * + * @return string + */ + public function getAuthPassword() + { + return $this->password; + } + + /** + * Get the token value for the "remember me" session. + * + * @return string|null + */ + public function getRememberToken() + { + if (! empty($this->getRememberTokenName())) { + return (string) $this->{$this->getRememberTokenName()}; + } + } + + /** + * Set the token value for the "remember me" session. + * + * @param string $value + * @return void + */ + public function setRememberToken($value) + { + if (! empty($this->getRememberTokenName())) { + $this->{$this->getRememberTokenName()} = $value; + } + } + + /** + * Get the column name for the "remember me" token. + * + * @return string + */ + public function getRememberTokenName() + { + return $this->rememberTokenName; + } + /** * Create a new factory instance for the model. * diff --git a/src/Models/View.php b/src/Models/View.php index ba66a83f8..1efb89454 100644 --- a/src/Models/View.php +++ b/src/Models/View.php @@ -6,9 +6,8 @@ use Canvas\Database\Factories\ViewFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; -class View extends Model +class View extends AbstractCanvasModel { use HasFactory; diff --git a/src/Models/Visit.php b/src/Models/Visit.php index f09a145eb..231bda327 100644 --- a/src/Models/Visit.php +++ b/src/Models/Visit.php @@ -6,9 +6,8 @@ use Canvas\Database\Factories\VisitFactory; use Illuminate\Database\Eloquent\Factories\HasFactory; -use Illuminate\Database\Eloquent\Model; -class Visit extends Model +class Visit extends AbstractCanvasModel { use HasFactory;