2020-10-09 18:35:44 +02:00
|
|
|
// Copyright (c) 2020, Andreas Schneider
|
|
|
|
// All rights reserved.
|
|
|
|
//
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer in the
|
|
|
|
// documentation and/or other materials provided with the distribution.
|
|
|
|
// * Neither the name of the <organization> nor the
|
|
|
|
// names of its contributors may be used to endorse or promote products
|
|
|
|
// derived from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
// DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
|
|
|
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-10-10 19:06:49 +02:00
|
|
|
"encoding/json"
|
2019-08-04 20:25:58 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-09 18:35:44 +02:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
uuid "github.com/satori/go.uuid"
|
2019-08-04 20:25:58 +02:00
|
|
|
"github.com/tidwall/buntdb"
|
|
|
|
)
|
|
|
|
|
|
|
|
type UserStore interface {
|
2020-10-09 18:35:44 +02:00
|
|
|
AddUser(user User) (err error)
|
2019-08-04 20:25:58 +02:00
|
|
|
GetUser(username string) (user User, err error)
|
2020-10-09 18:35:44 +02:00
|
|
|
GetUsers() ([]User, error)
|
2020-10-17 14:34:42 +02:00
|
|
|
UpdateUser(user User) error
|
2019-08-04 20:25:58 +02:00
|
|
|
RemoveUser(username string) (err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ShareStore interface {
|
2020-10-12 19:51:44 +02:00
|
|
|
CreateShare() (Share, error)
|
|
|
|
UpdateShareAttributes(share Share) error
|
|
|
|
RemoveShare(id uuid.UUID) error
|
|
|
|
AddUserToShare(share Share, username string, role ShareRole) error
|
|
|
|
RemoveUserFromShare(share Share, username string) error
|
|
|
|
AddLogin(share Share, username string, login Login) error
|
|
|
|
RemoveLogin(share Share, username string, loginName string) error
|
|
|
|
|
2020-10-24 16:57:00 +02:00
|
|
|
GetShare(id string) (Share, error)
|
2020-10-12 19:51:44 +02:00
|
|
|
GetShares() ([]Share, error)
|
2020-10-18 15:04:11 +02:00
|
|
|
GetShareUsers(share Share) ([]ShareUser, error)
|
|
|
|
GetShareLogins(share Share, username string) ([]Login, error)
|
2020-10-27 19:48:09 +01:00
|
|
|
GetShareAccess(share Share, username string) (ShareRole, error)
|
2020-10-12 19:51:44 +02:00
|
|
|
|
2020-10-17 14:34:42 +02:00
|
|
|
FindShareByLogin(username, loginName string) (LoginShare, error)
|
|
|
|
FindSharesByUser(username string) ([]UserShare, error)
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 19:51:44 +02:00
|
|
|
var _ UserStore = (*DBStore)(nil)
|
|
|
|
var _ ShareStore = (*DBStore)(nil)
|
2020-10-09 18:35:44 +02:00
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
type GlobalRole string
|
2020-10-09 18:35:44 +02:00
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
const (
|
2020-10-09 18:35:44 +02:00
|
|
|
GlobalRoleUser GlobalRole = "user"
|
2019-08-04 20:25:58 +02:00
|
|
|
GlobalRoleAdmin GlobalRole = "admin"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ShareRole string
|
2020-10-09 18:35:44 +02:00
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
const (
|
|
|
|
ShareRoleReader ShareRole = "reader"
|
|
|
|
ShareRoleWriter ShareRole = "writer"
|
2020-10-09 18:35:44 +02:00
|
|
|
ShareRoleAdmin ShareRole = "admin"
|
2019-08-04 20:25:58 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type User struct {
|
2020-10-18 10:33:29 +02:00
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
Role GlobalRole
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type Share struct {
|
2020-10-12 19:51:44 +02:00
|
|
|
UUID uuid.UUID
|
|
|
|
Name string
|
|
|
|
Description string
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ShareUser struct {
|
|
|
|
Username string
|
2020-10-09 18:35:44 +02:00
|
|
|
Role ShareRole
|
|
|
|
}
|
|
|
|
|
|
|
|
type Login struct {
|
2020-10-12 19:51:44 +02:00
|
|
|
LoginName string
|
2020-10-09 18:35:44 +02:00
|
|
|
Password string
|
2020-10-18 10:33:29 +02:00
|
|
|
ReadOnly bool
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 19:51:44 +02:00
|
|
|
// View on a share from the perspective of a user of that share.
|
|
|
|
type UserShare struct {
|
|
|
|
Share
|
|
|
|
Role ShareRole
|
|
|
|
}
|
|
|
|
|
|
|
|
// View on a share from the perspective of a specific login (with associated user).
|
|
|
|
type LoginShare struct {
|
|
|
|
Share
|
|
|
|
ShareUser
|
|
|
|
Login
|
|
|
|
}
|
|
|
|
|
|
|
|
const userPrefix = "user:"
|
|
|
|
const sharePrefix = "share:"
|
2020-10-17 14:34:42 +02:00
|
|
|
const loginSharePrefix = "loginshare:"
|
2020-10-18 10:33:29 +02:00
|
|
|
const shareuserPrefix = "shareuser:"
|
|
|
|
const shareloginPrefix = "sharelogin:"
|
2020-10-09 18:35:44 +02:00
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
type DBStore struct {
|
|
|
|
db *buntdb.DB
|
|
|
|
}
|
|
|
|
|
2020-10-09 18:35:44 +02:00
|
|
|
func (u User) key() string {
|
2020-10-12 19:51:44 +02:00
|
|
|
return userPrefix + u.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Share) key() string {
|
|
|
|
return sharePrefix + s.UUID.String()
|
2020-10-09 18:35:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-18 10:33:29 +02:00
|
|
|
func (s Share) userKey(username string) string {
|
|
|
|
return fmt.Sprintf("%s%s:%s", shareuserPrefix, s.UUID.String(), username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s Share) loginKey(username, loginName string) string {
|
|
|
|
return fmt.Sprintf("%s%s:%s:%s", shareloginPrefix, s.UUID.String(), username, loginName)
|
|
|
|
}
|
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
func NewDBStore(filename string) (*DBStore, error) {
|
|
|
|
db, err := buntdb.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DBStore{db}, nil
|
|
|
|
}
|
|
|
|
|
2020-10-09 18:35:44 +02:00
|
|
|
func (store *DBStore) Close() error {
|
2020-10-10 16:29:26 +02:00
|
|
|
if err := store.db.Shrink(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-09 18:35:44 +02:00
|
|
|
return store.db.Close()
|
|
|
|
}
|
|
|
|
|
2020-10-10 16:30:35 +02:00
|
|
|
var ErrUserExists = errors.New("user already exists")
|
2019-08-04 20:25:58 +02:00
|
|
|
var ErrUserNotFound = errors.New("user not found")
|
|
|
|
var ErrInvalidUsername = errors.New("invalid username")
|
|
|
|
|
2020-10-10 19:06:49 +02:00
|
|
|
func (u User) merge(updates User) (User, error) {
|
|
|
|
merged := u
|
2020-10-09 18:35:44 +02:00
|
|
|
|
2020-10-10 19:06:49 +02:00
|
|
|
if updates.Password != "" {
|
2020-10-17 14:50:21 +02:00
|
|
|
merged.Password = updates.Password
|
2020-10-09 18:35:44 +02:00
|
|
|
}
|
2020-10-10 19:06:49 +02:00
|
|
|
if updates.Role != "" {
|
|
|
|
merged.Role = updates.Role
|
2020-10-09 18:35:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-10 19:06:49 +02:00
|
|
|
return merged, nil
|
2020-10-09 18:35:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-12 19:51:44 +02:00
|
|
|
var ErrShareNotFound = errors.New("share not found")
|
2020-10-17 14:34:42 +02:00
|
|
|
var ErrLoginNotFound = errors.New("login not found")
|
2020-10-13 19:59:35 +02:00
|
|
|
var ErrLoginDuplicate = errors.New("login already exists")
|
2020-10-12 19:51:44 +02:00
|
|
|
|
2020-10-09 18:35:44 +02:00
|
|
|
func (store *DBStore) AddUser(user User) (err error) {
|
|
|
|
if strings.Contains(user.Username, ":") {
|
|
|
|
return ErrInvalidUsername
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
if err = store.db.Update(func(tx *buntdb.Tx) error {
|
2020-10-10 19:06:49 +02:00
|
|
|
userBytes, err := json.Marshal(user)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal user: %w", err)
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
2020-10-09 18:35:44 +02:00
|
|
|
|
2020-10-10 19:06:49 +02:00
|
|
|
if _, exists, err := tx.Set(user.key(), string(userBytes), nil); err != nil {
|
2019-08-04 20:25:58 +02:00
|
|
|
return err
|
2020-10-09 18:35:44 +02:00
|
|
|
} else if exists {
|
2020-10-10 16:30:35 +02:00
|
|
|
return ErrUserExists
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
2020-10-10 19:06:49 +02:00
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) GetUser(username string) (user User, err error) {
|
|
|
|
if strings.Contains(username, ":") {
|
|
|
|
return user, ErrInvalidUsername
|
|
|
|
}
|
|
|
|
if err := store.db.View(func(tx *buntdb.Tx) error {
|
2020-10-10 19:06:49 +02:00
|
|
|
user.Username = username
|
|
|
|
if val, err := tx.Get(user.key()); err != nil && err != buntdb.ErrNotFound {
|
2019-08-04 20:25:58 +02:00
|
|
|
return err
|
2020-10-09 18:35:44 +02:00
|
|
|
} else if err == buntdb.ErrNotFound {
|
|
|
|
return ErrUserNotFound
|
2020-10-10 19:06:49 +02:00
|
|
|
} else if err := json.Unmarshal([]byte(val), &user); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal user: %w", err)
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
2020-10-10 19:06:49 +02:00
|
|
|
|
2019-08-04 20:25:58 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return user, err
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
2020-10-09 18:35:44 +02:00
|
|
|
func (store *DBStore) GetUsers() (users []User, err error) {
|
|
|
|
err = store.db.View(func(tx *buntdb.Tx) error {
|
2020-10-10 19:06:49 +02:00
|
|
|
var processingError error
|
|
|
|
|
|
|
|
if err := tx.AscendKeys(userPrefix+"*", func(key, value string) bool {
|
2020-10-09 18:35:44 +02:00
|
|
|
var user User
|
2020-10-10 19:06:49 +02:00
|
|
|
|
|
|
|
if err := json.Unmarshal([]byte(value), &user); err != nil {
|
|
|
|
processingError = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just in case ...
|
|
|
|
user.Username = strings.TrimPrefix(key, userPrefix)
|
|
|
|
|
2020-10-09 18:35:44 +02:00
|
|
|
users = append(users, user)
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
2020-10-10 19:06:49 +02:00
|
|
|
return processingError
|
2019-08-04 20:25:58 +02:00
|
|
|
})
|
2020-10-09 18:35:44 +02:00
|
|
|
|
|
|
|
return users, err
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
2020-10-17 14:34:42 +02:00
|
|
|
func (store *DBStore) UpdateUser(user User) error {
|
2020-10-09 18:35:44 +02:00
|
|
|
if strings.Contains(user.Username, ":") {
|
|
|
|
return ErrInvalidUsername
|
|
|
|
}
|
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
2020-10-10 19:06:49 +02:00
|
|
|
var existingUser User
|
|
|
|
|
|
|
|
if val, err := tx.Get(user.key()); err != nil && err != buntdb.ErrNotFound {
|
|
|
|
return err
|
|
|
|
} else if err == buntdb.ErrNotFound {
|
|
|
|
return ErrUserNotFound
|
|
|
|
} else if err := json.Unmarshal([]byte(val), &existingUser); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal user: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mergedUser, err := existingUser.merge(user)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot merge user: %w", err)
|
|
|
|
}
|
|
|
|
mergedUser.Username = user.Username
|
|
|
|
|
|
|
|
userBytes, err := json.Marshal(mergedUser)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal user: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _, err = tx.Set(mergedUser.key(), string(userBytes), nil)
|
|
|
|
|
2020-10-09 18:35:44 +02:00
|
|
|
return err
|
|
|
|
})
|
2019-08-04 20:25:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) RemoveUser(username string) (err error) {
|
|
|
|
if strings.Contains(username, ":") {
|
|
|
|
return ErrInvalidUsername
|
|
|
|
}
|
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
2020-10-09 18:35:44 +02:00
|
|
|
user := User{Username: username}
|
|
|
|
if _, err := tx.Delete(user.key()); err == buntdb.ErrNotFound {
|
|
|
|
return ErrUserNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2019-08-04 20:25:58 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-12 19:51:44 +02:00
|
|
|
func (store *DBStore) CreateShare() (Share, error) {
|
|
|
|
share := Share{
|
|
|
|
UUID: uuid.NewV4(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := store.db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
b, err := json.Marshal(share)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal share: %w", err)
|
|
|
|
}
|
|
|
|
if _, _, err := tx.Set(share.key(), string(b), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return Share{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return share, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) UpdateShareAttributes(share Share) error {
|
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
var existingShare Share
|
|
|
|
if val, err := tx.Get(share.key()); err == buntdb.ErrNotFound {
|
|
|
|
return ErrShareNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
} else if err := json.Unmarshal([]byte(val), &existingShare); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal share %s: %w", share.UUID, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
existingShare.UUID = share.UUID
|
|
|
|
existingShare.Name = share.Name
|
|
|
|
existingShare.Description = share.Description
|
|
|
|
|
|
|
|
if b, err := json.Marshal(existingShare); err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal share %s: %w", share.UUID, err)
|
|
|
|
} else if _, _, err := tx.Set(existingShare.key(), string(b), nil); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) RemoveShare(id uuid.UUID) error {
|
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
share := Share{UUID: id}
|
2020-10-17 14:34:42 +02:00
|
|
|
|
|
|
|
var userNames []string
|
2020-10-18 10:33:29 +02:00
|
|
|
usersPrefix := share.userKey("")
|
2020-10-17 14:34:42 +02:00
|
|
|
if err := tx.AscendKeys(usersPrefix+"*", func(key, value string) bool {
|
|
|
|
userNames = append(userNames, strings.TrimPrefix(key, usersPrefix))
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, username := range userNames {
|
|
|
|
if err := store.removeUserFromShare(tx, share, username); err != nil {
|
2020-10-18 10:33:29 +02:00
|
|
|
return fmt.Errorf("cannot remove user %q from share: %w", username, err)
|
2020-10-17 14:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-12 19:51:44 +02:00
|
|
|
if _, err := tx.Delete(share.key()); err == buntdb.ErrNotFound {
|
|
|
|
return ErrShareNotFound
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) AddUserToShare(share Share, username string, role ShareRole) error {
|
2020-10-17 14:34:42 +02:00
|
|
|
if strings.Contains(username, ":") {
|
|
|
|
return ErrInvalidUsername
|
|
|
|
}
|
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
if _, err := tx.Get(share.key()); err != nil {
|
|
|
|
return ErrShareNotFound
|
|
|
|
}
|
2020-10-18 10:33:29 +02:00
|
|
|
if _, _, err := tx.Set(share.userKey(username), string(role), nil); err != nil {
|
|
|
|
return fmt.Errorf("cannot set user: %w", err)
|
2020-10-17 14:34:42 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-27 19:48:09 +01:00
|
|
|
func (store *DBStore) GetShareAccess(share Share, username string) (ShareRole, error) {
|
|
|
|
if strings.Contains(username, ":") {
|
|
|
|
return "", ErrInvalidUsername
|
|
|
|
}
|
|
|
|
|
|
|
|
var shareRole ShareRole
|
|
|
|
|
|
|
|
err := store.db.View(func(tx *buntdb.Tx) error {
|
|
|
|
if _, err := tx.Get(share.key()); err != nil {
|
|
|
|
return ErrShareNotFound
|
|
|
|
}
|
|
|
|
if val, err := tx.Get(share.userKey(username)); err == buntdb.ErrNotFound {
|
|
|
|
return ErrUserNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return fmt.Errorf("cannot get user: %w", err)
|
|
|
|
} else {
|
|
|
|
shareRole = ShareRole(val)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return shareRole, err
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:34:42 +02:00
|
|
|
func (store *DBStore) removeUserFromShare(tx *buntdb.Tx, share Share, username string) error {
|
|
|
|
var logins []string
|
2020-10-18 10:33:29 +02:00
|
|
|
loginsPrefix := share.loginKey(username, "")
|
2020-10-17 14:34:42 +02:00
|
|
|
if err := tx.AscendKeys(loginsPrefix+"*", func(key, value string) bool {
|
|
|
|
logins = append(logins, strings.TrimPrefix(key, loginsPrefix))
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, loginName := range logins {
|
|
|
|
shareIdString, err := tx.Delete(loginSharePrefix + username + ":" + loginName)
|
|
|
|
if err != nil {
|
2020-10-18 10:33:29 +02:00
|
|
|
return fmt.Errorf("cannot remove login ref: %w", err)
|
2020-10-17 14:34:42 +02:00
|
|
|
} else if shareIdString != share.UUID.String() {
|
|
|
|
return fmt.Errorf("inconsistent login ref %q", username+":"+loginName)
|
|
|
|
}
|
|
|
|
|
2020-10-18 10:33:29 +02:00
|
|
|
if _, err := tx.Delete(share.loginKey(username, loginName)); err != nil {
|
|
|
|
return fmt.Errorf("cannot remove login %q: %w", loginName, err)
|
2020-10-17 14:34:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-18 10:33:29 +02:00
|
|
|
if _, err := tx.Delete(share.userKey(username)); err != nil {
|
|
|
|
return fmt.Errorf("cannot remove user: %w", err)
|
2020-10-17 14:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-10-12 19:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) RemoveUserFromShare(share Share, username string) error {
|
2020-10-17 14:34:42 +02:00
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
return store.removeUserFromShare(tx, share, username)
|
|
|
|
})
|
2020-10-12 19:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) AddLogin(share Share, username string, login Login) error {
|
2020-10-17 14:34:42 +02:00
|
|
|
if strings.Contains(username, ":") {
|
|
|
|
return ErrInvalidUsername
|
|
|
|
}
|
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
// Validate model. Share should exist and the user should already be assigned.
|
|
|
|
if _, err := tx.Get(share.key()); err != nil {
|
|
|
|
return ErrShareNotFound
|
|
|
|
}
|
2020-10-18 10:33:29 +02:00
|
|
|
if _, err := tx.Get(share.userKey(username)); err != nil {
|
2020-10-17 14:34:42 +02:00
|
|
|
return ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep a direct reference from username:login to the share, so we can later on
|
|
|
|
// easily validate login attempts. Also this is an easy approach to check if the
|
|
|
|
// username:login pair is unique. Otherwise our login scheme wouldn't work the
|
|
|
|
// way we expect.
|
|
|
|
// If the existing value happens to be our shareId already, we assume we are just
|
|
|
|
// updating the existing model. No harm there.
|
|
|
|
if existingValue, exists, err := tx.Set(loginSharePrefix+username+":"+login.LoginName, share.UUID.String(), nil); err != nil {
|
2020-10-18 10:33:29 +02:00
|
|
|
return fmt.Errorf("cannot set login reference: %w", err)
|
2020-10-17 14:34:42 +02:00
|
|
|
} else if exists && existingValue != share.UUID.String() {
|
|
|
|
return ErrLoginDuplicate
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now simply update the current login information.
|
2020-10-18 10:33:29 +02:00
|
|
|
b, err := json.Marshal(login)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot marshal login: %w", err)
|
|
|
|
}
|
|
|
|
if _, _, err := tx.Set(share.loginKey(username, login.LoginName), string(b), nil); err != nil {
|
|
|
|
return fmt.Errorf("cannot set login: %w", err)
|
2020-10-17 14:34:42 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2020-10-12 19:51:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) RemoveLogin(share Share, username string, loginName string) error {
|
2020-10-17 14:34:42 +02:00
|
|
|
return store.db.Update(func(tx *buntdb.Tx) error {
|
|
|
|
if _, err := tx.Get(share.key()); err != nil {
|
|
|
|
return ErrShareNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
if shareId, err := tx.Delete(loginSharePrefix + username + ":" + loginName); err != nil {
|
|
|
|
return ErrLoginNotFound
|
|
|
|
} else if shareId != share.UUID.String() {
|
|
|
|
return ErrLoginNotFound
|
|
|
|
}
|
|
|
|
|
2020-10-18 10:33:29 +02:00
|
|
|
if _, err := tx.Delete(share.loginKey(username, loginName)); err != nil {
|
2020-10-17 14:34:42 +02:00
|
|
|
return ErrLoginNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2020-10-12 19:51:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-24 16:57:00 +02:00
|
|
|
func (store *DBStore) GetShare(id string) (Share, error) {
|
|
|
|
var share Share
|
|
|
|
if err := store.db.View(func(tx *buntdb.Tx) error {
|
|
|
|
val, err := tx.Get(sharePrefix + id)
|
|
|
|
if err == buntdb.ErrNotFound {
|
|
|
|
return ErrShareNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
share, err = unmarshalShare(id, val)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return Share{}, err
|
|
|
|
}
|
|
|
|
return share, nil
|
|
|
|
}
|
|
|
|
|
2020-10-12 19:51:44 +02:00
|
|
|
func (store *DBStore) GetShares() (shares []Share, err error) {
|
|
|
|
err = store.db.View(func(tx *buntdb.Tx) error {
|
|
|
|
var processingError error
|
|
|
|
|
|
|
|
if err := tx.AscendKeys(sharePrefix+"*", func(key, value string) bool {
|
|
|
|
idString := strings.TrimPrefix(key, sharePrefix)
|
2020-10-17 14:34:42 +02:00
|
|
|
share, err := unmarshalShare(idString, value)
|
|
|
|
if err != nil {
|
|
|
|
processingError = err
|
2020-10-12 19:51:44 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
shares = append(shares, share)
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return processingError
|
|
|
|
})
|
|
|
|
|
|
|
|
return shares, err
|
|
|
|
}
|
|
|
|
|
2020-10-18 15:04:11 +02:00
|
|
|
func (store *DBStore) GetShareUsers(share Share) (shareUsers []ShareUser, err error) {
|
|
|
|
err = store.db.View(func(tx *buntdb.Tx) error {
|
|
|
|
usersPrefix := share.userKey("")
|
|
|
|
if err := tx.AscendKeys(usersPrefix+"*", func(key, value string) bool {
|
|
|
|
var shareUser ShareUser
|
|
|
|
shareUser.Username = strings.TrimPrefix(key, usersPrefix)
|
|
|
|
shareUser.Role = ShareRole(value)
|
|
|
|
shareUsers = append(shareUsers, shareUser)
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (store *DBStore) GetShareLogins(share Share, username string) (logins []Login, err error) {
|
|
|
|
err = store.db.View(func(tx *buntdb.Tx) error {
|
|
|
|
var processingErr error
|
|
|
|
|
|
|
|
loginsPrefix := share.loginKey(username, "")
|
|
|
|
if err := tx.AscendKeys(loginsPrefix+"*", func(key, value string) bool {
|
|
|
|
var login Login
|
|
|
|
if err := json.Unmarshal([]byte(value), &login); err != nil {
|
|
|
|
processingErr = err
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
logins = append(logins, login)
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return processingErr
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-17 14:34:42 +02:00
|
|
|
func (store *DBStore) FindShareByLogin(username, loginName string) (loginShare LoginShare, err error) {
|
|
|
|
err = store.db.View(func(tx *buntdb.Tx) error {
|
|
|
|
shareIdString, err := tx.Get(loginSharePrefix + username + ":" + loginName)
|
|
|
|
if err == buntdb.ErrNotFound {
|
|
|
|
return ErrShareNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sharePayload, err := tx.Get(sharePrefix + shareIdString)
|
|
|
|
if err == buntdb.ErrNotFound {
|
|
|
|
// TODO warn about inconsistency
|
|
|
|
return ErrShareNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-18 10:33:29 +02:00
|
|
|
share, err := unmarshalShare(shareIdString, sharePayload)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
userRole, err := tx.Get(share.userKey(username))
|
2020-10-17 14:34:42 +02:00
|
|
|
if err == buntdb.ErrNotFound {
|
|
|
|
// TODO warn about inconsistency
|
|
|
|
return ErrShareNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-10-18 10:33:29 +02:00
|
|
|
loginPayload, err := tx.Get(share.loginKey(username, loginName))
|
2020-10-17 14:34:42 +02:00
|
|
|
if err == buntdb.ErrNotFound {
|
|
|
|
// TODO warn about inconsistency
|
|
|
|
return ErrShareNotFound
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
loginShare.Share = share
|
|
|
|
loginShare.ShareUser = ShareUser{
|
|
|
|
Username: username,
|
|
|
|
Role: ShareRole(userRole),
|
|
|
|
}
|
2020-10-18 10:33:29 +02:00
|
|
|
if err := json.Unmarshal([]byte(loginPayload), &loginShare.Login); err != nil {
|
|
|
|
return fmt.Errorf("cannot unmarshal login: %w", err)
|
2020-10-17 14:34:42 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
2020-10-12 19:51:44 +02:00
|
|
|
}
|
|
|
|
|
2020-10-18 15:04:11 +02:00
|
|
|
func (store *DBStore) FindSharesByUser(username string) (userShares []UserShare, err error) {
|
|
|
|
prefix := shareuserPrefix
|
|
|
|
suffix := ":" + username
|
|
|
|
|
|
|
|
err = store.db.View(func(tx *buntdb.Tx) error {
|
|
|
|
var processingError error
|
|
|
|
if err := tx.AscendKeys(prefix+"*"+suffix, func(key, value string) bool {
|
|
|
|
var userShare UserShare
|
|
|
|
shareIdString := strings.TrimSuffix(strings.TrimPrefix(key, prefix), suffix)
|
|
|
|
|
|
|
|
if sharePayload, err := tx.Get(sharePrefix + shareIdString); err == buntdb.ErrNotFound {
|
|
|
|
// TODO report inconsistency
|
|
|
|
processingError = ErrShareNotFound
|
|
|
|
return false
|
|
|
|
} else if err != nil {
|
|
|
|
processingError = err
|
|
|
|
return false
|
|
|
|
} else if err := json.Unmarshal([]byte(sharePayload), &userShare.Share); err != nil {
|
|
|
|
processingError = fmt.Errorf("cannot unmarshal share: %w", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
userShare.Role = ShareRole(value)
|
|
|
|
|
|
|
|
userShares = append(userShares, userShare)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return processingError
|
|
|
|
})
|
|
|
|
return
|
2020-10-12 19:51:44 +02:00
|
|
|
}
|
2020-10-17 14:34:42 +02:00
|
|
|
|
|
|
|
func unmarshalShare(idString, payload string) (Share, error) {
|
|
|
|
var share Share
|
|
|
|
if err := json.Unmarshal([]byte(payload), &share); err != nil {
|
2020-10-18 10:33:29 +02:00
|
|
|
return Share{}, fmt.Errorf("cannot unmarshal share: %w", err)
|
2020-10-17 14:34:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Just in case ...
|
|
|
|
if id, err := uuid.FromString(idString); err != nil {
|
|
|
|
return Share{}, fmt.Errorf("invalid uuid in db: %q: %w", idString, err)
|
|
|
|
} else {
|
|
|
|
share.UUID = id
|
|
|
|
}
|
|
|
|
|
|
|
|
return share, nil
|
|
|
|
}
|