You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.3 KiB

package auth
import (
"fmt"
)
type User struct {
ID string `json:"id"`
Name string `json:"name"`
}
type UserInfo struct {
User
Permissions []string `json:"permissions"`
}
type Result struct {
User *UserInfo `json:"user"`
Token string `json:"token,omitempty"`
Session string `json:"session,omitempty"`
PasswordChangeRequired bool `json:"passwordChangeRequired"`
}
func (user *UserInfo) HasIDOrPermission(userID, subject, action string) bool {
return user.HasID(userID) || user.HasPermission(subject, action)
}
func (user *UserInfo) HasID(id string) bool {
return user != nil && user.ID == id
}
func (user *UserInfo) HasOpPermission(userID, subject, action string) bool {
if !user.HasID(userID) && !user.HasPermission(subject, "admin") {
return false
}
return user.HasPermission(subject, action)
}
func (user *UserInfo) HasPermission(subject, action string) bool {
if user == nil {
return false
}
anyAll := "*.*"
anyAction := fmt.Sprintf("%s.*", subject)
anySubject := fmt.Sprintf("*.%s", action)
specific := fmt.Sprintf("%s.%s", subject, action)
if action == "admin" {
anyAction = specific
}
for _, perm := range user.Permissions {
if perm == anyAll || perm == anyAction || perm == anySubject || perm == specific {
return true
}
}
return false
}