✨ Simple User Quota (#367)
* ✨Simple User Quota * 👷Fix build with OpenSearch
This commit is contained in:
+13
-2
@@ -124,8 +124,19 @@ export class PostgresConnector extends AbstractConnector<PostgresConnectionOptio
|
||||
|
||||
private async alterTablePrimaryKey(entity: EntityDefinition) {
|
||||
if (entity.options.primaryKey) {
|
||||
const query = `ALTER TABLE "${entity.name}" ADD PRIMARY KEY (
|
||||
${entity.options.primaryKey.join(", ")});`;
|
||||
const query = `
|
||||
do $$
|
||||
begin
|
||||
IF NOT EXISTS
|
||||
(SELECT constraint_name
|
||||
FROM information_schema.table_constraints
|
||||
WHERE table_name = '${entity.name}'
|
||||
and constraint_type = 'PRIMARY KEY')
|
||||
THEN
|
||||
ALTER TABLE "${entity.name}" ADD PRIMARY KEY (
|
||||
${entity.options.primaryKey.join(", ")});
|
||||
END IF;
|
||||
end $$;`;
|
||||
try {
|
||||
await this.client.query(query);
|
||||
} catch (err) {
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
import SearchRepository from "../../../core/platform/services/search/repository";
|
||||
import { getLogger, logger, TdriveLogger } from "../../../core/platform/framework";
|
||||
import { CrudException, ListResult } from "../../../core/platform/framework/api/crud-service";
|
||||
import {
|
||||
CrudException,
|
||||
ExecutionContext,
|
||||
ListResult,
|
||||
} from "../../../core/platform/framework/api/crud-service";
|
||||
import Repository, {
|
||||
comparisonType,
|
||||
inType,
|
||||
@@ -111,6 +115,14 @@ export class DocumentsService {
|
||||
}
|
||||
};
|
||||
|
||||
userQuota = async (context: CompanyExecutionContext): Promise<number> => {
|
||||
const children = await this.repository.find({
|
||||
parent_id: "user_" + context.user.id,
|
||||
company_id: context.company.id,
|
||||
});
|
||||
return children.getEntities().reduce((sum, child) => sum + child.size, 0);
|
||||
};
|
||||
|
||||
/**
|
||||
* Fetches a drive element
|
||||
*
|
||||
@@ -1021,12 +1033,11 @@ export class DocumentsService {
|
||||
}
|
||||
|
||||
getTab = async (tabId: string, context: CompanyExecutionContext): Promise<DriveTdriveTab> => {
|
||||
const tab = await this.driveTdriveTabRepository.findOne(
|
||||
return await this.driveTdriveTabRepository.findOne(
|
||||
{ company_id: context.company.id, tab_id: tabId },
|
||||
{},
|
||||
context,
|
||||
);
|
||||
return tab;
|
||||
};
|
||||
|
||||
setTab = async (
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { FastifyInstance, FastifyPluginCallback } from "fastify";
|
||||
import { DocumentsController } from "./controllers";
|
||||
import { createDocumentSchema, createVersionSchema } from "./schemas";
|
||||
import profilerPlugin from "../../../utils/profiler";
|
||||
// import profilerPlugin from "../../../utils/profiler";
|
||||
|
||||
const baseUrl = "/companies/:company_id";
|
||||
const serviceUrl = `${baseUrl}/item`;
|
||||
@@ -9,10 +9,10 @@ const serviceUrl = `${baseUrl}/item`;
|
||||
const routes: FastifyPluginCallback = (fastify: FastifyInstance, _options, next) => {
|
||||
const documentsController = new DocumentsController();
|
||||
|
||||
fastify.register(profilerPlugin, {
|
||||
active: documentsController.profilingEnabled,
|
||||
outputDir: "profiles",
|
||||
});
|
||||
// fastify.register(profilerPlugin, {
|
||||
// active: documentsController.profilingEnabled,
|
||||
// outputDir: "profiles",
|
||||
// });
|
||||
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
|
||||
@@ -311,7 +311,5 @@ export class FileServiceImpl {
|
||||
}
|
||||
|
||||
function getFilePath(entity: File): string {
|
||||
return `${gr.platformServices.storage.getHomeDir()}/files/${entity.company_id}/${
|
||||
entity.user_id || "anonymous"
|
||||
}/${entity.id}`;
|
||||
return `/files/${entity.company_id}/${entity.user_id || "anonymous"}/${entity.id}`;
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
|
||||
{ id: request.currentUser.id },
|
||||
);
|
||||
|
||||
const authorization = user.role === "owner" || user.role === "admin" ? true : false;
|
||||
const authorization = user.role === "owner" || user.role === "admin";
|
||||
|
||||
if (!authorization) {
|
||||
throw fastify.httpErrors.unauthorized("Only owner and Company Administrator have permission");
|
||||
|
||||
@@ -33,6 +33,10 @@ import { getPublicUserRoom, getUserRoom } from "../../realtime";
|
||||
import NodeCache from "node-cache";
|
||||
import gr from "../../../global-resolver";
|
||||
import { formatUser } from "../../../../utils/users";
|
||||
import { getDefaultDriveItem } from "../../../documents/utils";
|
||||
import { CompanyExecutionContext } from "../../../documents/types";
|
||||
import { TYPE as DriveFileType, DriveFile } from "../../../documents/entities/drive-file";
|
||||
import config from "config";
|
||||
|
||||
export class UserServiceImpl {
|
||||
version: "1";
|
||||
@@ -40,6 +44,7 @@ export class UserServiceImpl {
|
||||
searchRepository: SearchRepository<User>;
|
||||
companyUserRepository: Repository<CompanyUser>;
|
||||
extUserRepository: Repository<ExternalUser>;
|
||||
driveFileRepository: Repository<DriveFile>;
|
||||
private deviceRepository: Repository<Device>;
|
||||
private cache: NodeCache;
|
||||
|
||||
@@ -56,6 +61,7 @@ export class UserServiceImpl {
|
||||
);
|
||||
|
||||
this.deviceRepository = await gr.database.getRepository<Device>(DeviceType, Device);
|
||||
this.driveFileRepository = await gr.database.getRepository<DriveFile>(DriveFileType, DriveFile);
|
||||
|
||||
this.cache = new NodeCache({ stdTTL: 0.2, checkperiod: 120 });
|
||||
|
||||
|
||||
@@ -64,6 +64,9 @@ export function formatCompany(
|
||||
[CompanyFeaturesEnum.COMPANY_DISPLAY_EMAIL]: JSON.parse(
|
||||
config.get("drive.featureDisplayEmail") || "true",
|
||||
),
|
||||
[CompanyFeaturesEnum.COMPANY_USER_QUOTA]: JSON.parse(
|
||||
config.get("drive.featureUserQuota") || "false",
|
||||
),
|
||||
},
|
||||
{
|
||||
...(res.plan?.features || {}),
|
||||
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
UserObject,
|
||||
UserParameters,
|
||||
CompanyUsersParameters,
|
||||
UserQuota,
|
||||
} from "./types";
|
||||
import Company from "../entities/company";
|
||||
import CompanyUser from "../entities/company_user";
|
||||
@@ -35,6 +36,7 @@ import { getCompanyRooms, getUserRooms } from "../realtime";
|
||||
import { formatCompany, getCompanyStats } from "../utils";
|
||||
import { formatUser } from "../../../utils/users";
|
||||
import gr from "../../global-resolver";
|
||||
import config from "config";
|
||||
|
||||
export class UsersCrudController
|
||||
implements
|
||||
@@ -318,6 +320,40 @@ export class UsersCrudController
|
||||
resources: [],
|
||||
};
|
||||
}
|
||||
|
||||
async qouta(
|
||||
request: FastifyRequest<{
|
||||
Params: UserParameters;
|
||||
Querystring: CompanyUsersParameters;
|
||||
}>,
|
||||
_reply: FastifyReply,
|
||||
): Promise<UserQuota> {
|
||||
const context = getExecutionContext(request);
|
||||
|
||||
let id = request.params.id;
|
||||
if (request.params.id === "me") {
|
||||
id = context.user.id;
|
||||
}
|
||||
|
||||
if (id != context.user.id) {
|
||||
//if admin or application wants to know user quota, it's not implemented yet
|
||||
throw new Error("Not implemented yes");
|
||||
}
|
||||
|
||||
const quota = await gr.services.documents.documents.userQuota({
|
||||
...context,
|
||||
company: { id: request.query.companyId || config.get("drive.defaultCompany") },
|
||||
});
|
||||
|
||||
const total: number = config.has("drive.defaultUserQuota")
|
||||
? config.get("drive.defaultUserQuota")
|
||||
: NaN;
|
||||
return {
|
||||
total: total,
|
||||
remaining: isNaN(total) ? NaN : total - quota,
|
||||
used: quota,
|
||||
} as UserQuota;
|
||||
}
|
||||
}
|
||||
|
||||
function getExecutionContext(request: FastifyRequest): ExecutionContext {
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
deleteDeviceSchema,
|
||||
getCompanySchema,
|
||||
getDevicesSchema,
|
||||
getQuotaSchema,
|
||||
getUserCompaniesSchema,
|
||||
getUserSchema,
|
||||
getUsersSchema,
|
||||
@@ -33,6 +34,15 @@ const routes: FastifyPluginCallback = (fastify: FastifyInstance, options, next)
|
||||
handler: usersController.get.bind(usersController),
|
||||
});
|
||||
|
||||
fastify.route({
|
||||
method: "GET",
|
||||
url: `${usersUrl}/:id/quota`,
|
||||
preHandler: accessControl,
|
||||
preValidation: fastify.authenticate,
|
||||
schema: getQuotaSchema,
|
||||
handler: usersController.qouta.bind(usersController),
|
||||
});
|
||||
|
||||
fastify.route({
|
||||
method: "POST",
|
||||
url: `${usersUrl}/me/preferences`,
|
||||
|
||||
@@ -95,6 +95,7 @@ export const companyObjectSchema = {
|
||||
[CompanyFeaturesEnum.COMPANY_SEARCH_USERS]: { type: "boolean" },
|
||||
[CompanyFeaturesEnum.COMPANY_SHARED_DRIVE]: { type: "boolean" },
|
||||
[CompanyFeaturesEnum.COMPANY_DISPLAY_EMAIL]: { type: "boolean" },
|
||||
[CompanyFeaturesEnum.COMPANY_USER_QUOTA]: { type: "boolean" },
|
||||
guests: { type: "number" }, // to rename or delete
|
||||
members: { type: "number" }, // to rename or delete
|
||||
storage: { type: "number" }, // to rename or delete
|
||||
@@ -143,6 +144,34 @@ export const getUserSchema = {
|
||||
},
|
||||
};
|
||||
|
||||
export const getQuotaSchema = {
|
||||
type: "object",
|
||||
properties: {
|
||||
companyId: { type: "string" },
|
||||
},
|
||||
response: {
|
||||
"2xx": {
|
||||
type: "object",
|
||||
properties: {
|
||||
used: { type: "number" },
|
||||
remaining: { type: "number" },
|
||||
total: { type: "number" },
|
||||
},
|
||||
},
|
||||
},
|
||||
tags: ["User"],
|
||||
params: {
|
||||
type: "object",
|
||||
description: "Users",
|
||||
properties: {
|
||||
id: {
|
||||
description: "User ID",
|
||||
type: "string",
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
export const setUserPreferencesSchema = {
|
||||
request: {
|
||||
properties: {
|
||||
|
||||
@@ -14,7 +14,7 @@ export interface UserParameters {
|
||||
}
|
||||
|
||||
export interface CompanyUsersParameters {
|
||||
/* user id */
|
||||
/* company id */
|
||||
companyId: string;
|
||||
}
|
||||
|
||||
@@ -86,6 +86,7 @@ export enum CompanyFeaturesEnum {
|
||||
COMPANY_SEARCH_USERS = "company:search_users",
|
||||
COMPANY_SHARED_DRIVE = "company:shared_drive",
|
||||
COMPANY_DISPLAY_EMAIL = "company:display_email",
|
||||
COMPANY_USER_QUOTA = "company:user_quota",
|
||||
}
|
||||
|
||||
export type CompanyFeaturesObject = {
|
||||
@@ -98,6 +99,7 @@ export type CompanyFeaturesObject = {
|
||||
[CompanyFeaturesEnum.COMPANY_SEARCH_USERS]?: boolean;
|
||||
[CompanyFeaturesEnum.COMPANY_SHARED_DRIVE]?: boolean;
|
||||
[CompanyFeaturesEnum.COMPANY_DISPLAY_EMAIL]?: boolean;
|
||||
[CompanyFeaturesEnum.COMPANY_USER_QUOTA]?: boolean;
|
||||
};
|
||||
|
||||
export type CompanyLimitsObject = {
|
||||
@@ -144,3 +146,9 @@ export interface RegisterDeviceParams {
|
||||
export interface DeregisterDeviceParams {
|
||||
value: "string";
|
||||
}
|
||||
|
||||
export interface UserQuota {
|
||||
used: number;
|
||||
remaining: number;
|
||||
total: number;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user