session + user structs, some light refactoring, user roles

This commit is contained in:
Simon Martens
2025-05-22 14:54:26 +02:00
parent 2316da4435
commit 3f57e7a18d
12 changed files with 287 additions and 58 deletions

View File

@@ -142,3 +142,15 @@ func (a *Agent) SetEditState(editState string) {
func (a *Agent) Comment() string {
return a.GetString(COMMENT_FIELD)
}
func (a *Agent) SetComment(comments string) {
a.Set(COMMENT_FIELD, comments)
}
func (a *Agent) Editor() string {
return a.GetString(EDITOR_FIELD)
}
func (a *Agent) SetEditor(editor string) {
a.Set(EDITOR_FIELD, editor)
}

View File

@@ -212,3 +212,11 @@ func (c *Content) Comment() string {
func (c *Content) SetComment(comment string) {
c.Set(COMMENT_FIELD, comment)
}
func (c *Content) Editor() string {
return c.GetString(EDITOR_FIELD)
}
func (c *Content) SetEditor(editor string) {
c.Set(EDITOR_FIELD, editor)
}

View File

@@ -387,6 +387,12 @@ var MUSENALM_MIME_TYPES = []string{
"image/svg+xml",
}
var USER_ROLES = []string{
"Admin",
"Editor",
"User",
}
var AGENT_RELATIONS = []string{
"Schöpfer",
"Autor:in",
@@ -507,4 +513,7 @@ const (
USERS_TABLE = "users"
USERS_SETTINGS_FIELD = "settings"
USERS_NAME_FIELD = "name"
USERS_ROLE_FIELD = "role"
USERS_AVATAR_FIELD = "avatar"
)

View File

@@ -235,3 +235,11 @@ func (e *Entry) Comment() string {
func (e *Entry) SetComment(comment string) {
e.Set(COMMENT_FIELD, comment)
}
func (e *Entry) Editor() string {
return e.GetString(EDITOR_FIELD)
}
func (e *Entry) SetEditor(editor string) {
e.Set(EDITOR_FIELD, editor)
}

View File

@@ -121,3 +121,11 @@ func (a *Item) Comment() string {
func (a *Item) SetComment(comments string) {
a.Set(COMMENT_FIELD, comments)
}
func (a *Item) Editor() string {
return a.GetString(EDITOR_FIELD)
}
func (a *Item) SetEditor(editor string) {
a.Set(EDITOR_FIELD, editor)
}

View File

@@ -81,3 +81,11 @@ func (p *Place) Comment() string {
func (p *Place) SetComment(comment string) {
p.Set(COMMENT_FIELD, comment)
}
func (p *Place) Editor() string {
return p.GetString(EDITOR_FIELD)
}
func (p *Place) SetEditor(editor string) {
p.Set(EDITOR_FIELD, editor)
}

View File

@@ -87,3 +87,11 @@ func (s *Series) Frequency() string {
func (s *Series) SetFrequency(frequency string) {
s.Set(SERIES_FREQUENCY_FIELD, frequency)
}
func (s *Series) Editor() string {
return s.GetString(EDITOR_FIELD)
}
func (s *Series) SetEditor(editor string) {
s.Set(EDITOR_FIELD, editor)
}

86
dbmodels/sessions.go Normal file
View File

@@ -0,0 +1,86 @@
package dbmodels
import (
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/types"
)
var _ core.RecordProxy = (*Place)(nil)
type Session struct {
core.BaseRecordProxy
}
func NewSession(record *core.Record) *Session {
i := &Session{}
i.SetProxyRecord(record)
return i
}
func (u *Session) TableName() string {
return USERS_TABLE
}
func (u *Session) Token() string {
return u.GetString(SESSIONS_TOKEN_FIELD)
}
func (u *Session) SetToken(token string) {
u.Set(SESSIONS_TOKEN_FIELD, token)
}
func (u *Session) User() string {
return u.GetString(SESSIONS_USER_FIELD)
}
func (u *Session) SetUser(userId string) {
u.Set(SESSIONS_USER_FIELD, userId)
}
func (u *Session) Created() string {
return u.GetString(CREATED_FIELD)
}
func (u *Session) Updated() string {
return u.GetString(UPDATED_FIELD)
}
func (u *Session) Expires() types.DateTime {
return u.GetDateTime(SESSIONS_EXPIRES_FIELD)
}
func (u *Session) SetExpires(expires types.DateTime) {
u.Set(SESSIONS_EXPIRES_FIELD, expires)
}
func (u *Session) IP() string {
return u.GetString(SESSIONS_IP_FIELD)
}
func (u *Session) SetIP(ip string) {
u.Set(SESSIONS_IP_FIELD, ip)
}
func (u *Session) UserAgent() string {
return u.GetString(SESSIONS_USER_AGENT_FIELD)
}
func (u *Session) SetUserAgent(userAgent string) {
u.Set(SESSIONS_USER_AGENT_FIELD, userAgent)
}
func (u *Session) LastAccess() types.DateTime {
return u.GetDateTime(SESSIONS_LAST_ACCESS_FIELD)
}
func (u *Session) SetLastAccess(lastAccess types.DateTime) {
u.Set(SESSIONS_LAST_ACCESS_FIELD, lastAccess)
}
func (u *Session) Persist() bool {
return u.GetBool(SESSIONS_PERSIST_FIELD)
}
func (u *Session) SetPersist(persist bool) {
u.Set(SESSIONS_PERSIST_FIELD, persist)
}

61
dbmodels/user.go Normal file
View File

@@ -0,0 +1,61 @@
package dbmodels
import (
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/tools/filesystem"
"github.com/pocketbase/pocketbase/tools/types"
)
var _ core.RecordProxy = (*Place)(nil)
type User struct {
core.BaseRecordProxy
}
func NewUser(record *core.Record) *User {
i := &User{}
i.SetProxyRecord(record)
return i
}
func (u *User) TableName() string {
return USERS_TABLE
}
// INFO: Email is already set on the core.Record
// TODO: We need to create a settings struct as soon as we have settings
func (u *User) Name() string {
return u.GetString(USERS_NAME_FIELD)
}
func (u *User) SetName(name string) {
u.Set(USERS_NAME_FIELD, name)
}
func (u *User) Created() types.DateTime {
return u.GetDateTime(CREATED_FIELD)
}
func (u *User) Updated() types.DateTime {
return u.GetDateTime(UPDATED_FIELD)
}
func (u *User) Role() string {
return u.GetString(USERS_ROLE_FIELD)
}
func (u *User) SetRole(role string) {
u.Set(USERS_ROLE_FIELD, role)
}
func (u *User) Avatar() string {
av := u.GetString(USERS_AVATAR_FIELD)
if av != "" {
return "/api/files/" + u.TableName() + "/" + u.Id + "/" + av
}
return av
}
func (u *User) SetAvatar(avatar *filesystem.File) {
u.Set(USERS_AVATAR_FIELD, avatar)
}