Matt Stephens
Matt Stephens

Reputation: 922

Plugin configuration

I am in the process of creating a user auth plugin that can use LDAP or fall back to a customised solution.

LDAP needs a series of information including domain, protocol and baseDN.

I was wondering what the best method of configuration would for for a plugin like this?

I thought using the bootstrap.php file would be best as this is a similar way most plugins use.

Example bootstrap config:

Configure::write('UserAuth.config', [
  'protocol' => 'ldap://',
  'domain' => 'example.net',
  'basedn' => 'dc=example,dc=net',
]);

A colleague of mine has suggested using the InstanceConfigTrait as a way of having a default config that can be overridden at another point.

Example InstanceConfigTrait config:

protected $_defaultConfig = [
  'protocol' => '',
  'domain' => '',
  'basedn' => '',
];

I understand why he suggested the InstanceConfigTrait but in reality there is no default config due to the fact LDAP can only be used if you have the correct information.

Any thoughts?

Upvotes: 1

Views: 62

Answers (1)

floriank
floriank

Reputation: 25698

Auth adapters should work all the same way, so when you create them . BaseAuthenticate and Authorize are already using the instance config trait. When you configure them the config data is passed there.

I would leave it up to the developer of the app (and do LDAP as plugin) how he passes his config to the adapters. This can be easly done in AppController::initialize() when auth is configured like in the provided links.

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Ldap.Ldap' => [
                'protocol' => 'ldap://',
                'domain' => 'example.net',
                'basedn' => 'dc=example,dc=net',
            ]
        ]
    ]);
}

Either hard code it there or simply use Configure::read()

public function initialize()
{
    parent::initialize();
    $this->loadComponent('Auth', [
        'authenticate' => [
            'Ldap.Ldap' => Configure::read('Ldap.connection')
        ]
    ]);
}

Upvotes: 2

Related Questions