Skip to content

Commit

Permalink
PRESIDECMS-2709 preside utils rest api feature endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
pixl8-brayden committed Sep 18, 2023
1 parent 34d98d5 commit abd2815
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
8 changes: 8 additions & 0 deletions system/config/Config.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,14 @@ component {
};

settings.rest.authProviders.token = { feature = "restTokenAuth" };

settings.rest.apis[ "/presideUtils" ] = {
authProvider = "presideApi"
, description = "Utility REST API for external systems to interact with Preside"
, dataApiQueueEnabled = false
};

settings.preside.restauthtoken = settings.env.preside_rest_auth_token ?: "";
}

private void function __setupMultilingualDefaults() {
Expand Down
14 changes: 14 additions & 0 deletions system/handlers/rest-apis/presideUtils/feature.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* @restUri /features/{feature}/
*/
component {
property name="features" inject="coldbox:setting:features";

private void function get( required string feature ) {
if ( StructKeyExists( features, arguments.feature ) ) {
restResponse.setData( features[ arguments.feature ] );
} else {
restResponse.setData( { enabled=false } );
}
}
}
16 changes: 16 additions & 0 deletions system/handlers/rest-apis/presideUtils/features.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @restUri /features/
*/
component {
property name="featureService" inject="featureService";

private void function get() {
var allFeatures = {};

for ( var feature in featureService.getAllEnabledFeatures() ) {
allFeatures[ feature ] = true;
}

restResponse.setData( allFeatures );
}
}
26 changes: 26 additions & 0 deletions system/handlers/rest/auth/PresideApi.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
component {
property name="configuredToken" inject="coldbox:setting:preside.restauthtoken";

private string function authenticate() {
var headers = getHTTPRequestData( false ).headers;
var authHeader = headers.Authorization ?: "";
var token = "";

try {
authHeader = toString( toBinary( listRest( authHeader, ' ' ) ) );
token = ListFirst( authHeader, ":" );

if ( !token.trim().len() ) {
throw( type="missing.token" );
}
} catch( any e ) {
return "";
}

if ( token == configuredToken ) {
return token;
}

return "";
}
}
33 changes: 33 additions & 0 deletions system/services/features/FeatureService.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ component singleton=true autodoc=true displayName="Feature service" {
*/
public any function init( required struct configuredFeatures ) {
_setConfiguredFeatures( arguments.configuredFeatures );
_setLocalCache( {} );
return this;
}

Expand Down Expand Up @@ -74,11 +75,43 @@ component singleton=true autodoc=true displayName="Feature service" {
return "";
}

public array function getAllEnabledFeatures() {
return _simpleLocalCache( "getAllEnabledFeatures", function() {
var allEnabledFeatures = [];

for ( var feature in _getConfiguredFeatures() ) {
if ( isFeatureEnabled( feature=feature ) ) {
ArrayAppend( allEnabledFeatures, feature );
}
}

return allEnabledFeatures;
} );
}

// PRIVATE HELPERS
private any function _simpleLocalCache( required string cacheKey, required any generator ) {
var cache = _getLocalCache();

if ( !StructKeyExists( cache, cacheKey ) ) {
cache[ cacheKey ] = generator();
}

return cache[ cacheKey ] ?: NullValue();
}

// GETTERS AND SETTERS
private struct function _getConfiguredFeatures() {
return _configuredFeatures;
}
private void function _setConfiguredFeatures( required struct configuredFeatures ) {
_configuredFeatures = arguments.configuredFeatures;
}

private struct function _getLocalCache() {
return _localCache;
}
private void function _setLocalCache( required struct localCache ) {
_localCache = arguments.localCache;
}
}

0 comments on commit abd2815

Please sign in to comment.