satellite/console: allow for project limits even with open registration

Change-Id: I4d2528880638882ab8c427bd926e0c4f4b0a5bab
This commit is contained in:
JT Olio 2020-03-10 17:01:19 -06:00 committed by jens
parent 7aa30d2f06
commit 520b16e824

View File

@ -6,12 +6,13 @@ package console
import (
"context"
"crypto/subtle"
"database/sql"
"fmt"
"sort"
"time"
"github.com/skyrings/skyring-common/tools/uuid"
"github.com/spacemonkeygo/monkit/v3"
monkit "github.com/spacemonkeygo/monkit/v3"
"github.com/zeebo/errs"
"go.uber.org/zap"
"golang.org/x/crypto/bcrypt"
@ -28,6 +29,8 @@ import (
var mon = monkit.Package()
const (
openRegistrationProjectLimit = 1
// maxLimit specifies the limit for all paged queries.
maxLimit = 50
tokenExpirationTime = 24 * time.Hour
@ -48,9 +51,8 @@ const (
teamMemberDoesNotExistErrMsg = `There is no account on this Satellite for the user(s) you have entered.
Please add team members with active accounts`
// TODO: remove after vanguard release
usedRegTokenVanguardErrMsg = "This registration token has already been used"
projLimitVanguardErrMsg = "Sorry, during the Vanguard release you have a limited number of projects"
usedRegTokenErrMsg = "This registration token has already been used"
projLimitErrMsg = "Sorry, project creation is limited for your account. Please contact support!"
)
var (
@ -399,7 +401,7 @@ func (s *Service) CreateUser(ctx context.Context, user CreateUser, tokenSecret R
// if a registration token is already associated with an user ID, that means the token is already used
// we should terminate the account creation process and return an error
if registrationToken.OwnerID != nil {
return nil, errs.New(usedRegTokenVanguardErrMsg)
return nil, errs.New(usedRegTokenErrMsg)
}
u, err = s.store.Users().GetByEmail(ctx, user.Email)
@ -830,7 +832,6 @@ func (s *Service) CreateProject(ctx context.Context, projectInfo ProjectInfo) (p
return nil, err
}
// TODO: remove after vanguard release
err = s.checkProjectLimit(ctx, auth.User.ID)
if err != nil {
return nil, ErrProjLimit.Wrap(err)
@ -1317,20 +1318,36 @@ func (s *Service) Authorize(ctx context.Context) (a Authorization, err error) {
}, nil
}
func (s *Service) getProjectLimit(ctx context.Context, userID uuid.UUID) (limit int, err error) {
defer mon.Task()(&ctx)(&err)
registrationToken, err := s.store.RegistrationTokens().GetByOwnerID(ctx, userID)
if err != nil {
if err == sql.ErrNoRows {
return openRegistrationProjectLimit, nil
}
return 0, err
}
return registrationToken.ProjectLimit, nil
}
// checkProjectLimit is used to check if user is able to create a new project
func (s *Service) checkProjectLimit(ctx context.Context, userID uuid.UUID) (err error) {
defer mon.Task()(&ctx)(&err)
registrationToken, err := s.store.RegistrationTokens().GetByOwnerID(ctx, userID)
projectLimit, err := s.getProjectLimit(ctx, userID)
if err != nil {
return err
return Error.Wrap(err)
}
projects, err := s.GetUsersProjects(ctx)
if err != nil {
return Error.Wrap(err)
}
if len(projects) >= registrationToken.ProjectLimit {
return ErrProjLimit.Wrap(errs.New(projLimitVanguardErrMsg))
if len(projects) >= projectLimit {
return ErrProjLimit.Wrap(errs.New(projLimitErrMsg))
}
return nil