Skip to content

Commit

Permalink
✨ Support anonymous list namespace api
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone committed Nov 21, 2023
1 parent 8fd894e commit f675fe6
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
16 changes: 16 additions & 0 deletions pkg/dal/dao/mocks/namespace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions pkg/dal/dao/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type NamespaceService interface {
GetByName(ctx context.Context, name string) (*models.Namespace, error)
// ListNamespace lists all namespaces.
ListNamespace(ctx context.Context, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.Namespace, int64, error)
// ListNamespaceWithAuth lists all namespaces with auth.
ListNamespaceWithAuth(ctx context.Context, userID int64, anonymous bool, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.Namespace, int64, error)
// CountNamespace counts all namespaces.
CountNamespace(ctx context.Context, name *string) (int64, error)
// DeleteByID deletes the namespace with the specified namespace ID.
Expand Down Expand Up @@ -139,6 +141,41 @@ func (s *namespaceService) ListNamespace(ctx context.Context, name *string, pagi
return q.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}

// ListNamespaceWithAuth lists all namespaces with auth.
func (s *namespaceService) ListNamespaceWithAuth(ctx context.Context, userID int64, anonymous bool, name *string, pagination types.Pagination, sort types.Sortable) ([]*models.Namespace, int64, error) {
pagination = utils.NormalizePagination(pagination)
q := s.tx.Namespace.WithContext(ctx)
if name != nil {
q = q.Where(s.tx.Namespace.Name.Like(fmt.Sprintf("%s%%", ptr.To(name))))
}
if anonymous { // find the public namespace
q = q.Where(s.tx.Namespace.Visibility.Eq(enums.VisibilityPublic))
} else { // find user id authenticated namespace
userObj, err := s.tx.User.WithContext(ctx).Where(s.tx.User.ID.Eq(userID)).First()
if err != nil {
return nil, 0, err
}
if !(userObj.Role == enums.UserRoleAdmin || userObj.Role == enums.UserRoleRoot) {
q = q.LeftJoin(s.tx.NamespaceRole, s.tx.Namespace.ID.EqCol(s.tx.NamespaceRole.NamespaceID), s.tx.NamespaceRole.UserID.Eq(userID)).
Where(s.tx.NamespaceRole.ID.IsNotNull()).Or(s.tx.Namespace.Visibility.Eq(enums.VisibilityPublic))
}
}
field, ok := s.tx.Namespace.GetFieldByName(ptr.To(sort.Sort))
if ok {
switch ptr.To(sort.Method) {
case enums.SortMethodDesc:
q = q.Order(field.Desc())
case enums.SortMethodAsc:
q = q.Order(field)
default:
q = q.Order(s.tx.Namespace.UpdatedAt.Desc())
}
} else {
q = q.Order(s.tx.Namespace.UpdatedAt.Desc())
}
return q.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}

// CountNamespace counts all namespaces.
func (s *namespaceService) CountNamespace(ctx context.Context, name *string) (int64, error) {
q := s.tx.Namespace.WithContext(ctx)
Expand Down
14 changes: 13 additions & 1 deletion pkg/handlers/namespaces/namespaces_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/rs/zerolog/log"

"github.com/go-sigma/sigma/pkg/consts"
"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/types"
"github.com/go-sigma/sigma/pkg/utils"
"github.com/go-sigma/sigma/pkg/xerrors"
Expand All @@ -44,6 +45,17 @@ import (
func (h *handler) ListNamespace(c echo.Context) error {
ctx := log.Logger.WithContext(c.Request().Context())

iuser := c.Get(consts.ContextUser)
if iuser == nil {
log.Error().Msg("Get user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}
user, ok := iuser.(*models.User)
if !ok {
log.Error().Msg("Convert user from header failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeUnauthorized)
}

var req types.ListNamespaceRequest
err := utils.BindValidate(c, &req)
if err != nil {
Expand All @@ -53,7 +65,7 @@ func (h *handler) ListNamespace(c echo.Context) error {
req.Pagination = utils.NormalizePagination(req.Pagination)

namespaceService := h.namespaceServiceFactory.New()
namespaceObjs, total, err := namespaceService.ListNamespace(ctx, req.Name, req.Pagination, req.Sortable)
namespaceObjs, total, err := namespaceService.ListNamespaceWithAuth(ctx, user.ID, false, req.Name, req.Pagination, req.Sortable)
if err != nil {
log.Error().Err(err).Msg("List namespace failed")
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeInternalError, err.Error())
Expand Down

0 comments on commit f675fe6

Please sign in to comment.