mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
b9acd275a2
License: MIT
Signed-off-by: Henrique Dias <hacdias@gmail.com>
Former-commit-id: 7cc4780fb876e5cbb50a2918bb7f59ef27c9df69 [formerly 172f465b8292a1427ca957034c25bd989b27665a] [formerly aa4115896142f42f126f3cce34256937b03c5e04 [formerly 46b221533a
]]
Former-commit-id: dbe998a5acfe50a763cef63316c824c49b626dad [formerly 92d7264061e532ceae3a98861f434734f4488b90]
Former-commit-id: c09fb725461ad375bac46a2648a588a1b6d88ffd
91 lines
2.0 KiB
Go
91 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"
|
|
)
|
|
|
|
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,
|
|
}
|
|
|
|
func runRules(st *storage.Storage, cmd *cobra.Command, users func(*users.User), global func(*settings.Settings)) {
|
|
id := getUserIdentifier(cmd)
|
|
if id != nil {
|
|
user, err := st.Users.Get("", id)
|
|
checkErr(err)
|
|
|
|
if users != nil {
|
|
users(user)
|
|
}
|
|
|
|
printRules(user.Rules, id)
|
|
return
|
|
}
|
|
|
|
settings, err := st.Settings.Get()
|
|
checkErr(err)
|
|
|
|
if global != nil {
|
|
global(settings)
|
|
}
|
|
|
|
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 {
|
|
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)
|
|
}
|
|
}
|
|
}
|
|
}
|