organisation

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package organisation implements GoatKit PaaS Core multi-tenancy.

Provides organisation entities, user membership, per-org sysconfig overrides, and org context resolution for request scoping.

Index

Constants

View Source
const (
	StatusActive    = "active"
	StatusSuspended = "suspended"
	StatusArchived  = "archived"
)

Organisation statuses.

View Source
const (
	RoleMember = "member"
	RoleAdmin  = "admin"
	RoleOwner  = "owner"
)

Membership roles.

View Source
const GinContextKey = "active_org_id"

GinContextKey is the key used to store the active org ID in gin context.

Variables

View Source
var OrgAwareTables = map[string]bool{
	"ticket":                true,
	"queue":                 true,
	"customer_user":         true,
	"gk_custom_field_value": true,
}

OrgAwareTables is the registry of tables that have an org_id column. Queries against these tables are automatically scoped to the active org. Tables not in this set pass through unmodified.

Functions

func ActiveOrgFromGin

func ActiveOrgFromGin(c *gin.Context) int64

ActiveOrgFromGin reads the active org ID from gin context. Returns 0 if not set.

func HandleAdminAddMember

func HandleAdminAddMember(repo *Repository) gin.HandlerFunc

HandleAdminAddMember handles POST /admin/api/organisations/:id/members.

func HandleAdminCreateOrg

func HandleAdminCreateOrg(repo *Repository) gin.HandlerFunc

HandleAdminCreateOrg handles POST /admin/api/organisations.

func HandleAdminDeleteOrg

func HandleAdminDeleteOrg(repo *Repository) gin.HandlerFunc

HandleAdminDeleteOrg handles DELETE /admin/api/organisations/:id.

func HandleAdminDeleteOrgConfig

func HandleAdminDeleteOrgConfig(repo *Repository) gin.HandlerFunc

HandleAdminDeleteOrgConfig handles DELETE /admin/api/organisations/:id/config/:name.

func HandleAdminListMembers

func HandleAdminListMembers(repo *Repository) gin.HandlerFunc

HandleAdminListMembers handles GET /admin/api/organisations/:id/members.

func HandleAdminListOrgConfigs

func HandleAdminListOrgConfigs(repo *Repository) gin.HandlerFunc

HandleAdminListOrgConfigs handles GET /admin/api/organisations/:id/config.

func HandleAdminListOrgs

func HandleAdminListOrgs(repo *Repository) gin.HandlerFunc

HandleAdminListOrgs handles GET /admin/organisations.

func HandleAdminRemoveMember

func HandleAdminRemoveMember(repo *Repository) gin.HandlerFunc

HandleAdminRemoveMember handles DELETE /admin/api/organisations/:id/members/:member_id.

func HandleAdminSetOrgConfig

func HandleAdminSetOrgConfig(repo *Repository) gin.HandlerFunc

HandleAdminSetOrgConfig handles PUT /admin/api/organisations/:id/config.

func HandleAdminUpdateOrg

func HandleAdminUpdateOrg(repo *Repository) gin.HandlerFunc

HandleAdminUpdateOrg handles PUT /admin/api/organisations/:id.

func HandleListUserOrgs

func HandleListUserOrgs(repo *Repository) gin.HandlerFunc

HandleListUserOrgs handles GET /api/v1/session/orgs to list the current user's organisations.

func HandleSwitchOrg

func HandleSwitchOrg(repo *Repository) gin.HandlerFunc

HandleSwitchOrg handles POST /api/v1/session/org to switch the active organisation.

func HasOrgContext

func HasOrgContext(ctx context.Context) bool

HasOrgContext returns true if the context has an active org set.

func IsValidRole

func IsValidRole(r string) bool

IsValidRole checks whether the given role is supported.

func IsValidStatus

func IsValidStatus(s string) bool

IsValidStatus checks whether the given status is supported.

func Middleware

func Middleware(repo *Repository) gin.HandlerFunc

Middleware resolves the active organisation for each request and sets it in both the gin context and the request context. The org is resolved from:

  1. Session cookie (active_org_id) — set by the org switcher
  2. User's default org — if no cookie set

In single-org mode (no organisations in DB), this is a no-op.

func OrgIDFromContext

func OrgIDFromContext(ctx context.Context) int64

OrgIDFromContext returns the active organisation ID from the context. Returns 0 if no org context is set (single-org mode).

func RegisterOrgAwareTable

func RegisterOrgAwareTable(table string)

RegisterOrgAwareTable adds a table to the org-scoping registry. Plugins can call this for their own tables that have an org_id column.

func ScopeQuery

func ScopeQuery(query string, args []any, orgID int64) (string, []any)

ScopeQuery rewrites a SQL query to include org_id filtering. Returns the modified query and updated args slice.

For SELECT/UPDATE/DELETE: appends "AND org_id = ?" to the WHERE clause (or adds "WHERE org_id = ?" if no WHERE exists).

For INSERT: does NOT modify (caller must include org_id in their INSERT).

If orgID is 0 (single-org mode) or no org-aware tables are found, returns the query unmodified.

func ValidRoles

func ValidRoles() []string

ValidRoles returns all supported membership roles.

func ValidStatuses

func ValidStatuses() []string

ValidStatuses returns all supported organisation statuses.

func WithOrgID

func WithOrgID(ctx context.Context, orgID int64) context.Context

WithOrgID returns a new context with the active organisation ID set.

Types

type OrgWithMemberCount

type OrgWithMemberCount struct {
	Organisation
	MemberCount int `json:"member_count"`
}

OrgWithMemberCount extends Organisation with a member count for admin list views.

type Organisation

type Organisation struct {
	ID                int64     `json:"id" db:"id"`
	Name              string    `json:"name" db:"name"`
	Slug              string    `json:"slug" db:"slug"`
	ParentID          *int64    `json:"parent_id,omitempty" db:"parent_id"`
	Status            string    `json:"status" db:"status"`
	CustomerCompanyID *string   `json:"customer_company_id,omitempty" db:"customer_company_id"`
	ValidID           int       `json:"valid_id" db:"valid_id"`
	CreateTime        time.Time `json:"create_time" db:"create_time"`
	CreateBy          int       `json:"create_by" db:"create_by"`
	ChangeTime        time.Time `json:"change_time" db:"change_time"`
	ChangeBy          int       `json:"change_by" db:"change_by"`
}

Organisation represents a row in gk_organisation.

func (*Organisation) IsActive

func (o *Organisation) IsActive() bool

IsActive returns true if the organisation is active and valid.

type Repository

type Repository struct {
	// contains filtered or unexported fields
}

Repository provides CRUD operations for organisations, memberships, and per-org sysconfig.

func NewRepository

func NewRepository() (*Repository, error)

NewRepository creates a repository using the global DB connection.

func NewRepositoryWithDB

func NewRepositoryWithDB(db *sql.DB) *Repository

NewRepositoryWithDB creates a repository with an explicit DB connection.

func (*Repository) AddMember

func (r *Repository) AddMember(m *UserOrganisation, createdBy int) (int64, error)

AddMember adds a user or customer to an organisation.

func (*Repository) CreateOrg

func (r *Repository) CreateOrg(o *Organisation, userID int) (int64, error)

CreateOrg inserts a new organisation. Returns the new row ID.

func (*Repository) DeleteOrg

func (r *Repository) DeleteOrg(id int64) error

DeleteOrg removes an organisation. Memberships and sysconfig_org cascade.

func (*Repository) DeleteOrgConfig

func (r *Repository) DeleteOrgConfig(orgID int64, name string) error

DeleteOrgConfig removes a per-org sysconfig override (falls through to system default).

func (*Repository) GetCustomerOrgs

func (r *Repository) GetCustomerOrgs(customerLogin string) ([]Organisation, error)

GetCustomerOrgs retrieves all organisations a customer belongs to.

func (*Repository) GetDefaultOrgForUser

func (r *Repository) GetDefaultOrgForUser(userID int) (*Organisation, error)

GetDefaultOrgForUser returns the user's default org, or nil if none.

func (*Repository) GetOrg

func (r *Repository) GetOrg(id int64) (*Organisation, error)

GetOrg retrieves an organisation by ID.

func (*Repository) GetOrgBySlug

func (r *Repository) GetOrgBySlug(slug string) (*Organisation, error)

GetOrgBySlug retrieves an organisation by slug.

func (*Repository) GetOrgConfig

func (r *Repository) GetOrgConfig(orgID int64, name string) ([]byte, error)

GetOrgConfig retrieves a per-org sysconfig value. Returns nil if not overridden.

func (*Repository) GetUserOrgs

func (r *Repository) GetUserOrgs(userID int) ([]Organisation, error)

GetUserOrgs retrieves all organisations a user (agent) belongs to.

func (*Repository) ListMembers

func (r *Repository) ListMembers(orgID int64) ([]UserOrganisation, error)

ListMembers retrieves all members of an organisation.

func (*Repository) ListOrgConfigs

func (r *Repository) ListOrgConfigs(orgID int64) ([]SysconfigOrg, error)

ListOrgConfigs lists all sysconfig overrides for an org.

func (*Repository) ListOrgs

func (r *Repository) ListOrgs(status string, activeOnly bool) ([]Organisation, error)

ListOrgs retrieves organisations with optional filters.

func (*Repository) RemoveMember

func (r *Repository) RemoveMember(membershipID int64) error

RemoveMember removes a membership by ID.

func (*Repository) SetDefaultOrg

func (r *Repository) SetDefaultOrg(userID int, orgID int64) error

SetDefaultOrg sets the default org for a user. Clears other defaults first.

func (*Repository) SetOrgConfig

func (r *Repository) SetOrgConfig(orgID int64, name string, value []byte, userID int) error

SetOrgConfig creates or updates a per-org sysconfig override.

func (*Repository) UpdateOrg

func (r *Repository) UpdateOrg(o *Organisation, userID int) error

UpdateOrg updates an existing organisation.

type SysconfigOrg

type SysconfigOrg struct {
	ID             int64     `json:"id" db:"id"`
	OrgID          int64     `json:"org_id" db:"org_id"`
	Name           string    `json:"name" db:"name"`
	EffectiveValue []byte    `json:"effective_value" db:"effective_value"`
	IsValid        int       `json:"is_valid" db:"is_valid"`
	CreateTime     time.Time `json:"create_time" db:"create_time"`
	CreateBy       int       `json:"create_by" db:"create_by"`
	ChangeTime     time.Time `json:"change_time" db:"change_time"`
	ChangeBy       int       `json:"change_by" db:"change_by"`
}

SysconfigOrg represents a per-org sysconfig override in sysconfig_org.

type UserOrganisation

type UserOrganisation struct {
	ID            int64     `json:"id" db:"id"`
	OrgID         int64     `json:"org_id" db:"org_id"`
	UserID        *int      `json:"user_id,omitempty" db:"user_id"`
	CustomerLogin *string   `json:"customer_login,omitempty" db:"customer_login"`
	Role          string    `json:"role" db:"role"`
	IsDefault     bool      `json:"is_default" db:"is_default"`
	CreateTime    time.Time `json:"create_time" db:"create_time"`
	CreateBy      int       `json:"create_by" db:"create_by"`
}

UserOrganisation represents a row in gk_user_organisation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL