@sync-in/server
Version:
The secure, open-source platform for file storage, sharing, collaboration, and sync
226 lines (225 loc) • 8.32 kB
JavaScript
/*
* 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, "UserModel", {
enumerable: true,
get: function() {
return UserModel;
}
});
const _classtransformer = require("class-transformer");
const _promises = /*#__PURE__*/ _interop_require_default(require("node:fs/promises"));
const _nodepath = /*#__PURE__*/ _interop_require_default(require("node:path"));
const _configenvironment = require("../../../configuration/config.environment");
const _spaces = require("../../spaces/constants/spaces");
const _user = require("../constants/user");
function _interop_require_default(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
function _ts_decorate(decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for(var i = decorators.length - 1; i >= 0; i--)if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
}
function _ts_metadata(k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
}
let UserModel = class UserModel {
toString() {
return `*User <${this.login}> (${this.id})*`;
}
removePassword() {
delete this.password;
}
setFullName() {
if (!this.fullName) {
this.fullName = `${this.firstName || ''} ${this.lastName || ''}`.trim();
}
}
get isAdmin() {
return this.role === _user.USER_ROLE.ADMINISTRATOR;
}
get isUser() {
return this.role === _user.USER_ROLE.USER || this.role === _user.USER_ROLE.ADMINISTRATOR;
}
get isGuest() {
return this.role === _user.USER_ROLE.GUEST;
}
get isLink() {
return this.role === _user.USER_ROLE.LINK;
}
get quotaIsExceeded() {
return this.storageQuota !== null && this.storageUsage >= this.storageQuota;
}
get homePath() {
if (this.isLink || this.isGuest) {
return this._homePath ||= _nodepath.default.join(_configenvironment.configuration.applications.files.tmpPath, this.isGuest ? 'guests' : 'links', this.login);
}
return this._homePath ||= _nodepath.default.join(_configenvironment.configuration.applications.files.usersPath, this.login);
}
get filesPath() {
return this._filesPath ||= _nodepath.default.join(this.homePath, _spaces.SPACE_REPOSITORY.FILES);
}
get trashPath() {
return this._trashPath ||= _nodepath.default.join(this.homePath, _spaces.SPACE_REPOSITORY.TRASH);
}
get tmpPath() {
return this._tmpPath ||= _nodepath.default.join(this.homePath, _user.USER_PATH.TMP);
}
get tasksPath() {
return this._tasksPath ||= _nodepath.default.join(this.tmpPath, _user.USER_PATH.TASKS);
}
async makePaths() {
if (this.isGuest || this.isLink) {
await _promises.default.mkdir(this.tasksPath, {
recursive: true
});
} else {
for (const p of [
this.filesPath,
this.trashPath,
this.tasksPath
]){
await _promises.default.mkdir(p, {
recursive: true
});
}
}
}
asOwner() {
return {
id: this.id,
login: this.login,
email: this.email,
fullName: this.fullName
};
}
getInitials() {
let initials;
if (this.firstName) {
if (this.lastName) {
initials = {
f: this.firstName.charAt(0),
l: this.lastName.charAt(0)
};
}
initials = {
f: this.firstName.charAt(0),
l: this.firstName.charAt(1)
};
} else {
initials = {
f: this.login.charAt(0),
l: this.login.charAt(1)
};
}
return `${initials.f.toUpperCase()}${initials.l.toLowerCase()}`;
}
setProfile() {
if (this.isLink) {
this.login = `Link (${this.id})`;
this.email = 'guest-link@sync-in';
}
}
setApplications() {
if (this.isGuest) {
// dynamically set the permissions
this.applications = Object.values(_user.GUEST_PERMISSION);
} else if (this.permissions) {
this.applications = this.permissions.split(_user.USER_PERMS_SEP);
}
delete this.permissions;
}
havePermission(permission) {
if (this.isAdmin) {
return true;
}
if (permission === _user.USER_PERMISSION.PERSONAL_SPACE && this.isGuest) {
return false;
}
return this.applications.indexOf(permission) !== -1;
}
haveRole(role) {
return this.role <= role;
}
static getHomePath(userLogin, isGuest = false, isLink = false) {
if (isGuest || isLink) {
return _nodepath.default.join(_configenvironment.configuration.applications.files.tmpPath, isGuest ? 'guests' : 'links', userLogin);
}
return _nodepath.default.join(_configenvironment.configuration.applications.files.usersPath, userLogin);
}
static getFilesPath(userLogin) {
return _nodepath.default.join(UserModel.getHomePath(userLogin), _spaces.SPACE_REPOSITORY.FILES);
}
static getTrashPath(userLogin) {
return _nodepath.default.join(UserModel.getHomePath(userLogin), _spaces.SPACE_REPOSITORY.TRASH);
}
static getTasksPath(userLogin, isGuest = false, isLink = false) {
return _nodepath.default.join(UserModel.getHomePath(userLogin, isGuest, isLink), _user.USER_PATH.TMP, _user.USER_PATH.TASKS);
}
static getRepositoryPath(userLogin, inTrash = false) {
if (inTrash) return UserModel.getTrashPath(userLogin);
return UserModel.getFilesPath(userLogin);
}
constructor(props, removePassword = true){
// permissions as list
this.applications = [];
Object.assign(this, props);
if (removePassword) {
// always remove the password field from model for obvious security reasons
// do not remove it from `props` to not mutate the object
this.removePassword();
}
this.setFullName();
this.setApplications();
this.setProfile();
}
};
_ts_decorate([
(0, _classtransformer.Exclude)(),
_ts_metadata("design:type", String)
], UserModel.prototype, "password", void 0);
_ts_decorate([
(0, _classtransformer.Exclude)(),
_ts_metadata("design:type", Number)
], UserModel.prototype, "impersonatedFromId", void 0);
_ts_decorate([
(0, _classtransformer.Exclude)({
toPlainOnly: true
}),
_ts_metadata("design:type", Number)
], UserModel.prototype, "exp", void 0);
_ts_decorate([
(0, _classtransformer.Expose)(),
_ts_metadata("design:type", Boolean),
_ts_metadata("design:paramtypes", [])
], UserModel.prototype, "isAdmin", null);
_ts_decorate([
(0, _classtransformer.Expose)(),
_ts_metadata("design:type", Boolean),
_ts_metadata("design:paramtypes", [])
], UserModel.prototype, "isUser", null);
_ts_decorate([
(0, _classtransformer.Expose)(),
_ts_metadata("design:type", Boolean),
_ts_metadata("design:paramtypes", [])
], UserModel.prototype, "isGuest", null);
_ts_decorate([
(0, _classtransformer.Expose)(),
_ts_metadata("design:type", Boolean),
_ts_metadata("design:paramtypes", [])
], UserModel.prototype, "isLink", null);
_ts_decorate([
(0, _classtransformer.Expose)(),
_ts_metadata("design:type", Boolean),
_ts_metadata("design:paramtypes", [])
], UserModel.prototype, "quotaIsExceeded", null);
//# sourceMappingURL=user.model.js.map