diff --git a/README.md b/README.md
index 9deb16cd4f..7827d4e48a 100644
--- a/README.md
+++ b/README.md
@@ -126,13 +126,6 @@ You can learn more about the Budibase API at the following places:
- [Build an app with Budibase and Next.js](https://budibase.com/blog/building-a-crud-app-with-budibase-and-next.js/)
-
-
-
-
-
-
-
## 🏁 Get started
Deploy Budibase self-hosted in your existing infrastructure, using Docker, Kubernetes, and Digital Ocean.
diff --git a/packages/backend-core/src/cache/writethrough.ts b/packages/backend-core/src/cache/writethrough.ts
index e64c116663..c331d791a6 100644
--- a/packages/backend-core/src/cache/writethrough.ts
+++ b/packages/backend-core/src/cache/writethrough.ts
@@ -119,8 +119,8 @@ export class Writethrough {
this.writeRateMs = writeRateMs
}
- async put(doc: any) {
- return put(this.db, doc, this.writeRateMs)
+ async put(doc: any, writeRateMs: number = this.writeRateMs) {
+ return put(this.db, doc, writeRateMs)
}
async get(id: string) {
diff --git a/packages/backend-core/src/security/roles.ts b/packages/backend-core/src/security/roles.ts
index b05cf79c8c..0d33031de5 100644
--- a/packages/backend-core/src/security/roles.ts
+++ b/packages/backend-core/src/security/roles.ts
@@ -122,7 +122,9 @@ export async function roleToNumber(id?: string) {
if (isBuiltin(id)) {
return builtinRoleToNumber(id)
}
- const hierarchy = (await getUserRoleHierarchy(id)) as RoleDoc[]
+ const hierarchy = (await getUserRoleHierarchy(id, {
+ defaultPublic: true,
+ })) as RoleDoc[]
for (let role of hierarchy) {
if (isBuiltin(role?.inherits)) {
return builtinRoleToNumber(role.inherits) + 1
@@ -192,12 +194,15 @@ export async function getRole(
/**
* Simple function to get all the roles based on the top level user role ID.
*/
-async function getAllUserRoles(userRoleId?: string): Promise {
+async function getAllUserRoles(
+ userRoleId?: string,
+ opts?: { defaultPublic?: boolean }
+): Promise {
// admins have access to all roles
if (userRoleId === BUILTIN_IDS.ADMIN) {
return getAllRoles()
}
- let currentRole = await getRole(userRoleId)
+ let currentRole = await getRole(userRoleId, opts)
let roles = currentRole ? [currentRole] : []
let roleIds = [userRoleId]
// get all the inherited roles
@@ -226,12 +231,16 @@ export async function getUserRoleIdHierarchy(
* Returns an ordered array of the user's inherited role IDs, this can be used
* to determine if a user can access something that requires a specific role.
* @param userRoleId The user's role ID, this can be found in their access token.
+ * @param opts optional - if want to default to public use this.
* @returns returns an ordered array of the roles, with the first being their
* highest level of access and the last being the lowest level.
*/
-export async function getUserRoleHierarchy(userRoleId?: string) {
+export async function getUserRoleHierarchy(
+ userRoleId?: string,
+ opts?: { defaultPublic?: boolean }
+) {
// special case, if they don't have a role then they are a public user
- return getAllUserRoles(userRoleId)
+ return getAllUserRoles(userRoleId, opts)
}
// this function checks that the provided permissions are in an array format
diff --git a/packages/backend-core/src/users/db.ts b/packages/backend-core/src/users/db.ts
index 78df262a4e..c071064713 100644
--- a/packages/backend-core/src/users/db.ts
+++ b/packages/backend-core/src/users/db.ts
@@ -25,12 +25,17 @@ import {
import {
getAccountHolderFromUserIds,
isAdmin,
+ isCreator,
validateUniqueUser,
} from "./utils"
import { searchExistingEmails } from "./lookup"
import { hash } from "../utils"
-type QuotaUpdateFn = (change: number, cb?: () => Promise) => Promise
+type QuotaUpdateFn = (
+ change: number,
+ creatorsChange: number,
+ cb?: () => Promise
+) => Promise
type GroupUpdateFn = (groupId: string, userIds: string[]) => Promise
type FeatureFn = () => Promise
type GroupGetFn = (ids: string[]) => Promise
@@ -241,7 +246,8 @@ export class UserDB {
}
const change = dbUser ? 0 : 1 // no change if there is existing user
- return UserDB.quotas.addUsers(change, async () => {
+ const creatorsChange = isCreator(dbUser) !== isCreator(user) ? 1 : 0
+ return UserDB.quotas.addUsers(change, creatorsChange, async () => {
await validateUniqueUser(email, tenantId)
let builtUser = await UserDB.buildUser(user, opts, tenantId, dbUser)
@@ -303,6 +309,7 @@ export class UserDB {
let usersToSave: any[] = []
let newUsers: any[] = []
+ let newCreators: any[] = []
const emails = newUsersRequested.map((user: User) => user.email)
const existingEmails = await searchExistingEmails(emails)
@@ -323,59 +330,66 @@ export class UserDB {
}
newUser.userGroups = groups
newUsers.push(newUser)
+ if (isCreator(newUser)) {
+ newCreators.push(newUser)
+ }
}
const account = await accountSdk.getAccountByTenantId(tenantId)
- return UserDB.quotas.addUsers(newUsers.length, async () => {
- // create the promises array that will be called by bulkDocs
- newUsers.forEach((user: any) => {
- usersToSave.push(
- UserDB.buildUser(
- user,
- {
- hashPassword: true,
- requirePassword: user.requirePassword,
- },
- tenantId,
- undefined, // no dbUser
- account
+ return UserDB.quotas.addUsers(
+ newUsers.length,
+ newCreators.length,
+ async () => {
+ // create the promises array that will be called by bulkDocs
+ newUsers.forEach((user: any) => {
+ usersToSave.push(
+ UserDB.buildUser(
+ user,
+ {
+ hashPassword: true,
+ requirePassword: user.requirePassword,
+ },
+ tenantId,
+ undefined, // no dbUser
+ account
+ )
)
- )
- })
+ })
- const usersToBulkSave = await Promise.all(usersToSave)
- await usersCore.bulkUpdateGlobalUsers(usersToBulkSave)
+ const usersToBulkSave = await Promise.all(usersToSave)
+ await usersCore.bulkUpdateGlobalUsers(usersToBulkSave)
- // Post-processing of bulk added users, e.g. events and cache operations
- for (const user of usersToBulkSave) {
- // TODO: Refactor to bulk insert users into the info db
- // instead of relying on looping tenant creation
- await platform.users.addUser(tenantId, user._id, user.email)
- await eventHelpers.handleSaveEvents(user, undefined)
- }
+ // Post-processing of bulk added users, e.g. events and cache operations
+ for (const user of usersToBulkSave) {
+ // TODO: Refactor to bulk insert users into the info db
+ // instead of relying on looping tenant creation
+ await platform.users.addUser(tenantId, user._id, user.email)
+ await eventHelpers.handleSaveEvents(user, undefined)
+ }
+
+ const saved = usersToBulkSave.map(user => {
+ return {
+ _id: user._id,
+ email: user.email,
+ }
+ })
+
+ // now update the groups
+ if (Array.isArray(saved) && groups) {
+ const groupPromises = []
+ const createdUserIds = saved.map(user => user._id)
+ for (let groupId of groups) {
+ groupPromises.push(UserDB.groups.addUsers(groupId, createdUserIds))
+ }
+ await Promise.all(groupPromises)
+ }
- const saved = usersToBulkSave.map(user => {
return {
- _id: user._id,
- email: user.email,
+ successful: saved,
+ unsuccessful,
}
- })
-
- // now update the groups
- if (Array.isArray(saved) && groups) {
- const groupPromises = []
- const createdUserIds = saved.map(user => user._id)
- for (let groupId of groups) {
- groupPromises.push(UserDB.groups.addUsers(groupId, createdUserIds))
- }
- await Promise.all(groupPromises)
}
-
- return {
- successful: saved,
- unsuccessful,
- }
- })
+ )
}
static async bulkDelete(userIds: string[]): Promise {
@@ -415,11 +429,12 @@ export class UserDB {
_deleted: true,
}))
const dbResponse = await usersCore.bulkUpdateGlobalUsers(toDelete)
+ const creatorsToDelete = usersToDelete.filter(isCreator)
- await UserDB.quotas.removeUsers(toDelete.length)
for (let user of usersToDelete) {
await bulkDeleteProcessing(user)
}
+ await UserDB.quotas.removeUsers(toDelete.length, creatorsToDelete.length)
// Build Response
// index users by id
@@ -468,7 +483,8 @@ export class UserDB {
await db.remove(userId, dbUser._rev)
- await UserDB.quotas.removeUsers(1)
+ const creatorsToDelete = isCreator(dbUser) ? 1 : 0
+ await UserDB.quotas.removeUsers(1, creatorsToDelete)
await eventHelpers.handleDeleteEvents(dbUser)
await cache.user.invalidateUser(userId)
await sessions.invalidateSessions(userId, { reason: "deletion" })
diff --git a/packages/backend-core/src/users/users.ts b/packages/backend-core/src/users/users.ts
index adf134f04a..ea4a17214d 100644
--- a/packages/backend-core/src/users/users.ts
+++ b/packages/backend-core/src/users/users.ts
@@ -14,11 +14,11 @@ import {
} from "../db"
import {
BulkDocsResponse,
- ContextUser,
SearchQuery,
SearchQueryOperators,
SearchUsersRequest,
User,
+ ContextUser,
DatabaseQueryOpts,
CouchFindOptions,
} from "@budibase/types"
diff --git a/packages/backend-core/tests/core/users/users.spec.js b/packages/backend-core/tests/core/users/users.spec.js
new file mode 100644
index 0000000000..ae7109344a
--- /dev/null
+++ b/packages/backend-core/tests/core/users/users.spec.js
@@ -0,0 +1,54 @@
+const _ = require('lodash/fp')
+const {structures} = require("../../../tests")
+
+jest.mock("../../../src/context")
+jest.mock("../../../src/db")
+
+const context = require("../../../src/context")
+const db = require("../../../src/db")
+
+const {getCreatorCount} = require('../../../src/users/users')
+
+describe("Users", () => {
+
+ let getGlobalDBMock
+ let getGlobalUserParamsMock
+ let paginationMock
+
+ beforeEach(() => {
+ jest.resetAllMocks()
+
+ getGlobalDBMock = jest.spyOn(context, "getGlobalDB")
+ getGlobalUserParamsMock = jest.spyOn(db, "getGlobalUserParams")
+ paginationMock = jest.spyOn(db, "pagination")
+ })
+
+ it("Retrieves the number of creators", async () => {
+ const getUsers = (offset, limit, creators = false) => {
+ const range = _.range(offset, limit)
+ const opts = creators ? {builder: {global: true}} : undefined
+ return range.map(() => structures.users.user(opts))
+ }
+ const page1Data = getUsers(0, 8)
+ const page2Data = getUsers(8, 12, true)
+ getGlobalDBMock.mockImplementation(() => ({
+ name : "fake-db",
+ allDocs: () => ({
+ rows: [...page1Data, ...page2Data]
+ })
+ }))
+ paginationMock.mockImplementationOnce(() => ({
+ data: page1Data,
+ hasNextPage: true,
+ nextPage: "1"
+ }))
+ paginationMock.mockImplementation(() => ({
+ data: page2Data,
+ hasNextPage: false,
+ nextPage: undefined
+ }))
+ const creatorsCount = await getCreatorCount()
+ expect(creatorsCount).toBe(4)
+ expect(paginationMock).toHaveBeenCalledTimes(2)
+ })
+})
diff --git a/packages/backend-core/tests/core/utilities/structures/licenses.ts b/packages/backend-core/tests/core/utilities/structures/licenses.ts
index 0e34f2e9bb..bb452f9ad5 100644
--- a/packages/backend-core/tests/core/utilities/structures/licenses.ts
+++ b/packages/backend-core/tests/core/utilities/structures/licenses.ts
@@ -123,6 +123,10 @@ export function customer(): Customer {
export function subscription(): Subscription {
return {
amount: 10000,
+ amounts: {
+ user: 10000,
+ creator: 0,
+ },
cancelAt: undefined,
currency: "usd",
currentPeriodEnd: 0,
@@ -131,6 +135,10 @@ export function subscription(): Subscription {
duration: PriceDuration.MONTHLY,
pastDueAt: undefined,
quantity: 0,
+ quantities: {
+ user: 0,
+ creator: 0,
+ },
status: "active",
}
}
diff --git a/packages/pro b/packages/pro
index 5ed0ee2aca..3820c0c93a 160000
--- a/packages/pro
+++ b/packages/pro
@@ -1 +1 @@
-Subproject commit 5ed0ee2aca9d754d80cd46bae412b24621afa47e
+Subproject commit 3820c0c93a3e448e10a60a9feb5396844b537ca8
diff --git a/packages/types/src/sdk/featureFlag.ts b/packages/types/src/sdk/featureFlag.ts
index 53aa4842c4..e3935bc7ee 100644
--- a/packages/types/src/sdk/featureFlag.ts
+++ b/packages/types/src/sdk/featureFlag.ts
@@ -1,5 +1,8 @@
export enum FeatureFlag {
LICENSING = "LICENSING",
+ // Feature IDs in Posthog
+ PER_CREATOR_PER_USER_PRICE = "18873",
+ PER_CREATOR_PER_USER_PRICE_ALERT = "18530",
}
export interface TenantFeatureFlags {
diff --git a/packages/types/src/sdk/licensing/billing.ts b/packages/types/src/sdk/licensing/billing.ts
index 35f366c811..bcbc7abd18 100644
--- a/packages/types/src/sdk/licensing/billing.ts
+++ b/packages/types/src/sdk/licensing/billing.ts
@@ -5,10 +5,17 @@ export interface Customer {
currency: string | null | undefined
}
+export interface SubscriptionItems {
+ user: number | undefined
+ creator: number | undefined
+}
+
export interface Subscription {
amount: number
+ amounts: SubscriptionItems | undefined
currency: string
quantity: number
+ quantities: SubscriptionItems | undefined
duration: PriceDuration
cancelAt: number | null | undefined
currentPeriodStart: number
diff --git a/packages/types/src/sdk/licensing/plan.ts b/packages/types/src/sdk/licensing/plan.ts
index 3e214a01ff..1604dfb8af 100644
--- a/packages/types/src/sdk/licensing/plan.ts
+++ b/packages/types/src/sdk/licensing/plan.ts
@@ -4,7 +4,9 @@ export enum PlanType {
PRO = "pro",
/** @deprecated */
TEAM = "team",
+ /** @deprecated */
PREMIUM = "premium",
+ PREMIUM_PLUS = "premium_plus",
BUSINESS = "business",
ENTERPRISE = "enterprise",
}
@@ -26,10 +28,12 @@ export interface AvailablePrice {
currency: string
duration: PriceDuration
priceId: string
+ type?: string
}
export enum PlanModel {
PER_USER = "perUser",
+ PER_CREATOR_PER_USER = "per_creator_per_user",
DAY_PASS = "dayPass",
}