add user to the front end

pull/144/head
Henrique Dias 2016-08-21 19:44:09 +01:00
parent 87de0ae1be
commit 076245339d
6 changed files with 634 additions and 19 deletions

View File

@ -10,7 +10,7 @@ import (
)
// BaseURL is the url of the assets
const BaseURL = "/_filemanagerinternal"
const BaseURL = "_filemanagerinternal"
// Serve provides the needed assets for the front-end
func Serve(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {

View File

@ -67,6 +67,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
for c.Next() {
var cfg = Config{UserConfig: &UserConfig{}}
cfg.PathScope = "."
cfg.BaseURL = ""
cfg.FrontMatter = "yaml"
cfg.HugoEnabled = false
cfg.Users = map[string]*UserConfig{}
@ -99,7 +100,6 @@ func Parse(c *caddy.Controller) ([]Config, error) {
}
cCfg.PathScope = c.Val()
cCfg.PathScope = strings.TrimSuffix(cCfg.PathScope, "/")
cCfg.Root = http.Dir(cCfg.PathScope)
case "styles":
if !c.NextArg() {
return configs, c.ArgErr()
@ -200,6 +200,8 @@ func Parse(c *caddy.Controller) ([]Config, error) {
})
// NEW USER BLOCK?
default:
cCfg.Root = http.Dir(cCfg.PathScope)
val := c.Val()
// Checks if it's a new user
if !strings.HasSuffix(val, ":") {
@ -230,6 +232,8 @@ func Parse(c *caddy.Controller) ([]Config, error) {
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL
cfg.Root = http.Dir(cfg.PathScope)
caddyConf := httpserver.GetConfig(c)
cfg.AbsoluteURL = strings.TrimSuffix(caddyConf.Addr.Path, "/") + "/" + cfg.BaseURL
cfg.AbsoluteURL = strings.Replace(cfg.AbsoluteURL, "//", "/", -1)
@ -238,6 +242,7 @@ func Parse(c *caddy.Controller) ([]Config, error) {
if err := appendConfig(cfg); err != nil {
return configs, err
}
}
return configs, nil

611
debug Normal file

File diff suppressed because one or more lines are too long

View File

@ -142,15 +142,15 @@ func (i *Info) Rename(w http.ResponseWriter, r *http.Request) (int, error) {
}
// ServeAsHTML is used to serve single file pages
func (i *Info) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
func (i *Info) ServeAsHTML(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.UserConfig) (int, error) {
if i.IsDir {
return i.serveListing(w, r, c)
return i.serveListing(w, r, c, u)
}
return i.serveSingleFile(w, r, c)
return i.serveSingleFile(w, r, c, u)
}
func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.UserConfig) (int, error) {
err := i.GetExtendedInfo()
if err != nil {
return errors.ToHTTPCode(err), err
@ -166,6 +166,7 @@ func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config
Path: i.RootPath,
IsDir: false,
Data: i,
User: u,
Config: c,
},
}
@ -184,7 +185,7 @@ func (i *Info) serveSingleFile(w http.ResponseWriter, r *http.Request, c *config
return page.PrintAsHTML(w, "single")
}
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Config, u *config.UserConfig) (int, error) {
var err error
file, err := c.Root.Open(i.RootPath)
@ -245,6 +246,7 @@ func (i *Info) serveListing(w http.ResponseWriter, r *http.Request, c *config.Co
Name: listing.Name,
Path: i.RootPath,
IsDir: true,
User: u,
Config: c,
Data: listing,
},

View File

@ -44,22 +44,18 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
user *config.UserConfig
)
// Set the current User
username, _, ok := r.BasicAuth()
if !ok {
user = c.UserConfig
}
if _, ok := c.Users[username]; ok {
user = c.Users[username]
}
for i := range f.Configs {
if httpserver.Path(r.URL.Path).Matches(f.Configs[i].BaseURL) {
c = &f.Configs[i]
serveAssets = httpserver.Path(r.URL.Path).Matches(c.BaseURL + assets.BaseURL)
// Set the current User
username, _, _ := r.BasicAuth()
if _, ok := c.Users[username]; ok {
user = c.Users[username]
}
if r.Method != http.MethodPost && !serveAssets {
fi, code, err = directory.GetInfo(r.URL, c)
if err != nil {
@ -105,7 +101,7 @@ func (f FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, err
}
}
code, err := fi.ServeAsHTML(w, r, c)
code, err := fi.ServeAsHTML(w, r, c, user)
if err != nil {
return errors.PrintHTML(w, code, err)
}

View File

@ -24,6 +24,7 @@ type Info struct {
Name string
Path string
IsDir bool
User *config.UserConfig
Config *config.Config
Data interface{}
}