mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
12b2c21522
Read https://github.com/filebrowser/filebrowser/pull/575.
Former-commit-id: 7aedcaaf72b863033e3f089d6df308d41a3fd00c [formerly bdbe4d49161b901c4adf9c245895a1be2d62e4a7] [formerly acfc1ec67c423e0b3e065a8c1f8897c5249af65b [formerly d309066def
]]
Former-commit-id: 0c7d925a38a68ccabdf2c4bbd8c302ee89b93509 [formerly a6173925a1382955d93b334ded93f70d6dddd694]
Former-commit-id: e032e0804dd051df86f42962de2b39caec5318b7
92 lines
2.0 KiB
Go
92 lines
2.0 KiB
Go
package cmd
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"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"
|
|
)
|
|
|
|
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",
|
|
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,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
cmd.Help()
|
|
os.Exit(0)
|
|
},
|
|
}
|
|
|
|
func runRules(cmd *cobra.Command, users func(*users.User, *storage.Storage), global func(*settings.Settings, *storage.Storage)) {
|
|
db := getDB()
|
|
defer db.Close()
|
|
st := getStorage(db)
|
|
|
|
id := getUserIdentifier(cmd)
|
|
if id != nil {
|
|
user, err := st.Users.Get(id)
|
|
checkErr(err)
|
|
|
|
if users != nil {
|
|
users(user, st)
|
|
}
|
|
|
|
printRules(user.Rules, id)
|
|
return
|
|
}
|
|
|
|
settings, err := st.Settings.Get()
|
|
checkErr(err)
|
|
|
|
if global != nil {
|
|
global(settings, st)
|
|
}
|
|
|
|
printRules(settings.Rules, id)
|
|
}
|
|
|
|
func getUserIdentifier(cmd *cobra.Command) interface{} {
|
|
id := mustGetUint(cmd, "id")
|
|
username := mustGetString(cmd, "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 {
|
|
fmt.Printf("Allow: %t\tRegex: %s\n", rule.Allow, rule.Regexp.Raw)
|
|
} else {
|
|
fmt.Printf("Allow: %t\tPath: %s\n", rule.Allow, rule.Path)
|
|
}
|
|
}
|
|
}
|