organisation

package
v0.11.0 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2026 License: Apache-2.0 Imports: 16 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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.

Variables

View Source
var OrgAwareTables = map[string]bool{
	"gk_identity_provider_org": true,
	"gk_org_plugin_access":     true,
	"gk_user_organisation":     true,
	"sysconfig_org":            true,
}

OrgAwareTables is the set of tables the plugin sandbox scopes to the caller's active organisation. It lists only tables whose rows each belong to exactly one organisation (org_id NOT NULL in both migration sets). Core tables such as ticket, queue and customer_user have no org_id column, so they are not scoped; tables with a nullable org_id (NULL = global row: gk_secure_config, gk_identity_provider, gk_recycle_bin, gk_deletion_log) are not scoped either, because "org_id = ?" would hide the global rows. Tables not in this set pass through unmodified.

Functions

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 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 ScopeQuery

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

ScopeQuery rewrites a plugin SQL statement so that every org-aware table it touches is restricted to orgID. It returns the rewritten query and the args with the org id inserted at the matching placeholder positions.

SELECT/UPDATE/DELETE: every reference to an org-aware table (main table, comma list, JOIN, subquery, CTE body, UNION branch) gets "<table or alias>.org_id = ?" in that query block's WHERE; the plugin's own predicate is wrapped in parentheses so "... OR 1=1" cannot escape the filter. A WHERE is added when the block has none. UPDATE may not assign org_id.

INSERT/REPLACE into an org-aware table is not rewritten; it is accepted only when the column list names org_id and every VALUES row supplies orgID for it (as a ? argument or a literal), and nothing after the rows mentions org_id.

Any other shape that mentions an org-aware table (DDL, TABLE t, a parenthesised table reference, a $N placeholder, more than one statement, an INSERT ... SELECT) is refused. Statements that mention no org-aware table, and every statement when orgID is 0 (single-org mode), pass through 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"`
	CaptivePlugin     *string   `json:"captive_plugin,omitempty" db:"captive_plugin"`
	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) GetCaptivePlugin

func (r *Repository) GetCaptivePlugin(orgID int64) (string, error)

GetCaptivePlugin returns the captive_plugin for an org, or empty string if none / org not found. Hot path on customer login and the portal guard middleware — keep it a single-column lookup, not a full Get.

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) SetCaptivePlugin

func (r *Repository) SetCaptivePlugin(orgID int64, plugin *string, userID int) error

SetCaptivePlugin updates only the captive_plugin column. `plugin` empty or nil disables capture. Kept as a narrow API so the admin UI can wire the Portal-tab checkbox without having to re-send the whole org row.

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