Export user
parent
70c00db16b
commit
3ac186305d
2
file.go
2
file.go
|
@ -38,7 +38,7 @@ type fileInfo struct {
|
||||||
|
|
||||||
// getInfo gets the file information and, in case of error, returns the
|
// getInfo gets the file information and, in case of error, returns the
|
||||||
// respective HTTP error code
|
// 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
|
var err error
|
||||||
|
|
||||||
i := &fileInfo{URL: c.PrefixURL + url.Path}
|
i := &fileInfo{URL: c.PrefixURL + url.Path}
|
||||||
|
|
|
@ -15,9 +15,10 @@ var (
|
||||||
ErrDuplicated = errors.New("Duplicated user")
|
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 {
|
type FileManager struct {
|
||||||
*user
|
*User
|
||||||
Assets *assets
|
Assets *assets
|
||||||
|
|
||||||
// BaseURL is the path where the GUI will be accessible. It musn't end with
|
// BaseURL is the path where the GUI will be accessible. It musn't end with
|
||||||
|
@ -35,7 +36,7 @@ type FileManager struct {
|
||||||
PrefixURL string
|
PrefixURL string
|
||||||
|
|
||||||
// Users is a map with the different configurations for each user.
|
// 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 is a function that is called before saving a file.
|
||||||
BeforeSave Command
|
BeforeSave Command
|
||||||
|
@ -45,10 +46,11 @@ type FileManager struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Command is a command function.
|
// 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.
|
// User contains the configuration for each user. It should be created
|
||||||
type user struct {
|
// using NewUser on a File Manager instance.
|
||||||
|
type User struct {
|
||||||
// scope is the physical path the user has access to.
|
// scope is the physical path the user has access to.
|
||||||
scope string
|
scope string
|
||||||
|
|
||||||
|
@ -100,16 +102,16 @@ type Rule struct {
|
||||||
// configuration to work.
|
// configuration to work.
|
||||||
func New(scope string) *FileManager {
|
func New(scope string) *FileManager {
|
||||||
m := &FileManager{
|
m := &FileManager{
|
||||||
user: &user{
|
User: &User{
|
||||||
AllowCommands: true,
|
AllowCommands: true,
|
||||||
AllowEdit: true,
|
AllowEdit: true,
|
||||||
AllowNew: true,
|
AllowNew: true,
|
||||||
Commands: []string{},
|
Commands: []string{},
|
||||||
Rules: []*Rule{},
|
Rules: []*Rule{},
|
||||||
},
|
},
|
||||||
Users: map[string]*user{},
|
Users: map[string]*User{},
|
||||||
BeforeSave: func(r *http.Request, m *FileManager, u *user) error { return nil },
|
BeforeSave: func(r *http.Request, m *FileManager, u *User) error { return nil },
|
||||||
AfterSave: 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{
|
Assets: &assets{
|
||||||
Templates: rice.MustFindBox("./_assets/templates"),
|
Templates: rice.MustFindBox("./_assets/templates"),
|
||||||
CSS: rice.MustFindBox("./_assets/css"),
|
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.
|
// SetScope updates a user scope and its virtual file system.
|
||||||
// If the user string is blank, it will change the base scope.
|
// If the user string is blank, it will change the base scope.
|
||||||
func (m *FileManager) SetScope(scope string, username string) error {
|
func (m *FileManager) SetScope(scope string, username string) error {
|
||||||
var u *user
|
var u *User
|
||||||
|
|
||||||
if username == "" {
|
if username == "" {
|
||||||
u = m.user
|
u = m.User
|
||||||
} else {
|
} else {
|
||||||
var ok bool
|
var ok bool
|
||||||
u, ok = m.Users[username]
|
u, ok = m.Users[username]
|
||||||
|
@ -205,22 +207,22 @@ func (m *FileManager) NewUser(username string) error {
|
||||||
return ErrDuplicated
|
return ErrDuplicated
|
||||||
}
|
}
|
||||||
|
|
||||||
m.Users[username] = &user{
|
m.Users[username] = &User{
|
||||||
scope: m.user.scope,
|
scope: m.User.scope,
|
||||||
fileSystem: m.user.fileSystem,
|
fileSystem: m.User.fileSystem,
|
||||||
handler: m.user.handler,
|
handler: m.User.handler,
|
||||||
Rules: m.user.Rules,
|
Rules: m.User.Rules,
|
||||||
AllowNew: m.user.AllowNew,
|
AllowNew: m.User.AllowNew,
|
||||||
AllowEdit: m.user.AllowEdit,
|
AllowEdit: m.User.AllowEdit,
|
||||||
AllowCommands: m.user.AllowCommands,
|
AllowCommands: m.User.AllowCommands,
|
||||||
Commands: m.user.Commands,
|
Commands: m.User.Commands,
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Allowed checks if the user has permission to access a directory/file.
|
// 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
|
var rule *Rule
|
||||||
i := len(u.Rules) - 1
|
i := len(u.Rules) - 1
|
||||||
|
|
||||||
|
|
6
http.go
6
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.
|
// 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) {
|
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
var (
|
var (
|
||||||
u *user
|
u *User
|
||||||
code int
|
code int
|
||||||
err error
|
err error
|
||||||
)
|
)
|
||||||
|
@ -37,7 +37,7 @@ func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, er
|
||||||
if _, ok := m.Users[username]; ok {
|
if _, ok := m.Users[username]; ok {
|
||||||
u = m.Users[username]
|
u = m.Users[username]
|
||||||
} else {
|
} else {
|
||||||
u = m.user
|
u = m.User
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks if the request URL is for the WebDav server
|
// 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.
|
// 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
|
var err error
|
||||||
|
|
||||||
// Checks for user permissions relatively to this path.
|
// Checks for user permissions relatively to this path.
|
||||||
|
|
|
@ -22,7 +22,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
// command handles the requests for VCS related commands: git, svn and mercurial
|
// 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.
|
// Upgrades the connection to a websocket and checks for errors.
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -10,7 +10,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// serveListing presents the user with a listage of a directory folder.
|
// 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
|
var err error
|
||||||
|
|
||||||
// Loads the content of the directory
|
// Loads the content of the directory
|
||||||
|
|
|
@ -14,7 +14,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// put is used to update a file that was edited
|
// 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 (
|
var (
|
||||||
data = map[string]interface{}{}
|
data = map[string]interface{}{}
|
||||||
file []byte
|
file []byte
|
||||||
|
|
|
@ -43,7 +43,7 @@ func parseSearch(value string) *searchOptions {
|
||||||
}
|
}
|
||||||
|
|
||||||
// search searches for a file or directory.
|
// 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.
|
// Upgrades the connection to a websocket and checks for errors.
|
||||||
conn, err := upgrader.Upgrade(w, r, nil)
|
conn, err := upgrader.Upgrade(w, r, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
|
|
||||||
// serveSingle serves a single file in an editor (if it is editable), shows the
|
// 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.
|
// 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
|
var err error
|
||||||
|
|
||||||
if err = i.RetrieveFileType(); err != nil {
|
if err = i.RetrieveFileType(); err != nil {
|
||||||
|
|
|
@ -33,7 +33,7 @@ type listing struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getListing gets the information about a specific directory and its files.
|
// 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
|
// Gets the directory information using the Virtual File System of
|
||||||
// the user configuration.
|
// the user configuration.
|
||||||
file, err := u.fileSystem.OpenFile(context.TODO(), filePath, os.O_RDONLY, 0)
|
file, err := u.fileSystem.OpenFile(context.TODO(), filePath, os.O_RDONLY, 0)
|
||||||
|
|
Loading…
Reference in New Issue