diff --git a/file.go b/file.go index e5b58571..4428c0a5 100644 --- a/file.go +++ b/file.go @@ -38,7 +38,7 @@ type fileInfo struct { // getInfo gets the file information and, in case of error, returns the // respective HTTP error code -func getInfo(url *url.URL, c *FileManager, u *user) (*fileInfo, error) { +func getInfo(url *url.URL, c *FileManager, u *User) (*fileInfo, error) { var err error i := &fileInfo{URL: c.PrefixURL + url.Path} diff --git a/filemanager.go b/filemanager.go index 195c63cd..afefbb41 100644 --- a/filemanager.go +++ b/filemanager.go @@ -15,9 +15,10 @@ var ( ErrDuplicated = errors.New("Duplicated user") ) -// FileManager is a file manager instance. +// FileManager is a file manager instance. It should be creating using the +// 'New' function and not directly. type FileManager struct { - *user + *User Assets *assets // BaseURL is the path where the GUI will be accessible. It musn't end with @@ -35,7 +36,7 @@ type FileManager struct { PrefixURL string // Users is a map with the different configurations for each user. - Users map[string]*user + Users map[string]*User // BeforeSave is a function that is called before saving a file. BeforeSave Command @@ -45,10 +46,11 @@ type FileManager struct { } // Command is a command function. -type Command func(r *http.Request, m *FileManager, u *user) error +type Command func(r *http.Request, m *FileManager, u *User) error -// user contains the configuration for each user. -type user struct { +// User contains the configuration for each user. It should be created +// using NewUser on a File Manager instance. +type User struct { // scope is the physical path the user has access to. scope string @@ -100,16 +102,16 @@ type Rule struct { // configuration to work. func New(scope string) *FileManager { m := &FileManager{ - user: &user{ + User: &User{ AllowCommands: true, AllowEdit: true, AllowNew: true, Commands: []string{}, Rules: []*Rule{}, }, - Users: map[string]*user{}, - BeforeSave: func(r *http.Request, m *FileManager, u *user) error { return nil }, - AfterSave: func(r *http.Request, m *FileManager, u *user) error { return nil }, + Users: map[string]*User{}, + BeforeSave: func(r *http.Request, m *FileManager, u *User) error { return nil }, + AfterSave: func(r *http.Request, m *FileManager, u *User) error { return nil }, Assets: &assets{ Templates: rice.MustFindBox("./_assets/templates"), CSS: rice.MustFindBox("./_assets/css"), @@ -174,10 +176,10 @@ func (m *FileManager) SetWebDavURL(url string) { // SetScope updates a user scope and its virtual file system. // If the user string is blank, it will change the base scope. func (m *FileManager) SetScope(scope string, username string) error { - var u *user + var u *User if username == "" { - u = m.user + u = m.User } else { var ok bool u, ok = m.Users[username] @@ -205,22 +207,22 @@ func (m *FileManager) NewUser(username string) error { return ErrDuplicated } - m.Users[username] = &user{ - scope: m.user.scope, - fileSystem: m.user.fileSystem, - handler: m.user.handler, - Rules: m.user.Rules, - AllowNew: m.user.AllowNew, - AllowEdit: m.user.AllowEdit, - AllowCommands: m.user.AllowCommands, - Commands: m.user.Commands, + m.Users[username] = &User{ + scope: m.User.scope, + fileSystem: m.User.fileSystem, + handler: m.User.handler, + Rules: m.User.Rules, + AllowNew: m.User.AllowNew, + AllowEdit: m.User.AllowEdit, + AllowCommands: m.User.AllowCommands, + Commands: m.User.Commands, } return nil } // Allowed checks if the user has permission to access a directory/file. -func (u user) Allowed(url string) bool { +func (u User) Allowed(url string) bool { var rule *Rule i := len(u.Rules) - 1 diff --git a/http.go b/http.go index 3ede6dfb..3aca33aa 100644 --- a/http.go +++ b/http.go @@ -18,7 +18,7 @@ func matchURL(first, second string) bool { // ServeHTTP determines if the request is for this plugin, and if all prerequisites are met. func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) { var ( - u *user + u *User code int err error ) @@ -37,7 +37,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er if _, ok := m.Users[username]; ok { u = m.Users[username] } else { - u = m.user + u = m.User } // Checks if the request URL is for the WebDav server @@ -115,7 +115,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er } // serveWebDAV handles the webDAV route of the File Manager. -func serveWebDAV(w http.ResponseWriter, r *http.Request, m *FileManager, u *user) (int, error) { +func serveWebDAV(w http.ResponseWriter, r *http.Request, m *FileManager, u *User) (int, error) { var err error // Checks for user permissions relatively to this path. diff --git a/http_command.go b/http_command.go index 7d7ac6fc..f3b442c1 100644 --- a/http_command.go +++ b/http_command.go @@ -22,7 +22,7 @@ var ( ) // command handles the requests for VCS related commands: git, svn and mercurial -func command(w http.ResponseWriter, r *http.Request, c *FileManager, u *user) (int, error) { +func command(w http.ResponseWriter, r *http.Request, c *FileManager, u *User) (int, error) { // Upgrades the connection to a websocket and checks for errors. conn, err := upgrader.Upgrade(w, r, nil) if err != nil { diff --git a/http_listing.go b/http_listing.go index cd881f86..8aefce63 100644 --- a/http_listing.go +++ b/http_listing.go @@ -10,7 +10,7 @@ import ( ) // serveListing presents the user with a listage of a directory folder. -func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *user, i *fileInfo) (int, error) { +func serveListing(w http.ResponseWriter, r *http.Request, c *FileManager, u *User, i *fileInfo) (int, error) { var err error // Loads the content of the directory diff --git a/http_put.go b/http_put.go index 360e4b74..67164119 100644 --- a/http_put.go +++ b/http_put.go @@ -14,7 +14,7 @@ import ( ) // put is used to update a file that was edited -func put(w http.ResponseWriter, r *http.Request, c *FileManager, u *user) (err error) { +func put(w http.ResponseWriter, r *http.Request, c *FileManager, u *User) (err error) { var ( data = map[string]interface{}{} file []byte diff --git a/http_search.go b/http_search.go index c4b48a24..daccd795 100644 --- a/http_search.go +++ b/http_search.go @@ -43,7 +43,7 @@ func parseSearch(value string) *searchOptions { } // search searches for a file or directory. -func search(w http.ResponseWriter, r *http.Request, c *FileManager, u *user) (int, error) { +func search(w http.ResponseWriter, r *http.Request, c *FileManager, u *User) (int, error) { // Upgrades the connection to a websocket and checks for errors. conn, err := upgrader.Upgrade(w, r, nil) if err != nil { diff --git a/http_single.go b/http_single.go index 2496a2ea..5aa356b1 100644 --- a/http_single.go +++ b/http_single.go @@ -7,7 +7,7 @@ import ( // serveSingle serves a single file in an editor (if it is editable), shows the // plain file, or downloads it if it can't be shown. -func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *user, i *fileInfo) (int, error) { +func serveSingle(w http.ResponseWriter, r *http.Request, c *FileManager, u *User, i *fileInfo) (int, error) { var err error if err = i.RetrieveFileType(); err != nil { diff --git a/listing.go b/listing.go index 1863b18a..b4cbbed7 100644 --- a/listing.go +++ b/listing.go @@ -33,7 +33,7 @@ type listing struct { } // getListing gets the information about a specific directory and its files. -func getListing(u *user, filePath string, baseURL string) (*listing, error) { +func getListing(u *User, filePath string, baseURL string) (*listing, error) { // Gets the directory information using the Virtual File System of // the user configuration. file, err := u.fileSystem.OpenFile(context.TODO(), filePath, os.O_RDONLY, 0) diff --git a/page.go b/page.go index 4d91ecb3..74c58011 100644 --- a/page.go +++ b/page.go @@ -35,7 +35,7 @@ type page struct { Name string Path string IsDir bool - User *user + User *User PrefixURL string BaseURL string WebDavURL string