github.com/polarismesh/polaris@v1.17.8/config/resource_listener.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 config
    19  
    20  import (
    21  	"context"
    22  	"strconv"
    23  
    24  	apiconfig "github.com/polarismesh/specification/source/go/api/v1/config_manage"
    25  	apisecurity "github.com/polarismesh/specification/source/go/api/v1/security"
    26  
    27  	"github.com/polarismesh/polaris/common/model"
    28  	"github.com/polarismesh/polaris/common/utils"
    29  )
    30  
    31  // ResourceHook The listener is placed before and after the resource operation, only normal flow
    32  type ResourceHook interface {
    33  	// Before
    34  	Before(ctx context.Context, resourceType model.Resource)
    35  	// After
    36  	After(ctx context.Context, resourceType model.Resource, res *ResourceEvent) error
    37  }
    38  
    39  // ResourceEvent 资源事件
    40  type ResourceEvent struct {
    41  	ConfigGroup *apiconfig.ConfigFileGroup
    42  }
    43  
    44  // Before this function is called before the resource operation
    45  func (s *serverAuthability) Before(ctx context.Context, resourceType model.Resource) {
    46  	// do nothing
    47  }
    48  
    49  // After this function is called after the resource operation
    50  func (s *serverAuthability) After(ctx context.Context, resourceType model.Resource, res *ResourceEvent) error {
    51  	switch resourceType {
    52  	case model.RConfigGroup:
    53  		return s.onConfigGroupResource(ctx, res)
    54  	default:
    55  		return nil
    56  	}
    57  }
    58  
    59  // onConfigGroupResource
    60  func (s *serverAuthability) onConfigGroupResource(ctx context.Context, res *ResourceEvent) error {
    61  	authCtx := ctx.Value(utils.ContextAuthContextKey).(*model.AcquireContext)
    62  
    63  	authCtx.SetAttachment(model.ResourceAttachmentKey, map[apisecurity.ResourceType][]model.ResourceEntry{
    64  		apisecurity.ResourceType_ConfigGroups: {
    65  			{
    66  				ID:    strconv.FormatUint(res.ConfigGroup.Id.GetValue(), 10),
    67  				Owner: utils.ParseOwnerID(ctx),
    68  			},
    69  		},
    70  	})
    71  
    72  	users := utils.ConvertStringValuesToSlice(res.ConfigGroup.UserIds)
    73  	removeUses := utils.ConvertStringValuesToSlice(res.ConfigGroup.RemoveUserIds)
    74  
    75  	groups := utils.ConvertStringValuesToSlice(res.ConfigGroup.GroupIds)
    76  	removeGroups := utils.ConvertStringValuesToSlice(res.ConfigGroup.RemoveGroupIds)
    77  
    78  	authCtx.SetAttachment(model.LinkUsersKey, utils.StringSliceDeDuplication(users))
    79  	authCtx.SetAttachment(model.RemoveLinkUsersKey, utils.StringSliceDeDuplication(removeUses))
    80  
    81  	authCtx.SetAttachment(model.LinkGroupsKey, utils.StringSliceDeDuplication(groups))
    82  	authCtx.SetAttachment(model.RemoveLinkGroupsKey, utils.StringSliceDeDuplication(removeGroups))
    83  
    84  	return s.strategyMgn.AfterResourceOperation(authCtx)
    85  }