Add Permissions

Former-commit-id: 2e7525d09892449d389bcc0472439fc1804c7f11 [formerly 79445d3c1da1a1db5e4a4e092dea17b734b7013f] [formerly ab98dc9020d2453f83bef6d74796737928623bcd [formerly 3bcfdb6221]]
Former-commit-id: 136f7b000432c5040eac66f062593fdfca928774 [formerly 6bd7faeef5df6ce6238c0109969be252ec44a178]
Former-commit-id: 2c75d989f349e64d57f694a5bfa66af92976757a
This commit is contained in:
Henrique Dias 2017-07-14 08:01:36 +01:00
parent 9af6519280
commit ec0ec5e6eb

View File

@ -45,6 +45,9 @@ type FileManager struct {
// 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
@ -84,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"` // Permissions added by plugins
Permissions map[string]bool `json:""` // Permissions added by plugins
// Commands is the list of commands the user can execute.
Commands []string `json:"commands"`
@ -184,6 +187,17 @@ 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
@ -277,6 +291,31 @@ func (m *FileManager) RegisterEventType(name string) error {
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.
func (m *FileManager) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
code, err := serveHTTP(&RequestContext{