-
Notifications
You must be signed in to change notification settings - Fork 0
/
service.go
203 lines (178 loc) · 5.41 KB
/
service.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package accountservice
import (
"github.com/bernishen/exception"
"github.com/bernishen/lion-go/oauth2/common/pwdprovider"
"github.com/bernishen/lion-go/oauth2/dal/dbpool"
"github.com/bernishen/lion-go/oauth2/dal/domain"
pd "github.com/bernishen/lion-go/oauth2/service/accountservice/domain"
"github.com/bernishen/lion-go/oauth2/service/pwdservice"
"github.com/bernishen/lion-go/oauth2/service/redisservice"
"github.com/bernishen/lion-go/oauth2/service/sessionservice"
"github.com/bernishen/lion-go/oauth2/service/tokenservice"
)
const (
signLogTag = "signuser"
)
// SignUp is user created a account for sign in this system.
func SignUp(user *pd.SignUpUser) (bool, *exception.Exception) {
db, ex := dbpool.Take()
if ex != nil {
return false, exception.NewException(exception.Error, 1001, ex.Message)
}
defer dbpool.Put(db)
var users []domain.User
db.Find(&users, "user_id = ?", user.UserID)
if len(users) > 0 {
return false, exception.NewException(exception.Error, 1002, "This 'UseID' is already in use. Please changed it and try again.")
}
u := &domain.User{
BaseModel: *domain.NewBase(),
UserID: user.UserID,
UserName: user.UserName,
Phone: user.Phone,
Email: user.Email,
PasswordVersion: pwdprovider.NowPasswordVersion(),
UserState: 1,
}
u.Password, ex = pwdservice.ConvertPwd(user.ClientID, user.Password, u.PasswordVersion)
if ex != nil {
return false, exception.NewException(exception.Error, 1003, "System Error, password converted failed.-->"+ex.Message)
}
db.Create(&u)
var u2 domain.User
db.First(&u2)
if &u2 == nil {
return false, exception.NewException(exception.Error, 1004, "An exception has occaurred. Please try again.")
}
return true, nil
}
// Delete is user delete the account.
func Delete(password string, clientID string, accessToken string) (bool, *exception.Exception) {
s, ex := sessionservice.VerifyGlobal(accessToken)
if ex != nil {
return false, ex
}
u, ex := verifyUser(clientID, s.UserID, password)
if ex != nil {
return false, ex
}
db, ex := dbpool.Take()
if ex != nil {
return false, exception.NewException(exception.Error, 1001, ex.Message)
}
defer dbpool.Put(db)
db.Delete(&u)
return true, nil
}
// SignIn the system by the UserID and Password, Authorize the user is legal.
// Return the access token.
func SignIn(clientID string, userID string, password string) (string, *exception.Exception) {
exists, ex := redisservice.Exits(signLogTag + userID)
if exists {
at, ex := redisservice.Get(signLogTag + userID)
if ex != nil {
_, ex := sessionservice.VerifyGlobal(at)
if ex == nil {
return at, nil
}
}
}
_, ex = verifyUser(clientID, userID, password)
if ex != nil {
return "", ex
}
roles, ex := findRoles(userID)
if ex != nil {
return "", ex.ResetCode(1005)
}
accessToken, ex := tokenservice.SignAccessToken(clientID)
if ex != nil {
return "", ex.ResetCode(1006)
}
_, ex = sessionservice.NewGlobal(userID, accessToken, roles)
if ex != nil {
return "", ex.ResetCode(1007)
}
_, ex = redisservice.Set(signLogTag+userID, accessToken, -1)
return accessToken, nil
}
func SignOut(accessToken string) *exception.Exception {
_, ex := sessionservice.VerifyGlobal(accessToken)
if ex != nil {
return ex
}
//ex = sessionservice.RemoveGlobal(accessToken)
if ex != nil {
return ex
}
return nil
}
func SignVerify(clientID string, accessToken string, password string) (bool, *exception.Exception) {
s, ex := sessionservice.VerifyGlobal(accessToken)
if ex != nil {
return false, ex
}
_, ex = verifyUser(clientID, s.UserID, password)
if ex != nil {
return false, ex
}
return true, nil
}
func verifyUser(clientID string, userid string, password string) (*domain.User, *exception.Exception) {
db, ex := dbpool.Take()
if ex != nil {
return nil, exception.NewException(exception.Error, 1001, ex.Message)
}
defer dbpool.Put(db)
var u domain.User
db.First(&u, "user_id = ?", userid)
if &u == nil {
return nil, exception.NewException(exception.Error, 1002, "Found not the user.")
}
pwd, ex := pwdservice.ConvertPwd(clientID, password, u.PasswordVersion)
if ex != nil {
return nil, ex.ResetCode(1003)
}
if pwd != u.Password {
return nil, exception.NewException(exception.Error, 1004, "Verify failed, becuse the UserID or Password is error.")
}
return &u, nil
}
// RegisterSystem : Registe this user in the system.
func RegisterSystem(systemID string, accessToken string) (string, *exception.Exception) {
//global, ex := sessionservice.VerifyGlobal(accessToken)
//if ex != nil {
// return "", ex.ResetCode(1001)
//}
//conn, ok := broadcast.ConnPool[systemID]
//if ok {
//
//}
//conn.WriteMessage()
system, _ := sessionservice.VerifySystem(systemID, accessToken)
if system == "" {
return sessionservice.GetToken(systemID, accessToken)
}
r := make((map[string][]domain.Role), 1)
roles := &r
systemRoles, ok := (*roles)[systemID]
if !ok {
return "", exception.NewException(exception.Error, 1002, "This user had not authority in the system.")
}
return sessionservice.NewSystem(systemID, accessToken, &systemRoles)
}
func findRoles(userID string) (*[]domain.Role, *exception.Exception) {
db, ex := dbpool.Take()
if ex != nil {
return nil, ex
}
defer dbpool.Put(db)
var unr []domain.UserNRole
db.Find(&unr, "user_id = ?", userID)
roles := make([]domain.Role, len(unr))
for i := 0; i < len(unr); i++ {
item := unr[i]
db.First(&roles[i], "ID = ?", item.RoleID)
}
return &roles, nil
}