@@ -2,7 +2,9 @@
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\FolderVisibility;
|
||||
use App\Models\Folder;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
@@ -36,4 +38,36 @@ class FolderFactory extends Factory
|
||||
'user_id' => $folder->user_id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the folder sits in the given organization, inheriting the
|
||||
* visibility the workspace defaults to.
|
||||
*/
|
||||
public function inOrganization(Organization $organization): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'organization_id' => $organization->id,
|
||||
'visibility' => Folder::defaultVisibilityFor($organization->id),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the folder is shared with everyone in its workspace.
|
||||
*/
|
||||
public function sharedWithWorkspace(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'visibility' => FolderVisibility::Workspace,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the folder is only visible to people it is shared with.
|
||||
*/
|
||||
public function private(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'visibility' => FolderVisibility::Private,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\FolderRole;
|
||||
use App\Models\Folder;
|
||||
use App\Models\Invitation;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
/**
|
||||
* @extends Factory<Invitation>
|
||||
*/
|
||||
class InvitationFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'inviter_id' => User::factory(),
|
||||
'email' => fake()->unique()->safeEmail(),
|
||||
'role' => FolderRole::Viewer->value,
|
||||
'inviteable_type' => Folder::class,
|
||||
'inviteable_id' => Folder::factory(),
|
||||
'token' => Str::random(64),
|
||||
'expires_at' => now()->addDays(7),
|
||||
'accepted_at' => null,
|
||||
'accepted_by_id' => null,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the invitation invites someone into a folder.
|
||||
*/
|
||||
public function forFolder(Folder $folder, FolderRole $role = FolderRole::Viewer): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'inviteable_type' => Folder::class,
|
||||
'inviteable_id' => $folder->id,
|
||||
'role' => $role->value,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the invitation invites someone into a workspace.
|
||||
*/
|
||||
public function forOrganization(Organization $organization, string $role = 'member'): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'inviteable_type' => Organization::class,
|
||||
'inviteable_id' => $organization->id,
|
||||
'role' => $role,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the invitation has been redeemed.
|
||||
*/
|
||||
public function accepted(): static
|
||||
{
|
||||
return $this->afterCreating(function (Invitation $invitation): void {
|
||||
$invitation->accepted_at = Carbon::now();
|
||||
$invitation->accepted_by_id = $invitation->accepted_by_id ?? $invitation->inviter_id;
|
||||
$invitation->save();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the invitation has lapsed.
|
||||
*/
|
||||
public function expired(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'expires_at' => now()->subDay(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace Database\Factories;
|
||||
use App\Models\Organization;
|
||||
use App\Models\User;
|
||||
use App\OrganizationRole;
|
||||
use App\WorkspaceType;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
@@ -21,6 +22,7 @@ class OrganizationFactory extends Factory
|
||||
{
|
||||
return [
|
||||
'name' => fake()->unique()->company(),
|
||||
'type' => WorkspaceType::Organization,
|
||||
'personal_workspace' => false,
|
||||
'lifetime_access' => false,
|
||||
];
|
||||
@@ -32,10 +34,22 @@ class OrganizationFactory extends Factory
|
||||
public function personal(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'type' => WorkspaceType::Personal,
|
||||
'personal_workspace' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the organization is a shared family workspace.
|
||||
*/
|
||||
public function family(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'type' => WorkspaceType::Family,
|
||||
'personal_workspace' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Give the organization perpetual access, as granted to accounts that
|
||||
* predate subscriptions.
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('organizations', function (Blueprint $table) {
|
||||
// Personal for every workspace created so far; family and
|
||||
// organization workspaces are introduced later.
|
||||
$table->string('type')->default('personal')->after('name');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('organizations', function (Blueprint $table) {
|
||||
$table->dropColumn('type');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('folders', function (Blueprint $table) {
|
||||
// Nullable so existing rows can be backfilled in a separate
|
||||
// migration; the Folder model always writes a value.
|
||||
$table->foreignId('organization_id')
|
||||
->nullable()
|
||||
->after('user_id')
|
||||
->constrained()
|
||||
->cascadeOnDelete();
|
||||
|
||||
$table->string('visibility')->default('private')->after('name');
|
||||
|
||||
$table->index(['organization_id', 'visibility']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('folders', function (Blueprint $table) {
|
||||
$table->dropIndex(['organization_id', 'visibility']);
|
||||
$table->dropConstrainedForeignId('organization_id');
|
||||
$table->dropColumn('visibility');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('todos', function (Blueprint $table) {
|
||||
// Nullable so existing rows can be backfilled in a separate
|
||||
// migration; the Todo model always writes a value.
|
||||
$table->foreignId('organization_id')
|
||||
->nullable()
|
||||
->after('user_id')
|
||||
->constrained()
|
||||
->cascadeOnDelete();
|
||||
|
||||
$table->index(['organization_id', 'completed_at']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('todos', function (Blueprint $table) {
|
||||
$table->dropIndex(['organization_id', 'completed_at']);
|
||||
$table->dropConstrainedForeignId('organization_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('folder_user', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('folder_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
|
||||
$table->string('role')->default('viewer');
|
||||
$table->foreignId('invited_by')->nullable()->constrained('users')->nullOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['folder_id', 'user_id']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('folder_user');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('invitations', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->foreignId('inviter_id')->constrained('users')->cascadeOnDelete();
|
||||
$table->string('email');
|
||||
// Workspace invitations store an OrganizationRole; folder
|
||||
// invitations store a FolderRole. The inviteable type selects
|
||||
// which role vocabulary applies.
|
||||
$table->string('role');
|
||||
$table->morphs('inviteable');
|
||||
$table->string('token', 64)->unique();
|
||||
$table->timestamp('expires_at')->nullable();
|
||||
$table->timestamp('accepted_at')->nullable();
|
||||
$table->foreignId('accepted_by_id')->nullable()->constrained('users')->nullOnDelete();
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['inviteable_type', 'inviteable_id', 'email']);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('invitations');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Give every existing folder and todo a workspace so the access rules have
|
||||
* something to hang off. A folder belongs to whatever workspace its owner
|
||||
* had active at the time the schema changed, which is the personal
|
||||
* workspace every account receives. Todos inherit the workspace of their
|
||||
* folder.
|
||||
*
|
||||
* The visibility backfill mirrors predated behaviour: folders in personal
|
||||
* workspaces are private, and folders in any other workspace start shared
|
||||
* with members. Rows that already have a value are left untouched, which
|
||||
* makes this safe to re-run.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
DB::table('folders')
|
||||
->join('users', 'users.id', '=', 'folders.user_id')
|
||||
->whereNull('folders.organization_id')
|
||||
->update(['folders.organization_id' => DB::raw('users.current_organization_id')]);
|
||||
|
||||
DB::table('todos')
|
||||
->join('folders', 'folders.id', '=', 'todos.folder_id')
|
||||
->whereNull('todos.organization_id')
|
||||
->update(['todos.organization_id' => DB::raw('folders.organization_id')]);
|
||||
|
||||
DB::table('folders')
|
||||
->join('organizations', 'organizations.id', '=', 'folders.organization_id')
|
||||
->update([
|
||||
'folders.visibility' => DB::raw(
|
||||
"CASE WHEN organizations.personal_workspace = 1 THEN 'private' ELSE 'workspace' END"
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* The organization id is derived from the folder's owner, so nulling it
|
||||
* out restores the pre-migration state. Visibility is not reverted because
|
||||
* reverting it would only be guesswork.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
DB::table('todos')->update(['organization_id' => null]);
|
||||
DB::table('folders')->update(['organization_id' => null]);
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Every folder and todo must belong to a workspace: the workspace backfill
|
||||
* has run by this point, the models fill the id on create, and the bulk
|
||||
* seeding and transfer paths set it explicitly. Enforce it so no row can
|
||||
* ever exist without knowing which workspace it belongs to.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('folders', function (Blueprint $table) {
|
||||
$table->foreignId('organization_id')->nullable(false)->change();
|
||||
});
|
||||
|
||||
Schema::table('todos', function (Blueprint $table) {
|
||||
$table->foreignId('organization_id')->nullable(false)->change();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('folders', function (Blueprint $table) {
|
||||
$table->foreignId('organization_id')->nullable()->change();
|
||||
});
|
||||
|
||||
Schema::table('todos', function (Blueprint $table) {
|
||||
$table->foreignId('organization_id')->nullable()->change();
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -21,9 +21,15 @@ class BaseDatabaseSeeder extends Seeder
|
||||
],
|
||||
);
|
||||
|
||||
// The organization is created by the model's created hook, which is
|
||||
// silenced while run under a parent seeder using WithoutModelEvents,
|
||||
// so create one here when needed.
|
||||
if ($admin->currentOrganization()->first() === null) {
|
||||
$admin->createPersonalOrganization();
|
||||
}
|
||||
|
||||
// Grandfather the admin so a fresh install is usable before any Stripe
|
||||
// price is configured. The organization itself is created by the
|
||||
// model's created hook.
|
||||
// price is configured.
|
||||
$admin->currentOrganization()->first()?->forceFill([
|
||||
'lifetime_access' => true,
|
||||
])->save();
|
||||
|
||||
@@ -29,6 +29,12 @@ class DatabaseSeeder extends Seeder
|
||||
],
|
||||
);
|
||||
|
||||
// Model events are disabled while seeding, so the created hook that
|
||||
// normally hands every account its personal workspace does not run.
|
||||
if ($user->current_organization_id === null) {
|
||||
$user->createPersonalOrganization();
|
||||
}
|
||||
|
||||
$work = $this->createTopLevelFolder($user, 'Work');
|
||||
$business = $this->createTopLevelFolder($user, 'Business');
|
||||
$personal = $this->createTopLevelFolder($user, 'Personal');
|
||||
@@ -69,7 +75,10 @@ class DatabaseSeeder extends Seeder
|
||||
foreach ($samples as [$description, $folderName, $daysFromToday, $status]) {
|
||||
$todo = Todo::firstOrCreate(
|
||||
['user_id' => $user->id, 'folder_id' => $foldersByName[$folderName]->id, 'description' => $description],
|
||||
['status' => TodoStatus::Open],
|
||||
[
|
||||
'status' => TodoStatus::Open,
|
||||
'organization_id' => $foldersByName[$folderName]->organization_id,
|
||||
],
|
||||
);
|
||||
|
||||
if ($daysFromToday === null) {
|
||||
@@ -97,7 +106,10 @@ class DatabaseSeeder extends Seeder
|
||||
{
|
||||
return Folder::firstOrCreate(
|
||||
['user_id' => $user->id, 'name' => $name, 'parent_id' => null],
|
||||
['color' => fake()->optional()->randomElement(Folder::COLORS)],
|
||||
[
|
||||
'organization_id' => $user->current_organization_id,
|
||||
'color' => fake()->optional()->randomElement(Folder::COLORS),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
@@ -105,7 +117,10 @@ class DatabaseSeeder extends Seeder
|
||||
{
|
||||
return Folder::firstOrCreate(
|
||||
['user_id' => $parent->user_id, 'name' => $name, 'parent_id' => $parent->id],
|
||||
['color' => fake()->optional()->randomElement(Folder::COLORS)],
|
||||
[
|
||||
'organization_id' => $parent->organization_id,
|
||||
'color' => fake()->optional()->randomElement(Folder::COLORS),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user