Jump to content

How to give permission for a non superuser to edit module config


JayGee
 Share

Recommended Posts

Hi all,

Title says it all really. Have created a little site-specific module and want to give a non-superuser access to edit the module settings. What's the best way to go about this.

I've added a nav item to the the setup menu using the getModuleInfo() array in the module file but this just returns a 'The process returned no content.' message. Should I be redirecting this the config screen or rendering the config screen into this page somehow?

Thanks,
J

Incidentally the icon I choose for the module isn't rendering - do I just choose the name from FontAwesome for this?

Link to comment
Share on other sites

After reading up further I see I should have built my own UI on a Process page rather than using a module config screen to achieve this. ?

I never realised there was such a disconnect between using module config screens and the UI of Process modules. It would be awesome if there was a way to optionally pull module config screen into the Process page view as most of the simple modules I need to make for sites don't need more than what the config screen provides other than granting permissions to non super users.

Don't know if anyone has come up against this or created a work-around?

Link to comment
Share on other sites

Settings Factory was created precisely for this use case - no need to create a module, module config;

You can also keep your fields in your module with default values, and let your site admins override those in the Settings Factory "settings".

To put it simply, if you are building out module-like code on the front end, but need to expose settings for things to your editors, Settings Factory has the ability to create unlimited settings pages, each with their own permissions...

  • Like 2
Link to comment
Share on other sites

Hi @Macrura thanks for this - module looks fantastic and not sure how I managed to pass it by until now! One quick question - can you export settings to other sites? One big advantage of coding modules is obviously the portability.

Link to comment
Share on other sites

yep - since the settings definitions are defined in json or php files they can be copied from site to site and then setup in the admin (simply create a page under /admin/ and then set the process as SettingsFactory, then type in the path to the settings definition file).

In terms of exporting actual settings, like the values, I have thought about that as well, as it would be nice to be able to back up the settings for any given single settings configuration.

  • Like 1
Link to comment
Share on other sites

  • 2 years later...
On 6/26/2021 at 10:20 PM, Macrura said:

Settings Factory was created precisely for this use case

Hi @Macrura. Maybe I'm being dim, but how do you use settings factory to maintain the config of another module? I know you can edit the module config 'raw/JSON' directly in the module settings (if you are superuser), but I don't see how to do it from settings factory as there doesn't seem to be a real json file.

I would like to have someone with 'settings-factory' permission to update the configs of certain modules (as per the OP, but not necessarily my own modules, so I can't hack their code).

EDIT:

Some progress by using a php file with (for example)

$data = $modules->getConfig('ProcessEmailToPage');
return ProcessEmailToPage::getModuleConfigInputfields($data);

That displays the config. Just need to work out how to update it - I guess a hook....

Link to comment
Share on other sites

Hi @MarkE,

One way is to create a json file with the fields you want to give access to from the other module. So if the other module had something like a checkbox, or select field, you would need to copy those field definitions into a new json file, say in your templates/settings folder. Then you would create a new admin page, using the Settings Factory process, and enter the path to that json file.

Now the settings factory is storing the override values for that module separately. So in order to now have those be used, it depends on how you are using that module, if it is being used on the front end, you could override the settings but it depends on which settings are public in that module, and how they are output. If you let me know which module you want to do this on i can see if I can figure it out..

Link to comment
Share on other sites

14 hours ago, Macrura said:

One way is to create a json file with the fields you want to give access to from the other module

Thaks @Macrura - Yeah - I thought about that, but reckoned that just calling the other module's getModuleConfigInputfields was easier. However, with ProcessEmailToPage for some reason, not all the fields got populated. Here is my code (in templates/process-email-to-page.php)

$wire->addHookBefore('ProcessSettingsFactory::execute', function(HookEvent $event) {
	$object = $event->object;
	if($object->input->post('submit_save')) {
		$settingsKey = $object->wire('page')->name;
		$form = $object->buildForm();
		$form->processInput($object->input->post);
		$myData = $form->children[0]->children->explode(['name', 'value']);
		$newData = [];
		foreach($myData as $item) {
			$newData[$item['name']] = $item['value'];
		}
		// Here I will call $modules->saveConfig('ProcessEmailToPage', $newData) once the debugging shows I have the full data
		bd(['form' => $form, 'myData' => $myData, 'newData' => $newData], 'In execute hook - save'); // newData is not complete
	}
});

$data = $modules->getConfig('ProcessEmailToPage');
$fields = ProcessEmailToPage::getModuleConfigInputfields($data);
return $fields;

The other modules I want to set similarly are WireMailMailgun, WireMailRouter and WireMailSmtp.

Link to comment
Share on other sites

8 hours ago, MarkE said:

However, with ProcessEmailToPage for some reason, not all the fields got populated

OK, the problem here seems to be that values of fieldset children are omitted by ProcessSettingsFactory. Somewhere between (in the buildForm() method) line 136 $form->add($fArray); and line 174 return $form; the values of the fieldset children get dropped. If I comment out the section

foreach($inputfields as $key => $cField) {
...
}

the field values load correctly

Link to comment
Share on other sites

1 hour ago, Robin S said:

I've released a module that allows non-superusers to configure selected modules, which might be helpful for your current project

Megastar! My module was going to be a process module a la SettingsFactory, but that does the job perfectly and saved me a task. Ta muchly!

  • Like 1
Link to comment
Share on other sites

On 10/6/2023 at 6:45 PM, MarkE said:

OK, the problem here seems to be that values of fieldset children are omitted by ProcessSettingsFactory. Somewhere between (in the buildForm() method) line 136 $form->add($fArray); and line 174 return $form; the values of the fieldset children get dropped. If I comment out the section

foreach($inputfields as $key => $cField) {
...
}

the field values load correctly

thanks for this - i'll see if i can fix that asap.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...