You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.9 KiB

2 years ago
package service
import (
"context"
2 years ago
user "git.diulo.com/mogfee/kit/api"
2 years ago
"git.diulo.com/mogfee/kit/middleware/jwt"
2 years ago
)
2 years ago
type userServer struct {
2 years ago
user.UnimplementedUserServer
2 years ago
tokenKey string
2 years ago
}
2 years ago
func NewUserService(tokenKey string) user.UserServer {
2 years ago
2 years ago
return &userServer{
tokenKey: tokenKey,
}
}
func (userServer) List(ctx context.Context, request *user.Request) (*user.Response, error) {
2 years ago
userInfo, _ := jwt.FromUserContext(ctx)
2 years ago
return &user.Response{
2 years ago
Token: userInfo.UserName,
2 years ago
}, nil
2 years ago
}
2 years ago
2 years ago
func (userServer) All(ctx context.Context, request *user.Request) (*user.Response, error) {
userInfo, _ := jwt.FromUserContext(ctx)
2 years ago
return &user.Response{
2 years ago
Token: userInfo.UserName,
2 years ago
}, nil
}
2 years ago
2 years ago
func (userServer) Auto(ctx context.Context, request *user.Request) (*user.Response, error) {
userInfo, ok := jwt.FromUserContext(ctx)
if ok {
return &user.Response{
Token: userInfo.UserName,
}, nil
} else {
return &user.Response{
Token: "no login",
}, nil
}
2 years ago
}
2 years ago
func (s *userServer) LoginWithList(ctx context.Context, request *user.Request) (*user.Response, error) {
token, _, err := jwt.GetToken(s.tokenKey, &jwt.UserInfo{
UserId: 1,
UserName: "test",
UserType: "user",
Permissions: []string{"user:list"},
UniqueId: "",
})
if err != nil {
return nil, err
}
return &user.Response{Token: token}, nil
}
func (s *userServer) Login(ctx context.Context, request *user.Request) (*user.Response, error) {
token, _, err := jwt.GetToken(s.tokenKey, &jwt.UserInfo{
UserId: 1,
UserName: "test",
UserType: "user",
UniqueId: "",
})
if err != nil {
return nil, err
}
return &user.Response{Token: token}, nil
2 years ago
}
1 year ago
func (s *userServer) Login1(ctx context.Context, request *user.Request) (*user.Response, error) {
token, _, err := jwt.GetToken(s.tokenKey, &jwt.UserInfo{
UserId: 1,
UserName: "test",
UserType: "user",
UniqueId: "",
})
if err != nil {
return nil, err
}
return &user.Response{Token: token}, nil
}