Drupal Custom Module Hook_menu not working with querystring params - drupal-7

I am sending a link out via email to users when they register that they must click and it will automatically direct and log them into the site.
I'm getting a 404 error when trying to access this link.
Example email sent out:
www.someurl.com/custom_confirm/verify?email=test#test.com&hash=somehash
My hook menu in custom_confirm looks like this:
function custom_confirm_menu(){
$items = array();
$items['custom_confirm/verify'] = array(
'title' => 'Confirming Registration',
'page callback' => 'verify_email',
'access callback' => TRUE,
);
return $items;
}
My function
function verify_email()
Is not being invoked when I hit this URL. It's giving me a 404 and not hitting any code inside the verify_email function.
Is there something wrong with this hook_menu? I don't understand why this is not working?

Related

Cakephp redirect in isAuthorized method not working

So after login in isAuthorized method I'm trying to redirect user based on a condition. But problem is it's not redirecting. Below the code that I have tried.
protected function isAuthorized($LoginUser)
{
if ($this->getTable('Users')->hasCompany($LoginUser) == false){
$this->redirect(['controller'=>'Companies','action'=>'edit']);
dd("hello");
}
}
It's not redirecting and getting hello message. How can I redirect after login user to another page based on condition ?
As mentioned in the comments, the auth component's authorization objects are supposed to return a boolean, and depending on that, let the component do the unauthorized handling-
What you could do, is for example dynamically set the component's unauthorizedRedirect option (and probably also authError) from the controller's authorization handler for that specific case (I guess you'd also have to exclude the respective company controller's action from that check, as otherwise you'll end up in an infinite redirect loop):
if (!$this->getTable('Users')->hasCompany($LoginUser)) {
$message = __('You must provide company information in order to proceed.');
$url = \Cake\Routing\Router::url([
'controller' => 'Companies',
'action' => 'add'
]);
$this->Auth->setConfig([
'authError' => $message,
'unauthorizedRedirect' => $url,
]);
return false;
}
// ...
return true;
If you find yourself in a situation where there's no such possibility, brute forcing a redirect by throwing a \Cake\Http\Exception\RedirectException could be a solution too, even though it's ideally avoided, it's better than dying, as it will at least emit a clean redirect response:
$url = \Cake\Routing\Router::url([
'controller' => 'Companies',
'action' => 'add'
]);
throw new \Cake\Http\Exception\RedirectException($url);
See also
Cookbook > Controllers > Components > AuthComponent > Configuration options

How to have different dashboards based on roles with cakedc plugins / users & acl

I am using CakeDC Users & ACL plugins in my CakePhp app. I have different roles for my users in my app and I would like to have different dashboards based on roles after login.
I extend the plugin with my own table and controller based on the documentation here, so I have MyUsersController and MyUsersTable which override the initial files of the plugin, UsersController and UsersTable. Everything works fine. I create an event in my events.php file which contains:
use CakeDC\Users\Controller\Component\UsersAuthComponent;
use Cake\Event\Event;
use Cake\Event\EventManager;
EventManager::instance()->on(
UsersAuthComponent::EVENT_AFTER_LOGIN,
['priority' => 99],
function (Event $event) {
if ($event->data['user']['role_id'] === 'bbcb3031-ebed-445e-8507-f9effb2de026') //the id of my client role{
return ['plugin' => 'CakeDC/Users', 'controller' => 'MyUsers', 'action' => 'index', '_full' => true, 'prefix' => false];
}
}
);
But it seems like the override is not working because I have an error:
Error: CakeDC/Users.MyUsersController could not be found.
In my URL I have /users/my-users instead of /my-users and I don't know why. I have test with a template file which is include in the plugin and the Users controller like this:
function (Event $event) {
if ($event->data['user']['role_id'] === 'bbcb3031-ebed-445e-8507-
f9effb2de026') //the id of role{
return ['plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile';
}
And it works. My URL redirect after login as a client is /profile.
Could someone help me to understand? Please tell me if it's not clear enough and if it's missing parts of codes that might be important to understand my problem.
I specify that I am beginner with Cake.
Your custom controller doesn't live in the CakeDC/Users plugin, hence you must disable the plugin key accordingly, so that the correct URL is being generated (assuming your routes are set up correctly) that connects to your controller, like this:
[
'plugin' => null,
'controller' => 'MyUsers',
'action' => 'index',
'_full' => true,
'prefix' => false
]
That would for example match the default fallback routes, generating a URL like /my-users.
See also:
Cookbook > Routing > Creating Links to Plugin Routes

Drupal 7 Access Callback Not Working Properly

Drupal 7 Hook_menu access callback is not returning the correct boolean.
Before we begin. YES! Cache is cleared... a lot.
I implemented a simple function for testing:
$items['tutor_review_selection'] = array(
'title' => t('example'),
'page callback' => 'my_module_example_page',
'access callback' => my_module_access( array('administrator') ),
'type' => MENU_NORMAL_ITEM
);
function my_module_access( $roles ) {
global $user;
$check = array_intersect($roles, array_values($user->roles));
return empty( $check ) ? FALSE : TRUE;
}
This returns TRUE for logged in and logged out users.
Here is the important part:
I call 'my_module_access' function in 'my_module_example_page' function and it works correctly.
Can anyone shine some light onto why this would not work in access callback?
Maybe something to do with order of operation?
Cache is cleared.
If you check the Drupal 7 hook_menu documentation you will see the following code:
function mymodule_menu() {
$items['abc/def'] = array(
'page callback' => 'mymodule_abc_view',
'page arguments' => array(1, 'foo'),
);
return $items;
}
'page callback' accepts a string, which is the callback function name. The arguments to be sent to that function are provided in the 'page arguments' array.
edit Note that you should probably create a permission and assign your roles to that permission, then check the permission instead of checking for specific roles.

drupal 7 access callback function

In drupal 7 I'm trying to give permissions of using a node if you are member of the same group as the node.
I want to use hook_menu and define my custom access check function. To this function I sent the nid as a parameter.
This is what I have got now, and I realy don't see why it is not working:
function modulename_pdf_menu() {
$items['pdf/node/%'] = array(
'page callback' => '_modulename_pdf',
'access callback' => '_modulename_pdf_access_check',
'access arguments' => array(2),
'type' => MENU_CALLBACK
);
return $items;
}
function _modulename_pdf_access_check($nid) {
echo $nid;
die();
}
I assume this should print my node id to the screen and stop. But it is still running the logic defined in _modulename_pdf. Any idea what I'm missing here?
Thanks in advance for your reply.
You are right, clear the cache and check it.

Show all regions when returning content from menu callback url

made a simple thankyou page (e.g. /product/3/thankyou) based on a menu callback in a custom module. the content shows up fine in the normal page layout but i want the regions and blocks to show up and they don't. suggestions?
// menu callback
function custom_menu() {
$items = array();
$items['product/%/thankyou'] = array(
'page callback' => 'custom_product_thankyou',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}
// theme function
function custom_theme() {
return array(
'product_review_thankyou' => array(
'variables' => array('node' => NULL),
'template' => 'product_review_thankyou',
),
);
}
// page callback
function custom_product_thankyou() {
$node = node_load(arg(1));
$output = theme('product_review_thankyou', array('node' => $node));
return $output;
}
I just tried your code in a drupal installation and i have no issues with missing blocks. Is it possible that you configured your blocks to be displayed only on certain pages?
The one block that i still couldn't get to show up (no matter what the block visibility settings were) was a 'menu block'. Problem was there wasn't a link for the thank you page in this block. So, I ended up having to add links on the configuration page with paths like product/[node_id]/thankyou, and then I disabled the links so they wouldn't be visible. Refreshed the page, and the block appeared.
To me this is a little annoying, as I kind of wanted this to be dynamic and not have to write in the product node ids. But either way, problem solved.

Resources