UNPKG

@sync-in/server

Version:

The secure, open-source platform for file storage, sharing, collaboration, and sync

112 lines (111 loc) 5.54 kB
/* * Copyright (C) 2012-2025 Johan Legrand <johan.legrand@sync-in.com> * This file is part of Sync-in | The open source file sync and share solution * See the LICENSE file for licensing details */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "SpaceEnv", { enumerable: true, get: function() { return SpaceEnv; } }); const _functions = require("../../../common/functions"); const _spaces = require("../constants/spaces"); const _paths = require("../utils/paths"); const _permissions = require("../utils/permissions"); let SpaceEnv = class SpaceEnv { setup(user, repository, rootAlias, paths, urlSegments, skipEndpointProtection = false) { // Ordering is important here this.setRepository(repository); this.setPaths(user, rootAlias, paths); this.setPermissions(skipEndpointProtection); this.setUrls(urlSegments); } setPermissions(skipEndpointProtection = false) { if (this.role === _spaces.SPACE_ROLE.IS_MANAGER) { /* If the user or the user groups have a manger role in this space, allow all permissions on space (but not on root) */ this.permissions = _spaces.SPACE_ALL_OPERATIONS; } else { /* The permissions are not unique since they can come from several user groups */ this.permissions = (0, _functions.uniquePermissions)(this.permissions); } if (this.inPersonalSpace) { /* The user space must have all rights when a no anchored root is targeted */ this.permissions = _spaces.SPACE_ALL_OPERATIONS; } /* If no root was anchored inherits from space permissions */ if (this.root?.id === 0) { this.root.permissions = this.permissions; } /* If we are in a root space, we have to intersect the space & the root permissions */ this.envPermissions = (0, _permissions.getEnvPermissions)(this, this.root); /* Protects against the deletion of virtual endpoints */ if (!skipEndpointProtection && this.envPermissions.indexOf(_spaces.SPACE_OPERATION.DELETE) > -1) { if ((this.inFilesRepository || this.inTrashRepository) && !this.paths.length && (this.root?.id || !this.root?.id && !this.root?.alias)) { /* Protects the spaces : /spaces/space_alias || /trash/space_alias */ /* Protects the root spaces : /spaces/space_alias/root_anchored */ this.envPermissions = (0, _permissions.removePermissions)(this.envPermissions, [ _spaces.SPACE_OPERATION.DELETE ]); } else if (this.inSharesRepository && !this.paths.length && (this.root?.id || this.root?.externalPath)) { /* Protects the shares to be deleted by the users : shares/share_alias */ this.envPermissions = (0, _permissions.removePermissions)(this.envPermissions, [ _spaces.SPACE_OPERATION.DELETE ]); } } } setPaths(user, rootAlias, paths) { this.paths = this.inSharesRepository && rootAlias ? [ rootAlias, ...paths ] : paths; if (!this.inSharesList) { // realPathFromSpace may throw a FileError exception ; [this.realBasePath, this.realPath] = (0, _paths.realPathFromSpace)(user, this, true); this.dbFile = (0, _paths.dbFileFromSpace)(user.id, this); } } browsePermissions() { const permissions = (0, _permissions.getEnvPermissions)(this, this.root); if (this.inPersonalSpace) { return permissions.split(_spaces.SPACE_PERMS_SEP).filter((p)=>p !== _spaces.SPACE_OPERATION.SHARE_INSIDE).join(_spaces.SPACE_PERMS_SEP); } else { return permissions; } } willExceedQuota(contentLength) { if (this.storageQuota) { return contentLength > this.storageQuota - this.storageUsage; } return false; } setRepository(repository) { this.repository = repository; this.inFilesRepository = repository === _spaces.SPACE_REPOSITORY.FILES; this.inTrashRepository = repository === _spaces.SPACE_REPOSITORY.TRASH; this.inSharesRepository = repository === _spaces.SPACE_REPOSITORY.SHARES; this.inSharesList = this.inSharesRepository && this.id === 0; this.inPersonalSpace = this.alias === _spaces.SPACE_ALIAS.PERSONAL; } setUrls(urlSegments) { this.url = urlSegments.join('/'); this.relativeUrl = urlSegments.length <= 2 ? '.' : urlSegments.slice(2).join('/'); } constructor(props, rootAlias = '', mustHaveRoot = true){ this.enabled = true; // states this.inFilesRepository = false; this.inTrashRepository = false; this.inSharesRepository = false; this.inPersonalSpace = false; this.inSharesList = false; this.quotaIsExceeded = false; Object.assign(this, props); if (mustHaveRoot && (this.root?.id === null || this.root?.id === undefined)) { /* The root is a resource located inside the space but not anchored, this is why we set the id to 0 and the permissions are inherited */ this.root = { id: 0, alias: rootAlias, name: rootAlias, permissions: this.permissions }; } this.quotaIsExceeded = this.storageQuota !== null && this.storageUsage >= this.storageQuota; } }; //# sourceMappingURL=space-env.model.js.map