k.account.organization
Current site organization, members, roles, departments, and tenants
| Execution Context | Prerequisites | Typical Use |
|---|---|---|
Server-side KScript (k.api, <script env="server">) | Site bound to an organization; roles requires an authenticated session; member/role/tenant/department mutations require an org admin | Inspect organization profile, members, and roles, and manage org sub-accounts, tenants, and departments |
Runtime Caveats
- Calling
k.account.organization.get(nameOrId)with an unknown name or ID returns a non-null wrapper object whose properties (name,id, etc.) throw an error when accessed. Always validate the identifier or wrap property reads intry / catch. - Accessing
org.rolesrequires a signed-in user with an active current organization; readingroleson an anonymous request throws an error. - Member and role management methods (
addUser,deleteUser,createMember,assignRole,saveRole,deleteRole) require an authenticated organization administrator in the current organization.
Quick Start
Read summary properties and collection counts for the current site's organization:
k.api.get(() => {
const org = k.account.organization.current
if (!org) return null
return {
id: String(org.id),
name: org.name,
displayName: org.displayName,
userCount: org.users.length,
memberCount: org.members.length,
tenantCount: org.tenants.length,
departmentCount: org.departments.length
}
})Quick Reference
| Method / Property | Description | Returns |
|---|---|---|
current | Returns the OrganizationModel bound to the current site | OrganizationModel |
get(nameOrId) | Reads an organization by name or ID (property access throws if missing) | OrganizationModel |
org.users / org.organizationUsers | Organization users and @organizationId sub-accounts | UserModel[] |
org.members / org.roles | Member view models (with roles) and org roles (roles requires login) | OrgUserViewModel[] / OrganizationRoleItem[] |
org.tenants / org.departments | Tenants and departments under the organization | TenantModel[] / DepartmentModel[] |
org.addUser / deleteUser / createMember | Add existing user, remove member, or create member (org admin required) | string / boolean / User |
org.assignRole / saveRole / deleteRole | Assign member roles, create/update or delete org roles (org admin required) | boolean / OrganizationRoleItem |
org.addOrUpdateOrganizationUser / deleteOrganizationUser / getOrganizationUser | Create/update, delete, or read org sub-accounts (name@orgId) | User | null / boolean |
org.addTenant / tenant.update / tenant.delete | Create, update, and delete organization tenants | void |
org.addDepartment / dep.update / dep.delete / dep.*User | Create, update, delete departments and manage department members | void |
Organization Reads & Collections
current
Reads summary metadata and collection properties for the current site's organization (confirm login before reading roles).
Parameters: None.
Returns: OrganizationModel | null. Organization model bound to the current site.
k.api.get(() => {
const org = k.account.organization.current
if (!org) return null
return {
id: String(org.id),
name: org.name,
displayName: org.displayName,
currency: org.currency,
serverLevel: org.serverLevel
}
})Member and User Collections
Reads user, sub-account, member view, role, tenant, and department collections on the organization object.
| Property | Type | Description |
|---|---|---|
users | UserModel[] | All users in the organization. |
organizationUsers | UserModel[] | Organization sub-accounts whose username ends with @organizationId. |
members | OrgUserViewModel[] | Member view models including isAdmin, roleId, roleName, and joinDate. |
roles | OrganizationRoleItem[] | Organization roles (requires a logged-in session; throws on anonymous requests). |
tenants | TenantModel[] | Tenants under the organization. |
departments | DepartmentModel[] | Departments under the organization. |
Returns: The corresponding collection array (accessing roles anonymously throws).
k.api.get(() => {
const org = k.account.organization.current
return {
userNames: org.users.slice(0, 10).map((u) => u.userName),
orgUserCount: org.organizationUsers.length,
members: org.members.slice(0, 10).map((m) => ({
userName: m.userName,
isAdmin: m.isAdmin,
roleName: m.roleName
}))
}
})get()
Retrieves an OrganizationModel by organization name or ID.
| Parameter | Type | Required | Description |
|---|---|---|---|
nameOrId | string | Yes | Organization name or organization ID. |
Returns: OrganizationModel. Accessing properties on the returned object throws if the specified organization does not exist.
k.api.get(() => {
try {
const org = k.account.organization.get("org-name-or-id")
return { id: String(org.id), name: org.name, displayName: org.displayName }
} catch (e) {
return null
}
})Members, Roles & Sub-Accounts
Member and Role Management
All methods below require a logged-in organization administrator in the current organization.
| Method | Parameters | Returns | Description |
|---|---|---|---|
addUser(userName) | userName: string | string | Adds an existing platform user to the organization and returns a status message. |
deleteUser(userName) | userName: string | boolean | Removes a member from the organization; returns true on success. |
createMember(userName, password, email, phone?) | string, string, string, string? | User | Creates a new organization member account and adds it to the organization. |
assignRole(userName, role?) | userName: string, role?: string | boolean | Assigns a role by name or ID to a member, or clears the role when role is omitted/empty. |
saveRole(name, displayName, id?) | string, string, string? | OrganizationRoleItem | Creates a role (when id is omitted) or updates an existing role (when id is supplied). |
deleteRole(role) | role: string | boolean | Deletes an organization role by name or ID. |
Returns: See the Returns column for each method above.
k.api.post(() => {
k.account.ensureLogin("/login")
if (!k.account.isLogin) return
const org = k.account.organization.current
const role = org.saveRole("editor", "Content Editor")
const assigned = org.assignRole(k.request.form.userName, role.name)
return { roleId: role.id, assigned }
})addOrUpdateOrganizationUser() and Organization Sub-Accounts
Creates, queries, or deletes organization sub-accounts (typically named name@organizationId).
| Method | Parameters | Returns | Description |
|---|---|---|---|
addOrUpdateOrganizationUser(name, password, isAdmin) | string, string, boolean | User | null | Creates or updates an organization sub-account. |
deleteOrganizationUser(name) | name: string | boolean | Deletes the specified organization sub-account; returns true on success. |
getOrganizationUser(name) | name: string | User | null | Reads the specified organization sub-account, or null if missing. |
Returns: See the Returns column for each method above.
k.api.post(() => {
const user = k.account.organization.current.addOrUpdateOrganizationUser(
k.request.form.name,
k.request.form.password,
k.request.form.isAdmin === "true"
)
return { userName: user ? user.userName : null }
})Tenants & Departments
Tenant Management (TenantModel)
Create tenants with addTenant(), and update or delete them via TenantModel items in org.tenants.
| Member | Parameters | Returns | Description |
|---|---|---|---|
org.addTenant(name, parentTenantId?) | name: string, parentTenantId?: string | void | Creates a tenant, optionally nested under parentTenantId. |
tenant.update(name, parentTenantId?) | name: string, parentTenantId?: string | void | Updates tenant name or parent tenant ID. |
tenant.delete() | None | void | Deletes the tenant. |
Returns: void.
k.api.post(() => {
const org = k.account.organization.current
org.addTenant(k.request.form.name)
return {
tenants: org.tenants.map((t) => ({ id: t.id, name: t.name }))
}
})Department Management (DepartmentModel)
Create departments with addDepartment(), and manage department metadata and membership through DepartmentModel items in org.departments.
| Member | Parameters | Returns | Description |
|---|---|---|---|
org.addDepartment(name, displayName, tenantId?, parentDepartment?) | string, string, string?, string? | void | Creates a department. tenantId may be omitted only when the organization has exactly one tenant; otherwise tenantId is required. |
dep.update(displayName, parentDepartment, tenantId?) | string, string, string? | void | Updates display name and parent department (name or ID; pass an empty string to clear). The optional tenantId is only validated against the department's existing tenant and cannot move a department across tenants (passing another tenant's ID throws an error). |
dep.delete() | None | void | Deletes the department. |
dep.addUser(userName, isManager, userFunction) | string, boolean, string | void | Adds a member to the department with manager flag and role function. |
dep.addTestUser(userName, isManager, userFunction) | string, boolean, string | void | Adds a test user to the department. |
dep.deleteUser(userName) | userName: string | void | Removes a user from the department. |
Returns: void.
k.api.post(() => {
const org = k.account.organization.current
org.addDepartment(k.request.form.name, k.request.form.displayName)
const dep = org.departments.find((d) => d.name === k.request.form.name)
return dep ? { id: dep.id, name: dep.name, display: dep.display } : null
})TypeScript Definitions
Show full TypeScript definitions
interface KOrganization {
current: OrganizationModel;
get(nameOrId: string): OrganizationModel;
}
interface OrganizationModel {
id: any;
name: string;
displayName: string;
serverLevel: number;
currency: string;
isPartner: boolean;
tenants: TenantModel[];
departments: DepartmentModel[];
users: UserModel[];
organizationUsers: UserModel[];
members: OrgUserViewModel[];
roles: OrganizationRoleItem[];
addUser(userName: string): string;
deleteUser(userName: string): boolean;
createMember(userName: string, password: string, email: string, phone?: string): User;
assignRole(userName: string, role?: string): boolean;
saveRole(name: string, displayName: string, id?: string): OrganizationRoleItem;
deleteRole(role: string): boolean;
addTenant(name: string, parentTenantId?: string): void;
addDepartment(name: string, displayName: string, tenantId?: string, parentDepartment?: string): void;
addOrUpdateOrganizationUser(name: string, password: string, isAdmin: boolean): User;
deleteOrganizationUser(name: string): boolean;
getOrganizationUser(name: string): User;
}
interface DepartmentModel {
id: string;
name: string;
display: string;
tenantId: string;
parentDepartmentId: string;
users: DepartmentUser[];
addUser(userName: string, isManager: boolean, userFunction: string): void;
addTestUser(userName: string, isManager: boolean, userFunction: string): void;
deleteUser(userName: string): void;
delete(): void;
update(displayName: string, parentDepartment: string, tenantId?: string): void;
}
interface TenantModel {
id: string;
orgId: string;
name: string;
parentTenantId: string;
update(name: string, parentTenantId?: string): void;
delete(): void;
}
interface OrgUserViewModel {
id: any;
currentOrgId: any;
currentOrgName: string;
isAdmin: boolean;
roleId?: any;
roleName: string;
roleDisplayName: string;
userName: string;
emailAddress: string;
firstName: string;
lastName: string;
fullName: string;
language: string;
joinDate: Date;
registrationDate: Date;
}
interface OrganizationRoleItem {
id: string;
name: string;
displayName: string;
organizationId: string;
}
interface DepartmentUser {
id: any;
userName: string;
isTestUser: boolean;
departmentId: any;
isManager: boolean;
function: string;
}