Skip to content

Commit

Permalink
admin access to the workbench no longer query all organization inform…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
kakj-go committed Oct 29, 2021
1 parent 984c5e4 commit cd63805
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 16 deletions.
25 changes: 25 additions & 0 deletions bundle/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,31 @@ func (b *Bundle) ListDopOrgs(req *apistructs.OrgSearchRequest) (*apistructs.Pagi
return &resp.Data, nil
}

func (b *Bundle) ListDopOrgsV2(req *apistructs.OrgSearchRequest) (*apistructs.PagingOrgDTO, error) {
host, err := b.urls.DOP()
if err != nil {
return nil, err
}
hc := b.hc

var resp apistructs.OrgSearchResponse
r, err := hc.Get(host).Path("/api/orgs/action/list").
Header(httputil.InternalHeader, "bundle").
Header("User-ID", req.UserID).
Param("q", req.Q).
Param("pageNo", strconv.Itoa(req.PageNo)).
Param("pageSize", strconv.Itoa(req.PageSize)).
Do().
JSON(&resp)
if err != nil {
return nil, apierrors.ErrInvoke.InternalError(err)
}
if !r.IsOK() || !resp.Success {
return nil, toAPIError(r.StatusCode(), resp.Error)
}
return &resp.Data, nil
}

// ListPublicOrgs get list of public orgs
func (b *Bundle) ListPublicOrgs(req *apistructs.OrgSearchRequest) (*apistructs.PagingOrgDTO, error) {
host, err := b.urls.CoreServices()
Expand Down
1 change: 1 addition & 0 deletions modules/core-services/endpoints/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,7 @@ func (e *Endpoints) Routes() []httpserver.Endpoint {
{Path: "/api/orgs/{idOrName}", Method: http.MethodGet, Handler: e.GetOrg},
{Path: "/api/orgs/{idOrName}", Method: http.MethodDelete, Handler: e.DeleteOrg},
{Path: "/api/orgs", Method: http.MethodGet, Handler: e.ListOrg},
{Path: "/api/orgs/action/list", Method: http.MethodGet, Handler: e.ListOrgV2},
{Path: "/api/orgs/actions/list-public", Method: http.MethodGet, Handler: e.ListPublicOrg},
// {Path: "/api/orgs/ingress/{orgID}/actions/update-ingress", Method: http.MethodGet, Handler: e.UpdateOrgIngress},
{Path: "/api/orgs/actions/get-by-domain", Method: http.MethodGet, Handler: e.GetOrgByDomain},
Expand Down
87 changes: 72 additions & 15 deletions modules/core-services/endpoints/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package endpoints
import (
"context"
"encoding/json"
"fmt"
"net/http"
"strconv"

Expand Down Expand Up @@ -297,34 +298,43 @@ func (e *Endpoints) ListOrg(ctx context.Context, r *http.Request, vars map[strin
if err != nil {
return errorresp.ErrResp(err)
}
var (
total int
orgs []model.Org
)
total, orgs, err = e.org.ListOrgs(orgIDs, req, all)
if err != nil {
logrus.Warnf("failed to get orgs, (%v)", err)
return apierrors.ErrListOrg.InternalError(err).ToResp(), nil
}

orgDTOs, err := e.coverOrgsToDto(r, orgs)
if err != nil {
return apierrors.ErrListOrg.InternalError(err).ToResp(), nil
}

return httpserver.OkResp(apistructs.PagingOrgDTO{
List: orgDTOs,
Total: total,
})
}

func (e *Endpoints) coverOrgsToDto(r *http.Request, orgs []model.Org) ([]apistructs.OrgDTO, error) {
var currentOrgID int64
var err error
v := r.Header.Get(httputil.OrgHeader)
if v != "" {
// ignore convert error
currentOrgID, err = strutil.Atoi64(v)
if err != nil {
return apierrors.ErrListOrg.InvalidParameter(strutil.Concat("orgID: ", v)).ToResp(), nil
return nil, err
}
}
if currentOrgID == 0 {
// compatible TODO: refactor it
userID, _ := user.GetUserID(r)
currentOrgID, _ = e.org.GetCurrentOrgByUser(userID.String())
}
var (
total int
orgs []model.Org
)
// TODO: move this logic to service layer
if all {
total, orgs, err = e.org.SearchByName(req.Q, req.PageNo, req.PageSize)
} else {
total, orgs, err = e.org.ListByIDsAndName(orgIDs, req.Q, req.PageNo, req.PageSize)
}
if err != nil {
logrus.Warnf("failed to get orgs, (%v)", err)
return apierrors.ErrListOrg.InternalError(err).ToResp(), nil
}

// 封装成API所需格式
orgDTOs := make([]apistructs.OrgDTO, 0, len(orgs))
Expand All @@ -342,6 +352,34 @@ func (e *Endpoints) ListOrg(ctx context.Context, r *http.Request, vars map[strin
orgDTOs[0].Selected = true
}

return orgDTOs, nil
}

func (e *Endpoints) ListOrgV2(ctx context.Context, r *http.Request, vars map[string]string) (httpserver.Responser, error) {
orgIDs, err := e.getOrgPermissionsIncludeAdmin(r)
if err != nil {
return errorresp.ErrResp(err)
}
// 获取请求参数
req, err := getOrgListParam(r)
if err != nil {
return errorresp.ErrResp(err)
}
var (
total int
orgs []model.Org
)
total, orgs, err = e.org.ListOrgs(orgIDs, req, false)
if err != nil {
logrus.Warnf("failed to get orgs, (%v)", err)
return apierrors.ErrListOrg.InternalError(err).ToResp(), nil
}

orgDTOs, err := e.coverOrgsToDto(r, orgs)
if err != nil {
return apierrors.ErrListOrg.InternalError(err).ToResp(), nil
}

return httpserver.OkResp(apistructs.PagingOrgDTO{
List: orgDTOs,
Total: total,
Expand Down Expand Up @@ -603,6 +641,25 @@ func (e *Endpoints) getOrgPermissions(r *http.Request) (bool, []int64, error) {
}
}

func (e *Endpoints) getOrgPermissionsIncludeAdmin(r *http.Request) ([]int64, error) {
// first check user
userID, _ := user.GetUserID(r)

if userID == "" {
return nil, apierrors.ErrListOrg.InternalError(fmt.Errorf("userID was empty"))
}

members, err := e.member.ListByScopeTypeAndUser(apistructs.OrgScope, userID.String())
if err != nil {
return nil, apierrors.ErrListOrg.InternalError(err)
}
var orgIDs []int64
for i := range members {
orgIDs = append(orgIDs, members[i].ScopeID)
}
return orgIDs, nil
}

// TODO: need refactor
func (e *Endpoints) getPermOrgs(r *http.Request) (bool, []int64, error) {
// 获取当前用户
Expand Down
18 changes: 18 additions & 0 deletions modules/core-services/services/org/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,24 @@ func (o *Org) ListByIDsAndName(orgIDs []int64, name string, pageNo, pageSize int
return o.db.GetOrgsByIDsAndName(orgIDs, name, pageNo, pageSize)
}

func (o *Org) ListOrgs(orgIDs []int64, req *apistructs.OrgSearchRequest, all bool) (int, []model.Org, error) {
var (
total int
orgs []model.Org
err error
)
if all {
total, orgs, err = o.SearchByName(req.Q, req.PageNo, req.PageSize)
} else {
total, orgs, err = o.ListByIDsAndName(orgIDs, req.Q, req.PageNo, req.PageSize)
}
if err != nil {
logrus.Warnf("failed to get orgs, (%v)", err)
return 0, nil, err
}
return total, orgs, nil
}

// ChangeCurrentOrg 切换用户当前所属企业
func (o *Org) ChangeCurrentOrg(userID string, req *apistructs.OrgChangeRequest) error {
// 检查用户是否匹配
Expand Down
34 changes: 34 additions & 0 deletions modules/openapi/api/apis/dop/cmdb_orgs_list.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2021 Terminus, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dop

import (
"github.com/erda-project/erda/apistructs"
"github.com/erda-project/erda/modules/openapi/api/apis"
)

var CMDB_ORG_LIST = apis.ApiSpec{
Path: "/api/orgs/action/list",
BackendPath: "/api/orgs/action/list",
Host: "dop.marathon.l4lb.thisdcos.directory:9527",
Scheme: "http",
Method: "GET",
TryCheckLogin: true,
CheckToken: true,
IsOpenAPI: true,
RequestType: apistructs.OrgSearchRequest{},
ResponseType: apistructs.OrgSearchResponse{},
Doc: "summary: 查询组织列表",
}
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (this *OrgSwitch) RenderList() error {
IdentityInfo: identity,
PageSize: DefaultPageSize,
}
pagingOrgDTO, err := this.ctxBdl.Bdl.ListDopOrgs(req)
pagingOrgDTO, err := this.ctxBdl.Bdl.ListDopOrgsV2(req)
if err != nil {
return err
}
Expand Down

0 comments on commit cd63805

Please sign in to comment.