diff --git a/src/Bundle/ChillMainBundle/Entity/SavedExport.php b/src/Bundle/ChillMainBundle/Entity/SavedExport.php index eec6b83e4..08f866cb6 100644 --- a/src/Bundle/ChillMainBundle/Entity/SavedExport.php +++ b/src/Bundle/ChillMainBundle/Entity/SavedExport.php @@ -15,6 +15,9 @@ use Chill\MainBundle\Doctrine\Model\TrackCreationInterface; use Chill\MainBundle\Doctrine\Model\TrackCreationTrait; use Chill\MainBundle\Doctrine\Model\TrackUpdateInterface; use Chill\MainBundle\Doctrine\Model\TrackUpdateTrait; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\ReadableCollection; use Doctrine\ORM\Mapping as ORM; use Ramsey\Uuid\Uuid; use Ramsey\Uuid\UuidInterface; @@ -50,9 +53,25 @@ class SavedExport implements TrackCreationInterface, TrackUpdateInterface #[ORM\ManyToOne(targetEntity: User::class)] private User $user; + /** + * @var Collection + */ + #[ORM\ManyToMany(targetEntity: User::class)] + #[ORM\JoinTable(name: 'chill_main_saved_export_users')] + private Collection $sharedWithUsers; + + /** + * @var Collection + */ + #[ORM\ManyToMany(targetEntity: UserGroup::class)] + #[ORM\JoinTable(name: 'chill_main_saved_export_usergroups')] + private Collection $sharedWithGroups; + public function __construct() { $this->id = Uuid::uuid4(); + $this->sharedWithUsers = new ArrayCollection(); + $this->sharedWithGroups = new ArrayCollection(); } public function getDescription(): string @@ -119,4 +138,63 @@ class SavedExport implements TrackCreationInterface, TrackUpdateInterface return $this; } + + public function addShare(User|UserGroup $shareUser): SavedExport + { + if ($shareUser instanceof User) { + if (!$this->sharedWithUsers->contains($shareUser)) { + $this->sharedWithUsers->add($shareUser); + } + } else { + if (!$this->sharedWithGroups->contains($shareUser)) { + $this->sharedWithGroups->add($shareUser); + } + } + + return $this; + } + + public function removeShare(User|UserGroup $shareUser): SavedExport + { + if ($shareUser instanceof User) { + $this->sharedWithUsers->removeElement($shareUser); + } else { + $this->sharedWithGroups->removeElement($shareUser); + } + + return $this; + } + + /** + * @return ReadableCollection + */ + public function getShare(): ReadableCollection + { + return new ArrayCollection([ + ...$this->sharedWithUsers->toArray(), + ...$this->sharedWithGroups->toArray(), + ]); + } + + /** + * Determines if the user is shared with either directly or through a group. + * + * @param User $user the user to check + * + * @return bool returns true if the user is shared with directly or via group, otherwise false + */ + public function isSharedWithUser(User $user): bool + { + if ($this->sharedWithUsers->contains($user)) { + return true; + } + + foreach ($this->sharedWithGroups as $group) { + if ($group->contains($user)) { + return true; + } + } + + return false; + } } diff --git a/src/Bundle/ChillMainBundle/migrations/Version20250410145342.php b/src/Bundle/ChillMainBundle/migrations/Version20250410145342.php new file mode 100644 index 000000000..21f4c6d0a --- /dev/null +++ b/src/Bundle/ChillMainBundle/migrations/Version20250410145342.php @@ -0,0 +1,49 @@ +addSql('CREATE TABLE chill_main_saved_export_users (savedexport_id UUID NOT NULL, user_id INT NOT NULL, PRIMARY KEY(savedexport_id, user_id))'); + $this->addSql('CREATE INDEX IDX_4A2B71EC24ECEDCA ON chill_main_saved_export_users (savedexport_id)'); + $this->addSql('CREATE INDEX IDX_4A2B71ECA76ED395 ON chill_main_saved_export_users (user_id)'); + $this->addSql('COMMENT ON COLUMN chill_main_saved_export_users.savedexport_id IS \'(DC2Type:uuid)\''); + $this->addSql('CREATE TABLE chill_main_saved_export_usergroups (savedexport_id UUID NOT NULL, usergroup_id INT NOT NULL, PRIMARY KEY(savedexport_id, usergroup_id))'); + $this->addSql('CREATE INDEX IDX_A12F30824ECEDCA ON chill_main_saved_export_usergroups (savedexport_id)'); + $this->addSql('CREATE INDEX IDX_A12F308D2112630 ON chill_main_saved_export_usergroups (usergroup_id)'); + $this->addSql('COMMENT ON COLUMN chill_main_saved_export_usergroups.savedexport_id IS \'(DC2Type:uuid)\''); + $this->addSql('ALTER TABLE chill_main_saved_export_users ADD CONSTRAINT FK_4A2B71EC24ECEDCA FOREIGN KEY (savedexport_id) REFERENCES chill_main_saved_export (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_main_saved_export_users ADD CONSTRAINT FK_4A2B71ECA76ED395 FOREIGN KEY (user_id) REFERENCES users (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_main_saved_export_usergroups ADD CONSTRAINT FK_A12F30824ECEDCA FOREIGN KEY (savedexport_id) REFERENCES chill_main_saved_export (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + $this->addSql('ALTER TABLE chill_main_saved_export_usergroups ADD CONSTRAINT FK_A12F308D2112630 FOREIGN KEY (usergroup_id) REFERENCES chill_main_user_group (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema): void + { + $this->addSql('ALTER TABLE chill_main_saved_export_users DROP CONSTRAINT FK_4A2B71EC24ECEDCA'); + $this->addSql('ALTER TABLE chill_main_saved_export_users DROP CONSTRAINT FK_4A2B71ECA76ED395'); + $this->addSql('ALTER TABLE chill_main_saved_export_usergroups DROP CONSTRAINT FK_A12F30824ECEDCA'); + $this->addSql('ALTER TABLE chill_main_saved_export_usergroups DROP CONSTRAINT FK_A12F308D2112630'); + $this->addSql('DROP TABLE chill_main_saved_export_users'); + $this->addSql('DROP TABLE chill_main_saved_export_usergroups'); + } +}