mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
9a54abfe62
Former-commit-id: 669963fd1d51d7e2d32520ffb0c1ecd5e4224c78 [formerly 8ef320b07699c842ae0325e4665d570fa4399481] [formerly d171df23332dff1d33f00a7407f5dc3f6dde1ef7 [formerly d0fd97d943
]]
Former-commit-id: dc9a921b032e1acee0845186e7a009badbe768c5 [formerly 0c8a0f739bd1d984ca0db2b0623da5fdb813f4e0]
Former-commit-id: 3005cd15261952387fde1adf13770e2c1ee52749
93 lines
2.0 KiB
Go
93 lines
2.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/rules"
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
"github.com/spf13/cobra"
|
|
"github.com/spf13/pflag"
|
|
)
|
|
|
|
func init() {
|
|
rootCmd.AddCommand(rulesCmd)
|
|
rulesCmd.PersistentFlags().StringP("username", "u", "", "username of user to which the rules apply")
|
|
rulesCmd.PersistentFlags().UintP("id", "i", 0, "id of user to which the rules apply")
|
|
}
|
|
|
|
var rulesCmd = &cobra.Command{
|
|
Use: "rules",
|
|
Version: rootCmd.Version,
|
|
Short: "Rules management utility",
|
|
Long: `On each subcommand you'll have available at least two flags:
|
|
"username" and "id". You must either set only one of them
|
|
or none. If you set one of them, the command will apply to
|
|
an user, otherwise it will be applied to the global set or
|
|
rules.`,
|
|
Args: cobra.NoArgs,
|
|
}
|
|
|
|
func runRules(st *storage.Storage, cmd *cobra.Command, users func(*users.User), global func(*settings.Settings)) {
|
|
id := getUserIdentifier(cmd.Flags())
|
|
if id != nil {
|
|
user, err := st.Users.Get("", id)
|
|
checkErr(err)
|
|
|
|
if users != nil {
|
|
users(user)
|
|
}
|
|
|
|
printRules(user.Rules, id)
|
|
return
|
|
}
|
|
|
|
s, err := st.Settings.Get()
|
|
checkErr(err)
|
|
|
|
if global != nil {
|
|
global(s)
|
|
}
|
|
|
|
printRules(s.Rules, id)
|
|
}
|
|
|
|
func getUserIdentifier(flags *pflag.FlagSet) interface{} {
|
|
id := mustGetUint(flags, "id")
|
|
username := mustGetString(flags, "username")
|
|
|
|
if id != 0 {
|
|
return id
|
|
} else if username != "" {
|
|
return username
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func printRules(rules []rules.Rule, id interface{}) {
|
|
if id == nil {
|
|
fmt.Printf("Global Rules:\n\n")
|
|
} else {
|
|
fmt.Printf("Rules for user %v:\n\n", id)
|
|
}
|
|
|
|
for id, rule := range rules {
|
|
fmt.Printf("(%d) ", id)
|
|
if rule.Regex {
|
|
if rule.Allow {
|
|
fmt.Printf("Allow Regex: \t%s\n", rule.Regexp.Raw)
|
|
} else {
|
|
fmt.Printf("Disallow Regex: \t%s\n", rule.Regexp.Raw)
|
|
}
|
|
} else {
|
|
if rule.Allow {
|
|
fmt.Printf("Allow Path: \t%s\n", rule.Path)
|
|
} else {
|
|
fmt.Printf("Disallow Path: \t%s\n", rule.Path)
|
|
}
|
|
}
|
|
}
|
|
}
|