-
Notifications
You must be signed in to change notification settings - Fork 50
/
user_conf.go
117 lines (99 loc) · 1.96 KB
/
user_conf.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
package proxy
import (
"encoding/gob"
"encoding/json"
"fmt"
"github.com/hidu/goutils"
"log"
"strings"
)
type users []string
type User struct {
ID string
Email string
NickName string
Picture string
PswMd5 string
}
func (us users) String() string {
return strings.Join(us, " | ")
}
func init() {
gob.Register(&User{})
}
func NewUsers() users {
return users{}
}
func (u *User) pswEnc() string {
return utils.StrMd5(fmt.Sprintf("%s201501116%s", u.ID, u.PswMd5))
}
func (u *User) String() string {
bs, _ := json.MarshalIndent(u, "", " ")
return string(bs)
}
func (u *User) DisplayName() string {
if u.NickName != "" {
return u.NickName
}
return u.ID
}
type usersConf struct {
users map[string]*User
}
func (us users) hasUser(id string) bool {
for _, n := range us {
if n == id || n == ":any" {
return true
}
}
return false
}
func (uc *usersConf) checkUser(id string, psw string) *User {
if u, has := uc.users[id]; has && u.PswMd5 == utils.StrMd5(psw) {
return u
}
return nil
}
func (uc *usersConf) getUser(id string) *User {
if u, has := uc.users[id]; has {
return u
}
return nil
}
func loadUsers(confPath string) (uc *usersConf) {
log.Println("loadUsers file:", confPath)
uc = &usersConf{
users: make(map[string]*User),
}
if !utils.File_exists(confPath) {
log.Println("usersFile not exists")
return
}
userInfoByte, err := utils.File_get_contents(confPath)
if err != nil {
log.Println("load user file failed:", confPath, err)
return
}
log.Println(string(userInfoByte))
lines := utils.LoadText2SliceMap(string(userInfoByte))
for _, line := range lines {
id, has := line["id"]
if !has || id == "" {
continue
}
if _, has := uc.users[id]; has {
log.Println("dup id in users:", id, line)
continue
}
user := new(User)
user.ID = id
if name, has := line["name"]; has {
user.NickName = name
}
if val, has := line["psw_md5"]; has {
user.PswMd5 = val
}
uc.users[user.ID] = user
}
return
}