Drupal permissions are not being applied - drupal

Why are my permissions not being applied?
$items['admin/mymodule'] = array(
'page callback' => 'mymodule_admin',
'access arguments' => array("admin mymodule"),
'access callback' => TRUE,
'type' => MENU_NORMAL_ITEM,
'file' => 'mymodule.admin.inc',
'title' => 'mymodule',
);
function mymodule_perm(){
return array("admin mymodule", "earnings_report");
}
When I go to Mysite/admin/mymodule, I am able to access it WITHOUT being logged in.
The permissions on admin/user/permissions are correctly set to only give access to "site developer" and "store administrator", and the anonymous user are not part of those roles.
I tried going to /admin/content/node-settings/rebuild and /admin/build/modules, but it didn't help.
The permission "earnings_report" is working as expected, but "admin mymodule" is not.
Thanks!

The 'access callback' => TRUE, line defines who can access admin/mymodule
You've set it to always be TRUE which means it can always be accessed. I think you need to change it to something like:
'access callback' => 'user_access',
'access arguments' => array('admin mymodule'),

Related

Drupal - Access to Path for anonymous users

I have created a path with a hook_menu function which is intended to be accessed anonymous users. Definition is :
##generic access point for API calls #
items['api'] = array(
'title' => 'api access point',
'description' => 'Pass all api calls thru single access point to simplify code',
'page callback' => 'xxxxxxx_utility_api',
'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK, );
The path www.mysite.com/api/aaa/bbb/...
returns JSON
and functions as long as I am logged in, but I need to allow anonymous access to the path
How can I configure drupal to allow anonymous access to a path that returns JSON
your need to have 2 levels and to delete access arguments and next clear cache menu (drush cc menu)
items['api/custom'] = array(
'title' => 'api access point',
'description' => 'Pass all api calls thru single access point to simplify code',
'page callback' => 'xxxxxxx_utility_api',
// 'access arguments' => array('access content'),
'access callback' => TRUE,
'type' => MENU_CALLBACK, );

Drupal 7 - menu user access for logged in users?

i'm building membership website, and i want to build pages only for users that logged in.
What i'm writing in the access callback to give access only to logged in users?
'access callback' => '?'
Thank you.
You can use the user_is_logged_in() function to check if a user is logged in. Like this:
$items['custmomenu'] => array(
'title' => 'yourtitle',
'page callback' => 'yourcallback function',
'access callback' => 'user_is_logged_in',
);
You need to use the following access callback as shown below:
$items['mypage'] => array(
'title' => 'My Page',
'page callback' => 'mypage_callback',
'access callback' = > 'user_is_logged_in',
);
More info about checking if a user is logged in:
http://oliverdavies.co.uk/blog/2013/01/09/checking-if-user-logged-drupal-right-way

how to override Drupal path from another module

I want my module to override the path was set by another module
Example:
Module A has register a path:
$menu['node/%id/test'] = array(
'title' => 'Test',
'page callback' => 'test_A',
'page arguments' => array(1),
'access callback' => 'test_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
)
Now I create module B and register the same path.
$menu['node/%id/test'] = array(
'title' => 'Test',
'page callback' => 'test_B',
'page arguments' => array(1),
'access callback' => 'test_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
)
Every request to this path
www.mysite.com/node/1/test
will route to module B not A.
What is the best way to override an existing path was set by other module?
You want to use an alter hook, hook_menu_alter():
function mymodule_menu_alter(&$items) {
$items['node/%id/test']['page callback'] = 'test_B';
}
Since you're just modifying an existing menu router definition, you only need to declare the part you want to change (e.g., the page callback function name). Note also $items is passed by reference, so you don't need to return anything.

I am creating a page using hook_menu() but I get "You are not authorized to access this page."

I am trying to create a very simple page in my module using hook_menu(), but after I test it I get, "You are not authorized to access this page". I can't figure out what I am doing wrong. Following is the code that I used.
Note that I created this module under the existing module package. For instance the module folder is xyz and I created a folder as xyz_mobile for the module, and I added xyz in the info as the package. I don't know if that would have anything to do with it.
function xyz_mobile_menu() {
$items['mobile'] = array(
'title' => 'page test',
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
return $items;
}
I'm assuming Drupal 6 here. You need the 'access arguments' and 'page callback' elements in your $items array:
function mymodule_menu() {
$items = array();
$items['mobile'] = array(
'title' => 'page test',
'page callback' => 'mymodule_my_function',
'access callback' => 'user_access',
'access arguments' => array('access content'), // or another permission
'type' => MENU_CALLBACK,
);
return $items;
}
The 'access callback' element contains the name of the function (in this case, user_access) that will check if the user has the permission specified in the 'access arguments' element.
The 'page callback' element will run your custom function.
function mymodule_my_function() {
return 'this is the test page';
}
Lastly, don't forget to clear the menu cache before you re-test.

Add a tab to Drupal node

I want to add a tab to Drupal node like in the following picture:
The picture has 3 tabs, Views, CVS Instructions, Revisions. I want to add another tab "Translation". What module should I use?
The picture was taken from http://drupal.org/project/panels_tabs
Thank you.
I would create a simple small module that has a hook_menu implementing the tab.
See the example here:
http://drupal.org/node/678984
As for the rest of your implementation, I don't know what you are trying to achieve, but this will add tabs.
Not sure if this is relevant but if you are actually looking to translate node content then have you investigated the Internationalization module?
The translation tab is handled by the module "Content translation" that depends from "Locale"; once you enabled the module, you need also to set which content types can be translated, and other settings that change the way a node of that content type is translated.
Not quite what was asked, but here is code for hook_menu in a custom module that sets up an administration menu option with 2 tabs.
/***************************************************************
* hook menu
*/
function acme_viewer_setup_menu(){
$items = array();
// administration setting - call from URL
$items['admin/settings/acme_viewer_setup'] = array(
'title' => 'Acme Misc Setup - viewer and Blog', // title in Admin menu
'description' => 'Acme Misc Setup: acme viewer & Blog',
'page callback' => 'drupal_get_form', // Retrieves form 'acme_viewer_setup_admin'
'page arguments' => array('acme_viewer_setup_admin'),
'access arguments' => array('access administration pages'), // only users who can access admin pages
'type' => MENU_NORMAL_ITEM,
);
// tab 1 - viewer
$items['admin/settings/acme_viewer_setup/viewer'] = array(
'title' => 'Configure viewer', // title in tab
'page callback' => 'drupal_get_form',
'page arguments' => array('acme_viewer_setup_admin'),
'access callback' => 'user_access',
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
// tab 2 - blog
$items['admin/settings/acme_viewer_setup/blog'] = array(
'title' => 'Configure Blog', // title in tab
'page callback' => 'drupal_get_form',
'page arguments' => array('blog_setup_admin'),
'access callback' => 'user_access',
'access arguments' => array('access administration pages'),
'type' => MENU_LOCAL_TASK,
);
return $items;
}

Resources