The "config.cfm" File - Mura Docs v7.0

The "config.cfm" File

This file is completely optional. However, if you have an ultra-simple plugin, this file can come in handy. In short, it simply contains a few variables you can share across the administrative pages of your plugin using the "cfinclude" method.

The Mura Scope Variable

As a Mura developer, you probably want a way to access the Mura Scope. Since your plugin is running under the Mura application itself, you will have access to its application scope variables and methods. However, when accessing the plugin directly, the Mura Scope itself will not be defined. So, using the information we learned from the Custom Instance of the Mura Scope section, we can easily create a variable to reference the Mura Scope using the code shown below to place in our config.cfm file.

// ./plugin/config.cfm
    // Mura Scope
    if ( !IsDefined('m') ) {
        siteid = session.keyExists('siteid') ? session.siteid : 'default';
        m = application.serviceFactory.getBean('m').init(siteid);
    }

The pluginConfig Variable

Now that we have a reference to the Mura Scope, as a Mura plugin developer, we'll also want access to the pluginConfig. By having access to the pluginConfig, the following methods are available to you.

Method Parameters Description
getDirectory none Returns the directory name of the plugin.
getPackage none Returns the value from the config.xml.cfm <package> node.
getName none Returns the Name (Alias) data entered when the plugin was installed and deployed.
getApplication none

Plugins have access to their own, exclusive application scope. Useful for creating and setting custom application variables associated only with your plugin. For example:

myPluginApplication = pluginConfig.getApplication();
myPluginAppication.set('colors', ['Red','White','Blue']);
pluginColors = myPluginApplication.get('colors');

If you wish to purge your plugin's application variables, you may use the following syntax:

// 'true' = purge the plugin's application variables
myPluginApplication = pluginConfig.getApplication(true);
getSession none

Plugins have access to their own, exclusive session scope. Useful for creating and setting custom session variables associated only with your plugin. For example:

myPluginSession = pluginConfig.getSession();
myPluginSession.set('fruit', ['Grapes','Oranges']);
pluginFruit = myPluginSession.get('fruit');

If you wish to purge your plugin's session variables, you may use the following syntax:

// 'true' = purge the plugin's session variables
myPluginSession = pluginConfig.getSession(true);
getAssignedSites none

Returns a recordset of of SiteIDs the plugin has been enabled for.

<cfset rsSites = pluginConfig.getAssignedSites()>
<ul>
<cfloop query="rsSites">
<li>#siteid#</li>
</cfloop>
</ul>
getSettings none Returns a struct of any plugin settings collected via the config.xml.cfm <settings> elements.
getSetting property Provides a way to obtain a specific plugin setting collected via the config.xml.cfm <settings> elements. Returns the value for the property parameter which was passed in.

Let's add some code to our config.cfm file to give us access to a variable named pluginConfig.

// ./plugin/config.cfm
    // Mura Scope
    if ( !IsDefined('m') ) {
        siteid = session.keyExists('siteid') ? session.siteid : 'default';
        m = application.serviceFactory.getBean('m').init(siteid);
    }
    // Plugin Config
    if ( !IsDefined('pluginConfig') ) {
        pluginConfig = m.getBean('pluginManager').getConfig('{YourPluginPackagNameGoesHere}');
    }

Restricting Access

Finally, in most cases, you'll want to make sure that groups which do not have access to the plugin's administrator area will not be able to access it. The pluginConfig variable has a special method labeled currentUserAccess, which checks the currently logged in user to see if any of the groups they belong to have been granted access or not. If it returns true, then they have been granted access. If false, then they have not been granted permissions to the plugin itself.

Using this method, we can easily add one more line of code to our config.cfm file, and redirect unauthorized users to the login screen. As a developer, you can probably come up with other options as well.

// ./plugin/config.cfm
    // Mura Scope
    if ( !IsDefined('m') ) {
        siteid = session.keyExists('siteid') ? session.siteid : 'default';
        m = application.serviceFactory.getBean('m').init(siteid);
    }
    // Plugin Config
    if ( !IsDefined('pluginConfig') ) {
        pluginConfig = m.getBean('pluginManager').getConfig('{YourPluginPackagNameGoesHere}');
    }
    // Check User Access ~ Redirect if Not Authorized
    if ( !pluginconfig.currentUserAccess() ) {
        location(
  url=m.globalConfig('context') & '/admin/index.cfm?muraAction=clogin.main'
  , addtoken=false
  );
    }

Summary

If you have a fairly simple plugin, the config.cfm file is a pretty easy way to include common variables and logic throughout your plugin's administrative pages. For more complex plugins, you may wish to use an Application.cfc file instead. An example may be found at https://github.com/stevewithington/MuraPlugin/blob/develop/Application.cfc. If you would like to use a front-end controller framework, be sure to check out MuraFW1, which is a base Mura CMS plugin using Framework One (FW/1) as its application framework.