@reldens/cms 0.64.0 → 0.66.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/manager.js CHANGED
@@ -5,26 +5,20 @@
5
5
  */
6
6
 
7
7
  const { TemplatesList } = require('./templates-list');
8
- const { DefaultTranslations } = require('./admin-manager/default-translations');
9
8
  const { AdminTemplatesLoader } = require('./admin-templates-loader');
10
- const { AdminManagerValidator } = require('./admin-manager-validator');
11
9
  const { MimeTypes } = require('./mime-types');
12
10
  const { AllowedExtensions } = require('./allowed-extensions');
13
11
  const { TemplatesToPathMapper } = require('./templates-to-path-mapper');
14
12
  const { AdminEntitiesGenerator } = require('./admin-entities-generator');
15
- const { LoadedEntitiesProcessor } = require('./loaded-entities-processor');
16
- const { EntitiesConfigProcessor } = require('./entities-config-processor');
17
- const { AdminManager } = require('./admin-manager');
18
13
  const { CmsPagesRouteManager } = require('./cms-pages-route-manager');
19
14
  const { Installer } = require('./installer');
20
- const { Frontend } = require('./frontend');
21
15
  const { CacheManager } = require('./cache/cache-manager');
22
16
  const { TemplateReloader } = require('./template-reloader');
23
- const { PasswordEncryptionHandler } = require('./password-encryption-handler');
24
- const { SitemapLoader } = require('./sitemap-loader');
17
+ const { ManagerComponentValidator } = require('./manager-component-validator');
18
+ const { ManagerConfigLoader } = require('./manager-config-loader');
19
+ const { ManagerServicesInitializer } = require('./manager-services-initializer');
25
20
  const { EventsManagerSingleton, Logger, sc } = require('@reldens/utils');
26
- const { DriversMap } = require('@reldens/storage');
27
- const { AppServerFactory, FileHandler, Encryptor } = require('@reldens/server-utils');
21
+ const { AppServerFactory, FileHandler } = require('@reldens/server-utils');
28
22
  const dotenv = require('dotenv');
29
23
  const mustache = require('mustache');
30
24
 
@@ -37,7 +31,7 @@ class Manager
37
31
  this.envFilePath = FileHandler.joinPaths(this.projectRoot, '.env');
38
32
  this.installLockPath = FileHandler.joinPaths(this.projectRoot, 'install.lock');
39
33
  dotenv.config({path: this.envFilePath});
40
- this.config = this.loadConfigFromEnv();
34
+ this.config = ManagerConfigLoader.loadFromEnv();
41
35
  this.adminTranslations = sc.get(props, 'adminTranslations', {});
42
36
  this.adminEntities = sc.get(props, 'adminEntities', {});
43
37
  this.rawRegisteredEntities = sc.get(props, 'rawRegisteredEntities', {});
@@ -117,96 +111,17 @@ class Manager
117
111
  prismaClient: this.prismaClient,
118
112
  postInstallCallback: this.initializeCmsAfterInstall.bind(this)
119
113
  });
120
- this.useProvidedServer = this.validateProvidedServer();
121
- this.useProvidedDataServer = this.validateProvidedDataServer();
122
- this.useProvidedAdminManager = this.validateProvidedAdminManager();
123
- this.useProvidedFrontend = this.validateProvidedFrontend();
114
+ this.servicesInitializer = new ManagerServicesInitializer(this);
115
+ this.useProvidedServer = ManagerComponentValidator.validateProvidedServer(this.app, this.appServer);
116
+ this.useProvidedDataServer = ManagerComponentValidator.validateProvidedDataServer(this.dataServer);
117
+ this.useProvidedAdminManager = ManagerComponentValidator.validateProvidedAdminManager(this.adminManager);
118
+ this.useProvidedFrontend = ManagerComponentValidator.validateProvidedFrontend(this.frontend);
124
119
  this.cmsPagesRouteManager = new CmsPagesRouteManager({
125
120
  dataServer: this.dataServer,
126
121
  events: this.events
127
122
  });
128
123
  }
129
124
 
130
- validateProvidedServer()
131
- {
132
- if(!this.app){
133
- return false;
134
- }
135
- if(!this.appServer){
136
- return false;
137
- }
138
- if('function' !== typeof this.app.use){
139
- Logger.critical('Invalid app instance provided - missing use method.');
140
- return false;
141
- }
142
- if('function' !== typeof this.appServer.listen){
143
- Logger.critical('Invalid appServer instance provided - missing listen method.');
144
- return false;
145
- }
146
- return true;
147
- }
148
-
149
- validateProvidedDataServer()
150
- {
151
- if(!this.dataServer){
152
- return false;
153
- }
154
- if('function' !== typeof this.dataServer.connect){
155
- Logger.critical('Invalid dataServer instance provided - missing connect method.');
156
- return false;
157
- }
158
- if('function' !== typeof this.dataServer.generateEntities){
159
- Logger.critical('Invalid dataServer instance provided - missing generateEntities method.');
160
- return false;
161
- }
162
- return true;
163
- }
164
-
165
- validateProvidedAdminManager()
166
- {
167
- if(!this.adminManager){
168
- return false;
169
- }
170
- if('function' !== typeof this.adminManager.setupAdmin){
171
- Logger.critical('Invalid adminManager instance provided - missing setupAdmin method.');
172
- return false;
173
- }
174
- return true;
175
- }
176
-
177
- validateProvidedFrontend()
178
- {
179
- if(!this.frontend){
180
- return false;
181
- }
182
- if('function' !== typeof this.frontend.initialize){
183
- Logger.critical('Invalid frontend instance provided - missing initialize method.');
184
- return false;
185
- }
186
- return true;
187
- }
188
-
189
- loadConfigFromEnv()
190
- {
191
- let envVars = process.env;
192
- return {
193
- host: sc.get(envVars, 'RELDENS_APP_HOST', 'http://localhost'),
194
- port: Number(sc.get(envVars, 'RELDENS_APP_PORT', 8080)),
195
- adminPath: sc.get(envVars, 'RELDENS_ADMIN_ROUTE_PATH', '/reldens-admin'),
196
- adminSecret: sc.get(envVars, 'RELDENS_ADMIN_SECRET', ''),
197
- database: {
198
- client: sc.get(envVars, 'RELDENS_DB_CLIENT', 'mysql'),
199
- host: sc.get(envVars, 'RELDENS_DB_HOST', 'localhost'),
200
- port: Number(sc.get(envVars, 'RELDENS_DB_PORT', 3306)),
201
- name: sc.get(envVars, 'RELDENS_DB_NAME', 'reldens_cms'),
202
- user: sc.get(envVars, 'RELDENS_DB_USER', ''),
203
- password: sc.get(envVars, 'RELDENS_DB_PASSWORD', ''),
204
- driver: sc.get(envVars, 'RELDENS_STORAGE_DRIVER', 'prisma')
205
- },
206
- publicUrl: sc.get(envVars, 'RELDENS_PUBLIC_URL', '')
207
- };
208
- }
209
-
210
125
  isInstalled()
211
126
  {
212
127
  return FileHandler.exists(this.installLockPath);
@@ -239,7 +154,7 @@ class Manager
239
154
  return true;
240
155
  }
241
156
  try {
242
- await this.initializeServices();
157
+ await this.servicesInitializer.initializeServices();
243
158
  Logger.info('CMS running on '+this.config.host+':'+this.config.port);
244
159
  return true;
245
160
  } catch (error) {
@@ -311,6 +226,21 @@ class Manager
311
226
  return appServerConfig;
312
227
  }
313
228
 
229
+ isCdnUrlInDirectives(cdnUrlWithProtocol, cdnHostname)
230
+ {
231
+ let directiveKeys = Object.keys(this.developmentExternalDomains);
232
+ for(let directiveKey of directiveKeys){
233
+ let domains = this.developmentExternalDomains[directiveKey];
234
+ if(!sc.isArray(domains)){
235
+ continue;
236
+ }
237
+ if(domains.includes(cdnUrlWithProtocol) || domains.includes(cdnHostname)){
238
+ return true;
239
+ }
240
+ }
241
+ return false;
242
+ }
243
+
314
244
  validateCdnMappingsInDevelopment()
315
245
  {
316
246
  if(!sc.isObject(this.domainCdnMapping) || sc.isArray(this.domainCdnMapping)){
@@ -331,19 +261,7 @@ class Manager
331
261
  let cdnUrl = this.domainCdnMapping[domain];
332
262
  let cdnHostname = cdnUrl.replace(/^https?:\/\//, '').split('/')[0];
333
263
  let cdnUrlWithProtocol = cdnUrl.split('/')[0];
334
- let foundInDirective = false;
335
- let directiveKeys = Object.keys(this.developmentExternalDomains);
336
- for(let directiveKey of directiveKeys){
337
- let domains = this.developmentExternalDomains[directiveKey];
338
- if(!sc.isArray(domains)){
339
- continue;
340
- }
341
- if(domains.includes(cdnUrlWithProtocol) || domains.includes(cdnHostname)){
342
- foundInDirective = true;
343
- break;
344
- }
345
- }
346
- if(!foundInDirective){
264
+ if(!this.isCdnUrlInDirectives(cdnUrlWithProtocol, cdnHostname)){
347
265
  domainsWithMissingCdn.push(domain);
348
266
  }
349
267
  }
@@ -363,7 +281,6 @@ class Manager
363
281
  this.appServerFactory.addHttpDomainsAsDevelopment();
364
282
  this.appServerFactory.detectDevelopmentMode();
365
283
  this.appServerFactory.setupSecurity();
366
- //Logger.debug('Development mode installation: '+this.appServerFactory.isDevelopmentMode);
367
284
  this.rawRegisteredEntities = props.loadedEntities.rawRegisteredEntities;
368
285
  this.entitiesTranslations = props.loadedEntities.entitiesTranslations;
369
286
  this.entitiesConfig = props.loadedEntities.entitiesConfig;
@@ -372,7 +289,7 @@ class Manager
372
289
  this.dataServer = props.dataServer;
373
290
  this.useProvidedDataServer = true;
374
291
  }
375
- let servicesResult = await this.initializeServices();
292
+ let servicesResult = await this.servicesInitializer.initializeServices();
376
293
  if(!servicesResult){
377
294
  Logger.critical('Failed to initialize services after installation.');
378
295
  return false;
@@ -384,281 +301,6 @@ class Manager
384
301
  return false;
385
302
  }
386
303
  }
387
-
388
- async initializeServices()
389
- {
390
- this.events.emit('reldens.cmsManagerInitializeServices', {manager: this});
391
- if(!this.useProvidedDataServer){
392
- if(!await this.initializeDataServer()){
393
- Logger.debug('Initialize Data Server failed.');
394
- return false;
395
- }
396
- }
397
- if(0 < Object.keys(this.entityAccess).length){
398
- await this.setupEntityAccess();
399
- }
400
- if(!this.loadProcessedEntities()){
401
- Logger.debug('Load Processed Entities for Entities failed.');
402
- return false;
403
- }
404
- if(!await this.generateAdminEntities()){
405
- Logger.debug('Generate Admin Entities for Entities failed.');
406
- return false;
407
- }
408
- if(!this.useProvidedAdminManager){
409
- if(!await this.initializeAdminManager()){
410
- Logger.debug('Initialize Admin Manager failed.');
411
- return false;
412
- }
413
- }
414
- if(!await this.initializeCmsPagesRouteManager()){
415
- Logger.debug('Initialize CMS Pages Route Manager failed.');
416
- return false;
417
- }
418
- if(!this.useProvidedFrontend){
419
- if(!await this.initializeFrontend()){
420
- Logger.debug('Initialize Frontend failed.');
421
- return false;
422
- }
423
- }
424
- this.sitemapLoader = new SitemapLoader({
425
- dataServer: this.dataServer,
426
- events: this.events,
427
- domainMapping: this.domainMapping
428
- });
429
- if(!this.useProvidedServer){
430
- await this.appServer.listen(this.config.port);
431
- }
432
- Logger.debug('Initialize Services successfully.');
433
- return true;
434
- }
435
-
436
- async setupEntityAccess()
437
- {
438
- let accessEntity = this.dataServer.getEntity('entitiesAccess');
439
- if(!accessEntity){
440
- Logger.warning('Entities Access not found.');
441
- return;
442
- }
443
- for(let entityName of Object.keys(this.entityAccess)){
444
- let accessConfig = this.entityAccess[entityName];
445
- if(!await accessEntity.loadOneBy('entity_name', entityName)){
446
- await accessEntity.create({
447
- entity_name: entityName,
448
- is_public: sc.get(accessConfig, 'public', false),
449
- allowed_operations: JSON.stringify(sc.get(accessConfig, 'operations', ['read']))
450
- });
451
- }
452
- }
453
- }
454
-
455
- loadProcessedEntities()
456
- {
457
- if(0 === Object.keys(this.processedEntities).length){
458
- let mergedConfig = EntitiesConfigProcessor.applyOverrides(
459
- this.entitiesConfig,
460
- this.entitiesConfigOverride,
461
- {projectRoot: this.projectRoot}
462
- );
463
- this.processedEntities = LoadedEntitiesProcessor.process(
464
- this.rawRegisteredEntities,
465
- this.entitiesTranslations,
466
- mergedConfig
467
- );
468
- }
469
- if(!this.processedEntities?.entities){
470
- Logger.critical('Processed entities undefined.');
471
- return false;
472
- }
473
- return true;
474
- }
475
-
476
- async generateAdminEntities()
477
- {
478
- if(0 < Object.keys(this.adminEntities).length){
479
- return true;
480
- }
481
- if(!this.dataServer.rawEntities && this.rawRegisteredEntities){
482
- this.dataServer.rawEntities = this.rawRegisteredEntities;
483
- }
484
- Logger.debug('Generate entities count: '+Object.keys(this.rawRegisteredEntities).length);
485
- await this.dataServer.generateEntities();
486
- this.adminEntities = this.adminEntitiesGenerator.generate(
487
- this.processedEntities.entities,
488
- this.dataServer.entityManager.entities
489
- );
490
- if(0 === Object.keys(this.adminEntities).length){
491
- Logger.critical('Admin entities generation failed - no entities available.');
492
- Logger.critical('CMS cannot start without admin entities.');
493
- return false;
494
- }
495
- return true;
496
- }
497
-
498
- async initializeDataServer()
499
- {
500
- let dbConfig = {
501
- client: this.config.database.client,
502
- config: {
503
- host: this.config.database.host,
504
- port: this.config.database.port,
505
- database: this.config.database.name,
506
- user: this.config.database.user,
507
- password: this.config.database.password
508
- },
509
- rawEntities: this.rawRegisteredEntities,
510
- entitiesConfig: this.entitiesConfig
511
- };
512
- let driverClass = DriversMap[this.config.database.driver];
513
- if(!driverClass){
514
- Logger.critical('Invalid database driver: '+this.config.database.driver);
515
- return false;
516
- }
517
- if('prisma' === this.config.database.driver && this.prismaClient){
518
- dbConfig.prismaClient = this.prismaClient;
519
- }
520
- this.dataServer = new driverClass(dbConfig);
521
- if(!await this.dataServer.connect()){
522
- Logger.critical('Failed to connect to database.');
523
- return false;
524
- }
525
- Logger.debug('Entities count: '+Object.keys(this.rawRegisteredEntities).length);
526
- await this.dataServer.generateEntities();
527
- return true;
528
- }
529
-
530
- async initializeAdminManager()
531
- {
532
- let authenticationCallback = this.authenticationCallback;
533
- if('db-users' === this.authenticationMethod && !authenticationCallback){
534
- authenticationCallback = async (email, password, roleId) => {
535
- Logger.debug('Running default "db-users" authentication.');
536
- let usersEntity = this.dataServer.getEntity('users');
537
- if(!usersEntity){
538
- Logger.critical('No users entity found.');
539
- return false;
540
- }
541
- let user = await usersEntity.loadOneBy('email', email);
542
- if(!user){
543
- Logger.debug('User not found by email: '+email+'.', user);
544
- return false;
545
- }
546
- if(Number(user.role_id) !== Number(roleId)){
547
- Logger.debug('Invalid user role ID: '+roleId+' / '+user.role_id+'.');
548
- return false;
549
- }
550
- let passwordResult = Encryptor.validatePassword(password, user.password) ? user : false;
551
- if(!passwordResult){
552
- Logger.debug('Invalid user password for: '+email+'.');
553
- }
554
- return passwordResult;
555
- };
556
- }
557
- this.templateReloader.trackTemplateFiles(this.mappedAdminTemplates);
558
- let adminFilesContents = await AdminTemplatesLoader.fetchAdminFilesContents(this.mappedAdminTemplates);
559
- let translations = sc.deepMergeProperties(
560
- sc.deepMergeProperties({}, DefaultTranslations),
561
- sc.deepMergeProperties(this.entitiesTranslations, this.adminTranslations)
562
- );
563
- this.events.emit('reldens.manager.initializeAdminManager', {
564
- manager: this,
565
- authenticationCallback,
566
- adminFilesContents,
567
- translations
568
- });
569
- this.initializePasswordEncryptionHandler();
570
- let adminConfig = {
571
- events: this.events,
572
- dataServer: this.dataServer,
573
- authenticationCallback,
574
- app: this.app,
575
- appServerFactory: this.appServerFactory,
576
- entities: this.adminEntities,
577
- validator: new AdminManagerValidator(),
578
- renderCallback: this.renderCallback.bind(this),
579
- secret: this.config.adminSecret,
580
- rootPath: this.config.adminPath,
581
- translations,
582
- adminFilesContents,
583
- mimeTypes: this.mimeTypes,
584
- allowedExtensions: this.allowedExtensions,
585
- adminRoleId: this.adminRoleId,
586
- stylesFilePath: this.stylesFilePath,
587
- scriptsFilePath: this.scriptsFilePath,
588
- cacheManager: this.cacheManager,
589
- branding: {
590
- companyName: this.companyName,
591
- logo: this.logo,
592
- favicon: this.favicon,
593
- copyRight: await FileHandler.fetchFileContents(
594
- FileHandler.joinPaths(this.projectAdminTemplatesPath, this.adminTemplatesList.defaultCopyRight)
595
- )
596
- }
597
- };
598
- this.adminManager = new AdminManager(adminConfig);
599
- this.adminManager.router.checkAndReloadAdminTemplates = async () => {
600
- return await this.templateReloader.handleAdminTemplateReload(this.adminManager);
601
- };
602
- await this.adminManager.setupAdmin();
603
- return true;
604
- }
605
-
606
- initializePasswordEncryptionHandler()
607
- {
608
- if(!this.enablePasswordEncryption){
609
- Logger.debug('Password encryption handler is disabled.');
610
- return false;
611
- }
612
- this.passwordEncryptionHandler = new PasswordEncryptionHandler({
613
- events: this.events,
614
- enabled: this.enablePasswordEncryption
615
- });
616
- this.passwordEncryptionHandler.registerEventListeners();
617
- Logger.debug('Password encryption handler initialized and registered.');
618
- return true;
619
- }
620
-
621
- async initializeCmsPagesRouteManager()
622
- {
623
- if(!this.dataServer){
624
- Logger.warning('CmsPagesRouteManager initialization skipped - missing dataServer.');
625
- return false;
626
- }
627
- this.cmsPagesRouteManager.dataServer = this.dataServer;
628
- Logger.debug('CmsPagesRouteManager initialized successfully');
629
- return true;
630
- }
631
-
632
- async renderCallback(template, params = {})
633
- {
634
- if(!template){
635
- return '';
636
- }
637
- return this.renderEngine.render(template, params);
638
- }
639
-
640
- async initializeFrontend()
641
- {
642
- this.frontend = new Frontend({
643
- app: this.app,
644
- dataServer: this.dataServer,
645
- events: this.events,
646
- renderEngine: this.renderEngine,
647
- projectRoot: this.projectRoot,
648
- appServerFactory: this.appServerFactory,
649
- defaultDomain: this.defaultDomain,
650
- domainMapping: this.domainMapping,
651
- siteKeyMapping: this.siteKeyMapping,
652
- domainPublicUrlMapping: this.domainPublicUrlMapping,
653
- domainCdnMapping: this.domainCdnMapping,
654
- defaultPublicUrl: this.config.publicUrl,
655
- templateExtensions: this.templateExtensions,
656
- entitiesConfig: this.entitiesConfig,
657
- cacheManager: this.cacheManager,
658
- handleFrontendTemplateReload: this.templateReloader.handleFrontendTemplateReload.bind(this.templateReloader)
659
- });
660
- return await this.frontend.initialize();
661
- }
662
304
  }
663
305
 
664
306
  module.exports.Manager = Manager;