/
login.go
92 lines (77 loc) · 2.11 KB
/
login.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
package service
import (
"context"
"fmt"
"github.com/chubaofs/chubaofs/console/cutil"
"github.com/chubaofs/chubaofs/proto"
"github.com/chubaofs/chubaofs/sdk/graphql/client"
"github.com/chubaofs/chubaofs/sdk/graphql/client/user"
"github.com/samsarahq/thunder/graphql"
"github.com/samsarahq/thunder/graphql/schemabuilder"
)
type LoginService struct {
client *client.MasterGClient
userClient *user.UserClient
}
func NewLoginService(client *client.MasterGClient) *LoginService {
return &LoginService{
client: client,
userClient: user.NewUserClient(client),
}
}
type UserToken struct {
UserID string `json:"userID"`
Token string `json:"token"`
Status bool `json:"status"`
UserInfo *user.UserInfo
}
func (ls *LoginService) login(ctx context.Context, args struct {
UserID string
Password string
empty bool
}) (*UserToken, error) {
_, err := ls.client.ValidatePassword(ctx, args.UserID, args.Password)
if err != nil {
return nil, err
}
ctx = context.WithValue(ctx, proto.UserKey, args.UserID)
userInfo, err := ls.userClient.GetUserInfoForLogin(ctx, args.UserID)
if err != nil {
return nil, err
}
return &UserToken{
UserID: userInfo.User_id,
Token: cutil.TokenRegister(userInfo),
Status: true,
}, nil
}
func (ls *LoginService) Schema() *graphql.Schema {
schema := schemabuilder.NewSchema()
query := schema.Query()
query.FieldFunc("login", ls.login)
return schema.MustBuild()
}
type permissionMode int
const ADMIN permissionMode = permissionMode(1)
const USER permissionMode = permissionMode(2)
func permissions(ctx context.Context, mode permissionMode) (userInfo *user.UserInfo, perm permissionMode, err error) {
userInfo = ctx.Value(proto.UserInfoKey).(*user.UserInfo)
perm = USER
if userInfo.User_type == uint8(proto.UserTypeRoot) || userInfo.User_type == uint8(proto.UserTypeAdmin) {
perm = ADMIN
}
if ADMIN&mode == ADMIN {
if perm == ADMIN {
return
}
}
if USER&mode == USER {
if perm == USER {
return
}
}
perm = permissionMode(0)
err = fmt.Errorf("user:[%s] permissions has err:[%d] your:[%d]", userInfo.User_id, mode, perm)
userInfo = nil
return
}