8000 feat: add admin API to create organizations by whoAbhishekSah · Pull Request #962 · raystack/frontier · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: add admin API to create organizations #962

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 23, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TAG := $(shell git rev-list --tags --max-count=1)
VERSION := $(shell git describe --tags ${TAG})
.PHONY: build check fmt lint test test-race vet test-cover-html help install proto ui compose-up-dev
.DEFAULT_GOAL := build
PROTON_COMMIT := "ee998369c2b211d8902c8e80d51d2aa75fac3a2d"
PROTON_COMMIT := "c4258fb47d4ac1021917561807ea5c9a6d55b056"

ui:
@echo " > generating ui build"
Expand Down
114 changes: 114 additions & 0 deletions core/organization/mocks/user_service.go

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

75 changes: 75 additions & 0 deletions core/organization/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/raystack/frontier/core/authenticate"

"github.com/raystack/frontier/pkg/str"
"github.com/raystack/frontier/pkg/utils"

"github.com/raystack/frontier/core/relation"
Expand Down Expand Up @@ -41,6 +42,8 @@ type RelationService interface {

type UserService interface {
GetByID(ctx context.Context, id string) (user.User, error)
GetByEmail(ctx context.Context, email string) (user.User, error)
Create(ctx context.Context, user user.User) (user.User, error)
}

type AuthnService interface {
Expand Down Expand Up @@ -337,3 +340,75 @@ func (s Service) MemberCount(ctx context.Context, orgID string) (int64, error) {
mc, err := s.policyService.OrgMemberCount(ctx, orgID)
return int64(mc.Count), err
}

func (s Service) AdminCreate(ctx context.Context, org Organization, ownerEmail string) (Organization, error) {
// Validate email
if !user.IsValidEmail(ownerEmail) {
return Organization{}, user.ErrInvalidEmail
}

// Check if organization already exists by name (including disabled orgs)
_, err := s.GetRaw(ctx, org.Name)
switch {
case err == nil:
return Organization{}, ErrConflict
case errors.Is(err, ErrNotExist):
// This is the expected case - proceed with creation
case errors.Is(err, ErrInvalidUUID), errors.Is(err, ErrInvalidID):
return Organization{}, ErrInvalidID
default:
return Organization{}, err
}

// Check if user exists
var usr user.User
usr, err = s.userService.GetByEmail(ctx, ownerEmail)
if err != nil {
if errors.Is(err, user.ErrNotExist) {
// User doesn't exist, create it
usr, err = s.userService.Create(ctx, user.User{
Email: ownerEmail,
Name: str.GenerateUserSlug(ownerEmail),
State: user.Enabled,
})
if err != nil {
return Organization{}, fmt.Errorf("failed to create user: %w", err)
}
} else {
return Organization{}, fmt.Errorf("failed to get user: %w", err)
}
}

// Get default state for org creation
defaultState, err := s.GetDefaultOrgStateOnCreate(ctx)
if err != nil {
return Organization{}, err
}

// Create organization
newOrg, err := s.repository.Create(ctx, Organization{
Name: org.Name,
Title: org.Title,
Avatar: org.Avatar,
Metadata: org.Metadata,
State: defaultState,
})
if err != nil {
return Organization{}, err
}

// Add user as organization owner
if err = s.AddMember(ctx, newOrg.ID, schema.OwnerRelationName, authenticate.Principal{
ID: usr.ID,
Type: schema.UserPrincipal,
}); err != nil {
return newOrg, fmt.Errorf("failed to add user as owner: %w", err)
}

// Attach org to central platform
if err = s.AttachToPlatform(ctx, newOrg.ID); err != nil {
return newOrg, fmt.Errorf("failed to attach to platform: %w", err)
}

return newOrg, nil
}
5 changes: 5 additions & 0 deletions core/user/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ func isValidEmail(str string) bool {
return err == nil
}

// IsValidEmail checks if the string is a valid email address
func IsValidEmail(str string) bool {
return isValidEmail(str)
}

type CSVExport struct {
UserID string `csv:"User ID"`
Name string `csv:"Name"`
Expand Down
58 changes: 58 additions & 0 deletions internal/api/v1beta1/mocks/organization_service.go

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

39 changes: 39 additions & 0 deletions internal/api/v1beta1/org.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type OrganizationService interface {
Get(ctx context.Context, idOrSlug string) (organization.Organization, error)
GetRaw(ctx context.Context, idOrSlug string) (organization.Organization, error)
Create(ctx context.Context, org organization.Organization) (organization.Organization, error)
AdminCreate(ctx context.Context, org organization.Organization, ownerEmail string) (organization.Organization, error)
List(ctx context.Context, f organization.Filter) ([]organization.Organization, error)
Update(ctx context.Context, toUpdate organization.Organization) (organization.Organization, error)
ListByUser(ctx context.Context, principal authenticate.Principal, flt organization.Filter) ([]organization.Organization, error)
Expand Down Expand Up @@ -99,6 +100,44 @@ func (h Handler) ListAllOrganizations(ctx context.Context, request *frontierv1be
}, nil
}

func (h Handler) AdminCreateOrganization(ctx context.Context, request *frontierv1beta1.AdminCreateOrganizationRequest) (*frontierv1beta1.AdminCreateOrganizationResponse, error) {
metaDataMap := metadata.Build(request.GetBody().GetMetadata().AsMap())

if err := h.metaSchemaService.Validate(metaDataMap, orgMetaSchema); err != nil {
return nil, grpcBadBodyMetaSchemaError
}

newOrg, err := h.orgService.AdminCreate(ctx, organization.Organization{
Name: request.GetBody().GetName(),
Title: request.GetBody().GetTitle(),
Avatar: request.GetBody().GetAvatar(),
Metadata: metaDataMap,
}, request.GetBody().GetOrgOwnerEmail())
if err != nil {
switch {
case errors.Is(err, user.ErrInvalidEmail):
return nil, grpcBadBodyError
case errors.Is(err, organization.ErrInvalidDetail):
return nil, grpcBadBodyError
case errors.Is(err, organization.ErrConflict):
return nil, grpcConflictError
default:
return nil, err
}
}

orgPB, err := transformOrgToPB(newOrg)
if err != nil {
return nil, err
}

audit.GetAuditor(ctx, newOrg.ID).LogWithAttrs(audit.OrgCreatedEvent, audit.OrgTarget(newOrg.ID), map[string]string{
"title": newOrg.Title,
"name": newOrg.Name,
})
return &frontierv1beta1.AdminCreateOrganizationResponse{Organization: orgPB}, nil
}

func (h Handler) CreateOrganization(ctx context.Context, request *frontierv1beta1.CreateOrganizationRequest) (*frontierv1beta1.CreateOrganizationResponse, error) {
metaDataMap := metadata.Build(request.GetBody().GetMetadata().AsMap())

Expand Down
3 changes: 3 additions & 0 deletions pkg/server/interceptors/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,9 @@ var authorizationValidationMap = map[string]func(ctx context.Context, handler *v
"/raystack.frontier.v1beta1.AdminService/SearchOrganizations": func(ctx context.Context, handler *v1beta1.Handler, req any) error {
return handler.IsSuperUser(ctx)
},
"/raystack.frontier.v1beta1.AdminService/AdminCreateOrganization": func(ctx context.Context, handler *v1beta1.Handler, req any) error {
return handler.IsSuperUser(ctx)
},
"/raystack.frontier.v1beta1.AdminService/SearchOrganizationUsers": func(ctx context.Context, handler *v1beta1.Handler, req any) error {
return handler.IsSuperUser(ctx)
},
Expand Down
Loading
Loading
0