github.com/polarismesh/polaris@v1.17.8/service/routing_config_v1_authability.go (about)

     1  /**
     2   * Tencent is pleased to support the open source community by making Polaris available.
     3   *
     4   * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
     5   *
     6   * Licensed under the BSD 3-Clause License (the "License");
     7   * you may not use this file except in compliance with the License.
     8   * You may obtain a copy of the License at
     9   *
    10   * https://opensource.org/licenses/BSD-3-Clause
    11   *
    12   * Unless required by applicable law or agreed to in writing, software distributed
    13   * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
    14   * CONDITIONS OF ANY KIND, either express or implied. See the License for the
    15   * specific language governing permissions and limitations under the License.
    16   */
    17  
    18  package service
    19  
    20  import (
    21  	"context"
    22  
    23  	apimodel "github.com/polarismesh/specification/source/go/api/v1/model"
    24  	apiservice "github.com/polarismesh/specification/source/go/api/v1/service_manage"
    25  	apitraffic "github.com/polarismesh/specification/source/go/api/v1/traffic_manage"
    26  
    27  	api "github.com/polarismesh/polaris/common/api/v1"
    28  	"github.com/polarismesh/polaris/common/model"
    29  	"github.com/polarismesh/polaris/common/utils"
    30  )
    31  
    32  // CreateRoutingConfigs creates routing configs
    33  func (svr *serverAuthAbility) CreateRoutingConfigs(
    34  	ctx context.Context, reqs []*apitraffic.Routing) *apiservice.BatchWriteResponse {
    35  	authCtx := svr.collectRouteRuleAuthContext(ctx, reqs, model.Create, "CreateRoutingConfigs")
    36  
    37  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    38  	if err != nil {
    39  		return api.NewBatchWriteResponseWithMsg(apimodel.Code_NotAllowedAccess, err.Error())
    40  	}
    41  
    42  	ctx = authCtx.GetRequestContext()
    43  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    44  
    45  	return svr.targetServer.CreateRoutingConfigs(ctx, reqs)
    46  }
    47  
    48  // DeleteRoutingConfigs deletes routing configs
    49  func (svr *serverAuthAbility) DeleteRoutingConfigs(
    50  	ctx context.Context, reqs []*apitraffic.Routing) *apiservice.BatchWriteResponse {
    51  	authCtx := svr.collectRouteRuleAuthContext(ctx, reqs, model.Delete, "DeleteRoutingConfigs")
    52  
    53  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    54  	if err != nil {
    55  		return api.NewBatchWriteResponseWithMsg(apimodel.Code_NotAllowedAccess, err.Error())
    56  	}
    57  
    58  	ctx = authCtx.GetRequestContext()
    59  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    60  
    61  	return svr.targetServer.DeleteRoutingConfigs(ctx, reqs)
    62  }
    63  
    64  // UpdateRoutingConfigs updates routing configs
    65  func (svr *serverAuthAbility) UpdateRoutingConfigs(
    66  	ctx context.Context, reqs []*apitraffic.Routing) *apiservice.BatchWriteResponse {
    67  	authCtx := svr.collectRouteRuleAuthContext(ctx, reqs, model.Modify, "UpdateRoutingConfigs")
    68  
    69  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    70  	if err != nil {
    71  		return api.NewBatchWriteResponseWithMsg(apimodel.Code_NotAllowedAccess, err.Error())
    72  	}
    73  
    74  	ctx = authCtx.GetRequestContext()
    75  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    76  
    77  	return svr.targetServer.UpdateRoutingConfigs(ctx, reqs)
    78  }
    79  
    80  // GetRoutingConfigs gets routing configs
    81  func (svr *serverAuthAbility) GetRoutingConfigs(
    82  	ctx context.Context, query map[string]string) *apiservice.BatchQueryResponse {
    83  	authCtx := svr.collectRouteRuleAuthContext(ctx, nil, model.Read, "GetRoutingConfigs")
    84  
    85  	_, err := svr.strategyMgn.GetAuthChecker().CheckConsolePermission(authCtx)
    86  	if err != nil {
    87  		return api.NewBatchQueryResponseWithMsg(apimodel.Code_NotAllowedAccess, err.Error())
    88  	}
    89  
    90  	ctx = authCtx.GetRequestContext()
    91  	ctx = context.WithValue(ctx, utils.ContextAuthContextKey, authCtx)
    92  
    93  	return svr.targetServer.GetRoutingConfigs(ctx, query)
    94  }