Hiding portions of PHP code based on user group

General questions relating to Joomla! 3.x.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10
Locked
sjkelley
Joomla! Apprentice
Joomla! Apprentice
Posts: 23
Joined: Tue Jul 03, 2007 2:12 pm

Hiding portions of PHP code based on user group

Post by sjkelley » Tue Jan 05, 2016 8:13 pm

I have a Joomla 3 site that is using a third-party component. It's a business listing directory component that features the ability for site users (via the front end) and admins (via the backend) to edit business listings.

The component uses the same PHP file (called "edit.php") for both front- and back-end access.

There are a few fields and other things I'd like to "hide" from the front-end users (i.e. those assigned to the Registered Group)... but the component itself doesn't have any option settings to provide thatlevel of control.

So how can this be done?

I have in the past used this method (see http://magazine.joomla.org/issues/issue ... admin-menu) to restrict various portions of the control panel by user group.

Is there any similar approach that can achieve what I'm looking to do?

bomkallo
Joomla! Explorer
Joomla! Explorer
Posts: 251
Joined: Fri Jun 14, 2013 7:27 am
Location: Poland

Re: Hiding portions of PHP code based on user group

Post by bomkallo » Fri Jan 08, 2016 3:14 pm

The idea is straight.
1. You need to check/investigate if you can override that edit.php in admin/frontend area. I've never touched backend admin but I presume it's possible to override, same frontend.

Since the file is responsible for both edit modes it would be good to see how it acts and works. The first thing we look for is a way to do an override because we don't want to make direct changes to core itself (this is the last action you'll take).

After you override - the file is yours. Edit.php displays records/whatever, you only need to add your custom php code that will additionaly get user and his group and change the portion of code regarding to this rights.

PS: my post is valid only if edit.php is standard view template that works with !J mvc standards :)

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17431
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Hiding portions of PHP code based on user group

Post by toivo » Fri Jan 08, 2016 3:23 pm

The methods presented in the article will still work in Joomla 3.x, even though 3.x had not yet been released when the article was published in 2012.
Is there any similar approach that can achieve what I'm looking to do?
You could clone the third-party component and add the key user groups into the options of the component so that the group ids do not have to be hard coded.

On the other hand, as @bomkallo pointed out, depending on how the third party component renders its forms and if you are mainly concerned about hiding input fields and rely on default values or hidden entries in the forms, you could use template overrides and edit the default view to suit each user group.

You may also want to simplify the code that renders the form, avoid conditional statements and clone your template, allowing a different override for each user group.

Ref. https://docs.joomla.org/J3.x:How_to_use ... _Overrides

If the component saves default values into missing fields, it is possible to write a plugin, triggered by the content event onContentPrepareForm, which removes fields from the JForm definitions or overrides the XML content.
Toivo Talikka, Global Moderator


Locked

Return to “General Questions/New to Joomla! 3.x”