2019-01-05 22:44:33 +00:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"github.com/spf13/pflag"
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
"github.com/filebrowser/filebrowser/v2/rules"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/storage"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{
|
2020-05-31 23:12:36 +00:00
|
|
|
Use: "rules",
|
|
|
|
Short: "Rules management utility",
|
2019-01-05 23:01:16 +00:00
|
|
|
Long: `On each subcommand you'll have available at least two flags:
|
2019-01-05 22:44:33 +00:00
|
|
|
"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.`,
|
2019-01-05 23:01:16 +00:00
|
|
|
Args: cobra.NoArgs,
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
func runRules(st *storage.Storage, cmd *cobra.Command, usersFn func(*users.User), globalFn func(*settings.Settings)) {
|
2019-01-08 14:07:55 +00:00
|
|
|
id := getUserIdentifier(cmd.Flags())
|
2019-01-05 22:44:33 +00:00
|
|
|
if id != nil {
|
2019-01-06 13:01:42 +00:00
|
|
|
user, err := st.Users.Get("", id)
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
if usersFn != nil {
|
|
|
|
usersFn(user)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
printRules(user.Rules, id)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-01-08 18:40:14 +00:00
|
|
|
s, err := st.Settings.Get()
|
2019-01-05 22:44:33 +00:00
|
|
|
checkErr(err)
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
if globalFn != nil {
|
|
|
|
globalFn(s)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 18:40:14 +00:00
|
|
|
printRules(s.Rules, id)
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 14:07:55 +00:00
|
|
|
func getUserIdentifier(flags *pflag.FlagSet) interface{} {
|
|
|
|
id := mustGetUint(flags, "id")
|
|
|
|
username := mustGetString(flags, "username")
|
2019-01-05 22:44:33 +00:00
|
|
|
|
|
|
|
if id != 0 {
|
|
|
|
return id
|
|
|
|
} else if username != "" {
|
|
|
|
return username
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
func printRules(rulez []rules.Rule, id interface{}) {
|
2019-01-05 22:44:33 +00:00
|
|
|
if id == nil {
|
|
|
|
fmt.Printf("Global Rules:\n\n")
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Rules for user %v:\n\n", id)
|
|
|
|
}
|
|
|
|
|
2020-05-31 23:12:36 +00:00
|
|
|
for id, rule := range rulez {
|
2019-01-05 22:44:33 +00:00
|
|
|
fmt.Printf("(%d) ", id)
|
|
|
|
if rule.Regex {
|
2019-01-06 13:21:31 +00:00
|
|
|
if rule.Allow {
|
|
|
|
fmt.Printf("Allow Regex: \t%s\n", rule.Regexp.Raw)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Disallow Regex: \t%s\n", rule.Regexp.Raw)
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
} else {
|
2019-01-06 13:21:31 +00:00
|
|
|
if rule.Allow {
|
|
|
|
fmt.Printf("Allow Path: \t%s\n", rule.Path)
|
|
|
|
} else {
|
|
|
|
fmt.Printf("Disallow Path: \t%s\n", rule.Path)
|
|
|
|
}
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|