2019-01-06 06:44:33 +08:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
|
|
|
"github.com/filebrowser/filebrowser/v2/rules"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/settings"
|
|
|
|
"github.com/filebrowser/filebrowser/v2/users"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rulesCmd.AddCommand(rulesAddCmd)
|
2019-01-06 21:21:31 +08:00
|
|
|
rulesAddCmd.Flags().BoolP("allow", "a", false, "indicates this is an allow rule")
|
|
|
|
rulesAddCmd.Flags().BoolP("regex", "r", false, "indicates this is a regex rule")
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var rulesAddCmd = &cobra.Command{
|
2019-01-06 21:21:31 +08:00
|
|
|
Use: "add <path|expression>",
|
2019-01-06 06:44:33 +08:00
|
|
|
Short: "Add a global rule or user rule",
|
2019-01-06 21:21:31 +08:00
|
|
|
Long: `Add a global rule or user rule.`,
|
|
|
|
Args: cobra.ExactArgs(1),
|
2019-01-08 04:24:23 +08:00
|
|
|
Run: python(func(cmd *cobra.Command, args []string, d pythonData) {
|
2019-01-08 22:07:55 +08:00
|
|
|
allow := mustGetBool(cmd.Flags(), "allow")
|
|
|
|
regex := mustGetBool(cmd.Flags(), "regex")
|
2019-01-06 21:21:31 +08:00
|
|
|
exp := args[0]
|
2019-01-06 06:44:33 +08:00
|
|
|
|
2019-01-06 21:21:31 +08:00
|
|
|
if regex {
|
|
|
|
regexp.MustCompile(exp)
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
rule := rules.Rule{
|
|
|
|
Allow: allow,
|
2019-01-06 21:21:31 +08:00
|
|
|
Regex: regex,
|
|
|
|
}
|
|
|
|
|
|
|
|
if regex {
|
|
|
|
rule.Regexp = &rules.Regexp{Raw: exp}
|
|
|
|
} else {
|
|
|
|
rule.Path = exp
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|
|
|
|
|
2019-01-08 04:24:23 +08:00
|
|
|
user := func(u *users.User) {
|
2019-01-06 06:44:33 +08:00
|
|
|
u.Rules = append(u.Rules, rule)
|
2019-01-08 04:24:23 +08:00
|
|
|
err := d.store.Users.Save(u)
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
2019-01-08 04:24:23 +08:00
|
|
|
global := func(s *settings.Settings) {
|
2019-01-06 06:44:33 +08:00
|
|
|
s.Rules = append(s.Rules, rule)
|
2019-01-08 04:24:23 +08:00
|
|
|
err := d.store.Settings.Save(s)
|
2019-01-06 06:44:33 +08:00
|
|
|
checkErr(err)
|
|
|
|
}
|
|
|
|
|
2019-01-08 04:24:23 +08:00
|
|
|
runRules(d.store, cmd, user, global)
|
|
|
|
}, pythonConfig{}),
|
2019-01-06 06:44:33 +08:00
|
|
|
}
|