add access_list methods
This commit is contained in:
1 parent
8a0ebe803a
commit
f215008349
20 files changed
+1511
-141
No files matched your search
@@ -0,0 +1,159 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
rd "github.com/go-redis/redis"
|
||||
rdLib "github.com/integration-system/isp-lib/redis"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/model"
|
||||
"isp-system-service/redis"
|
||||
)
|
||||
|
||||
var AccessList accessListController
|
||||
|
||||
type accessListController struct{}
|
||||
|
||||
// GetById godoc
|
||||
// @Tags accessList
|
||||
// @Summary Получить список доступности методов для приложения
|
||||
// @Description Возвращает список методов для приложения, для которых заданы настройки доступа
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body controller.Identity false "идентификатор приложения"
|
||||
// @Success 200 {array} controller.ModuleMethods "список методов по названию модуля"
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /access_list/get_by_id [POST]
|
||||
func (a accessListController) GetById(req Identity) (ModuleMethods, error) {
|
||||
if accessList, err := model.AccessListRep.GetByAppId(req.Id); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return a.convertAccessList(accessList), nil
|
||||
}
|
||||
}
|
||||
|
||||
// SetOne godoc
|
||||
// @Tags accessList
|
||||
// @Summary Настроить доступность метода для приложения
|
||||
// @Description Настраивает достуность
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body entity.AccessList false "объект для настройки доступа"
|
||||
// @Success 200 {object} controller.CountResponse "количество измененных строк"
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /access_list/set_one [POST]
|
||||
func (c accessListController) SetOne(request entity.AccessList) (*CountResponse, error) {
|
||||
var (
|
||||
resp = 0
|
||||
err error
|
||||
)
|
||||
if err := model.DbClient.RunInTransaction(func(repository model.AccessListRepository) error {
|
||||
if resp, err = repository.Upsert(request); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := redis.Client.Get().UseDb(rdLib.ApplicationPermissionDb, func(p rd.Pipeliner) error {
|
||||
key := fmt.Sprintf("%d|%s", request.AppId, request.Method)
|
||||
if _, err := p.Set(key, request.Value, 0).Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &CountResponse{Count: resp}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// SetList godoc
|
||||
// @Tags accessList
|
||||
// @Summary Настройть доступность списка методов для приложения
|
||||
// @Description Возвращает массив приложений с токенами по их идентификаторам
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Param body body controller.SetListRequest false "объект настройки доступа"
|
||||
// @Success 200 {object} controller.CountResponse "количество добавленных строк"
|
||||
// @Failure 500 {object} structure.GrpcError
|
||||
// @Router /access_list/set_list [POST]
|
||||
func (c accessListController) SetList(request SetListRequest) (*CountResponse, error) {
|
||||
resp := 0
|
||||
if err := model.DbClient.RunInTransaction(func(repository model.AccessListRepository) error {
|
||||
|
||||
oldAccessList, err := repository.DeleteById(request.AppId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
insertRequest := make([]entity.AccessList, len(request.Methods))
|
||||
redisMsetRequest := make([]interface{}, 0)
|
||||
for i, m := range request.Methods {
|
||||
insertRequest[i] = entity.AccessList{
|
||||
AppId: request.AppId,
|
||||
Method: m.Method,
|
||||
Value: m.Value,
|
||||
}
|
||||
redisMsetRequest = append(redisMsetRequest, fmt.Sprintf("%d|%s", request.AppId, m.Method), m.Value)
|
||||
}
|
||||
|
||||
redisDelRequest := make([]string, len(oldAccessList))
|
||||
for i, access := range oldAccessList {
|
||||
redisDelRequest[i] = fmt.Sprintf("%d|%s", request.AppId, access.Method)
|
||||
}
|
||||
|
||||
if resp, err = repository.Insert(insertRequest); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if _, err := redis.Client.Get().UseDb(rdLib.ApplicationPermissionDb, func(p rd.Pipeliner) error {
|
||||
if len(redisDelRequest) > 0 {
|
||||
if _, err := p.Del(redisDelRequest...).Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := p.MSet(redisMsetRequest...).Result(); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
return &CountResponse{Count: resp}, nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c accessListController) convertAccessList(accessLists []entity.AccessList) ModuleMethods {
|
||||
response := make(map[string][]MethodInfo)
|
||||
for _, access := range accessLists {
|
||||
moduleName, method := c.extractModuleName(access.Method)
|
||||
if methodStore, ok := response[moduleName]; ok {
|
||||
response[moduleName] = append(methodStore, MethodInfo{Value: access.Value, Method: method})
|
||||
} else {
|
||||
response[moduleName] = []MethodInfo{{Value: access.Value, Method: method}}
|
||||
}
|
||||
}
|
||||
return response
|
||||
}
|
||||
|
||||
func (accessListController) extractModuleName(method string) (string, string) {
|
||||
firstFound := false
|
||||
for i, value := range method {
|
||||
if value == '/' {
|
||||
if firstFound {
|
||||
return method[:i], method[i:]
|
||||
} else {
|
||||
firstFound = true
|
||||
}
|
||||
}
|
||||
}
|
||||
return method, ""
|
||||
}
|
||||
+45
-13
@@ -1,14 +1,17 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/model"
|
||||
|
||||
"fmt"
|
||||
rd "github.com/go-redis/redis"
|
||||
rdLib "github.com/integration-system/isp-lib/redis"
|
||||
_ "github.com/integration-system/isp-lib/structure"
|
||||
"github.com/integration-system/isp-lib/utils"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/metadata"
|
||||
"google.golang.org/grpc/status"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/model"
|
||||
"isp-system-service/redis"
|
||||
)
|
||||
|
||||
// GetApplications godoc
|
||||
@@ -144,25 +147,54 @@ func DeleteApplications(list []int32) (DeleteResponse, error) {
|
||||
if len(list) == 0 {
|
||||
return DeleteResponse{}, status.Errorf(codes.InvalidArgument, "At least one id are required")
|
||||
}
|
||||
var count = 0
|
||||
err := model.DbClient.RunInTransaction(func(appRep model.AppRepository, tokenRep model.TokenRepository) error {
|
||||
|
||||
var (
|
||||
count = 0
|
||||
err error
|
||||
)
|
||||
|
||||
if err = model.DbClient.RunInTransaction(func(
|
||||
appRep model.AppRepository, tokenRep model.TokenRepository, accessRep model.AccessListRepository) error {
|
||||
|
||||
for _, appId := range list {
|
||||
_, err := revokeTokensForApp(Identity{appId}, &tokenRep)
|
||||
if err != nil {
|
||||
if _, err := revokeTokensForApp(Identity{appId}, &tokenRep); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
res, err := appRep.DeleteApplications(list)
|
||||
if err != nil {
|
||||
|
||||
if count, err = appRep.DeleteApplications(list); err != nil {
|
||||
return err
|
||||
}
|
||||
count = res
|
||||
|
||||
redisDelRequest := make([]string, 0)
|
||||
for _, id := range list {
|
||||
accessList, err := accessRep.DeleteById(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
redisDelArray := make([]string, len(accessList))
|
||||
for i, access := range accessList {
|
||||
redisDelArray[i] = fmt.Sprintf("%d|%s", id, access.Method)
|
||||
}
|
||||
redisDelRequest = append(redisDelRequest, redisDelArray...)
|
||||
}
|
||||
if len(redisDelRequest) > 0 {
|
||||
if _, err := redis.Client.Get().UseDb(rdLib.ApplicationPermissionDb, func(p rd.Pipeliner) error {
|
||||
if _, err := p.Del(redisDelRequest...).Result(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
return DeleteResponse{}, err
|
||||
} else {
|
||||
return DeleteResponse{Deleted: count}, nil
|
||||
}
|
||||
return DeleteResponse{Deleted: count}, nil
|
||||
}
|
||||
|
||||
// GetSystemTree godoc
|
||||
|
||||
@@ -8,6 +8,10 @@ type DeleteResponse struct {
|
||||
Deleted int
|
||||
}
|
||||
|
||||
type CountResponse struct {
|
||||
Count int
|
||||
}
|
||||
|
||||
type Identity struct {
|
||||
Id int32 `json:"id" valid:"required~Required"`
|
||||
}
|
||||
@@ -43,7 +47,20 @@ type RevokeTokensRequest struct {
|
||||
AppId int32 `valid:"required~Required"`
|
||||
Tokens []string
|
||||
}
|
||||
|
||||
type CreateTokenRequest struct {
|
||||
AppId int32 `valid:"required~Required"`
|
||||
ExpireTimeMs int64
|
||||
}
|
||||
|
||||
type ModuleMethods map[string][]MethodInfo
|
||||
|
||||
type MethodInfo struct {
|
||||
Method string
|
||||
Value bool
|
||||
}
|
||||
|
||||
type SetListRequest struct {
|
||||
AppId int32
|
||||
Methods []MethodInfo
|
||||
}
|
||||
+10
-12
@@ -1,20 +1,18 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"isp-system-service/invoker"
|
||||
"time"
|
||||
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/model"
|
||||
|
||||
"fmt"
|
||||
"github.com/go-redis/redis"
|
||||
rd "github.com/go-redis/redis"
|
||||
"github.com/integration-system/isp-lib/config"
|
||||
rd "github.com/integration-system/isp-lib/redis"
|
||||
redisLib "github.com/integration-system/isp-lib/redis"
|
||||
"github.com/integration-system/isp-lib/token-gen"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
"isp-system-service/conf"
|
||||
"isp-system-service/entity"
|
||||
"isp-system-service/model"
|
||||
"isp-system-service/redis"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -79,7 +77,7 @@ func CreateToken(req CreateTokenRequest) (*AppWithToken, error) {
|
||||
|
||||
token, err = model.TokenRep.SaveToken(token)
|
||||
if err != nil {
|
||||
invoker.RedisClient.Del(token.Token)
|
||||
redis.Client.Get().Del(token.Token)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -147,7 +145,7 @@ func GetTokensByAppId(identity Identity) ([]entity.Token, error) {
|
||||
|
||||
func SetIdentityMapForTokenV2(token string, expireTime int64, idMap map[string]interface{}) error {
|
||||
cfg := config.Get().(*conf.Configuration)
|
||||
_, e := invoker.RedisClient.UseDbTx(rd.ApplicationTokenDb, func(p redis.Pipeliner) error {
|
||||
_, e := redis.Client.Get().UseDbTx(redisLib.ApplicationTokenDb, func(p rd.Pipeliner) error {
|
||||
t := fmt.Sprintf("%s|%s", token, cfg.InstanceUuid)
|
||||
stat := p.HMSet(t, idMap)
|
||||
err := stat.Err()
|
||||
@@ -228,7 +226,7 @@ func revokeTokens(tokens []string, tokenRep *model.TokenRepository) (*DeleteResp
|
||||
if len(tokens) == 0 {
|
||||
return &DeleteResponse{0}, nil
|
||||
}
|
||||
_, e := invoker.RedisClient.UseDbTx(rd.ApplicationTokenDb, func(p redis.Pipeliner) error {
|
||||
_, e := redis.Client.Get().UseDbTx(redisLib.ApplicationTokenDb, func(p rd.Pipeliner) error {
|
||||
deleted, err := tokenRep.DeleteTokens(tokens)
|
||||
count = deleted
|
||||
if err != nil {
|
||||
|
||||
Reference in new issue
Block a user