Add Permissions

pull/144/head
Henrique Dias 2017-07-14 08:01:36 +01:00
parent 7889b8488d
commit 3bcfdb6221
No known key found for this signature in database
GPG Key ID: 936F5EB68D786730
1 changed files with 40 additions and 1 deletions

View File

@ -45,6 +45,9 @@ type FileManager struct {
// 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
// A map with the runtime added permissions for a user.
BasePermissions map[string]bool
// A map of events to a slice of commands. // A map of events to a slice of commands.
Commands map[string][]string Commands map[string][]string
@ -84,7 +87,7 @@ type User struct {
AllowNew bool `json:"allowNew"` // Create files and folders AllowNew bool `json:"allowNew"` // Create files and folders
AllowEdit bool `json:"allowEdit"` // Edit/rename files AllowEdit bool `json:"allowEdit"` // Edit/rename files
AllowCommands bool `json:"allowCommands"` // Execute commands AllowCommands bool `json:"allowCommands"` // Execute commands
Permissions map[string]bool `json:"permissions"` // Permissions added by plugins Permissions map[string]bool `json:""` // Permissions added by plugins
// Commands is the list of commands the user can execute. // Commands is the list of commands the user can execute.
Commands []string `json:"commands"` Commands []string `json:"commands"`
@ -184,6 +187,17 @@ func New(database string, base User) (*FileManager, error) {
return nil, err 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 // Tries to fetch the users from the database and if there are
// any, add them to the current File Manager instance. // any, add them to the current File Manager instance.
var users []User var users []User
@ -277,6 +291,31 @@ func (m *FileManager) RegisterEventType(name string) error {
return m.db.Set("config", "commands", m.Commands) return m.db.Set("config", "commands", m.Commands)
} }
// RegisterPermission registers a new user permission and adds it to every
// 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 {
return nil
}
for _, u := range m.Users {
if u.Admin {
u.Permissions[name] = true
} else {
u.Permissions[name] = value
}
err := m.db.Save(u)
if err != nil {
return err
}
}
return nil
}
// 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) {
code, err := serveHTTP(&RequestContext{ code, err := serveHTTP(&RequestContext{