feat(profile): persist candidate edits and enforce company access
This commit is contained in:
1 parent
3ab6bd91a3
commit
73c7caa225
67 files changed
+5286
-229
No files matched your search
@@ -0,0 +1,97 @@
|
||||
package httpadapter
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
ogenmiddleware "github.com/ogen-go/ogen/middleware"
|
||||
|
||||
apiadapter "lincodex/backend/internal/adapter/primary/http/api"
|
||||
"lincodex/backend/internal/adapter/primary/http/problem"
|
||||
v1 "lincodex/backend/internal/adapter/primary/http/v1"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
"lincodex/backend/internal/usecase/organization"
|
||||
)
|
||||
|
||||
// CompanyAuthorizer checks the current authenticated account against a
|
||||
// company's active owner membership.
|
||||
type CompanyAuthorizer interface {
|
||||
AuthorizeCompany(ctx context.Context, actorID, organizationID uuid.UUID) (organization.CompanyAccess, error)
|
||||
}
|
||||
|
||||
var ownerCompanyOperations = map[apiadapter.OperationName]struct{}{
|
||||
apiadapter.GetOrganizationOperation: {},
|
||||
apiadapter.UpdateOrganizationOperation: {},
|
||||
apiadapter.ListOrganizationNeedsOperation: {},
|
||||
apiadapter.CreateOrganizationNeedOperation: {},
|
||||
apiadapter.GetOrganizationNeedOperation: {},
|
||||
apiadapter.UpdateOrganizationNeedOperation: {},
|
||||
apiadapter.ListOrganizationInvitationsOperation: {},
|
||||
apiadapter.CreateOrganizationInvitationOperation: {},
|
||||
apiadapter.GetOrganizationInvitationOperation: {},
|
||||
apiadapter.UpdateOrganizationInvitationOperation: {},
|
||||
apiadapter.SendOrganizationInvitationOperation: {},
|
||||
apiadapter.GetOrganizationInvitationContactsOperation: {},
|
||||
apiadapter.WithdrawOrganizationInvitationOperation: {},
|
||||
apiadapter.RaiseOrganizationInvitationSalaryOperation: {},
|
||||
}
|
||||
|
||||
// NewCompanyContextMiddleware verifies every owner-scoped company operation
|
||||
// after generated security and parameter decoding, then attaches the request
|
||||
// capability. Operations outside the explicit map pass through unchanged.
|
||||
func NewCompanyContextMiddleware(authorizer CompanyAuthorizer) apiadapter.Middleware {
|
||||
return func(request ogenmiddleware.Request, next ogenmiddleware.Next) (ogenmiddleware.Response, error) {
|
||||
if _, required := ownerCompanyOperations[request.OperationName]; !required {
|
||||
return next(request)
|
||||
}
|
||||
if authorizer == nil {
|
||||
return ogenmiddleware.Response{}, errors.New("httpadapter.NewCompanyContextMiddleware - company authorizer is not configured")
|
||||
}
|
||||
principal, ok := v1.PrincipalFromContext(request.Context)
|
||||
if !ok {
|
||||
return ogenmiddleware.Response{}, problem.New(request.Context, apiadapter.ProblemCodeUnauthenticated)
|
||||
}
|
||||
organizationID, ok := companyPathID(request)
|
||||
if !ok {
|
||||
return ogenmiddleware.Response{}, errors.New("httpadapter.NewCompanyContextMiddleware - decoded organization identifier has an unexpected type")
|
||||
}
|
||||
access, err := authorizer.AuthorizeCompany(request.Context, principal.UserID, organizationID)
|
||||
if err != nil {
|
||||
if response := companyAuthorizationProblem(request.Context, err); response != nil {
|
||||
return ogenmiddleware.Response{}, response
|
||||
}
|
||||
return ogenmiddleware.Response{}, fmt.Errorf("httpadapter.NewCompanyContextMiddleware - authorize company: %w", err)
|
||||
}
|
||||
if !access.Matches(principal.UserID, organizationID) {
|
||||
return ogenmiddleware.Response{}, errors.New("httpadapter.NewCompanyContextMiddleware - authorizer returned a mismatched company capability")
|
||||
}
|
||||
request.SetContext(organization.WithCompanyAccess(request.Context, access))
|
||||
return next(request)
|
||||
}
|
||||
}
|
||||
|
||||
func companyPathID(request ogenmiddleware.Request) (uuid.UUID, bool) {
|
||||
value, ok := request.Params.Path("org_id")
|
||||
if !ok {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
organizationID, ok := value.(uuid.UUID)
|
||||
return organizationID, ok && organizationID != uuid.Nil
|
||||
}
|
||||
|
||||
func companyAuthorizationProblem(ctx context.Context, err error) *apiadapter.ProblemStatusCode {
|
||||
switch {
|
||||
case errors.Is(err, organization.ErrEmailNotVerified):
|
||||
return problem.New(ctx, apiadapter.ProblemCodeEmailNotVerified)
|
||||
case errors.Is(err, organization.ErrForbidden), errors.Is(err, outbound.ErrOrganizationManagerRequired):
|
||||
return problem.New(ctx, apiadapter.ProblemCodeForbidden)
|
||||
case errors.Is(err, outbound.ErrOrganizationNotFound):
|
||||
return problem.New(ctx, apiadapter.ProblemCodeNotFound)
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
var _ CompanyAuthorizer = (*v1.OrganizationHandler)(nil)
|
||||
Reference in new issue
Block a user