Hy!

I have a module where autentified users can see in the menu the link for this module, can see the list of authors from database, can add, edit or delete the a student.
I have implemented hook_search and for a student found I created a page with details. Autentified users can see this page.
I don't know how to set access permission for anonymouse users to see the details. Anonymouse users can search in site, can see the authors found, but when click a student to receive details, get a message: Access denied You are not authorized to access this page.

This is the list of permission that I've created
function listauthors_perm()
{
return array('administer listauthors','list listauthors', 'edit listauthors','details listauthors', 'add listauthors');
}

And the page with details

$items['listauthors/details'] = array(
'title' => 'Author details',
'page callback' => 'listauthors_admin_details',
'access' => user_access('administer listauthors'),
//'access arguments' => array('administer listauthors'),
'type' => MENU_CALLBACK,
'file' => 'listauthors.admin.inc',
);

In user permission from User management I set to anonymouse and autentified the "details listauthors" permission.

Can somebody explain what it's wrong?

thx

Comments

gpk’s picture

>I set to anonymouse and autentified the "details listauthors" permission.
So anonymous doesn't have the 'administer listauthors' permission ... but for menu item 'listauthors/details' you have set the access property as user_access('administer listauthors') ...

gpk
----
www.alexoria.co.uk

mihksoft’s picture

I tried to set user_access('administer listauthors') || user_access('details listauthors') for listauthors/details but doesn't work. If I set for anonymouse the 'administer listauthors' permission then the anonymouse will saw the List of authors in menu. so .. is there another solution?

gpk’s picture

Did you remember to clear the menu cache after making each change to the menu definition?

gpk
----
www.alexoria.co.uk

mihksoft’s picture

I have clear the cache and still not working.Could u tell me if I can send you the module to take a look and if yes how to send it, please?

gpk’s picture

Given that 'administer listauthors' permission is working, 'details listauthors' should work in the same way. Make sure the user_access() call uses this permission (should not be necessary to || it with the 'administer listauthors' permission), clear the menu cache, check the permissions you have set for anonymous, and it *should* work ...

gpk
----
www.alexoria.co.uk

mihksoft’s picture

/**
* Implementation of hook_menu().
*/
function listauthors_menu()
{
$items['listauthors'] = array(
'title' => 'List of authors',
'description' => "listauthors table management.",
'page callback' => 'listauthors_admin_overview',
'access' => user_access('administer listauthors'),
//'access arguments' => array('administer listauthors'),
'file' => 'listauthors.admin.inc',
);

$items['listauthors/edit'] = array(
'title' => 'Edit author',
'page callback' => 'listauthors_admin_edit',
'access' => user_access('administer listauthors'),
//'access arguments' => array('administer listauthors'),
'type' => MENU_CALLBACK,
'file' => 'listauthors.admin.inc',
);

$items['listauthors/details'] = array(
'title' => 'Author details',
'page callback' => 'listauthors_admin_details',
'access' => user_access('administer listauthors') || user_access('details listauthors'),
//'access arguments' => array('administer listauthors'),
'type' => MENU_CALLBACK,
'file' => 'listauthors.admin.inc',
);

// $items['listauthors/delete'] = array(
// 'title' => 'Delete author',
// 'page callback' => 'drupal_get_form',
// 'page arguments' => array('listauthors_admin_delete_confirm'),
// 'access arguments' => array('administer listauthors'),
// 'type' => MENU_CALLBACK,
// 'file' => 'listauthors.admin.inc',
// );

$items['listauthors/list'] = array(
'title' => 'List',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
);

$items['listauthors/add'] = array(
'title' => 'Add author',
'page callback' => 'listauthors_admin_edit',
'access' => user_access('administer listauthors'),
//'access arguments' => array('administer listauthors'),
'type' => MENU_LOCAL_TASK,
'file' => 'listauthors.admin.inc',
);

/*$items['author/%user_uid_optional'] = array(
'title' => 'Cookbook list',
'page callback' => 'author_admin_overview',
'page arguments' => array(1),
'access arguments' => array('administer author')
);*/

return $items;
}

/**
* Post-confirmation; delete an URL alias.
*/
function listauthors_admin_delete($id = 0)
{
db_query('UPDATE {cb_author} SET author_isDeleted=1 WHERE author_id = %d', $id);
drupal_set_message(t('The author has been deleted.'));
}

/**
* Set an aliased author for a given Drupal author, preventing duplicates.
*/
function listauthors_set_author($id=NULL, $firstname='', $lastname='', $dateofbirth='', $dateofdeath='', $website='', $email='', $language='', $isDeleted=0)
{
global $user;
// First we check if we deal with an existing alias and delete or modify it based on pid.
if ($id)
{
// Update the existing alias.
db_query("UPDATE {cb_author} SET author_first_name = '%s',
author_last_name = '%s',
author_date_of_birth = '%s',
author_date_of_death = '%s',
author_website='%s',
author_email='%s',
author_language_id = '%s',
author_isDeleted='%d'
WHERE author_id = %d",
$firstname, $lastname, $dateofbirth, $dateofdeath, $website, $email, $language, $isDeleted, $id);

}
else
{

// A new alias. Add it to the database.
db_query("INSERT INTO {cb_author} (author_first_name,
author_last_name,
author_date_of_birth,
author_date_of_death,
author_website,
author_email,
author_language_id,
author_enterer_id,
author_isDeleted)
VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', %d, '%d')",
$firstname, $lastname, $dateofbirth, $dateofdeath, $website, $email, $language, $user->uid, $isDeleted);
}
}

/**
* Implementation of hook_perm().
*/
function listauthors_perm()
{
return array('administer listauthors','list listauthors', 'edit listauthors','details listauthors', 'add listauthors');
}

/**
* Fetch a specific URL alias from the database.
*/
function listauthors_load($id)
{
return db_fetch_array(db_query('SELECT * FROM {cb_author} WHERE author_id = %d', $id));
}

function listauthors_search($op = 'search', $keys = null)
{
$destination = 'destination=listauthors';
switch ($op)
{
case 'name':
return t('Authors');
case 'search':
$find = array();
// Replace wildcards with MySQL/PostgreSQL wildcards.
$keys = preg_replace('!\*+!', '%', $keys);

// Administrators can also search in the otherwise private email field.
$result = pager_query("SELECT * FROM {cb_author} WHERE LOWER(author_first_name) LIKE LOWER('%%%s%%')", 15, 0, NULL, $keys, $keys);
while ($account = db_fetch_object($result))
{
$find[] = array('title' => $account->author_first_name, 'link' => url('listauthors/details/'. $account->author_id, array('absolute' => TRUE, 'query' => $destination)));
}

return $find;
}
}

function listauthors_view($node, $teaser = FALSE, $page = FALSE)
{
$node = node_prepare($node, $teaser);
$node->content['listauthors'] = array(
'#value' => theme('listauthors', $node),
'#weight' => 1,
);

return $node;
}

function listauthors_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL)
{
switch ($op)
{
case 'view':
$node->content['listauthors'] = array(
'#value' => theme('listauthors', $node->author_first_name),
'#weight' => 10,
);
break;
}
}

function listauthors_node_info()
{
return array(
'listauthors' => array(
'name' => t('Authors'),
'module' => 'listauthors',
'description' => t("This is an example node type with a few fields."),
'has_title' => TRUE,
'title_label' => t('Example Title'),
'has_body' => TRUE,
'body_label' => t('Example Body'),
)
);
}

mihksoft’s picture

function listauthors_admin_details($id = 0)
{
global $user;

$sql = "SELECT * FROM {cb_author} WHERE author_id=%d";
$result = db_query($sql, $id);

$destination = 'destination=listauthors';

while ($author = db_fetch_object($result))
{
$langName = db_fetch_object(db_query('SELECT l.name
FROM {languages} l
INNER JOIN {cb_author} a
ON l.language = a.author_language_id
WHERE a.author_id = %d',$author->author_id));
$output = "
First Name: ".$author->author_first_name;
$output .= "
Last Name: ".$author->author_last_name;
$output .= "
Date of birth: ".$author->author_date_of_birth;
$output .= "
Website: ".l(t($author->author_website),$author->author_website);
$output .= "
E-mail: ".$author->author_email;
if (user_access('edit listauthors'))
{
if ($user->uid == $author->author_enterer_id)
{
$output .= "
".l(t('edit'), "listauthors/edit/$author->author_id", array('query' => $destination));
}
}
elseif (user_access("details listauthors"))
{
$output .= "anonymouse listauthors";
}
}
return $output;
}

mihksoft’s picture

This is my module. In user management I set permission for anonymouse user to be 'details listauthors'. I cleared the cache from database, I sign out and run search for 1. I receive 2 items in the Author tab fom search. When I click one of them, i receive the message: Access denied You are not authorized to access this page.
For autentified users I mark all permissions.
So this solution doesn't work.
U can check at http://www.cookbookscollection.com/

gpk’s picture

OK it looks like you are using 6.x? I've only built modules in 5.x, but a quick look at the 6.x menu documentation http://drupal.org/node/109157 suggests that you should not be using user_access('permission_name') but just define 'access arguments' => array('permission_1, permission_2') or whatever.

HTH

gpk
----
www.alexoria.co.uk

mihksoft’s picture

I make the changes, I have cleared the cache

$items['listauthors/details'] = array(
'title' => 'Author details',
'page callback' => 'listauthors_admin_details',
'access arguments' => array('administer listauthors','details listauthors'),
'type' => MENU_CALLBACK,
'file' => 'listauthors.admin.inc',
);

And when I access a link of an author after search I received the error:

* warning: array_fill() [function.array-fill]: Number of elements must be positive in /home/cookbook/public_html/includes/database.inc on line 235.
* warning: implode() [function.implode]: Invalid arguments passed in /home/cookbook/public_html/includes/database.inc on line 235.
* warning: array_keys() [function.array-keys]: The first argument should be an array in /home/cookbook/public_html/modules/user/user.module on line 500.
* user warning: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 query: SELECT p.perm FROM role r INNER JOIN permission p ON p.rid = r.rid WHERE r.rid IN () in /home/cookbook/public_html/modules/user/user.module on line 500.

I didn't find how to set two access permission to a item menu! Maybe the syntax is wrong.

gpk’s picture

of Drupal are you using?

gpk
----
www.alexoria.co.uk

mihksoft’s picture

6.1

gpk’s picture

OK I get it, that array syntax doesn't cope with multiple permissions. The second argument to http://api.drupal.org/api/function/user_access/6 is an account to check (user object) (usually omitted, in which case the current user is used). So $account = 'details listauthors' was being interpreted as a user object, but since it isn't one, $account->roles was empty on line 500 (http://cvs.drupal.org/viewvc.py/drupal/drupal/modules/user/user.module?a...) --> lots of errors.

You have 2 options:
Just use 'details listauthors' for this menu callback and make sure you give listauthor admins this permission, or define your own access callback. See the example printer_friendly_access() near the bottom of this page http://drupal.org/node/109157.

gpk
----
www.alexoria.co.uk

mihksoft’s picture

Change //'access arguments' => array('details listauthors'), with 'access callback' => TRUE and it works :D...thanks for helping me ;)

gpk’s picture

>with 'access callback' => TRUE
This means that anyone can view the listauthor details regardless of how you set the permissions at admin/user/permissions. 'access arguments' is ignored.

gpk
----
www.alexoria.co.uk

mihksoft’s picture

Now I have another problem :))
I have 3 roles: autentified, anonymouse and superuser. In user management -> permission I set 'details listauthor' for autentified and anonymouse, and for super user is unselected, that means autentified and anonymouse can see details for authors and can search them. I entered with superuser and I make a search. The user_access('details listauthor') give me 1 for superuser and the same value for autentified and anonymouse. I suppose that is not correct. I want that superuser don't see the Author tab when search for authors, only autentified and anonymouse. I mentioned that superuser have it's own module about authors with anothers rights and link menu.
Do you have any ideea what is wrong?

mihai

gpk’s picture

All roles (other than anonymous) inherit the permissions of the authenticated user role. If you want your 'superuser' role not to have the 'details listauthor' permission then you will need to set up your roles differently.

gpk
----
www.alexoria.co.uk

mihksoft’s picture

You want to say that if I enter into site with a user that have superuser role, it will have the permissions of the autentified role not the permission of superuser role?

gpk’s picture

It will have the permissions of both. The permissions of authenticated user always get added to those of other roles.

gpk
----
www.alexoria.co.uk

mihksoft’s picture

That means that will never solve my problem, because autentified must see details and superuser must not see details :(

gpk’s picture

OK, you need to create a new role for your regular users. These users then get put in roles 'authenticated user' (automatically) and *also* 'regular user' or whatever you call it. Superusers get roles 'authenticated user' (automatically) and 'superuser'. Only role 'regular user' gets the 'details listauthors' permission.

If you want people who register on your site to automatically get the 'regular user' role then check the list of modules http://drupal.org/node/206666#DRUPAL-6. There are certainly modules to do this automatically in D5, whether they've been upgraded to D6 yet I don't know.

gpk
----
www.alexoria.co.uk