visible menu only for guest user in joomla - joomla

I am trying to implement a menu in joomla 1.5 which should be visible only for guest user not for registered user.
For that I made changes in two files, those are administrator/components/com_menus/models/metadata/component.xml
and
modules/mod_mainmenu/helper.php
For the first file, I have added the following code in the line no 20:
<param name="show_to_guest_only" type="radio" default="0" label="Show to Guest only" description="Show menu to guest user only.">
<option value="0">No</option>
<option value="1">Yes</option>
</param>
And for the second file, I have added the following pieces of code just before the line no 50:
$row_params = new JParameter($row->params);
if ($row_params->get('show_to_guest_only') == 1 && $user->id ){
continue;
}
But after doing this also, I am not getting the expected result.
Can you please help me how to solve this issue.
Thanks in advance.

You shouldn't hack the core files. If nothing else copy mod_mainmenu to another name like mod_guestmenu and then do your changes there and installing.
You might want to use one of the advanced module management extensions available at extensions.joomla.org
MetaMod is one that springs to mind.

Try using this. It detects whether the users is a guest and also get the items of a specific menu type.
$user =& JFactory::getApplication();
$app =& JFactory::getApplication();
$menu = $app->getMenu();
$menu_items = $menu->getItems('menutype', 'mainmenu');
if ($user->guest) {
//your code goes in here
}
Hope this helps.

Related

Download database file attachments by their real names

I can't figure out how to get a database file attachment, to be downloaded with is real file name.
My model have many file attachements (many attachOne) and there is no problem to get link to them with
{{ model.myfile.filename }}
What I want to do is to get those files downloaded with their real file name.
I try to define an ajax event handler in my layout like so :
function onDonwload()
{
$path = post('path');
$name = post('name');
// Storage::exists('uploads/public/5ce/28c/3aa/5ce27c3aae590316657518.pdf'); => OK
// Storage::exists($path); =>OK
$path = storage_path().'/app/'. $path;
return Response::download( $path, $name);
}
and
<button data-request="onDonwload"
data-request-data="path: 'uploads/public/5ce/28c/3aa/5ce27c3aae590316657518.pdf', name: 'my real name">
Download
</button>
No missing file error, but get the browser to freeze with an alert that say "A webpage slow down your browser, what do you want to do?".
Did I miss an important point?
You should add separate page for downloading files, Ajax can not help you to download file ( may be it can but process is little complex and long)
create page with /file-download/:id here you can specify any url wit param :id and give it name file-download you can give any name you like for demo i used this name.
In that Page Html section will be Blank and in Page's code section add this code. here you can also check additional security check like user is logged in or not file is of related user or not. for now i am just checking file is related to particular Modal or not.
function onStart() {
$fileId = $this->param('id');
$file = \System\Models\File::find($fileId);
// for security please check attachment_type == your model with namespace
// only then lat use download file other wise user can download all the files by just passing id
if($file && $file->attachment_type == 'Backend\Models\User') { // here add your relation model name space for comparison
$file->output('attachment');
}
else {
echo "Unauthorised.";
}
exit();
}
Generating links, please replace page name with page you created.
<a href="{{ 'file-download'|page({'id': model.myfile.id}) }}" >{{ model.myfile.filename }}</a>
Now, when you click on link file should be downloaded with its original name, and for invalid file id it should show message Unauthorised..
if any doubts please comment.

Customize #can() to show a permissions overlay for pages/sections

At work I maintain a fairly complex Laravel application which is still growing as new features are implemented and improved upon.
We have non-technical administrators in this system who manage other users permissions and sometimes it can be hard to know what permission ends up blocking a user from accessing a certain page or what might give a user too much access. Better descriptions for permissions and the ability to simulate a user to see what they have access to is already something we have done.
In addition to this we would like to toggle overlays for permissions defined in blade templates, we might defines this permissions with
#can('update', $post)
<!-- Menu button to update a $post -->
#endcan
or
#can('manage_user_roles_and_permissions')
<!-- A table with many different functions
for managing user roles + permissions -->
#endcan
Is there a way I can modify the way the #can() works in blade templates so that I can add some javascript to show a popover for where a section starts and ends, like "The permission 'Show Post' is needed for this menu button to show" or "To see the following section a user needs the 'Manage user roles and permissions' permissions". Or even better if I could add a div with a red border around the section.
How can I append additional javascript/html where #can() is used in a blade template to show an overlay.
To solve this issue I need to extend blade, see Extending Blade in the Laravel documentation.
The following is a quick test that I did just to see if this was possible. $value in this case is a string which contains the content of a blade file before being processed. So I can use preg_match_all() to find the #can statements and then append my javascript where needed. I can find the #endcan in the same way but it is harder to know which #endcan belongs to which #can but it should be fairly easy to match from this point on.
<?php
namespace App\Providers;
use Blade;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Blade::extend(function($value)
{
$can_array = array();
preg_match_all('/(\s*)#(can)\(([^\)]*)\)(\s*)/', $value, $matches);
if (count($matches) > 0 && isset($matches[3])) {
foreach ($matches[3] as $match) {
if (!in_array($match, $can_array)) {
$can_array[] = $match;
}
}
}
foreach ($can_array as $ca) {
$value = str_replace("#can(" . $ca . ")", "#can(" . $ca . ") \r\n <!-- My javascript code goes here! -->", $value);
}
// TODO need to figure out a better way to handle this
$value = str_replace("#endcan", "#endcan \r\n <!-- Section ended here -->", $value);
return $value;
});
}
...
My source code now looks like this when viewing it, goal achieved!

You are not authorised to view this resource - Joomla

I am using joomla 2.5.9 version, and I would like Joomla to redirect me to the login page if I am not logged in when i click an article which the Permission Access is for Registered only, but instead Joomla returns me this message: You are not authorised to view this resource.
And I dont see any reason why joomla by default havent made it redirect to login page.
Thanks
This doesn't answer your exact question, but I think it's a good workaround. I'm working on the same issue. My approach at the moment is to check the messages for the "not authorised" string, and then set a flag based on that. You can then check that flag anywhere in template and either redirect, or just choose to optionally show the login form.`
/* get message from app */
$app = JFactory::getApplication();
$messages = $app->getMessageQueue();
/* set login flag to 0 */
$showlogin = 0;
/* if there is a message set... */
if (isset($messages[0])) {
/* loop through messages and check for the "not authorised" string */
foreach ($messages as $msg) {
if ($msg["type"] == "error" && strpos($msg["message"], "not authorised") ) {
/* if found, update login flag */
$showlogin = 1;
}
}
}
/* include in template body - you could redirect here instead of including login form */
if ($showlogin) { ?>
<jdoc:include type="modules" name="login-form" style="none" />
<?php } ?>
`
this happens when you try to access an article which is not visible, but the category is publically visible.
Seems like it is not considered a bug, but I think its a pretty unexpected "feature".
To fix this you can edit:
joomla/components/com_content/views/article/view.html.php
// Check the view access to the article (the model has already computed the values).
if ($item->params->get('access-view') == false && ($item->params->get('show_noauth', '0') == '0'))
{
$app->enqueueMessage(JText::_('JERROR_ALERTNOAUTHOR'), 'error');
$uri = urlencode(base64_encode(JURI::getInstance()->toString()));
JFactory::getApplication()->redirect(
JRoute::_('index.php?option=com_users&view=login&return='. $uri, false)
);
return;
}
This will show the login screen and return to the article after a succesfull login.
If you dont want to edit the core file (because you want to update your system), you have to create a system plugin to override this.

Get Ext/Module Name of Form Element

In admin configuration I have a section that has groups and fields. To one of those fields I'm doing some custom stuff via the <frontend_model> in _getElementHtml()
Is there a way to retrieve the module or extension name of the current extension within that function?
<sections>
<extensionname translate="label" module="extensionname">
<label>Extension Name</label>
sorry if my question isn't very clear... I know what I'm trying to do but stumbling in how to ask.
I'm not sure if this helps me make my question more clear but here is the solution I finally came up with...
protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element){
$modules = Mage::getConfig()->getNode('modules')->children();
$modulesArray = (array)$modules;
$module = '';
foreach($modulesArray as $k => $v){
if(strpos(strtolower($k), $this->getRequest()->getParam('section')) !== false){
$module = $k;
};
}
essentially I was wanting to get the Extensions or "Modules" name so that later on in the code I could call this
Mage::getConfig()->getNode()->modules->$module->version
that way I wouldn't have to hard code in the extension or modules name it could be dynamic...
IF.... there is a better way of doing this please let me know as I am just hacking my way through this lol.

Where is Itemid used to flag active menu item in Joomla 2.5.6?

Can anyone please help me with the process flow through which the Joomla Itemid parameter ends up being the highlighted menu item?
I have embedded a 3rd party application in Joomla and by temporarily changing the php environment within the application.
I am able to get joomla html and insert the 3rd party html by replacing a token.
Simplified Code:
if ($_SERVER['REQUEST_METHOD'] == 'GET' ) {
$_SERVER['REQUEST_METHOD'] = '';
}
$_SERVER['REQUEST_URI'] = '/joomla/index.php?view=mycom&option=com_mycom&Itemid=103';
$_SERVER['SCRIPT_NAME'] = $_SERVER['PHP_SELF'] = '/joomla/index.php';
$_SERVER['QUERY_STRING'] = 'view=mycom&option=com_mycom&Itemid=103';
ob_start();
require_once '/joomla/index.php';
$joomlaHTML = ob_get_clean();
echo str_replace($replacementToken, $thirdPartyHTML, $joomlaHTML);
In v1.5.x, the menu item with ID 103 is properly highlighted but in v2.5.6, it isn't and the Home item is always highlighted. I think it used to be highlighted correctly in v1.6.x and earlier versions of 2.5.x as well but not sure.
I wanted to find how the process flows (where this is set) so I can see what tweaks I need to make.
DELETED WRONG INFO
Thanks Dayo! you saved my day with this:
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);
I don't fully understand the breadcrumb part, but I managed to get it working by editing my component's controller.php to read:
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
// force highlight the external url menu item
$Itemid = JRequest::getVar('Itemid');
$menu = JSite::getMenu();
$menu->setActive($Itemid);
// force fix the breadcrumb
$app = JFactory::getApplication('site');
$pathway =& $app->getPathway();
$bcrumbs = &JPathway::getInstance('site');
// import Joomla controller library
jimport('joomla.application.component.controller');
/**
* MyCom Component Controller
*/
class MyComController extends JController
{
}
Look in the following File
Check the
/modules/mod_menu/mod_menu.php
File and you will see two functions has been called "getActive" and "getDefault"
Which can be find in following file
/libraries/joomla/application/menu.php
I think it can be customized easily now

Resources