2019-01-05 22:44:33 +00:00
|
|
|
package users
|
|
|
|
|
|
|
|
import (
|
|
|
|
"regexp"
|
|
|
|
|
2025-02-16 14:07:38 +00:00
|
|
|
jwt "github.com/golang-jwt/jwt/v4"
|
2019-01-05 22:44:33 +00:00
|
|
|
)
|
|
|
|
|
2024-11-21 00:15:30 +00:00
|
|
|
type AuthToken struct {
|
|
|
|
Key string `json:"key"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Created int64 `json:"createdAt"`
|
|
|
|
Expires int64 `json:"expiresAt"`
|
|
|
|
BelongsTo uint `json:"belongsTo"`
|
|
|
|
Permissions Permissions `json:"Permissions"`
|
|
|
|
jwt.RegisteredClaims `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Permissions struct {
|
|
|
|
Api bool `json:"api"`
|
|
|
|
Admin bool `json:"admin"`
|
|
|
|
Execute bool `json:"execute"`
|
|
|
|
Create bool `json:"create"`
|
|
|
|
Rename bool `json:"rename"`
|
|
|
|
Modify bool `json:"modify"`
|
|
|
|
Delete bool `json:"delete"`
|
|
|
|
Share bool `json:"share"`
|
|
|
|
Download bool `json:"download"`
|
|
|
|
}
|
|
|
|
|
2023-12-01 23:47:00 +00:00
|
|
|
// SortingSettings represents the sorting settings.
|
|
|
|
type Sorting struct {
|
|
|
|
By string `json:"by"`
|
|
|
|
Asc bool `json:"asc"`
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
// User describes a user.
|
|
|
|
type User struct {
|
2025-02-16 14:07:38 +00:00
|
|
|
StickySidebar bool `json:"stickySidebar"`
|
|
|
|
DarkMode bool `json:"darkMode"`
|
|
|
|
DisableSettings bool `json:"disableSettings"`
|
|
|
|
ID uint `storm:"id,increment" json:"id"`
|
|
|
|
Username string `storm:"unique" json:"username"`
|
|
|
|
Password string `json:"password,omitempty"`
|
|
|
|
Scope string `json:"scope"`
|
|
|
|
Locale string `json:"locale"`
|
|
|
|
LockPassword bool `json:"lockPassword"`
|
|
|
|
ViewMode string `json:"viewMode"`
|
|
|
|
SingleClick bool `json:"singleClick"`
|
|
|
|
Sorting Sorting `json:"sorting"`
|
|
|
|
Perm Permissions `json:"perm"`
|
|
|
|
Commands []string `json:"commands"`
|
|
|
|
Rules []Rule `json:"rules"`
|
|
|
|
ApiKeys map[string]AuthToken `json:"apiKeys,omitempty"`
|
|
|
|
ShowHidden bool `json:"showHidden"`
|
|
|
|
DateFormat bool `json:"dateFormat"`
|
|
|
|
GallerySize int `json:"gallerySize"`
|
|
|
|
ThemeColor string `json:"themeColor"`
|
|
|
|
QuickDownload bool `json:"quickDownload"`
|
|
|
|
DisableOnlyOfficeExt string `json:"disableOnlyOfficeExt"`
|
2019-01-05 22:44:33 +00:00
|
|
|
}
|
|
|
|
|
2024-02-10 00:13:02 +00:00
|
|
|
var PublicUser = User{
|
|
|
|
Username: "publicUser", // temp user not registered
|
|
|
|
Password: "publicUser", // temp user not registered
|
2025-02-16 14:07:38 +00:00
|
|
|
Scope: "/does/not/exist",
|
2024-02-10 00:13:02 +00:00
|
|
|
ViewMode: "normal",
|
|
|
|
LockPassword: true,
|
2024-11-21 00:15:30 +00:00
|
|
|
Perm: Permissions{
|
2024-02-10 00:13:02 +00:00
|
|
|
Create: false,
|
|
|
|
Rename: false,
|
|
|
|
Modify: false,
|
|
|
|
Delete: false,
|
2024-11-21 00:15:30 +00:00
|
|
|
Share: false,
|
2024-02-10 00:13:02 +00:00
|
|
|
Download: true,
|
|
|
|
Admin: false,
|
2024-11-21 00:15:30 +00:00
|
|
|
Api: false,
|
2024-02-10 00:13:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-01-05 22:44:33 +00:00
|
|
|
// GetRules implements rules.Provider.
|
2024-11-21 00:15:30 +00:00
|
|
|
func (u *User) GetRules() []Rule {
|
2019-01-05 22:44:33 +00:00
|
|
|
return u.Rules
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanExecute checks if an user can execute a specific command.
|
|
|
|
func (u *User) CanExecute(command string) bool {
|
|
|
|
if !u.Perm.Execute {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cmd := range u.Commands {
|
|
|
|
if regexp.MustCompile(cmd).MatchString(command) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|