user->uid alway is 1 for whatever user? - drupal

I'm new for Drupal , so I want to create a simple module that custom user profile, specically I want insert some tabs for user profile (the traditional user profile have 2 tabs "View tab" and "Edit Tab") so now I want add more 3 tabs.
Show History
Share profile
Shortcuts
but it only apply for user authenticated not admin profile (when admin show his profile ,the profile is traditional profile).
So it my code (my module's name is :tung_tab) and here is code of tung_tab.module:
function tung_tab_menu()
{
$items = array();
global $user;
//if (!user_access('administer'))
if($user->uid!=1)
{
$items['user/%/rvhistory'] = array(
'title' => 'Review History ',
'description' => 'Review History',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('tung_tab_confirm_reviewh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['user/%/sharehistory'] = array(
'title' => 'Share History',
'description' => 'Share History',
//'page callback' => 'drupal_get_form',
//'page arguments' => array('tung_tab_confirm_shareh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
$items['user/%/sc'] = array(
'title' => 'Shortcut',
'description' => 'Shortcut',
//'page callback' => 'getDescription',
//'page arguments' => array('tung_tab_confirm_shareh', 1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
}
return $items;
}
It work properlly for admin tab, but when I log out and then login with other user
but not thing is changed, so if I comment the line //if($user->uid), everthing is ok
so when I add more code in this line
'title' => 'Review History '.$user->uid,
When I'm admin It show that Review History 1
When I loged in with another account , everything is the same , Review History 1
I can't understand why? I think my code get some mistake
I put my module in "mysite/sites/default/modules/"

You shouldn't define the menu items inside the if condition. You should control the access to the menu callbacks using access callback and access arguments instead.
WRONG:
function tung_tab_menu()
{
$items = array();
global $user;
if($user->uid!=1)
{
// defined menu items
}
return $items;
}
CORRECT:
function tung_tab_menu()
{
$items = array();
global $user;
// define the menu items here...
return $items;
}
Try to fix that then see if you still get the same results.

Related

How to set permission in custom drupal module in Drupal7

I have created a custom Drupal7 form module , but I want to show the form to the authenticated users only . I want to create the module likewise that it could have check-box options in the people->permission section from where I could set the permission for this module for all type of users .Here is the menu_hook
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('access administration pages'),
'access callback' => TRUE
);
return $items;
}
I am new in drupal so any help regarding this would be appreciable.If someone could write down the hook_permission rather than giving examples then it would be a great help.
Here is the implementation of hook_permission
/**
* Implements hook_permission().
*/
function form_example_permission() {
return array(
'administer your module' => array(
'title' => t('Administer permission for your module'),
'description' => t('Some description that would appear on the permission page..'),
),
);
}
And you have to give the key of the returned array (administer your module) to access arguments in the implementation of hook_menu
So, your hook_menu implementation would become:
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access arguments' => array('administer your module'),
);
return $items;
}
Note that you'll have to flush the cache after you change anything in hook_menu. You can do it from admin/config/development/performace/
Try this. After adding this clear your cache and go to the people->permission and from there you could set permission for users .
function form_example_permission() {
return array(
'administer my module' => array(
'title' => t('Administer my module'),
'description' => t('Perform administration tasks for my module.'),
),
);
}

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 Tab in Profile

How can I add a tab into my personal profile (/users/my-name)?
I used this function, but nothuing shows up:
function tpzclassified_menu() {
$items['user/%user/kleinanzeigen'] = array(
'title' => t('Meine Kleinanzeigen'),
'page arguments' => array(1),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
You're missing the page callback property:
function tpzclassified_menu() {
$items['user/%user/kleinanzeigen'] = array(
'title' => t('Meine Kleinanzeigen'),
'page callback' => 'tpzclassified_kleinanzeigen',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'type' => MENU_LOCAL_TASK,
);
return $items;
}
function tpzclassified_kleinanzeigen($account) {
return 'This is the Meine Kleinanzeigen page';
}
Replace tpzclassified_kleinanzeigen with the function name that generates the page.
Also, never use 'access callback' => TRUE: it's a huge security hole. I've changed that to use user_view_access(), which checks to the see if the user is allowed to view %user's profile. You could use user_edit_access() if you wanted to check to see if a user is allowed to edit %user's profile.

Acces/See settings page for a custom odule

I upgraded a module from 5 to 6. I only have one problem:
I can access the settings page for it, but can not see the contents of it.
This is my code:
function agbnagscreen_menu() {
global $user;
$items = array();
if (agbnagscreen_nag($user)) {
// var_dump($_GET['q']); die();
drupal_goto(sprintf('%s/%s', AGBNAGSCREEN_NAGURL, base64_encode($_GET['q'])));
die();
}
$items['admin/settings/agbnagscreen'] = array(
// 'path' => 'admin/settings/agbnagscreen',
'title' => 'AGB nagscreen',
'access callback' => user_access('Einstellungen von AGB aendern'),
//'access' => user_access('Einstellungen von AGB aendern'),
'page callback' => 'drupal_get_form',
'callback arguments' => array('agbnagscreen_settings_fapi'),
);
$items[AGBNAGSCREEN_NAGURL] = array(
// 'path' => AGBNAGSCREEN_NAGURL,
'title' => 'Allgemeine Geschaeftsbedingungen',
'access' => TRUE,
'callback' => 'drupal_get_form',
'callback arguments' => array('agbnagscreen_fapi'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}
I think the problem is cause by this line:
'page callback' => 'drupal_get_form',
Is that correct? How can I write it, that it works?
You might want to read through the Drupal menu system (Drupal 6.x) handbook page to understand the changes to the menu system: you have several problems in your hook_menu implementation.
The conditional at the top will never fire: Drupal 6 only calls hook_menu() when the menu is rebuilt, not on every page load.
There is no callback: use page callback.
The page callback accepts page arguments, not callback arguments.
There is access: use access callback.
access callback always a string containing the function name, not a function, and defaults to "user_access": you need to supply access arguments.
A modified version of your hook_menu implementation might be:
function agbnagscreen_menu() {
$items = array();
$items['admin/settings/agbnagscreen'] = array(
'title' => 'AGB nagscreen',
'access arguments' => array('Einstellungen von AGB aendern'),
'page callback' => 'drupal_get_form',
'page arguments' => array('agbnagscreen_settings_fapi'),
);
$items[AGBNAGSCREEN_NAGURL] = array(
'title' => 'Allgemeine Geschaeftsbedingungen',
'access arguments' => array('access content'),
'page callback' => 'drupal_get_form',
'page arguments' => array('agbnagscreen_fapi'),
'type' => MENU_SUGGESTED_ITEM,
);
return $items;
}

Check if shown account is users account

I need a function for a plugin to check, if the shown account is the user's account or that from another one. I am using this function for that:
global $user; $account;
$account = user_load(array('uid' => arg(1)));
if ( $user->uid == $account->uid ) {
}
I am doing that inside a module but it does not work. When I go to my profile, I never see the output from the function.
Why?
Edit
Original context of this code:
function tpzclassified_menu() {
global $user;
$account = user_load(array('uid' => 1));
$account = user_load(array('uid' => 1));
if ($user->uid == $account1->uid) {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
}
return $items;
}
I need a function for a plugin to check, if the shown account is the user's account or that from another one.
What the reported code is doing is to verify if the logged in user is the Drupal super user (aka the user #1). If that is what you really need, then there is no need to call user_load() to load the user object of that user account. It's enough you use the following code:
global $user;
if ($user->uid == 1) {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
}
return $items;
In Drupal, there aren't two users with the same user ID, and 1 is the user ID for the Drupal super user (which is also called user #1 for the fact its user ID is 1).
The problem with this solution is that since Drupal 6, the menu callbacks are cached; conditionally adding menu callback doesn't have any effect, as the menu cache is cleared only when a new module is installed, or when a module is updated (and update.php is called). The only way to force Drupal to clear the menu cache is to use the following code:
if (!variable_get('menu_rebuild_needed', FALSE)) {
variable_set('menu_rebuild_needed', TRUE);
}
If you want to check if the current logged in user is accessing his own account you can use the following code:
function tpzclassified_menu() {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'user_view_access',
'access arguments' => array(1),
'weight' => 4,
);
return $items;
}
There is no reason to use a custom function, as user_view_access() already checks if the current user is looking his own account.
function user_view_access($account) {
return $account && $account->uid &&
(
// Always let users view their own profile.
($GLOBALS['user']->uid == $account->uid) ||
// Administrators can view all accounts.
user_access('administer users') ||
// The user is not blocked and logged in at least once.
($account->access && $account->status && user_access('access user profiles'))
);
}
Edit
Based on your comment, the issue is where you're putting this code: hook_menu() is only called when the menu is rebuilt: that's why you're not seeing anything happening.
You can't conditionally add items to the menu system like that: you have to register a menu item, then use the menu item's access callback to determine if the menu will show up for a specific user. The access callback works just like the page callback, but must return TRUE or FALSE. You might do something like this instead:
function tpzclassified_menu() {
$items['user/%user/classifieds'] = array(
'title' => 'Meine Kleinanzeigen',
'type' => MENU_LOCAL_TASK,
'page callback' => 'tpzclassified_user_page',
'page arguments' => array(1),
'access callback' => 'tpzclassified_user_access',
'access arguments' => array(1),
'weight' => 4,
);
return $items;
}
function tpzclassified_user_access($account) {
global $user;
if ($user->uid == $account->uid) {
return user_view_access($account);
}
}

Resources