chore: Unify term types

This commit is contained in:
Maksim Eltyshev
2026-02-10 21:14:56 +01:00
parent e7326303cd
commit a82ae49fe4
14 changed files with 31 additions and 176 deletions
@@ -31,7 +31,7 @@
* type: string
* minLength: 64
* maxLength: 64
* description: Terms signature hash based on user role
* description: Terms signature hash
* example: 940226c4c41f51afe3980ceb63704e752636526f4c52a4ea579e85b247493d94
* initialLanguage:
* type: string
@@ -184,14 +184,12 @@ module.exports = {
}
if (!user.termsSignature) {
const termsSignature = sails.hooks.terms.getSignatureByUserRole(user.role);
if (inputs.signature !== termsSignature) {
if (!sails.hooks.terms.isSignatureValid(inputs.signature)) {
throw Errors.INVALID_SIGNATURE;
}
const values = {
termsSignature,
termsSignature: inputs.signature,
termsAcceptedAt: new Date().toISOString(),
};
+2 -20
View File
@@ -5,7 +5,7 @@
/**
* @swagger
* /terms/{type}:
* /terms:
* get:
* summary: Get terms and conditions
* description: Retrieves terms and conditions in the specified language.
@@ -13,14 +13,6 @@
* - Terms
* operationId: getTerms
* parameters:
* - name: type
* in: path
* required: true
* description: Type of terms to retrieve
* schema:
* type: string
* enum: [general, extended]
* example: general
* - name: language
* in: query
* required: false
@@ -47,11 +39,6 @@
* - content
* - signature
* properties:
* type:
* type: string
* enum: [general, extended]
* description: Type of terms
* example: general
* language:
* type: string
* enum: [de-DE, en-US]
@@ -76,11 +63,6 @@
module.exports = {
inputs: {
type: {
type: 'string',
isIn: Object.values(sails.hooks.terms.Types),
required: true,
},
language: {
type: 'string',
isIn: User.LANGUAGES,
@@ -88,7 +70,7 @@ module.exports = {
},
async fn(inputs) {
const terms = await sails.hooks.terms.getPayload(inputs.type, inputs.language);
const terms = await sails.hooks.terms.getPayload(inputs.language);
return {
item: terms,
@@ -56,7 +56,7 @@ module.exports = {
}
}
if (!sails.hooks.terms.hasSignature(inputs.user.termsSignature)) {
if (!sails.hooks.terms.isSignatureValid(inputs.user.termsSignature)) {
const { token: pendingToken, payload: pendingTokenPayload } =
sails.helpers.utils.createJwtToken(
AccessTokenSteps.ACCEPT_TERMS,
@@ -82,12 +82,9 @@ module.exports = {
);
}
const termsType = sails.hooks.terms.getTypeByUserRole(inputs.user.role);
throw {
termsAcceptanceRequired: {
pendingToken,
termsType,
message: 'Terms acceptance required',
step: AccessTokenSteps.ACCEPT_TERMS,
},
-1
View File
@@ -34,7 +34,6 @@ module.exports = {
},
},
language: inputs.record.language || sails.config.i18n.defaultLocale,
termsType: sails.hooks.terms.getTypeByUserRole(inputs.record.role),
};
const gravatarUrl = sails.helpers.users.buildGravatarUrl(inputs.record);
+13 -40
View File
@@ -14,22 +14,18 @@
const fsPromises = require('fs').promises;
const crypto = require('crypto');
const Types = {
GENERAL: 'general',
EXTENDED: 'extended',
};
const LANGUAGES = ['de-DE', 'en-US'];
const DEFAULT_LANGUAGE = 'en-US';
const getContent = (language = DEFAULT_LANGUAGE) =>
fsPromises.readFile(`${sails.config.appPath}/terms/${language}.md`, 'utf8');
const hashContent = (content) => crypto.createHash('sha256').update(content).digest('hex');
module.exports = function defineTermsHook(sails) {
let signatureByType;
let signaturesSet;
let signature;
return {
Types,
LANGUAGES,
/**
@@ -39,49 +35,26 @@ module.exports = function defineTermsHook(sails) {
async initialize() {
sails.log.info('Initializing custom hook (`terms`)');
signatureByType = {
[Types.GENERAL]: hashContent(await this.getContent(Types.GENERAL)),
[Types.EXTENDED]: hashContent(await this.getContent(Types.EXTENDED)),
};
signaturesSet = new Set(Object.values(signatureByType));
const content = await getContent();
signature = hashContent(content);
},
async getPayload(type, language = DEFAULT_LANGUAGE) {
if (!Object.values(Types).includes(type)) {
throw new Error(`Unknown type: ${type}`);
}
async getPayload(language = DEFAULT_LANGUAGE) {
if (!LANGUAGES.includes(language)) {
language = DEFAULT_LANGUAGE; // eslint-disable-line no-param-reassign
}
const content = await getContent(language);
return {
type,
language,
content: await this.getContent(type, language),
signature: this.getSignatureByType(type),
content,
signature,
};
},
getTypeByUserRole(userRole) {
return userRole === User.Roles.ADMIN ? Types.EXTENDED : Types.GENERAL;
},
getContent(type, language = DEFAULT_LANGUAGE) {
return fsPromises.readFile(`${sails.config.appPath}/terms/${language}/${type}.md`, 'utf8');
},
getSignatureByType(type) {
return signatureByType[type];
},
getSignatureByUserRole(userRole) {
return signatureByType[this.getTypeByUserRole(userRole)];
},
hasSignature(signature) {
return signaturesSet.has(signature);
isSignatureValid(value) {
return value === signature;
},
};
};
-6
View File
@@ -24,7 +24,6 @@
* - avatar
* - phone
* - organization
* - termsType
* - isDeactivated
* - createdAt
* - updatedAt
@@ -143,11 +142,6 @@
* default: byDefault
* description: Default sort order for projects display (personal field)
* example: byDefault
* termsType:
* type: string
* enum: [general, extended]
* description: Type of terms applicable to the user based on role
* example: general
* isSsoUser:
* type: boolean
* default: false
+1 -1
View File
@@ -105,7 +105,7 @@ const protectedStaticDirServer = (prefix, getPathSegment) => (req, res, next) =>
module.exports.routes = {
'GET /api/bootstrap': 'bootstrap/show',
'GET /api/terms/:type': 'terms/show',
'GET /api/terms': 'terms/show',
'GET /api/config': 'config/show',
'PATCH /api/config': 'config/update',
@@ -1,4 +1,4 @@
# Beispiel-Nutzungsbedingungen - Allgemeine Benutzer
# Beispiel-Nutzungsbedingungen
_Nicht rechtsverbindlich. Dies ist ein Platzhaltertext nur zu Testzwecken._
_Letzte Aktualisierung: 14. August 2025_
@@ -33,4 +33,4 @@ Bei Fragen zu diesen Beispiel-Bedingungen wenden Sie sich bitte an `placeholder@
---
**Ende des Beispiels - Allgemeine Bedingungen**
**Ende des Beispiels**
-39
View File
@@ -1,39 +0,0 @@
# Beispiel-Nutzungsbedingungen - Administratoren
_Nicht rechtsverbindlich. Dies ist ein Platzhaltertext nur zu Testzwecken._
_Letzte Aktualisierung: 14. August 2025_
Willkommen, Administrator! Diese Beispiel-Nutzungsbedingungen ("Bedingungen") dienen **ausschließlich der Demonstration** und sind rechtlich nicht gültig. Diese erweiterte Version enthält zusätzliche Klauseln, die höhere Verantwortlichkeiten für Administratoren widerspiegeln.
---
## 1. Einführung
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sed nibh id elit ultricies posuere.
## 2. Teilnahmeberechtigung
Administratoren müssen mindestens 21 Jahre alt und von der Organisation autorisiert sein. Nulla rhoncus diam non dictum fermentum.
## 3. Administrative Pflichten
Als Administrator stimmen Sie zu:
- Benutzerkonten verantwortungsvoll zu verwalten.
- Sicherheitseinstellungen korrekt zu konfigurieren.
- Die Einhaltung von Datenschutzvorschriften zu gewährleisten.
## 4. Verbotene Administrative Handlungen
Administratoren dürfen nicht:
1. Unbefugten Zugriff gewähren.
2. Prüfprotokolle ohne triftigen Grund ändern.
3. Administrative Rechte zum persönlichen Vorteil nutzen.
## 5. Datenverwaltung
Sie sind verantwortlich für den Schutz der Benutzerdaten und die Systemintegrität.
## 6. Änderungen
Diese Beispiel-Bedingungen können jederzeit zu Testzwecken geändert werden.
## 7. Kontakt
Bei Fragen zu diesen Beispiel-Bedingungen wenden Sie sich bitte an `placeholder@example.com`.
---
**Ende des Beispiels – Erweiterte (Admin) Bedingungen**
@@ -1,4 +1,4 @@
# Example Terms of Service - General Users
# Example Terms of Service
_Not legally binding. This is placeholder text for testing purposes only._
_Last updated: August 14, 2025_
@@ -33,4 +33,4 @@ For questions about these example Terms, please contact `placeholder@example.com
---
**End of Example - General Terms**
**End of Example**
-39
View File
@@ -1,39 +0,0 @@
# Example Terms of Service - Admin Users
_Not legally binding. This is placeholder text for testing purposes only._
_Last updated: August 14, 2025_
Welcome, Admin! These Example Terms of Service ("Terms") are **for demonstration purposes only** and are not legally valid. This extended version contains additional clauses reflecting higher responsibilities for administrative users.
---
## 1. Introduction
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur sed nibh id elit ultricies posuere.
## 2. Eligibility
Admins must be at least 21 years old and authorized by the organization. Nulla rhoncus diam non dictum fermentum.
## 3. Administrative Responsibilities
As an administrator, you agree to:
- Manage user accounts responsibly.
- Configure security settings accurately.
- Ensure compliance with data protection rules.
## 4. Prohibited Administrative Actions
Admins may not:
1. Grant unauthorized access.
2. Alter audit logs without proper reason.
3. Use administrative privileges for personal gain.
## 5. Data Management
You are responsible for safeguarding user data and system integrity.
## 6. Modifications
These example Terms may be updated for testing purposes without notice.
## 7. Contact
For questions about these example Terms, please contact `placeholder@example.com`.
---
**End of Example – Extended (Admin) Terms**