feat(organization): add member limit feature
- Introduced a new member limit feature for organizations, allowing a maximum number of members to be set (default is 50; 0 means unlimited). - Updated API endpoints to handle member limit during organization creation and updates. - Implemented validation to prevent adding members when the limit is reached. - Enhanced UI components to display and manage member limits, including localization updates for English and Chinese. - Added migration files to support the new member limit column in the organizations table. This update improves organization management by allowing administrators to control membership size.
This commit is contained in:
@@ -98,7 +98,7 @@ func (r *organizationRepository) ListSearchable(ctx context.Context, query strin
|
||||
// Update updates an organization (Select ensures zero values like invite_code_validity_days=0 are persisted)
|
||||
func (r *organizationRepository) Update(ctx context.Context, org *types.Organization) error {
|
||||
return r.db.WithContext(ctx).Model(&types.Organization{}).Where("id = ?", org.ID).
|
||||
Select("name", "description", "require_approval", "searchable", "invite_code_validity_days", "updated_at").
|
||||
Select("name", "description", "require_approval", "searchable", "invite_code_validity_days", "member_limit", "updated_at").
|
||||
Updates(org).Error
|
||||
}
|
||||
|
||||
|
||||
@@ -18,6 +18,9 @@ import (
|
||||
// Default invite code validity in days; allowed values: 0 (never), 1, 7, 30
|
||||
const DefaultInviteCodeValidityDays = 7
|
||||
|
||||
// DefaultMemberLimit is the default max members per organization (0 = unlimited)
|
||||
const DefaultMemberLimit = 50
|
||||
|
||||
// ValidInviteCodeValidityDays are the allowed values for invite_code_validity_days
|
||||
var ValidInviteCodeValidityDays = map[int]bool{0: true, 1: true, 7: true, 30: true}
|
||||
|
||||
@@ -30,6 +33,8 @@ var (
|
||||
ErrInvalidRole = errors.New("invalid role")
|
||||
ErrInviteCodeExpired = errors.New("invite code has expired")
|
||||
ErrInvalidValidityDays = errors.New("invite_code_validity_days must be 0, 1, 7, or 30")
|
||||
ErrOrgMemberLimitReached = errors.New("organization member limit reached")
|
||||
ErrOrgMemberLimitTooLow = errors.New("member limit cannot be lower than current member count")
|
||||
)
|
||||
|
||||
// organizationService implements OrganizationService interface
|
||||
@@ -68,6 +73,13 @@ func (s *organizationService) CreateOrganization(ctx context.Context, userID str
|
||||
}
|
||||
validityDays = *req.InviteCodeValidityDays
|
||||
}
|
||||
memberLimit := DefaultMemberLimit
|
||||
if req.MemberLimit != nil {
|
||||
if *req.MemberLimit < 0 {
|
||||
return nil, errors.New("member_limit must be >= 0")
|
||||
}
|
||||
memberLimit = *req.MemberLimit
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
org := &types.Organization{
|
||||
@@ -79,6 +91,7 @@ func (s *organizationService) CreateOrganization(ctx context.Context, userID str
|
||||
InviteCode: generateInviteCode(),
|
||||
InviteCodeExpiresAt: resolveInviteExpiry(validityDays, now),
|
||||
InviteCodeValidityDays: validityDays,
|
||||
MemberLimit: memberLimit,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
@@ -179,6 +192,21 @@ func (s *organizationService) UpdateOrganization(ctx context.Context, id string,
|
||||
}
|
||||
org.InviteCodeValidityDays = *req.InviteCodeValidityDays
|
||||
}
|
||||
if req.MemberLimit != nil {
|
||||
if *req.MemberLimit < 0 {
|
||||
return nil, errors.New("member_limit must be >= 0")
|
||||
}
|
||||
if *req.MemberLimit > 0 {
|
||||
count, err := s.orgRepo.CountMembers(ctx, id)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if int64(*req.MemberLimit) < count {
|
||||
return nil, ErrOrgMemberLimitTooLow
|
||||
}
|
||||
}
|
||||
org.MemberLimit = *req.MemberLimit
|
||||
}
|
||||
org.UpdatedAt = time.Now()
|
||||
|
||||
if err := s.orgRepo.Update(ctx, org); err != nil {
|
||||
@@ -293,6 +321,20 @@ func (s *organizationService) AddMember(ctx context.Context, orgID string, userI
|
||||
return ErrInvalidRole
|
||||
}
|
||||
|
||||
org, err := s.orgRepo.GetByID(ctx, orgID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if org.MemberLimit > 0 {
|
||||
count, errCount := s.orgRepo.CountMembers(ctx, orgID)
|
||||
if errCount != nil {
|
||||
return errCount
|
||||
}
|
||||
if count >= int64(org.MemberLimit) {
|
||||
return ErrOrgMemberLimitReached
|
||||
}
|
||||
}
|
||||
|
||||
member := &types.OrganizationMember{
|
||||
ID: uuid.New().String(),
|
||||
OrganizationID: orgID,
|
||||
@@ -435,6 +477,17 @@ func (s *organizationService) JoinByInviteCode(ctx context.Context, inviteCode s
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Check member limit (0 = unlimited)
|
||||
if org.MemberLimit > 0 {
|
||||
count, errCount := s.orgRepo.CountMembers(ctx, org.ID)
|
||||
if errCount != nil {
|
||||
return nil, errCount
|
||||
}
|
||||
if count >= int64(org.MemberLimit) {
|
||||
return nil, ErrOrgMemberLimitReached
|
||||
}
|
||||
}
|
||||
|
||||
// Add user as viewer by default
|
||||
member := &types.OrganizationMember{
|
||||
ID: uuid.New().String(),
|
||||
@@ -506,6 +559,24 @@ func (s *organizationService) SubmitJoinRequest(ctx context.Context, orgID strin
|
||||
return nil, ErrPendingRequestExists
|
||||
}
|
||||
|
||||
// Reject if organization is already at member limit
|
||||
org, err := s.orgRepo.GetByID(ctx, orgID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrOrganizationNotFound) {
|
||||
return nil, ErrOrgNotFound
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
if org.MemberLimit > 0 {
|
||||
count, errCount := s.orgRepo.CountMembers(ctx, orgID)
|
||||
if errCount != nil {
|
||||
return nil, errCount
|
||||
}
|
||||
if count >= int64(org.MemberLimit) {
|
||||
return nil, ErrOrgMemberLimitReached
|
||||
}
|
||||
}
|
||||
|
||||
// Default to viewer if role is empty or invalid
|
||||
if requestedRole == "" || !requestedRole.IsValid() {
|
||||
requestedRole = types.OrgRoleViewer
|
||||
@@ -577,7 +648,20 @@ func (s *organizationService) ReviewJoinRequest(ctx context.Context, orgID strin
|
||||
}
|
||||
logger.Infof(ctx, "Upgrade request %s approved, user %s role updated to %s in organization %s", requestID, request.UserID, role, request.OrganizationID)
|
||||
} else {
|
||||
// Join: add new member
|
||||
// Join: check member limit then add new member
|
||||
org, errOrg := s.orgRepo.GetByID(ctx, request.OrganizationID)
|
||||
if errOrg != nil {
|
||||
return errOrg
|
||||
}
|
||||
if org.MemberLimit > 0 {
|
||||
count, errCount := s.orgRepo.CountMembers(ctx, request.OrganizationID)
|
||||
if errCount != nil {
|
||||
return errCount
|
||||
}
|
||||
if count >= int64(org.MemberLimit) {
|
||||
return ErrOrgMemberLimitReached
|
||||
}
|
||||
}
|
||||
member := &types.OrganizationMember{
|
||||
ID: uuid.New().String(),
|
||||
OrganizationID: request.OrganizationID,
|
||||
|
||||
@@ -178,6 +178,10 @@ func (h *OrganizationHandler) UpdateOrganization(c *gin.Context) {
|
||||
c.Error(apperrors.NewValidationError(err.Error()))
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrOrgMemberLimitTooLow) {
|
||||
c.Error(apperrors.NewValidationError("当前成员数已超过新的上限,请先移除成员或设置更大的上限"))
|
||||
return
|
||||
}
|
||||
c.Error(apperrors.NewForbiddenError("Permission denied or organization not found"))
|
||||
return
|
||||
}
|
||||
@@ -435,6 +439,10 @@ func (h *OrganizationHandler) JoinByInviteCode(c *gin.Context) {
|
||||
org, err := h.orgService.JoinByInviteCode(ctx, req.InviteCode, userID.(string), tenantID.(uint64))
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to join organization: %v", err)
|
||||
if errors.Is(err, service.ErrOrgMemberLimitReached) {
|
||||
c.Error(apperrors.NewValidationError("该空间成员已满,无法加入"))
|
||||
return
|
||||
}
|
||||
c.Error(apperrors.NewNotFoundError("Invalid invite code"))
|
||||
return
|
||||
}
|
||||
@@ -500,6 +508,10 @@ func (h *OrganizationHandler) SubmitJoinRequest(c *gin.Context) {
|
||||
request, err := h.orgService.SubmitJoinRequest(ctx, org.ID, userID.(string), tenantID.(uint64), req.Message, requestedRole)
|
||||
if err != nil {
|
||||
logger.Errorf(ctx, "Failed to submit join request: %v", err)
|
||||
if errors.Is(err, service.ErrOrgMemberLimitReached) {
|
||||
c.Error(apperrors.NewValidationError("该空间成员已满,无法提交加入申请"))
|
||||
return
|
||||
}
|
||||
if err.Error() == "pending request already exists" {
|
||||
c.Error(apperrors.NewValidationError("You have already submitted a request to join this organization"))
|
||||
return
|
||||
@@ -585,6 +597,10 @@ func (h *OrganizationHandler) JoinByOrganizationID(c *gin.Context) {
|
||||
c.Error(apperrors.NewForbiddenError("Organization not open for search"))
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrOrgMemberLimitReached) {
|
||||
c.Error(apperrors.NewValidationError("该空间成员已满,无法加入"))
|
||||
return
|
||||
}
|
||||
if errors.Is(err, service.ErrInvalidRole) {
|
||||
c.Error(apperrors.NewValidationError("Invalid role"))
|
||||
return
|
||||
@@ -810,6 +826,10 @@ func (h *OrganizationHandler) ReviewJoinRequest(c *gin.Context) {
|
||||
|
||||
if err := h.orgService.ReviewJoinRequest(ctx, orgID, requestID, req.Approved, userID.(string), req.Message, assignRole); err != nil {
|
||||
logger.Errorf(ctx, "Failed to review join request: %v", err)
|
||||
if errors.Is(err, service.ErrOrgMemberLimitReached) {
|
||||
c.Error(apperrors.NewValidationError("空间成员已满,无法通过该加入申请"))
|
||||
return
|
||||
}
|
||||
if err.Error() == "request has already been reviewed" {
|
||||
c.Error(apperrors.NewValidationError("Request has already been reviewed"))
|
||||
return
|
||||
@@ -1093,6 +1113,7 @@ func (h *OrganizationHandler) toOrgResponse(ctx context.Context, org *types.Orga
|
||||
IsOwner: org.OwnerID == currentUserID,
|
||||
RequireApproval: org.RequireApproval,
|
||||
Searchable: org.Searchable,
|
||||
MemberLimit: org.MemberLimit,
|
||||
InviteCodeValidityDays: org.InviteCodeValidityDays,
|
||||
CreatedAt: org.CreatedAt,
|
||||
UpdatedAt: org.UpdatedAt,
|
||||
@@ -1265,6 +1286,10 @@ func (h *OrganizationHandler) InviteMember(c *gin.Context) {
|
||||
// Add member
|
||||
if err := h.orgService.AddMember(ctx, orgID, req.UserID, invitedUser.TenantID, req.Role); err != nil {
|
||||
logger.Errorf(ctx, "Failed to add member: %v", err)
|
||||
if errors.Is(err, service.ErrOrgMemberLimitReached) {
|
||||
c.Error(apperrors.NewValidationError("该空间成员已满,无法添加新成员"))
|
||||
return
|
||||
}
|
||||
c.Error(apperrors.NewInternalServerError("Failed to add member"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -60,6 +60,8 @@ type Organization struct {
|
||||
RequireApproval bool `json:"require_approval" gorm:"default:false"`
|
||||
// Whether the space is open for search (discoverable; non-members can search and join by org ID)
|
||||
Searchable bool `json:"searchable" gorm:"default:false"`
|
||||
// Max members allowed; 0 means no limit
|
||||
MemberLimit int `json:"member_limit" gorm:"default:50"`
|
||||
// Creation time
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// Last updated time
|
||||
@@ -218,6 +220,7 @@ type CreateOrganizationRequest struct {
|
||||
Description string `json:"description" binding:"max=1000"`
|
||||
Avatar string `json:"avatar" binding:"omitempty,max=512"` // optional avatar URL
|
||||
InviteCodeValidityDays *int `json:"invite_code_validity_days"` // optional: 0=never, 1, 7, 30; default 7
|
||||
MemberLimit *int `json:"member_limit"` // optional: max members; 0=unlimited; default 50
|
||||
}
|
||||
|
||||
// UpdateOrganizationRequest represents a request to update an organization
|
||||
@@ -226,8 +229,9 @@ type UpdateOrganizationRequest struct {
|
||||
Description *string `json:"description" binding:"omitempty,max=1000"`
|
||||
Avatar *string `json:"avatar" binding:"omitempty,max=512"` // optional avatar URL
|
||||
RequireApproval *bool `json:"require_approval"`
|
||||
Searchable *bool `json:"searchable"` // open for search so others can discover and join
|
||||
Searchable *bool `json:"searchable"` // open for search so others can discover and join
|
||||
InviteCodeValidityDays *int `json:"invite_code_validity_days"` // 0=never, 1, 7, 30
|
||||
MemberLimit *int `json:"member_limit"` // max members; 0=unlimited
|
||||
}
|
||||
|
||||
// AddMemberRequest represents a request to add a member to an organization
|
||||
@@ -295,6 +299,7 @@ type OrganizationResponse struct {
|
||||
InviteCodeValidityDays int `json:"invite_code_validity_days"`
|
||||
RequireApproval bool `json:"require_approval"`
|
||||
Searchable bool `json:"searchable"`
|
||||
MemberLimit int `json:"member_limit"` // 0 = unlimited
|
||||
MemberCount int `json:"member_count"`
|
||||
ShareCount int `json:"share_count"` // 共享到该组织的知识库数量
|
||||
PendingJoinRequestCount int `json:"pending_join_request_count"` // 待审批加入申请数(仅管理员可见)
|
||||
|
||||
Reference in New Issue
Block a user