2022-12-25 16:41:38 +00:00
|
|
|
package user
|
2022-01-23 05:02:16 +00:00
|
|
|
|
2022-01-31 16:44:58 +00:00
|
|
|
import (
|
2022-12-26 03:29:55 +00:00
|
|
|
"database/sql"
|
|
|
|
"encoding/json"
|
2022-01-31 16:44:58 +00:00
|
|
|
"errors"
|
2022-12-26 03:29:55 +00:00
|
|
|
"fmt"
|
|
|
|
_ "github.com/mattn/go-sqlite3" // SQLite driver
|
2023-01-16 04:29:46 +00:00
|
|
|
"github.com/stripe/stripe-go/v74"
|
2022-12-26 03:29:55 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
"heckel.io/ntfy/log"
|
|
|
|
"heckel.io/ntfy/util"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
2022-01-31 16:44:58 +00:00
|
|
|
)
|
2022-01-23 05:02:16 +00:00
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
const (
|
2023-01-22 04:15:22 +00:00
|
|
|
tierIDPrefix = "ti_"
|
|
|
|
tierIDLength = 8
|
|
|
|
syncTopicPrefix = "st_"
|
|
|
|
syncTopicLength = 16
|
|
|
|
userIDPrefix = "u_"
|
|
|
|
userIDLength = 12
|
|
|
|
userPasswordBcryptCost = 10
|
|
|
|
userAuthIntentionalSlowDownHash = "$2a$10$YFCQvqQDwIIwnJM1xkAYOeih0dg17UVGanaTStnrSzC8NCWxcLDwy" // Cost should match userPasswordBcryptCost
|
|
|
|
userStatsQueueWriterInterval = 33 * time.Second
|
2023-01-23 03:21:30 +00:00
|
|
|
userHardDeleteAfterDuration = 7 * 24 * time.Hour
|
2023-01-22 04:15:22 +00:00
|
|
|
tokenPrefix = "tk_"
|
|
|
|
tokenLength = 32
|
|
|
|
tokenMaxCount = 10 // Only keep this many tokens in the table per user
|
|
|
|
tokenExpiryDuration = 72 * time.Hour // Extend tokens by this much
|
2022-12-26 03:29:55 +00:00
|
|
|
)
|
|
|
|
|
2022-12-31 15:16:14 +00:00
|
|
|
var (
|
2023-01-01 20:21:43 +00:00
|
|
|
errNoTokenProvided = errors.New("no token provided")
|
|
|
|
errTopicOwnedByOthers = errors.New("topic owned by others")
|
2023-01-06 01:22:34 +00:00
|
|
|
errNoRows = errors.New("no rows found")
|
2022-12-31 15:16:14 +00:00
|
|
|
)
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// Manager-related queries
|
|
|
|
const (
|
2022-12-29 18:08:47 +00:00
|
|
|
createTablesQueriesNoTx = `
|
2023-01-08 02:04:13 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS tier (
|
2023-01-22 04:15:22 +00:00
|
|
|
id TEXT PRIMARY KEY,
|
2022-12-26 03:29:55 +00:00
|
|
|
code TEXT NOT NULL,
|
2023-01-09 20:40:46 +00:00
|
|
|
name TEXT NOT NULL,
|
2022-12-26 03:29:55 +00:00
|
|
|
messages_limit INT NOT NULL,
|
2023-01-07 14:34:02 +00:00
|
|
|
messages_expiry_duration INT NOT NULL,
|
2022-12-26 03:29:55 +00:00
|
|
|
emails_limit INT NOT NULL,
|
2023-01-08 02:04:13 +00:00
|
|
|
reservations_limit INT NOT NULL,
|
2022-12-26 03:29:55 +00:00
|
|
|
attachment_file_size_limit INT NOT NULL,
|
|
|
|
attachment_total_size_limit INT NOT NULL,
|
2023-01-14 11:43:44 +00:00
|
|
|
attachment_expiry_duration INT NOT NULL,
|
2023-01-25 15:05:54 +00:00
|
|
|
attachment_bandwidth_limit INT NOT NULL,
|
2023-01-14 11:43:44 +00:00
|
|
|
stripe_price_id TEXT
|
2022-12-26 03:29:55 +00:00
|
|
|
);
|
2023-01-14 11:43:44 +00:00
|
|
|
CREATE UNIQUE INDEX idx_tier_code ON tier (code);
|
|
|
|
CREATE UNIQUE INDEX idx_tier_price_id ON tier (stripe_price_id);
|
2022-12-26 03:29:55 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS user (
|
2023-01-22 04:15:22 +00:00
|
|
|
id TEXT PRIMARY KEY,
|
2023-01-23 03:21:30 +00:00
|
|
|
tier_id TEXT,
|
2022-12-26 03:29:55 +00:00
|
|
|
user TEXT NOT NULL,
|
|
|
|
pass TEXT NOT NULL,
|
2023-01-10 02:53:21 +00:00
|
|
|
role TEXT CHECK (role IN ('anonymous', 'admin', 'user')) NOT NULL,
|
|
|
|
prefs JSON NOT NULL DEFAULT '{}',
|
|
|
|
sync_topic TEXT NOT NULL,
|
|
|
|
stats_messages INT NOT NULL DEFAULT (0),
|
|
|
|
stats_emails INT NOT NULL DEFAULT (0),
|
2023-01-14 11:43:44 +00:00
|
|
|
stripe_customer_id TEXT,
|
2023-01-16 04:29:46 +00:00
|
|
|
stripe_subscription_id TEXT,
|
|
|
|
stripe_subscription_status TEXT,
|
2023-01-16 15:35:12 +00:00
|
|
|
stripe_subscription_paid_until INT,
|
|
|
|
stripe_subscription_cancel_at INT,
|
2023-01-23 03:21:30 +00:00
|
|
|
created INT NOT NULL,
|
|
|
|
deleted INT,
|
2023-01-08 02:04:13 +00:00
|
|
|
FOREIGN KEY (tier_id) REFERENCES tier (id)
|
2022-12-26 03:29:55 +00:00
|
|
|
);
|
|
|
|
CREATE UNIQUE INDEX idx_user ON user (user);
|
2023-01-14 11:43:44 +00:00
|
|
|
CREATE UNIQUE INDEX idx_user_stripe_customer_id ON user (stripe_customer_id);
|
|
|
|
CREATE UNIQUE INDEX idx_user_stripe_subscription_id ON user (stripe_subscription_id);
|
2022-12-26 03:29:55 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS user_access (
|
2023-01-22 04:15:22 +00:00
|
|
|
user_id TEXT NOT NULL,
|
2022-12-26 03:29:55 +00:00
|
|
|
topic TEXT NOT NULL,
|
|
|
|
read INT NOT NULL,
|
|
|
|
write INT NOT NULL,
|
2023-01-01 20:21:43 +00:00
|
|
|
owner_user_id INT,
|
2022-12-26 03:29:55 +00:00
|
|
|
PRIMARY KEY (user_id, topic),
|
2023-01-05 20:20:44 +00:00
|
|
|
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,
|
|
|
|
FOREIGN KEY (owner_user_id) REFERENCES user (id) ON DELETE CASCADE
|
2023-01-01 20:21:43 +00:00
|
|
|
);
|
2022-12-26 03:29:55 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS user_token (
|
2023-01-22 04:15:22 +00:00
|
|
|
user_id TEXT NOT NULL,
|
2022-12-26 03:29:55 +00:00
|
|
|
token TEXT NOT NULL,
|
|
|
|
expires INT NOT NULL,
|
|
|
|
PRIMARY KEY (user_id, token),
|
|
|
|
FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE
|
|
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS schemaVersion (
|
|
|
|
id INT PRIMARY KEY,
|
|
|
|
version INT NOT NULL
|
|
|
|
);
|
2023-01-23 03:21:30 +00:00
|
|
|
INSERT INTO user (id, user, pass, role, sync_topic, created)
|
|
|
|
VALUES ('` + everyoneID + `', '*', '', 'anonymous', '', UNIXEPOCH())
|
2023-01-10 02:53:21 +00:00
|
|
|
ON CONFLICT (id) DO NOTHING;
|
2022-12-26 03:29:55 +00:00
|
|
|
`
|
2022-12-29 18:08:47 +00:00
|
|
|
createTablesQueries = `BEGIN; ` + createTablesQueriesNoTx + ` COMMIT;`
|
2023-01-05 20:20:44 +00:00
|
|
|
builtinStartupQueries = `
|
|
|
|
PRAGMA foreign_keys = ON;
|
|
|
|
`
|
|
|
|
|
2023-01-22 04:15:22 +00:00
|
|
|
selectUserByIDQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
SELECT u.id, u.user, u.pass, u.role, u.prefs, u.sync_topic, u.stats_messages, u.stats_emails, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, deleted, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_price_id
|
2023-01-22 04:15:22 +00:00
|
|
|
FROM user u
|
|
|
|
LEFT JOIN tier t on t.id = u.tier_id
|
|
|
|
WHERE u.id = ?
|
|
|
|
`
|
2022-12-26 03:29:55 +00:00
|
|
|
selectUserByNameQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
SELECT u.id, u.user, u.pass, u.role, u.prefs, u.sync_topic, u.stats_messages, u.stats_emails, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, deleted, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_price_id
|
2022-12-26 03:29:55 +00:00
|
|
|
FROM user u
|
2023-01-17 15:09:37 +00:00
|
|
|
LEFT JOIN tier t on t.id = u.tier_id
|
2023-01-23 03:21:30 +00:00
|
|
|
WHERE user = ?
|
2022-12-26 03:29:55 +00:00
|
|
|
`
|
|
|
|
selectUserByTokenQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
SELECT u.id, u.user, u.pass, u.role, u.prefs, u.sync_topic, u.stats_messages, u.stats_emails, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, deleted, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_price_id
|
2022-12-26 03:29:55 +00:00
|
|
|
FROM user u
|
|
|
|
JOIN user_token t on u.id = t.user_id
|
2023-01-17 15:09:37 +00:00
|
|
|
LEFT JOIN tier t on t.id = u.tier_id
|
2022-12-29 16:09:45 +00:00
|
|
|
WHERE t.token = ? AND t.expires >= ?
|
2022-12-26 03:29:55 +00:00
|
|
|
`
|
2023-01-14 11:43:44 +00:00
|
|
|
selectUserByStripeCustomerIDQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
SELECT u.id, u.user, u.pass, u.role, u.prefs, u.sync_topic, u.stats_messages, u.stats_emails, u.stripe_customer_id, u.stripe_subscription_id, u.stripe_subscription_status, u.stripe_subscription_paid_until, u.stripe_subscription_cancel_at, deleted, t.code, t.name, t.messages_limit, t.messages_expiry_duration, t.emails_limit, t.reservations_limit, t.attachment_file_size_limit, t.attachment_total_size_limit, t.attachment_expiry_duration, t.attachment_bandwidth_limit, t.stripe_price_id
|
2023-01-14 11:43:44 +00:00
|
|
|
FROM user u
|
2023-01-17 15:09:37 +00:00
|
|
|
LEFT JOIN tier t on t.id = u.tier_id
|
2023-01-14 11:43:44 +00:00
|
|
|
WHERE u.stripe_customer_id = ?
|
|
|
|
`
|
2022-12-26 03:29:55 +00:00
|
|
|
selectTopicPermsQuery = `
|
2022-12-27 02:27:07 +00:00
|
|
|
SELECT read, write
|
|
|
|
FROM user_access a
|
|
|
|
JOIN user u ON u.id = a.user_id
|
2023-01-03 01:08:37 +00:00
|
|
|
WHERE (u.user = ? OR u.user = ?) AND ? LIKE a.topic
|
2022-12-27 02:27:07 +00:00
|
|
|
ORDER BY u.user DESC
|
2022-12-26 03:29:55 +00:00
|
|
|
`
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2023-01-10 02:53:21 +00:00
|
|
|
insertUserQuery = `
|
2023-01-23 03:21:30 +00:00
|
|
|
INSERT INTO user (id, user, pass, role, sync_topic, created)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?)
|
2023-01-10 02:53:21 +00:00
|
|
|
`
|
2022-12-28 18:28:28 +00:00
|
|
|
selectUsernamesQuery = `
|
|
|
|
SELECT user
|
|
|
|
FROM user
|
|
|
|
ORDER BY
|
|
|
|
CASE role
|
|
|
|
WHEN 'admin' THEN 1
|
|
|
|
WHEN 'anonymous' THEN 3
|
|
|
|
ELSE 2
|
|
|
|
END, user
|
|
|
|
`
|
2023-01-11 03:51:51 +00:00
|
|
|
updateUserPassQuery = `UPDATE user SET pass = ? WHERE user = ?`
|
|
|
|
updateUserRoleQuery = `UPDATE user SET role = ? WHERE user = ?`
|
|
|
|
updateUserPrefsQuery = `UPDATE user SET prefs = ? WHERE user = ?`
|
2023-01-22 04:15:22 +00:00
|
|
|
updateUserStatsQuery = `UPDATE user SET stats_messages = ?, stats_emails = ? WHERE id = ?`
|
2023-01-11 03:51:51 +00:00
|
|
|
updateUserStatsResetAllQuery = `UPDATE user SET stats_messages = 0, stats_emails = 0`
|
2023-01-23 03:21:30 +00:00
|
|
|
updateUserDeletedQuery = `UPDATE user SET deleted = ? WHERE id = ?`
|
|
|
|
deleteUsersMarkedQuery = `DELETE FROM user WHERE deleted < ?`
|
2023-01-11 03:51:51 +00:00
|
|
|
deleteUserQuery = `DELETE FROM user WHERE user = ?`
|
2022-12-26 03:29:55 +00:00
|
|
|
|
2022-12-28 18:28:28 +00:00
|
|
|
upsertUserAccessQuery = `
|
2023-01-01 20:21:43 +00:00
|
|
|
INSERT INTO user_access (user_id, topic, read, write, owner_user_id)
|
|
|
|
VALUES ((SELECT id FROM user WHERE user = ?), ?, ?, ?, (SELECT IIF(?='',NULL,(SELECT id FROM user WHERE user=?))))
|
2022-12-28 18:28:28 +00:00
|
|
|
ON CONFLICT (user_id, topic)
|
2023-01-01 20:21:43 +00:00
|
|
|
DO UPDATE SET read=excluded.read, write=excluded.write, owner_user_id=excluded.owner_user_id
|
|
|
|
`
|
|
|
|
selectUserAccessQuery = `
|
2023-01-03 01:08:37 +00:00
|
|
|
SELECT topic, read, write
|
2023-01-01 20:21:43 +00:00
|
|
|
FROM user_access
|
|
|
|
WHERE user_id = (SELECT id FROM user WHERE user = ?)
|
|
|
|
ORDER BY write DESC, read DESC, topic
|
|
|
|
`
|
2023-01-03 01:08:37 +00:00
|
|
|
selectUserReservationsQuery = `
|
|
|
|
SELECT a_user.topic, a_user.read, a_user.write, a_everyone.read AS everyone_read, a_everyone.write AS everyone_write
|
|
|
|
FROM user_access a_user
|
|
|
|
LEFT JOIN user_access a_everyone ON a_user.topic = a_everyone.topic AND a_everyone.user_id = (SELECT id FROM user WHERE user = ?)
|
|
|
|
WHERE a_user.user_id = a_user.owner_user_id
|
|
|
|
AND a_user.owner_user_id = (SELECT id FROM user WHERE user = ?)
|
|
|
|
ORDER BY a_user.topic
|
|
|
|
`
|
2023-01-06 02:15:10 +00:00
|
|
|
selectUserReservationsCountQuery = `
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM user_access
|
|
|
|
WHERE user_id = owner_user_id AND owner_user_id = (SELECT id FROM user WHERE user = ?)
|
|
|
|
`
|
2023-01-06 15:45:38 +00:00
|
|
|
selectUserHasReservationQuery = `
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM user_access
|
|
|
|
WHERE user_id = owner_user_id
|
|
|
|
AND owner_user_id = (SELECT id FROM user WHERE user = ?)
|
|
|
|
AND topic = ?
|
|
|
|
`
|
2023-01-01 20:21:43 +00:00
|
|
|
selectOtherAccessCountQuery = `
|
2023-01-06 01:22:34 +00:00
|
|
|
SELECT COUNT(*)
|
2023-01-01 20:21:43 +00:00
|
|
|
FROM user_access
|
|
|
|
WHERE (topic = ? OR ? LIKE topic)
|
|
|
|
AND (owner_user_id IS NULL OR owner_user_id != (SELECT id FROM user WHERE user = ?))
|
2022-12-28 18:28:28 +00:00
|
|
|
`
|
2023-01-09 20:40:46 +00:00
|
|
|
deleteAllAccessQuery = `DELETE FROM user_access`
|
|
|
|
deleteUserAccessQuery = `
|
|
|
|
DELETE FROM user_access
|
|
|
|
WHERE user_id = (SELECT id FROM user WHERE user = ?)
|
|
|
|
OR owner_user_id = (SELECT id FROM user WHERE user = ?)
|
|
|
|
`
|
|
|
|
deleteTopicAccessQuery = `
|
|
|
|
DELETE FROM user_access
|
|
|
|
WHERE (user_id = (SELECT id FROM user WHERE user = ?) OR owner_user_id = (SELECT id FROM user WHERE user = ?))
|
|
|
|
AND topic = ?
|
|
|
|
`
|
2022-12-26 03:29:55 +00:00
|
|
|
|
2023-01-22 04:15:22 +00:00
|
|
|
selectTokenCountQuery = `SELECT COUNT(*) FROM user_token WHERE user_id = ?`
|
|
|
|
insertTokenQuery = `INSERT INTO user_token (user_id, token, expires) VALUES (?, ?, ?)`
|
2022-12-26 03:29:55 +00:00
|
|
|
updateTokenExpiryQuery = `UPDATE user_token SET expires = ? WHERE user_id = (SELECT id FROM user WHERE user = ?) AND token = ?`
|
2023-01-23 03:21:30 +00:00
|
|
|
deleteTokenQuery = `DELETE FROM user_token WHERE user_id = ? AND token = ?`
|
|
|
|
deleteAllTokenQuery = `DELETE FROM user_token WHERE user_id = ?`
|
2022-12-26 03:29:55 +00:00
|
|
|
deleteExpiredTokensQuery = `DELETE FROM user_token WHERE expires < ?`
|
2023-01-06 01:22:34 +00:00
|
|
|
deleteExcessTokensQuery = `
|
|
|
|
DELETE FROM user_token
|
|
|
|
WHERE (user_id, token) NOT IN (
|
|
|
|
SELECT user_id, token
|
|
|
|
FROM user_token
|
2023-01-22 04:15:22 +00:00
|
|
|
WHERE user_id = ?
|
2023-01-06 01:22:34 +00:00
|
|
|
ORDER BY expires DESC
|
|
|
|
LIMIT ?
|
|
|
|
)
|
|
|
|
`
|
2023-01-08 02:04:13 +00:00
|
|
|
|
|
|
|
insertTierQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
INSERT INTO tier (id, code, name, messages_limit, messages_expiry_duration, emails_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_price_id)
|
|
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
2023-01-08 02:04:13 +00:00
|
|
|
`
|
2023-01-21 03:47:37 +00:00
|
|
|
selectTiersQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
SELECT id, code, name, messages_limit, messages_expiry_duration, emails_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_price_id
|
2023-01-17 15:09:37 +00:00
|
|
|
FROM tier
|
|
|
|
`
|
2023-01-14 11:43:44 +00:00
|
|
|
selectTierByCodeQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
SELECT id, code, name, messages_limit, messages_expiry_duration, emails_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_price_id
|
2023-01-14 11:43:44 +00:00
|
|
|
FROM tier
|
|
|
|
WHERE code = ?
|
|
|
|
`
|
|
|
|
selectTierByPriceIDQuery = `
|
2023-01-25 15:05:54 +00:00
|
|
|
SELECT id, code, name, messages_limit, messages_expiry_duration, emails_limit, reservations_limit, attachment_file_size_limit, attachment_total_size_limit, attachment_expiry_duration, attachment_bandwidth_limit, stripe_price_id
|
2023-01-14 11:43:44 +00:00
|
|
|
FROM tier
|
|
|
|
WHERE stripe_price_id = ?
|
|
|
|
`
|
2023-01-21 03:47:37 +00:00
|
|
|
updateUserTierQuery = `UPDATE user SET tier_id = (SELECT id FROM tier WHERE code = ?) WHERE user = ?`
|
2023-01-09 20:40:46 +00:00
|
|
|
deleteUserTierQuery = `UPDATE user SET tier_id = null WHERE user = ?`
|
2023-01-14 11:43:44 +00:00
|
|
|
|
2023-01-16 04:29:46 +00:00
|
|
|
updateBillingQuery = `
|
|
|
|
UPDATE user
|
2023-01-16 15:35:12 +00:00
|
|
|
SET stripe_customer_id = ?, stripe_subscription_id = ?, stripe_subscription_status = ?, stripe_subscription_paid_until = ?, stripe_subscription_cancel_at = ?
|
2023-01-16 04:29:46 +00:00
|
|
|
WHERE user = ?
|
|
|
|
`
|
2022-12-26 03:29:55 +00:00
|
|
|
)
|
2022-12-03 20:20:59 +00:00
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// Schema management queries
|
|
|
|
const (
|
2022-12-29 18:08:47 +00:00
|
|
|
currentSchemaVersion = 2
|
2022-12-26 03:29:55 +00:00
|
|
|
insertSchemaVersion = `INSERT INTO schemaVersion VALUES (1, ?)`
|
2022-12-29 18:08:47 +00:00
|
|
|
updateSchemaVersion = `UPDATE schemaVersion SET version = ? WHERE id = 1`
|
2022-12-26 03:29:55 +00:00
|
|
|
selectSchemaVersionQuery = `SELECT version FROM schemaVersion WHERE id = 1`
|
2022-12-29 18:08:47 +00:00
|
|
|
|
|
|
|
// 1 -> 2 (complex migration!)
|
|
|
|
migrate1To2RenameUserTableQueryNoTx = `
|
|
|
|
ALTER TABLE user RENAME TO user_old;
|
|
|
|
`
|
2023-01-22 04:15:22 +00:00
|
|
|
migrate1To2SelectAllOldUsernamesNoTx = `SELECT user FROM user_old`
|
|
|
|
migrate1To2InsertUserNoTx = `
|
2023-01-23 03:21:30 +00:00
|
|
|
INSERT INTO user (id, user, pass, role, sync_topic, created)
|
|
|
|
SELECT ?, user, pass, role, ?, UNIXEPOCH() FROM user_old WHERE user = ?
|
2023-01-22 04:15:22 +00:00
|
|
|
`
|
2022-12-29 18:08:47 +00:00
|
|
|
migrate1To2InsertFromOldTablesAndDropNoTx = `
|
|
|
|
INSERT INTO user_access (user_id, topic, read, write)
|
|
|
|
SELECT u.id, a.topic, a.read, a.write
|
|
|
|
FROM user u
|
|
|
|
JOIN access a ON u.user = a.user;
|
|
|
|
|
|
|
|
DROP TABLE access;
|
|
|
|
DROP TABLE user_old;
|
|
|
|
`
|
2023-01-22 04:15:22 +00:00
|
|
|
migrate1To2UpdateSyncTopicNoTx = `UPDATE user SET sync_topic = ? WHERE id = ?`
|
2022-12-26 03:29:55 +00:00
|
|
|
)
|
2022-01-23 05:02:16 +00:00
|
|
|
|
2022-12-28 03:14:14 +00:00
|
|
|
// Manager is an implementation of Manager. It stores users and access control list
|
2022-12-26 03:29:55 +00:00
|
|
|
// in a SQLite database.
|
2022-12-28 03:14:14 +00:00
|
|
|
type Manager struct {
|
2023-01-06 01:22:34 +00:00
|
|
|
db *sql.DB
|
|
|
|
defaultAccess Permission // Default permission if no ACL matches
|
|
|
|
statsQueue map[string]*User // Username -> User, for "unimportant" user updates
|
|
|
|
mu sync.Mutex
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2022-12-28 03:14:14 +00:00
|
|
|
var _ Auther = (*Manager)(nil)
|
2022-12-26 03:29:55 +00:00
|
|
|
|
2022-12-28 03:14:14 +00:00
|
|
|
// NewManager creates a new Manager instance
|
2023-01-05 20:20:44 +00:00
|
|
|
func NewManager(filename, startupQueries string, defaultAccess Permission) (*Manager, error) {
|
2023-01-06 01:22:34 +00:00
|
|
|
return newManager(filename, startupQueries, defaultAccess, userStatsQueueWriterInterval)
|
2022-12-29 16:09:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewManager creates a new Manager instance
|
2023-01-06 01:22:34 +00:00
|
|
|
func newManager(filename, startupQueries string, defaultAccess Permission, statsWriterInterval time.Duration) (*Manager, error) {
|
2022-12-26 03:29:55 +00:00
|
|
|
db, err := sql.Open("sqlite3", filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-29 18:08:47 +00:00
|
|
|
if err := setupDB(db); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-05 20:20:44 +00:00
|
|
|
if err := runStartupQueries(db, startupQueries); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-12-28 03:14:14 +00:00
|
|
|
manager := &Manager{
|
2023-01-06 01:22:34 +00:00
|
|
|
db: db,
|
|
|
|
defaultAccess: defaultAccess,
|
|
|
|
statsQueue: make(map[string]*User),
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
2022-12-29 16:09:45 +00:00
|
|
|
go manager.userStatsQueueWriter(statsWriterInterval)
|
2022-12-26 03:29:55 +00:00
|
|
|
return manager, nil
|
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2023-01-23 03:21:30 +00:00
|
|
|
// Authenticate checks username and password and returns a User if correct, and the user has not been
|
|
|
|
// marked as deleted. The method returns in constant-ish time, regardless of whether the user exists or
|
|
|
|
// the password is correct or incorrect.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) Authenticate(username, password string) (*User, error) {
|
2022-12-26 03:29:55 +00:00
|
|
|
if username == Everyone {
|
|
|
|
return nil, ErrUnauthenticated
|
|
|
|
}
|
|
|
|
user, err := a.User(username)
|
|
|
|
if err != nil {
|
2023-01-05 20:20:44 +00:00
|
|
|
log.Trace("authentication of user %s failed (1): %s", username, err.Error())
|
2023-01-22 04:15:22 +00:00
|
|
|
bcrypt.CompareHashAndPassword([]byte(userAuthIntentionalSlowDownHash), []byte("intentional slow-down to avoid timing attacks"))
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil, ErrUnauthenticated
|
2023-01-23 03:21:30 +00:00
|
|
|
} else if user.Deleted {
|
|
|
|
log.Trace("authentication of user %s failed (2): user marked deleted", username)
|
|
|
|
bcrypt.CompareHashAndPassword([]byte(userAuthIntentionalSlowDownHash), []byte("intentional slow-down to avoid timing attacks"))
|
|
|
|
return nil, ErrUnauthenticated
|
|
|
|
} else if err := bcrypt.CompareHashAndPassword([]byte(user.Hash), []byte(password)); err != nil {
|
|
|
|
log.Trace("authentication of user %s failed (3): %s", username, err.Error())
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil, ErrUnauthenticated
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2022-12-28 18:28:28 +00:00
|
|
|
// AuthenticateToken checks if the token exists and returns the associated User if it does.
|
|
|
|
// The method sets the User.Token value to the token that was used for authentication.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) AuthenticateToken(token string) (*User, error) {
|
2022-12-28 18:46:18 +00:00
|
|
|
if len(token) != tokenLength {
|
|
|
|
return nil, ErrUnauthenticated
|
|
|
|
}
|
2022-12-26 03:29:55 +00:00
|
|
|
user, err := a.userByToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrUnauthenticated
|
|
|
|
}
|
|
|
|
user.Token = token
|
|
|
|
return user, nil
|
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2022-12-28 18:28:28 +00:00
|
|
|
// CreateToken generates a random token for the given user and returns it. The token expires
|
2023-01-06 01:22:34 +00:00
|
|
|
// after a fixed duration unless ExtendToken is called. This function also prunes tokens for the
|
|
|
|
// given user, if there are too many of them.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) CreateToken(user *User) (*Token, error) {
|
2023-01-22 04:15:22 +00:00
|
|
|
token, expires := util.RandomStringPrefix(tokenPrefix, tokenLength), time.Now().Add(tokenExpiryDuration)
|
2023-01-06 01:22:34 +00:00
|
|
|
tx, err := a.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2023-01-22 04:15:22 +00:00
|
|
|
if _, err := tx.Exec(insertTokenQuery, user.ID, token, expires.Unix()); err != nil {
|
2023-01-06 01:22:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
rows, err := tx.Query(selectTokenCountQuery, user.ID)
|
2023-01-06 01:22:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if !rows.Next() {
|
|
|
|
return nil, errNoRows
|
|
|
|
}
|
|
|
|
var tokenCount int
|
|
|
|
if err := rows.Scan(&tokenCount); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tokenCount >= tokenMaxCount {
|
|
|
|
// This pruning logic is done in two queries for efficiency. The SELECT above is a lookup
|
|
|
|
// on two indices, whereas the query below is a full table scan.
|
2023-01-22 04:15:22 +00:00
|
|
|
if _, err := tx.Exec(deleteExcessTokensQuery, user.ID, tokenMaxCount); err != nil {
|
2023-01-06 01:22:34 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Token{
|
|
|
|
Value: token,
|
2022-12-28 18:46:18 +00:00
|
|
|
Expires: expires,
|
2022-12-26 03:29:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2022-12-28 18:28:28 +00:00
|
|
|
// ExtendToken sets the new expiry date for a token, thereby extending its use further into the future.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) ExtendToken(user *User) (*Token, error) {
|
2022-12-31 15:16:14 +00:00
|
|
|
if user.Token == "" {
|
|
|
|
return nil, errNoTokenProvided
|
|
|
|
}
|
2023-01-06 01:22:34 +00:00
|
|
|
newExpires := time.Now().Add(tokenExpiryDuration)
|
2022-12-26 03:29:55 +00:00
|
|
|
if _, err := a.db.Exec(updateTokenExpiryQuery, newExpires.Unix(), user.Name, user.Token); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Token{
|
|
|
|
Value: user.Token,
|
2022-12-28 18:46:18 +00:00
|
|
|
Expires: newExpires,
|
2022-12-26 03:29:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2022-12-28 18:28:28 +00:00
|
|
|
// RemoveToken deletes the token defined in User.Token
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) RemoveToken(user *User) error {
|
2022-12-26 03:29:55 +00:00
|
|
|
if user.Token == "" {
|
|
|
|
return ErrUnauthorized
|
|
|
|
}
|
2023-01-23 03:21:30 +00:00
|
|
|
if _, err := a.db.Exec(deleteTokenQuery, user.ID, user.Token); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2022-12-28 18:28:28 +00:00
|
|
|
// RemoveExpiredTokens deletes all expired tokens from the database
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) RemoveExpiredTokens() error {
|
2022-12-26 03:29:55 +00:00
|
|
|
if _, err := a.db.Exec(deleteExpiredTokensQuery, time.Now().Unix()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-26 03:30:53 +00:00
|
|
|
|
2023-01-23 03:21:30 +00:00
|
|
|
// RemoveDeletedUsers deletes all users that have been marked deleted for
|
|
|
|
func (a *Manager) RemoveDeletedUsers() error {
|
|
|
|
if _, err := a.db.Exec(deleteUsersMarkedQuery, time.Now().Unix()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-28 20:51:09 +00:00
|
|
|
// ChangeSettings persists the user settings
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) ChangeSettings(user *User) error {
|
2023-01-10 02:53:21 +00:00
|
|
|
prefs, err := json.Marshal(user.Prefs)
|
2022-12-26 03:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-10 02:53:21 +00:00
|
|
|
if _, err := a.db.Exec(updateUserPrefsQuery, string(prefs), user.Name); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-01-23 05:54:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 03:51:51 +00:00
|
|
|
// ResetStats resets all user stats in the user database. This touches all users.
|
|
|
|
func (a *Manager) ResetStats() error {
|
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
|
|
|
if _, err := a.db.Exec(updateUserStatsResetAllQuery); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
a.statsQueue = make(map[string]*User)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-28 20:51:09 +00:00
|
|
|
// EnqueueStats adds the user to a queue which writes out user stats (messages, emails, ..) in
|
|
|
|
// batches at a regular interval
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) EnqueueStats(user *User) {
|
2022-12-26 03:29:55 +00:00
|
|
|
a.mu.Lock()
|
|
|
|
defer a.mu.Unlock()
|
2023-01-22 04:15:22 +00:00
|
|
|
a.statsQueue[user.ID] = user
|
2022-12-08 02:26:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-29 16:09:45 +00:00
|
|
|
func (a *Manager) userStatsQueueWriter(interval time.Duration) {
|
|
|
|
ticker := time.NewTicker(interval)
|
2022-12-26 03:29:55 +00:00
|
|
|
for range ticker.C {
|
|
|
|
if err := a.writeUserStatsQueue(); err != nil {
|
2023-01-07 14:34:02 +00:00
|
|
|
log.Warn("User Manager: Writing user stats queue failed: %s", err.Error())
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-25 16:41:38 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) writeUserStatsQueue() error {
|
2022-12-26 03:29:55 +00:00
|
|
|
a.mu.Lock()
|
|
|
|
if len(a.statsQueue) == 0 {
|
|
|
|
a.mu.Unlock()
|
2023-01-07 14:34:02 +00:00
|
|
|
log.Trace("User Manager: No user stats updates to commit")
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
statsQueue := a.statsQueue
|
|
|
|
a.statsQueue = make(map[string]*User)
|
|
|
|
a.mu.Unlock()
|
|
|
|
tx, err := a.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2023-01-07 14:34:02 +00:00
|
|
|
log.Debug("User Manager: Writing user stats queue for %d user(s)", len(statsQueue))
|
2023-01-22 04:15:22 +00:00
|
|
|
for userID, u := range statsQueue {
|
|
|
|
log.Trace("User Manager: Updating stats for user %s: messages=%d, emails=%d", userID, u.Stats.Messages, u.Stats.Emails)
|
|
|
|
if _, err := tx.Exec(updateUserStatsQuery, u.Stats.Messages, u.Stats.Emails, userID); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
2022-12-08 02:26:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// Authorize returns nil if the given user has access to the given topic using the desired
|
|
|
|
// permission. The user param may be nil to signal an anonymous user.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) Authorize(user *User, topic string, perm Permission) error {
|
2022-12-26 03:29:55 +00:00
|
|
|
if user != nil && user.Role == RoleAdmin {
|
|
|
|
return nil // Admin can do everything
|
|
|
|
}
|
|
|
|
username := Everyone
|
|
|
|
if user != nil {
|
|
|
|
username = user.Name
|
|
|
|
}
|
|
|
|
// Select the read/write permissions for this user/topic combo. The query may return two
|
2023-01-01 20:21:43 +00:00
|
|
|
// rows (one for everyone, and one for the user), but prioritizes the user.
|
2023-01-03 01:08:37 +00:00
|
|
|
rows, err := a.db.Query(selectTopicPermsQuery, Everyone, username, topic)
|
2022-12-26 03:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if !rows.Next() {
|
2023-01-03 02:12:42 +00:00
|
|
|
return a.resolvePerms(a.defaultAccess, perm)
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
|
|
|
var read, write bool
|
|
|
|
if err := rows.Scan(&read, &write); err != nil {
|
|
|
|
return err
|
|
|
|
} else if err := rows.Err(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-03 02:12:42 +00:00
|
|
|
return a.resolvePerms(NewPermission(read, write), perm)
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
2022-12-18 19:35:05 +00:00
|
|
|
|
2023-01-03 02:12:42 +00:00
|
|
|
func (a *Manager) resolvePerms(base, perm Permission) error {
|
|
|
|
if perm == PermissionRead && base.IsRead() {
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil
|
2023-01-03 02:12:42 +00:00
|
|
|
} else if perm == PermissionWrite && base.IsWrite() {
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ErrUnauthorized
|
|
|
|
}
|
2022-12-18 19:35:05 +00:00
|
|
|
|
2022-12-29 00:55:11 +00:00
|
|
|
// AddUser adds a user with the given username, password and role
|
2023-01-23 03:21:30 +00:00
|
|
|
func (a *Manager) AddUser(username, password string, role Role) error {
|
2022-12-26 03:29:55 +00:00
|
|
|
if !AllowedUsername(username) || !AllowedRole(role) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), userPasswordBcryptCost)
|
2022-12-26 03:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
userID := util.RandomStringPrefix(userIDPrefix, userIDLength)
|
|
|
|
syncTopic, now := util.RandomStringPrefix(syncTopicPrefix, syncTopicLength), time.Now().Unix()
|
2023-01-23 03:21:30 +00:00
|
|
|
if _, err = a.db.Exec(insertUserQuery, userID, username, hash, role, syncTopic, now); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-12-17 20:17:52 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// RemoveUser deletes the user with the given username. The function returns nil on success, even
|
|
|
|
// if the user did not exist in the first place.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) RemoveUser(username string) error {
|
2022-12-26 03:29:55 +00:00
|
|
|
if !AllowedUsername(username) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
2023-01-05 20:20:44 +00:00
|
|
|
// Rows in user_access, user_token, etc. are deleted via foreign keys
|
|
|
|
if _, err := a.db.Exec(deleteUserQuery, username); err != nil {
|
2022-12-29 18:08:47 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-05 20:20:44 +00:00
|
|
|
return nil
|
2022-12-08 02:26:18 +00:00
|
|
|
}
|
|
|
|
|
2023-01-23 03:21:30 +00:00
|
|
|
// MarkUserRemoved sets the deleted flag on the user, and deletes all access tokens. This prevents
|
|
|
|
// successful auth via Authenticate. A background process will delete the user at a later date.
|
|
|
|
func (a *Manager) MarkUserRemoved(user *User) error {
|
|
|
|
if !AllowedUsername(user.Name) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
|
|
|
tx, err := a.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
if _, err := a.db.Exec(deleteUserAccessQuery, user.Name, user.Name); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(deleteAllTokenQuery, user.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(updateUserDeletedQuery, time.Now().Add(userHardDeleteAfterDuration).Unix(), user.ID); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// Users returns a list of users. It always also returns the Everyone user ("*").
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) Users() ([]*User, error) {
|
2022-12-26 03:29:55 +00:00
|
|
|
rows, err := a.db.Query(selectUsernamesQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
usernames := make([]string, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var username string
|
|
|
|
if err := rows.Scan(&username); err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
usernames = append(usernames, username)
|
|
|
|
}
|
|
|
|
rows.Close()
|
|
|
|
users := make([]*User, 0)
|
|
|
|
for _, username := range usernames {
|
|
|
|
user, err := a.User(username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
users = append(users, user)
|
|
|
|
}
|
|
|
|
return users, nil
|
2022-01-24 04:02:39 +00:00
|
|
|
}
|
|
|
|
|
2023-01-14 11:43:44 +00:00
|
|
|
// User returns the user with the given username if it exists, or ErrUserNotFound otherwise.
|
2022-12-26 03:29:55 +00:00
|
|
|
// You may also pass Everyone to retrieve the anonymous user and its Grant list.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) User(username string) (*User, error) {
|
2022-12-26 03:29:55 +00:00
|
|
|
rows, err := a.db.Query(selectUserByNameQuery, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return a.readUser(rows)
|
2022-12-21 02:18:33 +00:00
|
|
|
}
|
|
|
|
|
2023-01-22 04:15:22 +00:00
|
|
|
// UserByID returns the user with the given ID if it exists, or ErrUserNotFound otherwise
|
|
|
|
func (a *Manager) UserByID(id string) (*User, error) {
|
|
|
|
rows, err := a.db.Query(selectUserByIDQuery, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return a.readUser(rows)
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:50:06 +00:00
|
|
|
// UserByStripeCustomer returns the user with the given Stripe customer ID if it exists, or ErrUserNotFound otherwise.
|
2023-01-14 11:43:44 +00:00
|
|
|
func (a *Manager) UserByStripeCustomer(stripeCustomerID string) (*User, error) {
|
|
|
|
rows, err := a.db.Query(selectUserByStripeCustomerIDQuery, stripeCustomerID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return a.readUser(rows)
|
|
|
|
}
|
|
|
|
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) userByToken(token string) (*User, error) {
|
2022-12-29 16:09:45 +00:00
|
|
|
rows, err := a.db.Query(selectUserByTokenQuery, token, time.Now().Unix())
|
2022-12-26 03:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return a.readUser(rows)
|
2022-01-23 05:02:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) readUser(rows *sql.Rows) (*User, error) {
|
2022-12-26 03:29:55 +00:00
|
|
|
defer rows.Close()
|
2023-01-22 04:15:22 +00:00
|
|
|
var id, username, hash, role, prefs, syncTopic string
|
2023-01-18 00:40:03 +00:00
|
|
|
var stripeCustomerID, stripeSubscriptionID, stripeSubscriptionStatus, stripePriceID, tierCode, tierName sql.NullString
|
2022-12-26 03:29:55 +00:00
|
|
|
var messages, emails int64
|
2023-01-25 15:05:54 +00:00
|
|
|
var messagesLimit, messagesExpiryDuration, emailsLimit, reservationsLimit, attachmentFileSizeLimit, attachmentTotalSizeLimit, attachmentExpiryDuration, attachmentBandwidthLimit, stripeSubscriptionPaidUntil, stripeSubscriptionCancelAt, deleted sql.NullInt64
|
2022-12-26 03:29:55 +00:00
|
|
|
if !rows.Next() {
|
2023-01-14 11:43:44 +00:00
|
|
|
return nil, ErrUserNotFound
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
2023-01-25 15:05:54 +00:00
|
|
|
if err := rows.Scan(&id, &username, &hash, &role, &prefs, &syncTopic, &messages, &emails, &stripeCustomerID, &stripeSubscriptionID, &stripeSubscriptionStatus, &stripeSubscriptionPaidUntil, &stripeSubscriptionCancelAt, &deleted, &tierCode, &tierName, &messagesLimit, &messagesExpiryDuration, &emailsLimit, &reservationsLimit, &attachmentFileSizeLimit, &attachmentTotalSizeLimit, &attachmentExpiryDuration, &attachmentBandwidthLimit, &stripePriceID); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil, err
|
|
|
|
} else if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
user := &User{
|
2023-01-22 04:15:22 +00:00
|
|
|
ID: id,
|
2023-01-10 02:53:21 +00:00
|
|
|
Name: username,
|
|
|
|
Hash: hash,
|
|
|
|
Role: Role(role),
|
|
|
|
Prefs: &Prefs{},
|
|
|
|
SyncTopic: syncTopic,
|
2022-12-26 03:29:55 +00:00
|
|
|
Stats: &Stats{
|
|
|
|
Messages: messages,
|
|
|
|
Emails: emails,
|
|
|
|
},
|
2023-01-16 04:29:46 +00:00
|
|
|
Billing: &Billing{
|
|
|
|
StripeCustomerID: stripeCustomerID.String, // May be empty
|
|
|
|
StripeSubscriptionID: stripeSubscriptionID.String, // May be empty
|
|
|
|
StripeSubscriptionStatus: stripe.SubscriptionStatus(stripeSubscriptionStatus.String), // May be empty
|
|
|
|
StripeSubscriptionPaidUntil: time.Unix(stripeSubscriptionPaidUntil.Int64, 0), // May be zero
|
2023-01-16 15:35:12 +00:00
|
|
|
StripeSubscriptionCancelAt: time.Unix(stripeSubscriptionCancelAt.Int64, 0), // May be zero
|
2023-01-16 04:29:46 +00:00
|
|
|
},
|
2023-01-23 03:21:30 +00:00
|
|
|
Deleted: deleted.Valid,
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
2023-01-10 02:53:21 +00:00
|
|
|
if err := json.Unmarshal([]byte(prefs), user.Prefs); err != nil {
|
|
|
|
return nil, err
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
2023-01-08 02:04:13 +00:00
|
|
|
if tierCode.Valid {
|
2023-01-14 11:43:44 +00:00
|
|
|
// See readTier() when this is changed!
|
2023-01-08 02:04:13 +00:00
|
|
|
user.Tier = &Tier{
|
|
|
|
Code: tierCode.String,
|
2023-01-09 20:40:46 +00:00
|
|
|
Name: tierName.String,
|
2022-12-26 03:29:55 +00:00
|
|
|
MessagesLimit: messagesLimit.Int64,
|
2023-01-09 20:40:46 +00:00
|
|
|
MessagesExpiryDuration: time.Duration(messagesExpiryDuration.Int64) * time.Second,
|
2022-12-26 03:29:55 +00:00
|
|
|
EmailsLimit: emailsLimit.Int64,
|
2023-01-08 02:04:13 +00:00
|
|
|
ReservationsLimit: reservationsLimit.Int64,
|
2022-12-26 03:29:55 +00:00
|
|
|
AttachmentFileSizeLimit: attachmentFileSizeLimit.Int64,
|
|
|
|
AttachmentTotalSizeLimit: attachmentTotalSizeLimit.Int64,
|
2023-01-09 20:40:46 +00:00
|
|
|
AttachmentExpiryDuration: time.Duration(attachmentExpiryDuration.Int64) * time.Second,
|
2023-01-25 15:05:54 +00:00
|
|
|
AttachmentBandwidthLimit: attachmentBandwidthLimit.Int64,
|
2023-01-17 15:09:37 +00:00
|
|
|
StripePriceID: stripePriceID.String, // May be empty
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
2022-01-23 05:02:16 +00:00
|
|
|
|
2023-01-03 01:08:37 +00:00
|
|
|
// Grants returns all user-specific access control entries
|
|
|
|
func (a *Manager) Grants(username string) ([]Grant, error) {
|
2022-12-26 03:29:55 +00:00
|
|
|
rows, err := a.db.Query(selectUserAccessQuery, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
grants := make([]Grant, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var topic string
|
2023-01-03 01:08:37 +00:00
|
|
|
var read, write bool
|
|
|
|
if err := rows.Scan(&topic, &read, &write); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return nil, err
|
|
|
|
} else if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
grants = append(grants, Grant{
|
|
|
|
TopicPattern: fromSQLWildcard(topic),
|
2023-01-03 02:12:42 +00:00
|
|
|
Allow: NewPermission(read, write),
|
2022-12-26 03:29:55 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return grants, nil
|
|
|
|
}
|
2022-01-23 05:02:16 +00:00
|
|
|
|
2023-01-03 01:08:37 +00:00
|
|
|
// Reservations returns all user-owned topics, and the associated everyone-access
|
|
|
|
func (a *Manager) Reservations(username string) ([]Reservation, error) {
|
|
|
|
rows, err := a.db.Query(selectUserReservationsQuery, Everyone, username)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
reservations := make([]Reservation, 0)
|
|
|
|
for rows.Next() {
|
|
|
|
var topic string
|
2023-01-03 02:12:42 +00:00
|
|
|
var ownerRead, ownerWrite bool
|
2023-01-03 01:08:37 +00:00
|
|
|
var everyoneRead, everyoneWrite sql.NullBool
|
2023-01-03 02:12:42 +00:00
|
|
|
if err := rows.Scan(&topic, &ownerRead, &ownerWrite, &everyoneRead, &everyoneWrite); err != nil {
|
2023-01-03 01:08:37 +00:00
|
|
|
return nil, err
|
|
|
|
} else if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
reservations = append(reservations, Reservation{
|
2023-01-03 02:12:42 +00:00
|
|
|
Topic: topic,
|
|
|
|
Owner: NewPermission(ownerRead, ownerWrite),
|
|
|
|
Everyone: NewPermission(everyoneRead.Bool, everyoneWrite.Bool), // false if null
|
2023-01-03 01:08:37 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return reservations, nil
|
|
|
|
}
|
|
|
|
|
2023-01-06 15:45:38 +00:00
|
|
|
// HasReservation returns true if the given topic access is owned by the user
|
|
|
|
func (a *Manager) HasReservation(username, topic string) (bool, error) {
|
|
|
|
rows, err := a.db.Query(selectUserHasReservationQuery, username, topic)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if !rows.Next() {
|
|
|
|
return false, errNoRows
|
|
|
|
}
|
|
|
|
var count int64
|
|
|
|
if err := rows.Scan(&count); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return count > 0, nil
|
|
|
|
}
|
|
|
|
|
2023-01-06 02:15:10 +00:00
|
|
|
// ReservationsCount returns the number of reservations owned by this user
|
|
|
|
func (a *Manager) ReservationsCount(username string) (int64, error) {
|
|
|
|
rows, err := a.db.Query(selectUserReservationsCountQuery, username)
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if !rows.Next() {
|
|
|
|
return 0, errNoRows
|
|
|
|
}
|
|
|
|
var count int64
|
|
|
|
if err := rows.Scan(&count); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// ChangePassword changes a user's password
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) ChangePassword(username, password string) error {
|
2023-01-22 04:15:22 +00:00
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), userPasswordBcryptCost)
|
2022-12-26 03:29:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := a.db.Exec(updateUserPassQuery, hash, username); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-23 05:02:16 +00:00
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// ChangeRole changes a user's role. When a role is changed from RoleUser to RoleAdmin,
|
|
|
|
// all existing access control entries (Grant) are removed, since they are no longer needed.
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) ChangeRole(username string, role Role) error {
|
2022-12-26 03:29:55 +00:00
|
|
|
if !AllowedUsername(username) || !AllowedRole(role) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
|
|
|
if _, err := a.db.Exec(updateUserRoleQuery, string(role), username); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if role == RoleAdmin {
|
2023-01-09 20:40:46 +00:00
|
|
|
if _, err := a.db.Exec(deleteUserAccessQuery, username, username); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-23 20:30:30 +00:00
|
|
|
|
2023-01-21 03:47:37 +00:00
|
|
|
// ChangeTier changes a user's tier using the tier code. This function does not delete reservations, messages,
|
|
|
|
// or attachments, even if the new tier has lower limits in this regard. That has to be done elsewhere.
|
2023-01-08 02:04:13 +00:00
|
|
|
func (a *Manager) ChangeTier(username, tier string) error {
|
|
|
|
if !AllowedUsername(username) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
2023-01-21 03:47:37 +00:00
|
|
|
t, err := a.Tier(tier)
|
2023-01-08 02:04:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-01-21 03:47:37 +00:00
|
|
|
} else if err := a.checkReservationsLimit(username, t.ReservationsLimit); err != nil {
|
|
|
|
return err
|
2023-01-08 02:04:13 +00:00
|
|
|
}
|
2023-01-21 03:47:37 +00:00
|
|
|
if _, err := a.db.Exec(updateUserTierQuery, tier, username); err != nil {
|
|
|
|
return err
|
2023-01-08 02:04:13 +00:00
|
|
|
}
|
2023-01-21 03:47:37 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResetTier removes the tier from the given user
|
|
|
|
func (a *Manager) ResetTier(username string) error {
|
|
|
|
if !AllowedUsername(username) && username != Everyone && username != "" {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
} else if err := a.checkReservationsLimit(username, 0); err != nil {
|
2023-01-08 02:04:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-21 03:47:37 +00:00
|
|
|
_, err := a.db.Exec(deleteUserTierQuery, username)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Manager) checkReservationsLimit(username string, reservationsLimit int64) error {
|
|
|
|
u, err := a.User(username)
|
|
|
|
if err != nil {
|
2023-01-08 02:04:13 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-21 03:47:37 +00:00
|
|
|
if u.Tier != nil && reservationsLimit < u.Tier.ReservationsLimit {
|
|
|
|
reservations, err := a.Reservations(username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if int64(len(reservations)) > reservationsLimit {
|
|
|
|
return ErrTooManyReservations
|
|
|
|
}
|
|
|
|
}
|
2023-01-08 02:04:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-01 20:21:43 +00:00
|
|
|
// CheckAllowAccess tests if a user may create an access control entry for the given topic.
|
|
|
|
// If there are any ACL entries that are not owned by the user, an error is returned.
|
2023-01-22 04:15:22 +00:00
|
|
|
// FIXME is this the same as HasReservation?
|
2023-01-01 20:21:43 +00:00
|
|
|
func (a *Manager) CheckAllowAccess(username string, topic string) error {
|
|
|
|
if (!AllowedUsername(username) && username != Everyone) || !AllowedTopic(topic) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
|
|
|
rows, err := a.db.Query(selectOtherAccessCountQuery, topic, topic, username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
if !rows.Next() {
|
2023-01-06 01:22:34 +00:00
|
|
|
return errNoRows
|
2023-01-01 20:21:43 +00:00
|
|
|
}
|
|
|
|
var otherCount int
|
|
|
|
if err := rows.Scan(&otherCount); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if otherCount > 0 {
|
|
|
|
return errTopicOwnedByOthers
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// AllowAccess adds or updates an entry in th access control list for a specific user. It controls
|
2023-01-01 20:21:43 +00:00
|
|
|
// read/write access to a topic. The parameter topicPattern may include wildcards (*). The ACL entry
|
|
|
|
// owner may either be a user (username), or the system (empty).
|
2023-01-21 03:47:37 +00:00
|
|
|
func (a *Manager) AllowAccess(username string, topicPattern string, permission Permission) error {
|
2023-01-01 20:21:43 +00:00
|
|
|
if !AllowedUsername(username) && username != Everyone {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
} else if !AllowedTopicPattern(topicPattern) {
|
2022-12-26 03:29:55 +00:00
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
2023-01-21 03:47:37 +00:00
|
|
|
owner := ""
|
|
|
|
if _, err := a.db.Exec(upsertUserAccessQuery, username, toSQLWildcard(topicPattern), permission.IsRead(), permission.IsWrite(), owner, owner); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-01-31 16:44:58 +00:00
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// ResetAccess removes an access control list entry for a specific username/topic, or (if topic is
|
|
|
|
// empty) for an entire user. The parameter topicPattern may include wildcards (*).
|
2022-12-28 03:14:14 +00:00
|
|
|
func (a *Manager) ResetAccess(username string, topicPattern string) error {
|
2022-12-26 03:29:55 +00:00
|
|
|
if !AllowedUsername(username) && username != Everyone && username != "" {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
} else if !AllowedTopicPattern(topicPattern) && topicPattern != "" {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
|
|
|
if username == "" && topicPattern == "" {
|
|
|
|
_, err := a.db.Exec(deleteAllAccessQuery, username)
|
|
|
|
return err
|
|
|
|
} else if topicPattern == "" {
|
2023-01-09 20:40:46 +00:00
|
|
|
_, err := a.db.Exec(deleteUserAccessQuery, username, username)
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-09 20:40:46 +00:00
|
|
|
_, err := a.db.Exec(deleteTopicAccessQuery, username, username, toSQLWildcard(topicPattern))
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-01-22 04:15:22 +00:00
|
|
|
// AddReservation creates two access control entries for the given topic: one with full read/write access for the
|
|
|
|
// given user, and one for Everyone with the permission passed as everyone. The user also owns the entries, and
|
|
|
|
// can modify or delete them.
|
|
|
|
func (a *Manager) AddReservation(username string, topic string, everyone Permission) error {
|
|
|
|
if !AllowedUsername(username) || username == Everyone || !AllowedTopic(topic) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
|
|
|
tx, err := a.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
if _, err := tx.Exec(upsertUserAccessQuery, username, topic, true, true, username, username); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(upsertUserAccessQuery, Everyone, topic, everyone.IsRead(), everyone.IsWrite(), username, username); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveReservations deletes the access control entries associated with the given username/topic, as
|
|
|
|
// well as all entries with Everyone/topic. This is the counterpart for AddReservation.
|
2023-01-21 03:47:37 +00:00
|
|
|
func (a *Manager) RemoveReservations(username string, topics ...string) error {
|
|
|
|
if !AllowedUsername(username) || username == Everyone || len(topics) == 0 {
|
2023-01-09 20:40:46 +00:00
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
2023-01-21 03:47:37 +00:00
|
|
|
for _, topic := range topics {
|
|
|
|
if !AllowedTopic(topic) {
|
|
|
|
return ErrInvalidArgument
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tx, err := a.db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
|
|
|
for _, topic := range topics {
|
|
|
|
if _, err := tx.Exec(deleteTopicAccessQuery, username, username, topic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(deleteTopicAccessQuery, Everyone, Everyone, topic); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tx.Commit()
|
2022-01-23 20:30:30 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
// DefaultAccess returns the default read/write access if no access control entry matches
|
2023-01-03 02:12:42 +00:00
|
|
|
func (a *Manager) DefaultAccess() Permission {
|
|
|
|
return a.defaultAccess
|
2022-01-31 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-08 02:04:13 +00:00
|
|
|
// CreateTier creates a new tier in the database
|
|
|
|
func (a *Manager) CreateTier(tier *Tier) error {
|
2023-01-22 04:15:22 +00:00
|
|
|
tierID := util.RandomStringPrefix(tierIDPrefix, tierIDLength)
|
2023-01-25 15:05:54 +00:00
|
|
|
if _, err := a.db.Exec(insertTierQuery, tierID, tier.Code, tier.Name, tier.MessagesLimit, int64(tier.MessagesExpiryDuration.Seconds()), tier.EmailsLimit, tier.ReservationsLimit, tier.AttachmentFileSizeLimit, tier.AttachmentTotalSizeLimit, int64(tier.AttachmentExpiryDuration.Seconds()), tier.AttachmentBandwidthLimit, tier.StripePriceID); err != nil {
|
2023-01-08 02:04:13 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:50:06 +00:00
|
|
|
// ChangeBilling updates a user's billing fields, namely the Stripe customer ID, and subscription information
|
2023-01-21 03:47:37 +00:00
|
|
|
func (a *Manager) ChangeBilling(username string, billing *Billing) error {
|
|
|
|
if _, err := a.db.Exec(updateBillingQuery, nullString(billing.StripeCustomerID), nullString(billing.StripeSubscriptionID), nullString(string(billing.StripeSubscriptionStatus)), nullInt64(billing.StripeSubscriptionPaidUntil.Unix()), nullInt64(billing.StripeSubscriptionCancelAt.Unix()), username); err != nil {
|
2023-01-14 11:43:44 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:50:06 +00:00
|
|
|
// Tiers returns a list of all Tier structs
|
2023-01-17 15:09:37 +00:00
|
|
|
func (a *Manager) Tiers() ([]*Tier, error) {
|
|
|
|
rows, err := a.db.Query(selectTiersQuery)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
tiers := make([]*Tier, 0)
|
|
|
|
for {
|
|
|
|
tier, err := a.readTier(rows)
|
|
|
|
if err == ErrTierNotFound {
|
|
|
|
break
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tiers = append(tiers, tier)
|
|
|
|
}
|
|
|
|
return tiers, nil
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:50:06 +00:00
|
|
|
// Tier returns a Tier based on the code, or ErrTierNotFound if it does not exist
|
2023-01-14 11:43:44 +00:00
|
|
|
func (a *Manager) Tier(code string) (*Tier, error) {
|
|
|
|
rows, err := a.db.Query(selectTierByCodeQuery, code)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
defer rows.Close()
|
2023-01-14 11:43:44 +00:00
|
|
|
return a.readTier(rows)
|
|
|
|
}
|
|
|
|
|
2023-01-18 20:50:06 +00:00
|
|
|
// TierByStripePrice returns a Tier based on the Stripe price ID, or ErrTierNotFound if it does not exist
|
2023-01-14 11:43:44 +00:00
|
|
|
func (a *Manager) TierByStripePrice(priceID string) (*Tier, error) {
|
|
|
|
rows, err := a.db.Query(selectTierByPriceIDQuery, priceID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-17 15:09:37 +00:00
|
|
|
defer rows.Close()
|
2023-01-14 11:43:44 +00:00
|
|
|
return a.readTier(rows)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Manager) readTier(rows *sql.Rows) (*Tier, error) {
|
2023-01-22 04:15:22 +00:00
|
|
|
var id, code, name string
|
2023-01-18 00:40:03 +00:00
|
|
|
var stripePriceID sql.NullString
|
2023-01-25 15:05:54 +00:00
|
|
|
var messagesLimit, messagesExpiryDuration, emailsLimit, reservationsLimit, attachmentFileSizeLimit, attachmentTotalSizeLimit, attachmentExpiryDuration, attachmentBandwidthLimit sql.NullInt64
|
2023-01-14 11:43:44 +00:00
|
|
|
if !rows.Next() {
|
|
|
|
return nil, ErrTierNotFound
|
|
|
|
}
|
2023-01-25 15:05:54 +00:00
|
|
|
if err := rows.Scan(&id, &code, &name, &messagesLimit, &messagesExpiryDuration, &emailsLimit, &reservationsLimit, &attachmentFileSizeLimit, &attachmentTotalSizeLimit, &attachmentExpiryDuration, &attachmentBandwidthLimit, &stripePriceID); err != nil {
|
2023-01-14 11:43:44 +00:00
|
|
|
return nil, err
|
|
|
|
} else if err := rows.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// When changed, note readUser() as well
|
|
|
|
return &Tier{
|
2023-01-22 04:15:22 +00:00
|
|
|
ID: id,
|
2023-01-14 11:43:44 +00:00
|
|
|
Code: code,
|
|
|
|
Name: name,
|
|
|
|
MessagesLimit: messagesLimit.Int64,
|
|
|
|
MessagesExpiryDuration: time.Duration(messagesExpiryDuration.Int64) * time.Second,
|
|
|
|
EmailsLimit: emailsLimit.Int64,
|
|
|
|
ReservationsLimit: reservationsLimit.Int64,
|
|
|
|
AttachmentFileSizeLimit: attachmentFileSizeLimit.Int64,
|
|
|
|
AttachmentTotalSizeLimit: attachmentTotalSizeLimit.Int64,
|
|
|
|
AttachmentExpiryDuration: time.Duration(attachmentExpiryDuration.Int64) * time.Second,
|
2023-01-25 15:05:54 +00:00
|
|
|
AttachmentBandwidthLimit: attachmentBandwidthLimit.Int64,
|
2023-01-17 15:09:37 +00:00
|
|
|
StripePriceID: stripePriceID.String, // May be empty
|
2023-01-14 11:43:44 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
func toSQLWildcard(s string) string {
|
|
|
|
return strings.ReplaceAll(s, "*", "%")
|
2022-01-31 16:44:58 +00:00
|
|
|
}
|
|
|
|
|
2022-12-26 03:29:55 +00:00
|
|
|
func fromSQLWildcard(s string) string {
|
|
|
|
return strings.ReplaceAll(s, "%", "*")
|
|
|
|
}
|
|
|
|
|
2023-01-05 20:20:44 +00:00
|
|
|
func runStartupQueries(db *sql.DB, startupQueries string) error {
|
|
|
|
if _, err := db.Exec(startupQueries); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(builtinStartupQueries); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:08:47 +00:00
|
|
|
func setupDB(db *sql.DB) error {
|
2022-12-26 03:29:55 +00:00
|
|
|
// If 'schemaVersion' table does not exist, this must be a new database
|
|
|
|
rowsSV, err := db.Query(selectSchemaVersionQuery)
|
|
|
|
if err != nil {
|
2022-12-29 18:08:47 +00:00
|
|
|
return setupNewDB(db)
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
|
|
|
defer rowsSV.Close()
|
|
|
|
|
|
|
|
// If 'schemaVersion' table exists, read version and potentially upgrade
|
|
|
|
schemaVersion := 0
|
|
|
|
if !rowsSV.Next() {
|
|
|
|
return errors.New("cannot determine schema version: database file may be corrupt")
|
|
|
|
}
|
|
|
|
if err := rowsSV.Scan(&schemaVersion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rowsSV.Close()
|
|
|
|
|
|
|
|
// Do migrations
|
|
|
|
if schemaVersion == currentSchemaVersion {
|
|
|
|
return nil
|
2022-12-29 18:08:47 +00:00
|
|
|
} else if schemaVersion == 1 {
|
|
|
|
return migrateFrom1(db)
|
2022-12-26 03:29:55 +00:00
|
|
|
}
|
|
|
|
return fmt.Errorf("unexpected schema version found: %d", schemaVersion)
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:08:47 +00:00
|
|
|
func setupNewDB(db *sql.DB) error {
|
|
|
|
if _, err := db.Exec(createTablesQueries); err != nil {
|
2022-12-26 03:29:55 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := db.Exec(insertSchemaVersion, currentSchemaVersion); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-12-29 18:08:47 +00:00
|
|
|
|
|
|
|
func migrateFrom1(db *sql.DB) error {
|
|
|
|
log.Info("Migrating user database schema: from 1 to 2")
|
|
|
|
tx, err := db.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2023-01-22 04:15:22 +00:00
|
|
|
// Rename user -> user_old, and create new tables
|
2022-12-29 18:08:47 +00:00
|
|
|
if _, err := tx.Exec(migrate1To2RenameUserTableQueryNoTx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tx.Exec(createTablesQueriesNoTx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
// Insert users from user_old into new user table, with ID and sync_topic
|
|
|
|
rows, err := tx.Query(migrate1To2SelectAllOldUsernamesNoTx)
|
2023-01-10 20:41:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
2023-01-22 04:15:22 +00:00
|
|
|
usernames := make([]string, 0)
|
2023-01-10 20:41:08 +00:00
|
|
|
for rows.Next() {
|
2023-01-22 04:15:22 +00:00
|
|
|
var username string
|
|
|
|
if err := rows.Scan(&username); err != nil {
|
2023-01-10 20:41:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
usernames = append(usernames, username)
|
2023-01-10 20:41:08 +00:00
|
|
|
}
|
|
|
|
if err := rows.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
for _, username := range usernames {
|
|
|
|
userID := util.RandomStringPrefix(userIDPrefix, userIDLength)
|
|
|
|
syncTopic := util.RandomStringPrefix(syncTopicPrefix, syncTopicLength)
|
|
|
|
if _, err := tx.Exec(migrate1To2InsertUserNoTx, userID, syncTopic, username); err != nil {
|
2023-01-10 20:41:08 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2023-01-22 04:15:22 +00:00
|
|
|
// Migrate old "access" table to "user_access" and drop "access" and "user_old"
|
|
|
|
if _, err := tx.Exec(migrate1To2InsertFromOldTablesAndDropNoTx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-29 18:08:47 +00:00
|
|
|
if _, err := tx.Exec(updateSchemaVersion, 2); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := tx.Commit(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil // Update this when a new version is added
|
|
|
|
}
|
2023-01-16 04:29:46 +00:00
|
|
|
|
|
|
|
func nullString(s string) sql.NullString {
|
|
|
|
if s == "" {
|
|
|
|
return sql.NullString{}
|
|
|
|
}
|
|
|
|
return sql.NullString{String: s, Valid: true}
|
|
|
|
}
|
|
|
|
|
|
|
|
func nullInt64(v int64) sql.NullInt64 {
|
|
|
|
if v == 0 {
|
|
|
|
return sql.NullInt64{}
|
|
|
|
}
|
|
|
|
return sql.NullInt64{Int64: v, Valid: true}
|
|
|
|
}
|