2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
2019-01-06 13:21:31 +00:00
|
|
|
"strconv"
|
2019-01-05 22:44:33 +00:00
|
|
|
"text/tabwriter"
|
|
|
|
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
2020-05-31 23:12:36 +00:00
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(usersCmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
var usersCmd = &cobra.Command{
|
2019-02-15 11:54:44 +00:00
|
|
|
Use: "users",
|
|
|
|
Short: "Users management utility",
|
|
|
|
Long: `Users management utility.`,
|
|
|
|
Args: cobra.NoArgs,
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
func printUsers(usrs []*users.User) {
|
2021-07-26 10:00:05 +00:00
|
|
|
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) //nolint:gomnd
|
2020-11-23 18:06:37 +00:00
|
|
|
fmt.Fprintln(w, "ID\tUsername\tScope\tLocale\tV. Mode\tS.Click\tAdmin\tExecute\tCreate\tRename\tModify\tDelete\tShare\tDownload\tPwd Lock")
|
2019-01-05 22:44:33 +00:00
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
for _, u := range usrs {
|
2020-11-23 18:06:37 +00:00
|
|
|
fmt.Fprintf(w, "%d\t%s\t%s\t%s\t%s\t%t\t%t\t%t\t%t\t%t\t%t\t%t\t%t\t%t\t%t\t\n",
|
2020-05-31 23:12:36 +00:00
|
|
|
u.ID,
|
|
|
|
u.Username,
|
|
|
|
u.Scope,
|
|
|
|
u.Locale,
|
|
|
|
u.ViewMode,
|
2020-11-23 18:06:37 +00:00
|
|
|
u.SingleClick,
|
2020-05-31 23:12:36 +00:00
|
|
|
u.Perm.Admin,
|
|
|
|
u.Perm.Execute,
|
|
|
|
u.Perm.Create,
|
|
|
|
u.Perm.Rename,
|
|
|
|
u.Perm.Modify,
|
|
|
|
u.Perm.Delete,
|
|
|
|
u.Perm.Share,
|
|
|
|
u.Perm.Download,
|
|
|
|
u.LockPassword,
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Flush()
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
func parseUsernameOrID(arg string) (username string, id uint) {
|
2021-07-26 10:00:05 +00:00
|
|
|
id64, err := strconv.ParseUint(arg, 10, 64) //nolint:gomnd
|
2019-01-06 13:21:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return arg, 0
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
2020-05-31 23:12:36 +00:00
|
|
|
return "", uint(id64)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 10:50:37 +00:00
|
|
|
func addUserFlags(flags *pflag.FlagSet) {
|
|
|
|
flags.Bool("perm.admin", false, "admin perm for users")
|
|
|
|
flags.Bool("perm.execute", true, "execute perm for users")
|
|
|
|
flags.Bool("perm.create", true, "create perm for users")
|
|
|
|
flags.Bool("perm.rename", true, "rename perm for users")
|
|
|
|
flags.Bool("perm.modify", true, "modify perm for users")
|
|
|
|
flags.Bool("perm.delete", true, "delete perm for users")
|
|
|
|
flags.Bool("perm.share", true, "share perm for users")
|
|
|
|
flags.Bool("perm.download", true, "download perm for users")
|
|
|
|
flags.String("sorting.by", "name", "sorting mode (name, size or modified)")
|
|
|
|
flags.Bool("sorting.asc", false, "sorting by ascending order")
|
|
|
|
flags.Bool("lockPassword", false, "lock password")
|
|
|
|
flags.StringSlice("commands", nil, "a list of the commands a user can execute")
|
|
|
|
flags.String("scope", ".", "scope for users")
|
|
|
|
flags.String("locale", "en", "locale for users")
|
|
|
|
flags.String("viewMode", string(users.ListViewMode), "view mode for users")
|
2020-11-23 18:06:37 +00:00
|
|
|
flags.Bool("singleClick", false, "use single clicks only")
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 14:07:55 +00:00
|
|
|
func getViewMode(flags *pflag.FlagSet) users.ViewMode {
|
|
|
|
viewMode := users.ViewMode(mustGetString(flags, "viewMode"))
|
2019-01-05 22:44:33 +00:00
|
|
|
if viewMode != users.ListViewMode && viewMode != users.MosaicViewMode {
|
|
|
|
checkErr(errors.New("view mode must be \"" + string(users.ListViewMode) + "\" or \"" + string(users.MosaicViewMode) + "\""))
|
|
|
|
}
|
|
|
|
return viewMode
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
//nolint:gocyclo
|
2019-01-08 14:07:55 +00:00
|
|
|
func getUserDefaults(flags *pflag.FlagSet, defaults *settings.UserDefaults, all bool) {
|
2019-01-05 22:44:33 +00:00
|
|
|
visit := func(flag *pflag.Flag) {
|
|
|
|
switch flag.Name {
|
|
|
|
case "scope":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Scope = mustGetString(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "locale":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Locale = mustGetString(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "viewMode":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.ViewMode = getViewMode(flags)
|
2020-11-23 18:06:37 +00:00
|
|
|
case "singleClick":
|
|
|
|
defaults.SingleClick = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.admin":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Admin = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.execute":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Execute = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.create":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Create = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.rename":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Rename = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.modify":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Modify = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.delete":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Delete = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.share":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Share = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "perm.download":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Perm.Download = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "commands":
|
2019-01-08 14:07:55 +00:00
|
|
|
commands, err := flags.GetStringSlice(flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
defaults.Commands = commands
|
|
|
|
case "sorting.by":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Sorting.By = mustGetString(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
case "sorting.asc":
|
2019-01-08 14:07:55 +00:00
|
|
|
defaults.Sorting.Asc = mustGetBool(flags, flag.Name)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if all {
|
2019-01-08 14:07:55 +00:00
|
|
|
flags.VisitAll(visit)
|
2019-01-05 22:44:33 +00:00
|
|
|
} else {
|
2019-01-08 14:07:55 +00:00
|
|
|
flags.Visit(visit)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|