Export user

pull/144/head
Henrique Dias 2017-06-25 15:24:16 +01:00
parent 70c00db16b
commit 3ac186305d
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
10 changed files with 35 additions and 33 deletions

View File

@ -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}

View File

@ -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

View File

@ -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.

View File

@ -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 {

View File

@ -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

View File

@ -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

View File

@ -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 {

View File

@ -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 {

View File

@ -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)

View File

@ -35,7 +35,7 @@ type page struct {
Name string
Path string
IsDir bool
User *user
User *User
PrefixURL string
BaseURL string
WebDavURL string