forked from hidu/api-front
/
user_conf.go
81 lines (71 loc) · 1.47 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
package proxy
import (
"fmt"
"github.com/hidu/goutils"
"log"
)
type users []string
type user struct {
Name string
PswMd5 string
}
func (u *user) pswEnc() string {
return utils.StrMd5(fmt.Sprintf("%s201501116%s", u.Name, u.PswMd5))
}
type usersConf struct {
users map[string]*user
}
func (us users) hasUser(name string) bool {
for _, n := range us {
if n == name {
return true
}
}
return false
}
func (uc *usersConf) checkUser(name string, psw string) *user {
if u, has := uc.users[name]; has && u.PswMd5 == utils.StrMd5(psw) {
return u
}
return nil
}
func (uc *usersConf) getUser(name string) *user {
if u, has := uc.users[name]; 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 {
name, has := line["name"]
if !has || name == "" {
continue
}
if _, has := uc.users[name]; has {
log.Println("dup name in users:", name, line)
continue
}
user := new(user)
user.Name = name
if val, has := line["psw_md5"]; has {
user.PswMd5 = val
}
uc.users[user.Name] = user
}
return
}