mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-08 12:40:59 -06:00
72 lines
1.7 KiB
Go
72 lines
1.7 KiB
Go
package user
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/shroff/phylum/server/internal/command/common"
|
|
"github.com/shroff/phylum/server/internal/core/user"
|
|
"github.com/sirupsen/logrus"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
func SetupCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
GroupID: "admin",
|
|
Use: "user",
|
|
Short: "User Management",
|
|
}
|
|
cmd.AddCommand([]*cobra.Command{
|
|
setupUserAddCommand(),
|
|
setupUserModCommand(),
|
|
setupUserChrootCommand(),
|
|
setupUserListCommand(),
|
|
setupBookmarksCommand(),
|
|
}...)
|
|
|
|
flags := cmd.PersistentFlags()
|
|
flags.Bool("auto-migrate", true, "Automatically migrate database schema")
|
|
viper.BindPFlag("auto_migrate", flags.Lookup("auto-migrate"))
|
|
|
|
return cmd
|
|
}
|
|
|
|
func setupUserChrootCommand() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "chroot <username> [ <path> | <uuid> ]",
|
|
Short: "Set User Root",
|
|
Args: cobra.ExactArgs(2),
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
email := args[0]
|
|
idOrPath := args[1]
|
|
userManager := user.ManagerFromContext(context.Background())
|
|
if user, err := userManager.UserByEmail(email); err != nil {
|
|
logrus.Fatal(err)
|
|
} else {
|
|
if res, err := common.RootFileSystem().ResourceByPathOrUUID(idOrPath); err != nil {
|
|
logrus.Fatal(err)
|
|
} else {
|
|
userManager.UpdateUserRoot(user, res.ID())
|
|
}
|
|
}
|
|
},
|
|
}
|
|
return cmd
|
|
}
|
|
|
|
func setupUserListCommand() *cobra.Command {
|
|
return &cobra.Command{
|
|
Use: "list",
|
|
Short: "List Users",
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
users, err := user.ManagerFromContext(context.Background()).ListUsers(nil)
|
|
if err != nil {
|
|
logrus.Fatal(err)
|
|
}
|
|
for _, user := range users {
|
|
logrus.Infof("%24s : %s", user.Username, user.DisplayName)
|
|
}
|
|
},
|
|
}
|
|
}
|