...
1 package model
2
3 import (
4 "fmt"
5 "strings"
6 "time"
7
8 . "codeberg.org/tslocum/sriracha/util"
9 )
10
11 type AccountRole int
12
13
14 const (
15 RoleSuperAdmin AccountRole = 1
16 RoleAdmin AccountRole = 2
17 RoleMod AccountRole = 3
18 RoleDisabled AccountRole = 99
19 )
20
21 func FormatRole(role AccountRole) string {
22 switch role {
23 case RoleSuperAdmin:
24 return "Super-administrator"
25 case RoleAdmin:
26 return "Administrator"
27 case RoleMod:
28 return "Moderator"
29 case RoleDisabled:
30 return "Disabled"
31 default:
32 return "Unknown"
33 }
34 }
35
36 type Account struct {
37 ID int
38 Username string
39 Password string
40 Role AccountRole
41 LastActive int64
42 Session string
43 Style string
44 Locale string
45 }
46
47 func (a *Account) Validate() error {
48 switch {
49 case strings.TrimSpace(a.Username) == "":
50 return fmt.Errorf("username must be set")
51 case !AlphaNumericAndSymbols.MatchString(a.Username):
52 return fmt.Errorf("username must only consist of letters, numbers, hyphens and underscores")
53 }
54 return nil
55 }
56
57 func (a *Account) LastActiveDate() string {
58 if a.LastActive == 0 {
59 return "Never"
60 }
61 return time.Unix(a.LastActive, 0).Format("2006-01-02 15:04:05 MST")
62 }
63
View as plain text