refactoring
This commit is contained in:
1 parent
f215008349
commit
0aec014161
13 files changed
+330
-311
No files matched your search
+38
-38
@@ -9,6 +9,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/domain"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/model"
|
||||
"isp-system-service/redis"
|
||||
@@ -23,12 +24,11 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
Token tokenController
|
||||
generator = tg.NewGenerator(tg.DefaultAlphabet)
|
||||
)
|
||||
|
||||
type BoolResponse struct {
|
||||
Ok bool
|
||||
}
|
||||
type tokenController struct{}
|
||||
|
||||
// CreateToken godoc
|
||||
// @Tags token
|
||||
@@ -36,12 +36,12 @@ type BoolResponse struct {
|
||||
// @Description Созддает токен и привязывает его к приложению
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body controller.CreateTokenRequest true "Объект создания токена"
|
||||
// @Success 200 {object} controller.AppWithToken
|
||||
// @Param body body domain.CreateTokenRequest true "Объект создания токена"
|
||||
// @Success 200 {object} domain.AppWithToken
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/create_token [POST]
|
||||
func CreateToken(req CreateTokenRequest) (*AppWithToken, error) {
|
||||
m, err, app := GetIdMap(req.AppId)
|
||||
func (c tokenController) CreateToken(req domain.CreateTokenRequest) (*domain.AppWithToken, error) {
|
||||
m, err, app := c.getIdMap(req.AppId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -70,7 +70,7 @@ func CreateToken(req CreateTokenRequest) (*AppWithToken, error) {
|
||||
CreatedAt: time.Now(),
|
||||
}
|
||||
|
||||
err = SetIdentityMapForToken(token, m)
|
||||
err = c.setIdentityMapForToken(token, m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -81,7 +81,7 @@ func CreateToken(req CreateTokenRequest) (*AppWithToken, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
arr, err := enrichWithTokens(*app)
|
||||
arr, err := Application.enrichWithTokens(*app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -95,20 +95,20 @@ func CreateToken(req CreateTokenRequest) (*AppWithToken, error) {
|
||||
// @Description Отвязывает токены от приложений и удялет их
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body controller.RevokeTokensRequest true "Объект отзыва токенов"
|
||||
// @Success 200 {object} controller.AppWithToken
|
||||
// @Param body body domain.RevokeTokensRequest true "Объект отзыва токенов"
|
||||
// @Success 200 {object} domain.AppWithToken
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/revoke_tokens [POST]
|
||||
func RevokeTokens(req RevokeTokensRequest) (*AppWithToken, error) {
|
||||
func (c tokenController) RevokeTokens(req domain.RevokeTokensRequest) (*domain.AppWithToken, error) {
|
||||
app, err := model.AppRep.GetApplicationById(req.AppId)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, err = revokeTokens(req.Tokens, &model.TokenRep)
|
||||
_, err = c.revokeTokens(req.Tokens, &model.TokenRep)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res, err := enrichWithTokens(*app)
|
||||
res, err := Application.enrichWithTokens(*app)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -121,12 +121,12 @@ func RevokeTokens(req RevokeTokensRequest) (*AppWithToken, error) {
|
||||
// @Description Отвязывает токены от приложений и удаляет их по идентификатору приложения
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body controller.Identity true "Идентификатор приложения"
|
||||
// @Success 200 {object} controller.DeleteResponse
|
||||
// @Param body body domain.Identity true "Идентификатор приложения"
|
||||
// @Success 200 {object} domain.DeleteResponse
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/revoke_tokens_for_app [POST]
|
||||
func RevokeTokensForApp(identity Identity) (*DeleteResponse, error) {
|
||||
return revokeTokensForApp(identity, &model.TokenRep)
|
||||
func (c tokenController) RevokeTokensForApp(identity domain.Identity) (*domain.DeleteResponse, error) {
|
||||
return c.revokeTokensForApp(identity, &model.TokenRep)
|
||||
}
|
||||
|
||||
// GetTokensByAppId godoc
|
||||
@@ -135,15 +135,19 @@ func RevokeTokensForApp(identity Identity) (*DeleteResponse, error) {
|
||||
// @Description Возвращает список токенов, привязанных к приложению
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body controller.Identity true "Идентификатор приложения"
|
||||
// @Param body body domain.Identity true "Идентификатор приложения"
|
||||
// @Success 200 {array} entity.Token
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /token/get_tokens_by_app_id [POST]
|
||||
func GetTokensByAppId(identity Identity) ([]entity.Token, error) {
|
||||
func (tokenController) GetTokensByAppId(identity domain.Identity) ([]entity.Token, error) {
|
||||
return model.TokenRep.GetTokensByAppId(identity.Id)
|
||||
}
|
||||
|
||||
func SetIdentityMapForTokenV2(token string, expireTime int64, idMap map[string]interface{}) error {
|
||||
func (c tokenController) setIdentityMapForToken(token entity.Token, idMap map[string]interface{}) error {
|
||||
return c.SetIdentityMapForTokenV2(token.Token, token.ExpireTime, idMap)
|
||||
}
|
||||
|
||||
func (tokenController) SetIdentityMapForTokenV2(token string, expireTime int64, idMap map[string]interface{}) error {
|
||||
cfg := config.Get().(*conf.Configuration)
|
||||
_, e := redis.Client.Get().UseDbTx(redisLib.ApplicationTokenDb, func(p rd.Pipeliner) error {
|
||||
t := fmt.Sprintf("%s|%s", token, cfg.InstanceUuid)
|
||||
@@ -160,11 +164,7 @@ func SetIdentityMapForTokenV2(token string, expireTime int64, idMap map[string]i
|
||||
return e
|
||||
}
|
||||
|
||||
func SetIdentityMapForToken(token entity.Token, idMap map[string]interface{}) error {
|
||||
return SetIdentityMapForTokenV2(token.Token, token.ExpireTime, idMap)
|
||||
}
|
||||
|
||||
func GetIdMap(appId int32) (map[string]interface{}, error, *entity.Application) {
|
||||
func (tokenController) getIdMap(appId int32) (map[string]interface{}, error, *entity.Application) {
|
||||
app, err := model.AppRep.GetApplicationById(appId)
|
||||
if err != nil {
|
||||
return nil, err, nil
|
||||
@@ -181,50 +181,50 @@ func GetIdMap(appId int32) (map[string]interface{}, error, *entity.Application)
|
||||
return nil, status.Errorf(codes.NotFound, "Service with id %d not found", app.ServiceId), app
|
||||
}
|
||||
|
||||
domain, err := model.DomainRep.GetDomainById(service.DomainId)
|
||||
domainInfo, err := model.DomainRep.GetDomainById(service.DomainId)
|
||||
if err != nil {
|
||||
return nil, err, app
|
||||
}
|
||||
if domain == nil {
|
||||
if domainInfo == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "Domain with id %d not found", service.DomainId), app
|
||||
}
|
||||
|
||||
system, err := model.SystemRep.GetSystemById(domain.SystemId)
|
||||
system, err := model.SystemRep.GetSystemById(domainInfo.SystemId)
|
||||
if err != nil {
|
||||
return nil, err, app
|
||||
}
|
||||
if system == nil {
|
||||
return nil, status.Errorf(codes.NotFound, "System with id %d not found", domain.SystemId), app
|
||||
return nil, status.Errorf(codes.NotFound, "System with id %d not found", domainInfo.SystemId), app
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
SystemIdentityFieldInDb: system.Id,
|
||||
DomainIdentityFieldInDb: domain.Id,
|
||||
DomainIdentityFieldInDb: domainInfo.Id,
|
||||
ServiceIdentityFieldInDb: service.Id,
|
||||
ApplicationIdentityFieldInDb: app.Id,
|
||||
}, nil, app
|
||||
}
|
||||
|
||||
func revokeTokensForApp(identity Identity, tokenRep *model.TokenRepository) (*DeleteResponse, error) {
|
||||
tokens, err := GetTokensByAppId(identity)
|
||||
func (c tokenController) revokeTokensForApp(identity domain.Identity, tokenRep *model.TokenRepository) (*domain.DeleteResponse, error) {
|
||||
tokens, err := c.GetTokensByAppId(identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
l := len(tokens)
|
||||
if l == 0 {
|
||||
return &DeleteResponse{0}, nil
|
||||
return &domain.DeleteResponse{Deleted: 0}, nil
|
||||
}
|
||||
tokenIdList := make([]string, l)
|
||||
for i, t := range tokens {
|
||||
tokenIdList[i] = t.Token
|
||||
}
|
||||
return revokeTokens(tokenIdList, tokenRep)
|
||||
return c.revokeTokens(tokenIdList, tokenRep)
|
||||
}
|
||||
|
||||
func revokeTokens(tokens []string, tokenRep *model.TokenRepository) (*DeleteResponse, error) {
|
||||
func (tokenController) revokeTokens(tokens []string, tokenRep *model.TokenRepository) (*domain.DeleteResponse, error) {
|
||||
var count = 0
|
||||
if len(tokens) == 0 {
|
||||
return &DeleteResponse{0}, nil
|
||||
return &domain.DeleteResponse{Deleted: 0}, nil
|
||||
}
|
||||
_, e := redis.Client.Get().UseDbTx(redisLib.ApplicationTokenDb, func(p rd.Pipeliner) error {
|
||||
deleted, err := tokenRep.DeleteTokens(tokens)
|
||||
@@ -238,5 +238,5 @@ func revokeTokens(tokens []string, tokenRep *model.TokenRepository) (*DeleteResp
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
return &DeleteResponse{count}, nil
|
||||
return &domain.DeleteResponse{Deleted: count}, nil
|
||||
}
|
||||
Reference in new issue
Block a user