Gastansicht, Verbesserungen bei Planung
This commit is contained in:
1 parent
b3afa3ef32
commit
c08d43e7dd
38 files changed
+3845
-219
No files matched your search
@@ -0,0 +1,325 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"lektorenplan/domain"
|
||||
)
|
||||
|
||||
// ReaderContact is one reader account together with the person it represents.
|
||||
// The e-mail address is only filled when the requesting session holds the data
|
||||
// key for that person, i.e. after the owner shared it.
|
||||
type ReaderContact struct {
|
||||
UserID string
|
||||
Person domain.Person
|
||||
}
|
||||
|
||||
// ReaderDirectory lists the active reader accounts linked to a tenant. The
|
||||
// contacts are hydrated with the current request's keyring, so addresses that
|
||||
// were not shared remain empty and locked.
|
||||
func (s *Store) ReaderDirectory(ctx context.Context, tenantID string) ([]ReaderContact, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT u.id, u.linked_person_id
|
||||
FROM users u
|
||||
JOIN people p ON p.tenant_id=u.linked_tenant_id AND p.id=u.linked_person_id
|
||||
WHERE u.active=1 AND u.role='reader' AND u.linked_tenant_id=?
|
||||
ORDER BY p.last_name COLLATE NOCASE, p.first_name COLLATE NOCASE, u.id`, tenantID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
type readerRow struct {
|
||||
userID string
|
||||
personID string
|
||||
}
|
||||
var ids []readerRow
|
||||
for rows.Next() {
|
||||
var row readerRow
|
||||
if err := rows.Scan(&row.userID, &row.personID); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
ids = append(ids, row)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
rows.Close()
|
||||
|
||||
out := make([]ReaderContact, 0, len(ids))
|
||||
for _, row := range ids {
|
||||
person, err := s.Person(ctx, tenantID, row.personID)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, ReaderContact{UserID: row.userID, Person: *person})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ReaderForPerson returns the active reader account linked to a person.
|
||||
func (s *Store) ReaderForPerson(ctx context.Context, tenantID, personID string) (string, bool) {
|
||||
var userID string
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT id FROM users
|
||||
WHERE active=1 AND role='reader' AND linked_tenant_id=? AND linked_person_id=?
|
||||
ORDER BY id LIMIT 1`, tenantID, personID).Scan(&userID)
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return userID, true
|
||||
}
|
||||
|
||||
// ContactSharesForOwner lists the reader accounts that may see the owner's
|
||||
// e-mail address.
|
||||
func (s *Store) ContactSharesForOwner(ctx context.Context, tenantID, ownerPersonID string) (map[string]bool, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT viewer_user_id FROM contact_shares
|
||||
WHERE tenant_id=? AND owner_person_id=?`, tenantID, ownerPersonID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
out := map[string]bool{}
|
||||
for rows.Next() {
|
||||
var userID string
|
||||
if err := rows.Scan(&userID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out[userID] = true
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ContactSharesForViewer lists the persons whose e-mail addresses were shared
|
||||
// with one reader account.
|
||||
func (s *Store) ContactSharesForViewer(ctx context.Context, tenantID, viewerUserID string) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT owner_person_id FROM contact_shares
|
||||
WHERE tenant_id=? AND viewer_user_id=? ORDER BY owner_person_id`, tenantID, viewerUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var personID string
|
||||
if err := rows.Scan(&personID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, personID)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
func (s *Store) contactShareViewers(ctx context.Context, tenantID, ownerPersonID string) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT viewer_user_id FROM contact_shares
|
||||
WHERE tenant_id=? AND owner_person_id=? ORDER BY viewer_user_id`, tenantID, ownerPersonID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var userID string
|
||||
if err := rows.Scan(&userID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, userID)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
|
||||
// ContactShareExists reports whether ownerPersonID shared the address with the
|
||||
// given account.
|
||||
func (s *Store) ContactShareExists(ctx context.Context, tenantID, ownerPersonID, viewerUserID string) bool {
|
||||
var count int
|
||||
err := s.db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM contact_shares
|
||||
WHERE tenant_id=? AND owner_person_id=? AND viewer_user_id=?`,
|
||||
tenantID, ownerPersonID, viewerUserID).Scan(&count)
|
||||
return err == nil && count > 0
|
||||
}
|
||||
|
||||
// SetContactShares replaces the viewer list of one owner. Newly selected
|
||||
// readers receive the encrypted person scope; removed readers lose their
|
||||
// wrapper immediately. The returned lists contain the actual changes.
|
||||
func (s *Store) SetContactShares(ctx context.Context, keyring *Keyring, tenantID, ownerUserID, ownerPersonID string, viewerUserIDs []string) (added, removed []string, err error) {
|
||||
// Refuse before touching the consent table when the session cannot grant
|
||||
// the encrypted scope; otherwise a failed save would leave an announced
|
||||
// share without a usable key.
|
||||
if s.SecretIsInitialized(ctx, tenantID) && keyring == nil {
|
||||
return nil, nil, errKeyLocked
|
||||
}
|
||||
current, err := s.ContactSharesForOwner(ctx, tenantID, ownerPersonID)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
wanted := map[string]bool{}
|
||||
for _, raw := range viewerUserIDs {
|
||||
userID := strings.TrimSpace(raw)
|
||||
if userID == "" || userID == ownerUserID || wanted[userID] {
|
||||
continue
|
||||
}
|
||||
var count int
|
||||
if err := s.db.QueryRowContext(ctx, `
|
||||
SELECT COUNT(*) FROM users
|
||||
WHERE id=? AND active=1 AND role='reader' AND linked_tenant_id=?
|
||||
AND linked_person_id<>'' AND linked_person_id<>?`,
|
||||
userID, tenantID, ownerPersonID).Scan(&count); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if count == 0 {
|
||||
return nil, nil, errors.New("Ein ausgewähltes Konto ist kein aktiver Leserzugang dieses Mandanten")
|
||||
}
|
||||
wanted[userID] = true
|
||||
}
|
||||
for userID := range current {
|
||||
if !wanted[userID] {
|
||||
removed = append(removed, userID)
|
||||
}
|
||||
}
|
||||
for userID := range wanted {
|
||||
if !current[userID] {
|
||||
added = append(added, userID)
|
||||
}
|
||||
}
|
||||
sort.Strings(added)
|
||||
sort.Strings(removed)
|
||||
|
||||
if err := s.transaction(ctx, func(tx *sql.Tx) error {
|
||||
for _, userID := range removed {
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
DELETE FROM contact_shares
|
||||
WHERE tenant_id=? AND owner_person_id=? AND viewer_user_id=?`,
|
||||
tenantID, ownerPersonID, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
for _, userID := range added {
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
INSERT INTO contact_shares(tenant_id, owner_person_id, viewer_user_id, granted_at)
|
||||
VALUES(?, ?, ?, ?)
|
||||
ON CONFLICT DO NOTHING`,
|
||||
tenantID, ownerPersonID, userID, time.Now().UTC().Format(time.RFC3339)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// A pending swap is only possible while both sides still release
|
||||
// their addresses. Revoking a share cancels affected requests so the
|
||||
// plan never keeps a swap-requested mark for an impossible swap.
|
||||
if _, err := tx.ExecContext(ctx, `
|
||||
UPDATE swap_requests SET status=?, responded_at=?
|
||||
WHERE tenant_id=? AND status=? AND NOT (
|
||||
EXISTS (
|
||||
SELECT 1 FROM contact_shares a
|
||||
WHERE a.tenant_id=swap_requests.tenant_id
|
||||
AND a.owner_person_id=swap_requests.target_person_id
|
||||
AND a.viewer_user_id=swap_requests.requester_user_id
|
||||
) AND EXISTS (
|
||||
SELECT 1 FROM contact_shares b
|
||||
WHERE b.tenant_id=swap_requests.tenant_id
|
||||
AND b.owner_person_id=swap_requests.requester_person_id
|
||||
AND b.viewer_user_id=swap_requests.target_user_id
|
||||
)
|
||||
)`, SwapCancelled, time.Now().UTC().Format(time.RFC3339), tenantID, SwapPending); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if s.SecretIsInitialized(ctx, tenantID) {
|
||||
scope := KeyScope{Type: ScopePerson, TenantID: tenantID, ID: ownerPersonID}
|
||||
if len(added) > 0 {
|
||||
dek, err := s.ensureScope(ctx, keyring, scope)
|
||||
if err != nil {
|
||||
return added, removed, err
|
||||
}
|
||||
if err := s.grantScopeDEK(ctx, keyring, scope, dek, added); err != nil {
|
||||
return added, removed, err
|
||||
}
|
||||
}
|
||||
for _, userID := range removed {
|
||||
if _, err := s.db.ExecContext(ctx, `
|
||||
DELETE FROM key_wrappers
|
||||
WHERE scope_type=? AND tenant_id=? AND scope_id=? AND user_id=?`,
|
||||
ScopePerson, tenantID, ownerPersonID, userID); err != nil {
|
||||
return added, removed, err
|
||||
}
|
||||
}
|
||||
}
|
||||
return added, removed, nil
|
||||
}
|
||||
|
||||
// readerRosterHash fingerprints the current reader accounts of a tenant. The
|
||||
// owner's own account is excluded.
|
||||
func (s *Store) readerRosterHash(ctx context.Context, tenantID, ownerUserID string) (string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT id, linked_person_id FROM users
|
||||
WHERE active=1 AND role='reader' AND linked_tenant_id=? AND id<>?
|
||||
ORDER BY id`, tenantID, ownerUserID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer rows.Close()
|
||||
hash := sha256.New()
|
||||
for rows.Next() {
|
||||
var userID, personID string
|
||||
if err := rows.Scan(&userID, &personID); err != nil {
|
||||
return "", err
|
||||
}
|
||||
hash.Write([]byte(userID + "|" + personID + "\n"))
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(hash.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// ContactRosterChanged reports whether the reader directory changed since the
|
||||
// owner last reviewed the sharing list. known is false before the first save.
|
||||
func (s *Store) ContactRosterChanged(ctx context.Context, tenantID, ownerUserID, ownerPersonID string) (changed, known bool, err error) {
|
||||
current, err := s.readerRosterHash(ctx, tenantID, ownerUserID)
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
var stored string
|
||||
err = s.db.QueryRowContext(ctx, `
|
||||
SELECT roster_hash FROM contact_reviews
|
||||
WHERE tenant_id=? AND owner_person_id=?`, tenantID, ownerPersonID).Scan(&stored)
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return false, false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, false, err
|
||||
}
|
||||
return stored != current, true, nil
|
||||
}
|
||||
|
||||
// MarkContactRosterReviewed stores the current reader directory fingerprint.
|
||||
func (s *Store) MarkContactRosterReviewed(ctx context.Context, tenantID, ownerUserID, ownerPersonID string) error {
|
||||
hash, err := s.readerRosterHash(ctx, tenantID, ownerUserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = s.db.ExecContext(ctx, `
|
||||
INSERT INTO contact_reviews(tenant_id, owner_person_id, roster_hash, reviewed_at)
|
||||
VALUES(?, ?, ?, ?)
|
||||
ON CONFLICT(tenant_id, owner_person_id) DO UPDATE SET
|
||||
roster_hash=excluded.roster_hash, reviewed_at=excluded.reviewed_at`,
|
||||
tenantID, ownerPersonID, hash, time.Now().UTC().Format(time.RFC3339))
|
||||
return err
|
||||
}
|
||||
Reference in new issue
Block a user