File tree Expand file tree Collapse file tree 5 files changed +90
-0
lines changed
Expand file tree Collapse file tree 5 files changed +90
-0
lines changed Original file line number Diff line number Diff line change @@ -15,6 +15,11 @@ public function up(): void
1515 $ table ->uuid ('id ' )->primary ();
1616 $ table ->string ('type ' );
1717 $ table ->morphs ('notifiable ' );
18+ $ table ->foreignId ('site_id ' )
19+ ->nullable ()
20+ ->constrained ('sites ' )
21+ ->cascadeOnUpdate ()
22+ ->cascadeOnDelete ();
1823 $ table ->text ('data ' );
1924 $ table ->timestamp ('read_at ' )->nullable ();
2025 $ table ->timestamps ();
Original file line number Diff line number Diff line change 1616use Eclipse \Core \Models \User ;
1717use Eclipse \Core \Models \User \Permission ;
1818use Eclipse \Core \Models \User \Role ;
19+ use Eclipse \Core \Notifications \Channels \SiteDatabaseChannel ;
1920use Eclipse \Core \Policies \User \RolePolicy ;
2021use Eclipse \Core \Providers \AdminPanelProvider ;
2122use Eclipse \Core \Providers \HorizonServiceProvider ;
2829use Illuminate \Auth \Events \Login ;
2930use Illuminate \Database \Eloquent \Model ;
3031use Illuminate \Mail \Events \MessageSent ;
32+ use Illuminate \Notifications \Channels \DatabaseChannel ;
3133use Illuminate \Support \Facades \Config ;
3234use Illuminate \Support \Facades \DB ;
3335use Illuminate \Support \Facades \Event ;
@@ -108,6 +110,8 @@ public function register(): self
108110 return new Registry ;
109111 });
110112
113+ $ this ->app ->bind (DatabaseChannel::class, SiteDatabaseChannel::class);
114+
111115 return $ this ;
112116 }
113117
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Eclipse \Core \Models ;
4+
5+ use Illuminate \Database \Eloquent \Builder ;
6+ use Illuminate \Notifications \DatabaseNotification as BaseDatabaseNotification ;
7+ use Illuminate \Support \Facades \Context ;
8+
9+ class DatabaseNotification extends BaseDatabaseNotification
10+ {
11+ /**
12+ * Allow storing tenant/site identifier alongside the notification payload.
13+ */
14+ protected $ fillable = [
15+ 'id ' ,
16+ 'type ' ,
17+ 'notifiable_type ' ,
18+ 'notifiable_id ' ,
19+ 'data ' ,
20+ 'read_at ' ,
21+ 'site_id ' ,
22+ ];
23+
24+ protected static function booted (): void
25+ {
26+ static ::addGlobalScope ('site ' , function (Builder $ builder ): void {
27+ $ siteId = Context::get ('site ' );
28+
29+ if ($ siteId !== null ) {
30+ $ builder ->where ($ builder ->getModel ()->getTable ().'.site_id ' , $ siteId );
31+ }
32+ });
33+ }
34+ }
Original file line number Diff line number Diff line change 1515use Illuminate \Database \Eloquent \Model ;
1616use Illuminate \Database \Eloquent \Relations \BelongsTo ;
1717use Illuminate \Database \Eloquent \Relations \HasMany ;
18+ use Illuminate \Database \Eloquent \Relations \MorphMany ;
1819use Illuminate \Database \Eloquent \SoftDeletes ;
1920use Illuminate \Foundation \Auth \User as Authenticatable ;
2021use Illuminate \Notifications \Notifiable ;
@@ -184,6 +185,15 @@ public function getSettings(string $settingsClass = UserSettings::class): Settin
184185 return $ settingsClass ::forUser ($ this ->id );
185186 }
186187
188+ /**
189+ * Override notifications relation to use site-aware notification model.
190+ */
191+ public function notifications (): MorphMany
192+ {
193+ return $ this ->morphMany (DatabaseNotification::class, 'notifiable ' )
194+ ->orderBy ('created_at ' , 'desc ' );
195+ }
196+
187197 /**
188198 * The channels the user receives notification broadcasts on.
189199 */
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Eclipse \Core \Notifications \Channels ;
4+
5+ use Illuminate \Notifications \Channels \DatabaseChannel ;
6+ use Illuminate \Notifications \Notification ;
7+ use Illuminate \Support \Facades \Context ;
8+
9+ class SiteDatabaseChannel extends DatabaseChannel
10+ {
11+ /**
12+ * Build the payload stored in the notifications table and
13+ * append the current site id so rows are tenant-aware.
14+ */
15+ protected function buildPayload ($ notifiable , Notification $ notification ): array
16+ {
17+ $ payload = parent ::buildPayload ($ notifiable , $ notification );
18+ $ fromNotification = $ this ->resolveSiteIdFromNotification ($ notification );
19+ $ resolved = $ fromNotification ?? Context::get ('site ' );
20+ $ payload ['site_id ' ] = $ resolved ;
21+
22+ return $ payload ;
23+ }
24+
25+ /**
26+ * Prefer an explicit site id coming from the notification instance
27+ * (useful when dispatching from queues) before resolving ambient context.
28+ */
29+ protected function resolveSiteIdFromNotification (Notification $ notification ): ?int
30+ {
31+ if (method_exists ($ notification , 'getSiteId ' )) {
32+ return $ notification ->getSiteId ();
33+ }
34+
35+ return null ;
36+ }
37+ }
You can’t perform that action at this time.
0 commit comments