mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-07 04:00:17 -06:00
[server] Get rid of default user
This commit is contained in:
@@ -5,38 +5,31 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/shroff/phylum/server/internal/core/db"
|
||||
"github.com/shroff/phylum/server/internal/core/fs"
|
||||
"github.com/shroff/phylum/server/internal/core/user"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var u user.User
|
||||
var u *user.User
|
||||
var f fs.FileSystem
|
||||
|
||||
func RootFileSystem() fs.FileSystem {
|
||||
var rootUser user.User
|
||||
if user, err := user.ManagerFromContext(context.Background()).UserByEmail(db.DefaultUserUsername); err != nil {
|
||||
fmt.Println("unable to find root user: " + err.Error())
|
||||
os.Exit(1)
|
||||
} else {
|
||||
rootUser = user
|
||||
}
|
||||
|
||||
return rootUser.OpenFileSystem(context.Background())
|
||||
return fs.OpenOmniscient(context.Background())
|
||||
}
|
||||
|
||||
func User(cmd *cobra.Command) user.User {
|
||||
if u.Username == "" {
|
||||
func User(cmd *cobra.Command) *user.User {
|
||||
if u == nil {
|
||||
if value, err := cmd.Flags().GetString("user"); err != nil {
|
||||
fmt.Println("could not read user: " + err.Error())
|
||||
os.Exit(1)
|
||||
} else if value == "" {
|
||||
u = nil
|
||||
} else {
|
||||
if user, err := user.ManagerFromContext(context.Background()).UserByEmail(value); err != nil {
|
||||
fmt.Println("could not find user '" + value + "': " + err.Error())
|
||||
os.Exit(1)
|
||||
} else {
|
||||
u = user
|
||||
u = &user
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -45,7 +38,12 @@ func User(cmd *cobra.Command) user.User {
|
||||
|
||||
func UserFileSystem(cmd *cobra.Command) fs.FileSystem {
|
||||
if f == nil {
|
||||
f = User(cmd).OpenFileSystem(context.Background())
|
||||
user := User(cmd)
|
||||
if user == nil {
|
||||
f = fs.OpenOmniscient(context.Background())
|
||||
} else {
|
||||
f = user.OpenFileSystem(context.Background())
|
||||
}
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
|
||||
"github.com/shroff/phylum/server/internal/command/common"
|
||||
"github.com/shroff/phylum/server/internal/core/user"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@@ -32,7 +33,11 @@ func setupBookmarksListCommand() *cobra.Command {
|
||||
Short: "List Bookmarks",
|
||||
Args: cobra.ExactArgs(0),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
if bookmarks, err := user.ManagerFromContext(context.Background()).ListBookmarks(common.User(cmd)); err != nil {
|
||||
u := common.User(cmd)
|
||||
if u == nil {
|
||||
logrus.Fatal("unable to list bookmarks: user not specified")
|
||||
}
|
||||
if bookmarks, err := user.ManagerFromContext(context.Background()).ListBookmarks(*u); err != nil {
|
||||
fmt.Println("unable to list bookmark: " + err.Error())
|
||||
os.Exit(1)
|
||||
} else {
|
||||
@@ -51,13 +56,18 @@ func setupBookmarksRemoveCommand() *cobra.Command {
|
||||
Short: "Remove Bookmark",
|
||||
Args: cobra.ExactArgs(1),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
u := common.User(cmd)
|
||||
if u == nil {
|
||||
logrus.Fatal("unable to remove bookmark: user not specified")
|
||||
}
|
||||
|
||||
r, err := common.UserFileSystem(cmd).ResourceByPathOrUUID(args[0])
|
||||
if err != nil {
|
||||
fmt.Println("unable to remove bookmark: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := user.ManagerFromContext(context.Background()).RemoveBookmark(common.User(cmd), r.ID()); err != nil {
|
||||
if err := user.ManagerFromContext(context.Background()).RemoveBookmark(*u, r.ID()); err != nil {
|
||||
fmt.Println("unable to remove bookmark: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
@@ -72,6 +82,10 @@ func setupBookmarksAddCommand() *cobra.Command {
|
||||
Short: "Add Bookmark",
|
||||
Args: cobra.RangeArgs(1, 2),
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
u := common.User(cmd)
|
||||
if u == nil {
|
||||
logrus.Fatal("unable to add bookmark: user not specified")
|
||||
}
|
||||
r, err := common.UserFileSystem(cmd).ResourceByPathOrUUID(args[0])
|
||||
if err != nil {
|
||||
fmt.Println("unable to add bookmark: " + err.Error())
|
||||
@@ -82,7 +96,7 @@ func setupBookmarksAddCommand() *cobra.Command {
|
||||
name = args[1]
|
||||
}
|
||||
|
||||
if _, err := user.ManagerFromContext(context.Background()).AddBookmark(common.User(cmd), r, name); err != nil {
|
||||
if _, err := user.ManagerFromContext(context.Background()).AddBookmark(*u, r, name); err != nil {
|
||||
fmt.Println("unable to add bookmark: " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
@@ -1,60 +0,0 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
const DefaultUserUsername = "phylum"
|
||||
const DefaultUserDisplayName = "Phylum"
|
||||
|
||||
func BootstrapData(ctx context.Context) error {
|
||||
const q = "SELECT username FROM users WHERE username = $1::TEXT"
|
||||
d := Get(ctx)
|
||||
row := d.QueryRow(q, DefaultUserUsername)
|
||||
if err := row.Scan(nil); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
err = d.RunInTx(insertData)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertData(d Handler) error {
|
||||
const createDir = `INSERT INTO resources(id, parent, name, dir, content_length, content_type, content_sha256)
|
||||
VALUES ($1::UUID, $2::UUID, $3::TEXT, TRUE, 0, '', '')`
|
||||
|
||||
// Create root folder
|
||||
rootID, _ := uuid.NewV7()
|
||||
if _, err := d.Exec(createDir, rootID, nil, ""); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create home folder
|
||||
homeID, _ := uuid.NewV7()
|
||||
if _, err := d.Exec(createDir, homeID, rootID, "home"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Create user home folder
|
||||
userHomeID, _ := uuid.NewV7()
|
||||
if _, err := d.Exec(createDir, userHomeID, homeID, DefaultUserUsername); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
const createRootUser = `INSERT INTO users(username, display_name, password_hash, root, home, permissions)
|
||||
VALUES ($1::TEXT, $2::TEXT, '', $3::UUID, $4::UUID, -1)`
|
||||
|
||||
if _, err := d.Exec(createRootUser, DefaultUserUsername, DefaultUserDisplayName, rootID, userHomeID); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func checkVersion(ctx context.Context, skipMigration bool) error {
|
||||
|
||||
// Nothing to do
|
||||
if currentSchemaVersion == latestSchemaVersion {
|
||||
return BootstrapData(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
if skipMigration {
|
||||
@@ -51,7 +51,7 @@ func checkVersion(ctx context.Context, skipMigration bool) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return BootstrapData(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
func Migrate(ctx context.Context, version int) error {
|
||||
@@ -86,7 +86,7 @@ func Migrate(ctx context.Context, version int) error {
|
||||
return err
|
||||
}
|
||||
if version == latestSchemaVersion {
|
||||
return BootstrapData(ctx)
|
||||
return nil
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/shroff/phylum/server/internal/core/db"
|
||||
"github.com/shroff/phylum/server/internal/core/storage"
|
||||
"github.com/shroff/phylum/server/internal/core/util/crypt"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type ResourceBindConflictResolution int32
|
||||
@@ -70,6 +71,32 @@ func Open(ctx context.Context, username string, root uuid.UUID, fullAccess bool)
|
||||
}
|
||||
}
|
||||
|
||||
func OpenOmniscient(ctx context.Context) FileSystem {
|
||||
id, err := _readRootID(context.Background())
|
||||
if err != nil {
|
||||
logrus.Fatal("could not read root id: " + err.Error())
|
||||
}
|
||||
return Open(ctx, "", id, true)
|
||||
}
|
||||
|
||||
func _readRootID(ctx context.Context) (uuid.UUID, error) {
|
||||
const q = "SELECT id FROM resources WHERE parent IS NULL"
|
||||
d := db.Get(ctx)
|
||||
row := d.QueryRow(q)
|
||||
var id uuid.UUID
|
||||
if err := row.Scan(&id); err != nil {
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
const createDir = "INSERT INTO resources(id, name, dir) VALUES ($1::UUID, '', TRUE)"
|
||||
id, _ := uuid.NewV7()
|
||||
_, err = d.Exec(createDir, id, nil, "")
|
||||
return id, err
|
||||
}
|
||||
return uuid.Nil, err
|
||||
} else {
|
||||
return id, nil
|
||||
}
|
||||
}
|
||||
|
||||
func OpenFromPublink(ctx context.Context, id string, password string, path string) (Resource, error) {
|
||||
d := db.Get(ctx)
|
||||
link, err := getPublink(d, id)
|
||||
|
||||
Reference in New Issue
Block a user