Skip to content

Commit

Permalink
Split Permissions.ts into Permissions.ts and GetterSetter.ts to bette…
Browse files Browse the repository at this point in the history
…r modularize functions.

Re-order module inclusion to "fix" (??) variables not getting initialized.
Add code to Accounts and Domains to verify entity field structure is initialized
    There is some ordering problem that causes the function to not get set in the const assignment
Replace all instances of FieldDefn getters and setters with "noGetter" rather than "undefined"
Add "automatic_networking" to DomainEntity
  • Loading branch information
Misterblue committed Nov 3, 2020
1 parent da160e0 commit 2928148
Show file tree
Hide file tree
Showing 17 changed files with 500 additions and 437 deletions.
17 changes: 8 additions & 9 deletions src/Entities/AccountEntity.ts
Expand Up @@ -19,11 +19,10 @@ import { Accounts } from '@Entities/Accounts';
import { AccountRoles } from '@Entities/AccountRoles';
import { AccountAvailability } from '@Entities/AccountAvailability';

import { FieldDefn, Perm } from '@Route-Tools/Permissions';
import { isStringValidator, isBooleanValidator, isPathValidator, isNumberValidator, isSArraySet, isDateValidator } from '@Route-Tools/Permissions';
import { simpleGetter, simpleSetter, sArraySetter, dateStringGetter } from '@Route-Tools/Permissions';
import { verifyAllSArraySetValues } from '@Route-Tools/Permissions';
import { getEntityField, setEntityField, getEntityUpdateForField } from '@Route-Tools/Permissions';
import { isStringValidator, isBooleanValidator, isPathValidator, isNumberValidator, isSArraySet, isDateValidator } from '@Route-Tools/GetterSetter';
import { simpleGetter, simpleSetter, sArraySetter, dateStringGetter } from '@Route-Tools/GetterSetter';
import { FieldDefn, noGetter, noSetter, verifyAllSArraySetValues } from '@Route-Tools/GetterSetter';
import { getEntityField, setEntityField, getEntityUpdateForField } from '@Route-Tools/GetterSetter';

import { VKeyedCollection } from '@Tools/vTypes';
import { IsNullOrEmpty } from '@Tools/Misc';
Expand Down Expand Up @@ -310,7 +309,7 @@ export const accountFields: { [key: string]: FieldDefn } = {
setter: (pField: FieldDefn, pEntity: Entity, pVal: any): void => {
Accounts.storePassword((pEntity as AccountEntity), pVal);
},
getter: undefined,
getter: noGetter,
// An update to the password means updates to hash and salt fields.
updater: (pField: FieldDefn, pEntity: Entity, pUpdates: VKeyedCollection): void => {
pUpdates.passwordHash = (pEntity as AccountEntity).passwordHash;
Expand Down Expand Up @@ -374,7 +373,7 @@ export const accountFields: { [key: string]: FieldDefn } = {
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isStringValidator,
setter: undefined,
setter: noSetter,
getter: simpleGetter
},
'when_account_created': {
Expand All @@ -383,7 +382,7 @@ export const accountFields: { [key: string]: FieldDefn } = {
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isDateValidator,
setter: undefined,
setter: noSetter,
getter: dateStringGetter
},
'time_of_last_heartbeat': {
Expand All @@ -392,7 +391,7 @@ export const accountFields: { [key: string]: FieldDefn } = {
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isDateValidator,
setter: undefined,
setter: noSetter,
getter: dateStringGetter
},
};
20 changes: 19 additions & 1 deletion src/Entities/Accounts.ts
Expand Up @@ -17,7 +17,7 @@ import { Config } from '@Base/config';

import crypto from 'crypto';

import { AccountEntity, setAccountField } from '@Entities/AccountEntity';
import { AccountEntity, accountFields, setAccountField } from '@Entities/AccountEntity';
import { AccountRoles } from '@Entities/AccountRoles';
import { Domains } from '@Entities/Domains';
import { Places } from '@Entities/Places';
Expand All @@ -33,6 +33,24 @@ import { Logger } from '@Tools/Logging';

export let accountCollection = 'accounts';

// Initialize account management.
export function initAccounts(): void {
// DEBUG DEBUG: for unknown reasons some field ops end up 'undefined'
Object.keys(accountFields).forEach( fieldName => {
const defn = accountFields[fieldName];
if (typeof(defn.validate) !== 'function') {
Logger.error(`initAccounts: field ${defn.entity_field} validator is not a function`);
};
if (typeof(defn.getter) !== 'function') {
Logger.error(`initAccounts: field ${defn.entity_field} getter is not a function`);
};
if (typeof(defn.setter) !== 'function') {
Logger.error(`initAccounts: field ${defn.entity_field} setter is not a function`);
};
});
// END DEBUG DEBUG
};

export const Accounts = {
async getAccountWithId(pAccountId: string): Promise<AccountEntity> {
return IsNullOrEmpty(pAccountId) ? null : getObject(accountCollection,
Expand Down
22 changes: 16 additions & 6 deletions src/Entities/DomainEntity.ts
Expand Up @@ -17,10 +17,10 @@ import { Entity } from '@Entities/Entity';
import { AccountEntity } from '@Entities/AccountEntity';
import { AuthToken } from '@Entities/AuthToken';

import { FieldDefn } from '@Route-Tools/Permissions';
import { isStringValidator, isNumberValidator, isBooleanValidator, isSArraySet, isDateValidator } from '@Route-Tools/Permissions';
import { simpleGetter, simpleSetter, noOverwriteSetter, sArraySetter, dateStringGetter } from '@Route-Tools/Permissions';
import { getEntityField, setEntityField, getEntityUpdateForField } from '@Route-Tools/Permissions';
import { FieldDefn } from '@Route-Tools/GetterSetter';
import { isStringValidator, isNumberValidator, isBooleanValidator, isSArraySet, isDateValidator } from '@Route-Tools/GetterSetter';
import { simpleGetter, simpleSetter, noGetter, noSetter, noOverwriteSetter, sArraySetter, dateStringGetter } from '@Route-Tools/GetterSetter';
import { getEntityField, setEntityField, getEntityUpdateForField } from '@Route-Tools/GetterSetter';

import { VKeyedCollection } from '@Tools/vTypes';
import { Logger } from '@Tools/Logging';
Expand All @@ -40,6 +40,7 @@ export class DomainEntity implements Entity {
public networkAddr: string; // reported network address
public networkPort: string; // reported network address
public networkingMode: string; // one of "full", ?
public automaticNetworking: string; // one of "disabled", ?
public restricted: boolean; // 'true' if restricted to users with accounts
public numUsers: number; // regular users logged in
public anonUsers: number; // number of anonymous users
Expand Down Expand Up @@ -189,6 +190,15 @@ export const domainFields: { [key: string]: FieldDefn } = {
setter: simpleSetter,
getter: simpleGetter
},
'automatic_networking': {
entity_field: 'automaticNetworking',
request_field_name: 'automatic_networking',
get_permissions: [ 'all' ],
set_permissions: [ 'domain' ],
validate: isStringValidator,
setter: simpleSetter,
getter: simpleGetter
},
'networking_mode': {
entity_field: 'networkingMode',
request_field_name: 'networking_mode',
Expand Down Expand Up @@ -322,7 +332,7 @@ export const domainFields: { [key: string]: FieldDefn } = {
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isDateValidator,
setter: undefined,
setter: noSetter,
getter: dateStringGetter
},
'time_of_last_heartbeat': {
Expand All @@ -331,7 +341,7 @@ export const domainFields: { [key: string]: FieldDefn } = {
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isDateValidator,
setter: undefined,
setter: noSetter,
getter: dateStringGetter
},
'last_sender_key': {
Expand Down
17 changes: 15 additions & 2 deletions src/Entities/Domains.ts
Expand Up @@ -15,7 +15,7 @@

import Config from '@Base/config';

import { DomainEntity } from '@Entities/DomainEntity';
import { DomainEntity, domainFields } from '@Entities/DomainEntity';

import { CriteriaFilter } from '@Entities/EntityFilters/CriteriaFilter';
import { GenericFilter } from '@Entities/EntityFilters/GenericFilter';
Expand All @@ -33,6 +33,20 @@ export let domainCollection = 'domains';
// Initialize domain management.
// Periodic checks on liveness of domains and resetting of values if not talking
export function initDomains(): void {
// DEBUG DEBUG: for unknown reasons some field ops end up 'undefined'
Object.keys(domainFields).forEach( fieldName => {
const defn = domainFields[fieldName];
if (typeof(defn.validate) !== 'function') {
Logger.error(`initDomains: field ${defn.entity_field} validator is not a function`);
};
if (typeof(defn.getter) !== 'function') {
Logger.error(`initDomains: field ${defn.entity_field} getter is not a function`);
};
if (typeof(defn.setter) !== 'function') {
Logger.error(`initDomains: field ${defn.entity_field} setter is not a function`);
};
});
// END DEBUG DEBUG

setInterval( async () => {
// Find domains that are not heartbeating and reset activity if not talking
Expand All @@ -52,7 +66,6 @@ export function initDomains(): void {
}, 1000 * 60 * 2 );
};


export const Domains = {
async getDomainWithId(pDomainId: string): Promise<DomainEntity> {
return IsNullOrEmpty(pDomainId) ? null : getObject(domainCollection,
Expand Down
11 changes: 6 additions & 5 deletions src/Entities/PlaceEntity.ts
Expand Up @@ -19,10 +19,11 @@ import { AuthToken } from '@Entities/AuthToken';
import { Domains } from '@Entities/Domains';
import { Places } from '@Entities/Places';

import { checkAccessToEntity, FieldDefn, Perm } from '@Route-Tools/Permissions';
import { isStringValidator, isSArraySet, isPathValidator, isDateValidator } from '@Route-Tools/Permissions';
import { simpleGetter, simpleSetter, sArraySetter, dateStringGetter } from '@Route-Tools/Permissions';
import { getEntityField, setEntityField, getEntityUpdateForField } from '@Route-Tools/Permissions';
import { checkAccessToEntity, Perm } from '@Route-Tools/Permissions';
import { FieldDefn } from '@Route-Tools/GetterSetter';
import { isStringValidator, isSArraySet, isPathValidator, isDateValidator } from '@Route-Tools/GetterSetter';
import { simpleGetter, simpleSetter, noSetter, sArraySetter, dateStringGetter } from '@Route-Tools/GetterSetter';
import { getEntityField, setEntityField, getEntityUpdateForField } from '@Route-Tools/GetterSetter';

import { VKeyedCollection } from '@Tools/vTypes';
import { IsNullOrEmpty, IsNotNullOrEmpty } from '@Tools/Misc';
Expand Down Expand Up @@ -195,7 +196,7 @@ export const placeFields: { [key: string]: FieldDefn } = {
get_permissions: [ 'all' ],
set_permissions: [ 'none' ],
validate: isDateValidator,
setter: undefined,
setter: noSetter,
getter: dateStringGetter
}
};
6 changes: 2 additions & 4 deletions src/Monitoring/StatsMetaverse.ts
Expand Up @@ -15,19 +15,17 @@

import { Config } from '@Base/config';

import os from 'os';

import { Accounts } from '@Entities/Accounts';

import { Monitoring } from '@Monitoring/Monitoring';
import { Stat } from '@Monitoring/Stat';
import { ValueStat } from '@Monitoring/ValueStat';
import { ValueHistogram } from '@Monitoring/ValueHistogram';
import { Logger } from '@Tools/Logging';
import { GenericFilter } from '@Entities/EntityFilters/GenericFilter';
import { Domain } from 'domain';
import { Domains } from '@Entities/Domains';

import { Logger } from '@Tools/Logging';

// All the OS statistics
export class StatsMetaverse extends Stat {

Expand Down
14 changes: 8 additions & 6 deletions src/index.ts
Expand Up @@ -24,21 +24,22 @@ import express from 'express';
import cors from 'cors';
import fs from 'fs';
import crypto from 'crypto';
import glob from 'glob';
import morgan from 'morgan';

import { Router, RequestHandler, Request, Response, NextFunction } from 'express';

import { setupDB } from '@Tools/Db';

import glob from 'glob';
import morgan from 'morgan';
import { Logger, initLogging, morganOptions } from '@Tools/Logging';
import { initAccounts } from '@Entities/Accounts';
import { initDomains } from '@Entities/Domains';
import { initTokens } from '@Entities/Tokens';
import { initSessions } from '@Entities/Sessions';
import { initRequests } from '@Entities/Requests';
import { IsNotNullOrEmpty } from '@Tools/Misc';
import { initMonitoring } from '@Monitoring/Monitoring';

import { setupDB } from '@Tools/Db';
import { IsNotNullOrEmpty } from '@Tools/Misc';
import { Logger, initLogging, morganOptions } from '@Tools/Logging';

initializeConfiguration()
.catch ( err => {
Logger.error('main: failured configuration: ' + err);
Expand All @@ -49,6 +50,7 @@ initializeConfiguration()
initSessions();
initTokens();
initRequests();
initAccounts();
initDomains();
return setupDB();
})
Expand Down

0 comments on commit 2928148

Please sign in to comment.