Skip to content

Commit

Permalink
Merge pull request #500 from openinfradev/develop
Browse files Browse the repository at this point in the history
240521 develop to release
  • Loading branch information
ktkfree authored May 21, 2024
2 parents 419a724 + e0c8cd8 commit 3e417da
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 30 deletions.
3 changes: 1 addition & 2 deletions internal/model/policy-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ func (pt *PolicyTemplate) IsPermittedToOrganization(organizationId *string) bool
}

if pt.IsTksTemplate() {
return len(pt.PermittedOrganizationIds) == 0 ||
slices.Contains(pt.PermittedOrganizationIds, *organizationId)
return slices.Contains(pt.PermittedOrganizationIds, *organizationId)
}

return pt.OrganizationId != nil && *organizationId == *pt.OrganizationId
Expand Down
27 changes: 2 additions & 25 deletions internal/repository/policy-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,6 @@ func (r *PolicyTemplateRepository) FetchForOrganization(ctx context.Context, org
pg = pagination.NewPagination(nil)
}

// 다음과 같은 쿼리를 생성해서 tks 템플릿에 대해선 PermittedOrganizations가 비거나, PermittedOrganizations에 해당 organizations이 속하는 템플릿을 찾음
// organization 템플릿은 organizationId가 매칭되는 것을 찾음, 이를 통해 해당 사용자가 사용할 수 있는 모든 템플릿을 fetch
// select id from policy_templates where
// (
// type = 'tks'
// and (
// id not in (select policy_template_id from policy_template_permitted_organizations) -- PermitedOrganizations이 빈 경우, 모두에게 허용
// or id in (select policy_template_id from policy_template_permitted_organizations organization where organization_id = 'orgid') -- PermitedOrganizations 허용된 경우
// )
// )
// or (type = 'organization' and organization_id='orgid')
subQueryAloowedAll := r.db.Table("policy_template_permitted_organizations").Select("policy_template_id")
subQueryMatchId := r.db.Table("policy_template_permitted_organizations").Select("policy_template_id").
Where("organization_id = ?", organizationId)

Expand All @@ -156,12 +144,7 @@ func (r *PolicyTemplateRepository) FetchForOrganization(ctx context.Context, org
Where(
// tks 템플릿인 경우
r.db.Where("type = ?", "tks").
Where(
// permitted_organizations이 비어있거나
r.db.Where("id not in (?)", subQueryAloowedAll).
Or("id in (?)", subQueryMatchId),
// permitted_organization에 매칭되는 템플릿 아이디가 있거나
),
Where("id in (?)", subQueryMatchId),
).
Or(
// organization 타입 템플릿이면서 organization_id가 매칭
Expand All @@ -178,7 +161,6 @@ func (r *PolicyTemplateRepository) FetchForOrganization(ctx context.Context, org
}

func (r *PolicyTemplateRepository) CountTksTemplateByOrganization(ctx context.Context, organizationId string) (count int64, err error) {
subQueryAloowedAll := r.db.Table("policy_template_permitted_organizations").Select("policy_template_id")
subQueryMatchId := r.db.Table("policy_template_permitted_organizations").Select("policy_template_id").
Where("organization_id = ?", organizationId)

Expand All @@ -187,12 +169,7 @@ func (r *PolicyTemplateRepository) CountTksTemplateByOrganization(ctx context.Co
Where(
// tks 템플릿인 경우
r.db.Where("type = ?", "tks").
Where(
// permitted_organizations이 비어있거나
r.db.Where("id not in (?)", subQueryAloowedAll).
Or("id in (?)", subQueryMatchId),
// permitted_organization에 매칭되는 템플릿 아이디가 있거나
),
Where("id in (?)", subQueryMatchId),
).Count(&count).Error

return
Expand Down
4 changes: 1 addition & 3 deletions internal/usecase/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"strings"

mapset "github.com/deckarep/golang-set/v2"
"github.com/google/uuid"
"github.com/openinfradev/tks-api/internal/middleware/auth/request"
"github.com/openinfradev/tks-api/internal/model"
Expand Down Expand Up @@ -508,8 +507,7 @@ func (u *PolicyUsecase) GetMandatoryPolicies(ctx context.Context, organizationId
for _, policyTemplate := range policyTemplates {
templateId := policyTemplate.ID.String()

if len(policyTemplate.PermittedOrganizationIds) == 0 ||
mapset.NewSet(policyTemplate.PermittedOrganizationIds...).Contains(organizationId) {
if slices.Contains(policyTemplate.PermittedOrganizationIds, organizationId) {
templateMaps[templateId] = &domain.MandatoryTemplateInfo{
TemplateName: policyTemplate.TemplateName,
TemplateId: templateId,
Expand Down

0 comments on commit 3e417da

Please sign in to comment.