From e78e10614698d6431380606d103055618b7b0a9e Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Fri, 14 Jul 2017 08:25:37 +0100 Subject: [PATCH] Third party permissions working --- assets/src/components/User.vue | 27 ++++++++++++++++++++++++--- caddy/hugo/setup.go | 6 ++++++ filemanager.go | 31 +++++++++++++++---------------- users.go | 8 ++++++++ 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/assets/src/components/User.vue b/assets/src/components/User.vue index 8221bec1..a885f07a 100644 --- a/assets/src/components/User.vue +++ b/assets/src/components/User.vue @@ -17,6 +17,9 @@

Create new files and directories

Edit, rename and delete files or directories.

Execute commands

+

+ {{ capitalize(key) }} +

Commands

@@ -62,6 +65,7 @@ export default { allowNew: false, allowEdit: false, allowCommands: false, + permissions: {}, password: '', username: '', filesystem: '', @@ -86,16 +90,20 @@ export default { this.allowCommands = true this.allowEdit = true this.allowNew = true + for (let key in this.permissions) { + this.permissions[key] = true + } } }, methods: { fetchData () { + let user = this.$route.params[0] + if (this.$route.path === '/users/new') { - this.reset() - return + user = 'base' } - api.getUser(this.$route.params[0]).then(user => { + api.getUser(user).then(user => { this.id = user.ID this.admin = user.admin this.allowCommands = user.allowCommands @@ -105,6 +113,7 @@ export default { this.username = user.username this.commands = user.commands.join(' ') this.css = user.css + this.permissions = user.permissions for (let rule of user.rules) { if (rule.allow) { @@ -127,11 +136,22 @@ export default { this.$router.push({ path: '/users/new' }) }) }, + capitalize (name) { + let splitted = name.split(/(?=[A-Z])/) + name = '' + + for (let i = 0; i < splitted.length; i++) { + name += splitted[i].charAt(0).toUpperCase() + splitted[i].slice(1) + ' ' + } + + return name.slice(0, -1) + }, reset () { this.id = 0 this.admin = false this.allowNew = false this.allowEdit = false + this.permissins = {} this.allowCommands = false this.password = '' this.username = '' @@ -171,6 +191,7 @@ export default { allowCommands: this.allowCommands, allowNew: this.allowNew, allowEdit: this.allowEdit, + permissions: this.permissions, css: this.css, commands: this.commands.split(' '), rules: [] diff --git a/caddy/hugo/setup.go b/caddy/hugo/setup.go index 4c94cb6a..590e61d4 100644 --- a/caddy/hugo/setup.go +++ b/caddy/hugo/setup.go @@ -106,6 +106,7 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) { AllowCommands: true, AllowEdit: true, AllowNew: true, + Permissions: map[string]bool{}, Commands: []string{"git", "svn", "hg"}, Rules: []*filemanager.Rule{{ Regex: true, @@ -148,6 +149,11 @@ func parse(c *caddy.Controller) ([]*filemanager.FileManager, error) { return nil, err } + err = m.RegisterPermission("allowPublish", true) + if err != nil { + return nil, err + } + m.SetBaseURL(admin) m.SetPrefixURL(strings.TrimSuffix(caddyConf.Addr.Path, "/")) configs = append(configs, m) diff --git a/filemanager.go b/filemanager.go index ba5543b5..becbac3e 100644 --- a/filemanager.go +++ b/filemanager.go @@ -42,12 +42,12 @@ type FileManager struct { // edited directly. Use SetBaseURL. BaseURL string + // The Default User needed to build the New User page. + DefaultUser *User + // Users is a map with the different configurations for each user. Users map[string]*User - // A map with the runtime added permissions for a user. - BasePermissions map[string]bool - // A map of events to a slice of commands. Commands map[string][]string @@ -87,7 +87,7 @@ type User struct { AllowNew bool `json:"allowNew"` // Create files and folders AllowEdit bool `json:"allowEdit"` // Edit/rename files AllowCommands bool `json:"allowCommands"` // Execute commands - Permissions map[string]bool `json:""` // Permissions added by plugins + Permissions map[string]bool `json:"permissions"` // Permissions added by plugins // Commands is the list of commands the user can execute. Commands []string `json:"commands"` @@ -132,6 +132,7 @@ var DefaultUser = User{ AllowCommands: true, AllowEdit: true, AllowNew: true, + Permissions: map[string]bool{}, Commands: []string{}, Rules: []*Rule{}, CSS: "", @@ -187,17 +188,6 @@ func New(database string, base User) (*FileManager, error) { return nil, err } - // Tries to get the base permissions from the database. - err = db.Get("config", "permissions", &m.BasePermissions) - if err != nil && err == storm.ErrNotFound { - m.BasePermissions = map[string]bool{} - err = db.Set("config", "permissions", m.BasePermissions) - } - - if err != nil { - return nil, err - } - // Tries to fetch the users from the database and if there are // any, add them to the current File Manager instance. var users []User @@ -233,6 +223,9 @@ func New(database string, base User) (*FileManager, error) { // Attaches db to this File Manager instance. m.db = db + base.Username = "" + base.Password = "" + m.DefaultUser = &base return m, nil } @@ -295,11 +288,17 @@ func (m *FileManager) RegisterEventType(name string) error { // user with it default's 'value'. If the user is an admin, it will // be true. func (m *FileManager) RegisterPermission(name string, value bool) error { - if _, ok := m.BasePermissions[name]; ok { + if _, ok := m.DefaultUser.Permissions[name]; ok { return nil } + m.DefaultUser.Permissions[name] = value + for _, u := range m.Users { + if u.Permissions == nil { + u.Permissions = map[string]bool{} + } + if u.Admin { u.Permissions[name] = true } else { diff --git a/users.go b/users.go index a164be29..a5982f3a 100644 --- a/users.go +++ b/users.go @@ -52,6 +52,10 @@ func usersGetHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) return renderJSON(w, users) } + if r.URL.Path == "/base" { + return renderJSON(w, c.FM.DefaultUser) + } + // Otherwise we just want one, specific, user. sid := strings.TrimPrefix(r.URL.Path, "/") sid = strings.TrimSuffix(sid, "/") @@ -277,6 +281,10 @@ func usersPutHandler(c *RequestContext, w http.ResponseWriter, r *http.Request) u.Password = pw } + if u.Permissions == nil { + u.Permissions = c.FM.DefaultUser.Permissions + } + // Updates the whole User struct because we always are supposed // to send a new entire object. err = c.FM.db.Save(&u)