-
Notifications
You must be signed in to change notification settings - Fork 12
/
users.go
163 lines (145 loc) · 3.57 KB
/
users.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2020 Alexey Krivonogov. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package main
import (
"fmt"
"os"
"path/filepath"
es "eonza/script"
"eonza/users"
"github.com/labstack/echo/v4"
"gopkg.in/yaml.v3"
)
type Fav struct {
Name string `json:"name" yaml:"name"`
IsFolder bool `json:"isfolder" yaml:"isfolder,omitempty"`
Children []Fav `json:"children,omitempty" yaml:"children,omitempty"`
}
type History struct {
Editor []string `yaml:"editor"`
Run []string `yaml:"run"`
}
// UserSettings stores the user's settings
type UserSettings struct {
ID uint32 `json:"id" yaml:"id"`
FormAlign uint32 `json:"formalign" yaml:"formalign"`
Lang string `json:"lang" yaml:"lang"`
History History `json:"history" yaml:"history"`
Favs []Fav `json:"favs" yaml:"favs"`
}
// User stores user's parameters
type User struct { // Deprecated
ID uint32
Nickname string
PublicKey []byte
}
var (
userSettings = make(map[uint32]UserSettings)
)
func LoadUsersSettings() error {
var err error
for _, item := range GetUsers() {
var (
data []byte
user UserSettings
)
user.Lang = appInfo.Lang
data, err = os.ReadFile(filepath.Join(cfg.Users.Dir, item.Nickname+UserExt))
if err == nil {
if err = yaml.Unmarshal(data, &user); err != nil {
return err
}
}
user.ID = item.ID
userSettings[user.ID] = user
}
return nil
}
// AddHistoryEditor adds the history item to the user's settings
func AddHistoryEditor(id uint32, name string) error {
var (
cur UserSettings
)
cur = userSettings[id]
ret := make([]string, 1, HistoryLimit+1)
ret[0] = name
for _, item := range cur.History.Editor {
if item != name {
ret = append(ret, item)
if len(ret) == HistoryLimit {
break
}
}
}
cur.History.Editor = ret
userSettings[id] = cur
return SaveUser(id)
}
// GetHistory returns the history list
func GetHistory(c echo.Context, list []string) []ScriptItem {
ret := make([]ScriptItem, 0, len(list))
for _, item := range list {
script := getScript(item)
if script == nil {
continue
}
ret = append(ret, ScriptItem{
Name: item,
Title: es.ReplaceVars(script.Settings.Title, script.Langs[c.(*Auth).Lang],
&langRes[GetLangId(c.(*Auth).User)]),
})
}
return ret
}
// GetHistoryEditor returns the history list
func GetHistoryEditor(c echo.Context) []ScriptItem {
return GetHistory(c, userSettings[c.(*Auth).User.ID].History.Editor)
}
// LatestHistory returns the latest open project
func LatestHistoryEditor(c echo.Context) (ret string) {
list := GetHistoryEditor(c)
if len(list) > 0 {
return list[0].Name
}
return
}
// AddHistoryRun adds the launched item to the user's settings
func AddHistoryRun(id uint32, name string) error {
var (
cur UserSettings
)
cur = userSettings[id]
ret := make([]string, 1, RunLimit+1)
ret[0] = name
for _, item := range cur.History.Run {
if item != name {
ret = append(ret, item)
if len(ret) == RunLimit {
break
}
}
}
cur.History.Run = ret
userSettings[id] = cur
return SaveUser(id)
}
// GetHistoryRun returns the launchedhistory list
/*func GetHistoryRun(id uint32) []ScriptItem {
return GetHistory(userSettings[id].History.Run)
}*/
func SaveUser(id uint32) error {
data, err := yaml.Marshal(userSettings[id])
if err != nil {
return err
}
user, ok := GetUser(id)
if !ok {
return fmt.Errorf(`Access denied`)
}
return os.WriteFile(filepath.Join(cfg.Users.Dir,
user.Nickname+UserExt), data, 0777 /*os.ModePerm*/)
}
func RootUserSettings() UserSettings {
return userSettings[users.XRootID]
}