/** * Create and add elements to this form * * @param array $formData */ public function createElements(array $formData) { $this->addElement('text', 'name', array('required' => true, 'label' => $this->translate('Backend Name'), 'description' => $this->translate('The name of this user group backend that is used to differentiate it from others'))); $resourceNames = $this->getLdapResourceNames(); $this->addElement('select', 'resource', array('required' => true, 'autosubmit' => true, 'label' => $this->translate('LDAP Connection'), 'description' => $this->translate('The LDAP connection to use for this backend.'), 'multiOptions' => array_combine($resourceNames, $resourceNames))); $resource = ResourceFactory::create(isset($formData['resource']) && in_array($formData['resource'], $resourceNames) ? $formData['resource'] : $resourceNames[0]); $userBackendNames = $this->getLdapUserBackendNames($resource); if (!empty($userBackendNames)) { $userBackends = array_combine($userBackendNames, $userBackendNames); $userBackends['none'] = $this->translate('None', 'usergroupbackend.ldap.user_backend'); } else { $userBackends = array('none' => $this->translate('None', 'usergroupbackend.ldap.user_backend')); } $this->addElement('select', 'user_backend', array('required' => true, 'autosubmit' => true, 'label' => $this->translate('User Backend'), 'description' => $this->translate('The user backend to link with this user group backend.'), 'multiOptions' => $userBackends)); $groupBackend = new LdapUserGroupBackend($resource); if ($formData['type'] === 'ldap') { $defaults = $groupBackend->getOpenLdapDefaults(); $groupConfigDisabled = $userConfigDisabled = null; // MUST BE null, do NOT change this to false! } else { // $formData['type'] === 'msldap' $defaults = $groupBackend->getActiveDirectoryDefaults(); $groupConfigDisabled = $userConfigDisabled = true; } $this->createGroupConfigElements($defaults, $groupConfigDisabled); if (count($userBackends) === 1 || isset($formData['user_backend']) && $formData['user_backend'] === 'none') { $this->createUserConfigElements($defaults, $userConfigDisabled); } $this->addElement('hidden', 'backend', array('disabled' => true, 'value' => $formData['type'])); }
/** * Create and add elements to this form * * @param array $formData */ public function createElements(array $formData) { $resourceNames = $this->getLdapResourceNames(); $this->addElement('select', 'resource', array('required' => true, 'autosubmit' => true, 'label' => $this->translate('LDAP Connection'), 'description' => $this->translate('The LDAP connection to use for this backend.'), 'multiOptions' => array_combine($resourceNames, $resourceNames))); $resource = ResourceFactory::create(isset($formData['resource']) && in_array($formData['resource'], $resourceNames) ? $formData['resource'] : $resourceNames[0]); $userBackends = array('none' => $this->translate('None', 'usergroupbackend.ldap.user_backend')); $userBackendNames = $this->getLdapUserBackendNames($resource); if (!empty($userBackendNames)) { $userBackends = array_merge($userBackends, array_combine($userBackendNames, $userBackendNames)); } $this->addElement('select', 'user_backend', array('required' => true, 'autosubmit' => true, 'label' => $this->translate('User Backend'), 'description' => $this->translate('The user backend to link with this user group backend.'), 'multiOptions' => $userBackends)); $groupBackend = new LdapUserGroupBackend($resource); if ($formData['type'] === 'ldap') { $defaults = $groupBackend->getOpenLdapDefaults(); $groupConfigDisabled = $userConfigDisabled = null; // MUST BE null, do NOT change this to false! } else { // $formData['type'] === 'msldap' $defaults = $groupBackend->getActiveDirectoryDefaults(); $groupConfigDisabled = $userConfigDisabled = true; } $dnDisabled = null; // MUST BE null if (isset($formData['user_backend']) && $formData['user_backend'] !== 'none') { $userBackend = UserBackend::create($formData['user_backend']); $defaults->merge(array('user_base_dn' => $userBackend->getBaseDn(), 'user_class' => $userBackend->getUserClass(), 'user_name_attribute' => $userBackend->getUserNameAttribute(), 'user_filter' => $userBackend->getFilter())); $userConfigDisabled = $dnDisabled = true; } $this->createGroupConfigElements($defaults, $groupConfigDisabled); $this->createUserConfigElements($defaults, $userConfigDisabled, $dnDisabled); }
/** * Create and return a user group backend with the given name and given configuration applied to it * * @param string $name * @param ConfigObject $backendConfig * * @return UserGroupBackendInterface * * @throws ConfigurationError */ public static function create($name, ConfigObject $backendConfig) { if ($backendConfig->name !== null) { $name = $backendConfig->name; } if (!($backendType = strtolower($backendConfig->backend))) { throw new ConfigurationError('Configuration for user group backend "%s" is missing the \'backend\' directive', $name); } if (in_array($backendType, static::$defaultBackends)) { // The default backend check is the first one because of performance reasons: // Do not attempt to load a custom user group backend unless it's actually required } elseif (($customClass = static::getCustomUserGroupBackend($backendType)) !== null) { $backend = new $customClass($backendConfig); if (!is_a($backend, 'Icinga\\Authentication\\UserGroup\\UserGroupBackendInterface')) { throw new ConfigurationError('Cannot utilize user group backend of type "%s".' . ' Class "%s" does not implement UserGroupBackendInterface', $backendType, $customClass); } $backend->setName($name); return $backend; } else { throw new ConfigurationError('Configuration for user group backend "%s" defines an invalid backend type.' . ' Backend type "%s" is not supported', $name, $backendType); } if ($backendConfig->resource === null) { throw new ConfigurationError('Configuration for user group backend "%s" is missing the \'resource\' directive', $name); } $resource = ResourceFactory::create($backendConfig->resource); switch ($backendType) { case 'db': $backend = new DbUserGroupBackend($resource); break; case 'ini': $backend = new IniUserGroupBackend($resource); break; case 'ldap': case 'msldap': $backend = new LdapUserGroupBackend($resource); $backend->setConfig($backendConfig); break; } $backend->setName($name); return $backend; }