Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Fix pre-existing non-permitted lists on a subscriber being wiped incorrectly on update. Closes #2902.

Kailash Nadh97b72e99f102
diff --git a/cmd/public.go b/cmd/public.go
index ef02df62..a4bad50d 100644
--- a/cmd/public.go
+++ b/cmd/public.go
@@ -773,7 +773,7 @@ func (a *App) processSubForm(c echo.Context) (bool, error) {
}
// Update the subscriber's subscriptions in the DB.
- _, hasOptin, err := a.core.UpdateSubscriberWithLists(sub.ID, sub, nil, listUUIDs, false, false, true)
+ _, hasOptin, err := a.core.UpdateSubscriberWithLists(sub.ID, sub, nil, listUUIDs, false, false, true, nil)
if err == nil {
return hasOptin, nil
}
diff --git a/cmd/subscribers.go b/cmd/subscribers.go
index 80d88c84..6fb5dc25 100644
--- a/cmd/subscribers.go
+++ b/cmd/subscribers.go
@@ -285,7 +285,15 @@ func (a *App) UpdateSubscriber(c echo.Context) error {
// Update the subscriber in the DB.
id := getID(c)
- out, _, err := a.core.UpdateSubscriberWithLists(id, req.Subscriber, listIDs, nil, req.PreconfirmSubs, true, false)
+
+ // Get the user's permitted lists to pass to the update query so that lists on the subscribers
+ // to which they don't have permissions are preserved/left as-is when deleteLists=true.
+ allPerm, permittedLists := user.GetPermittedLists(auth.PermTypeManage)
+ if allPerm {
+ permittedLists = []int{}
+ }
+
+ out, _, err := a.core.UpdateSubscriberWithLists(id, req.Subscriber, listIDs, nil, req.PreconfirmSubs, true, false, permittedLists)
if err != nil {
return err
}
diff --git a/internal/core/subscribers.go b/internal/core/subscribers.go
index 95332f61..ed08c52b 100644
--- a/internal/core/subscribers.go
+++ b/internal/core/subscribers.go
@@ -385,7 +385,7 @@ func (c *Core) UpdateSubscriber(id int, sub models.Subscriber) (models.Subscribe
// UpdateSubscriberWithLists updates a subscriber's properties.
// If deleteLists is set to true, all existing subscriptions are deleted and only
// the ones provided are added or retained.
-func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDs []int, listUUIDs []string, preconfirm, deleteLists, assertOptin bool) (models.Subscriber, bool, error) {
+func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDs []int, listUUIDs []string, preconfirm, deleteLists, assertOptin bool, permittedListIDs []int) (models.Subscriber, bool, error) {
subStatus := models.SubscriptionStatusUnconfirmed
if preconfirm {
subStatus = models.SubscriptionStatusConfirmed
@@ -411,7 +411,8 @@ func (c *Core) UpdateSubscriberWithLists(id int, sub models.Subscriber, listIDs
pq.Array(listIDs),
pq.Array(listUUIDs),
subStatus,
- deleteLists)
+ deleteLists,
+ pq.Array(permittedListIDs))
if err != nil {
c.log.Printf("error updating subscriber: %v", err)
return models.Subscriber{}, false, echo.NewHTTPError(http.StatusInternalServerError,
diff --git a/queries/subscribers.sql b/queries/subscribers.sql
index a5aec3c1..a439d113 100644
--- a/queries/subscribers.sql
+++ b/queries/subscribers.sql
@@ -175,7 +175,9 @@ listIDs AS (
ELSE uuid=ANY($7::UUID[]) END)
),
d AS (
- DELETE FROM subscriber_lists WHERE $9 = TRUE AND subscriber_id = $1 AND list_id != ALL(SELECT id FROM listIDs)
+ DELETE FROM subscriber_lists WHERE $9 = TRUE AND subscriber_id = $1
+ AND list_id != ALL(SELECT id FROM listIDs)
+ AND (CARDINALITY($10::INT[]) = 0 OR list_id = ANY($10::INT[]))
)
INSERT INTO subscriber_lists (subscriber_id, list_id, status)
VALUES(