How to logout and display message after login under certain condition? - drupal

Given a Drupal 7 website I want to customize log in behavior: When a users logs in I want to check if they are in a blacklist. If they are in that list I want them to be automatically logged out and told about the reason they are being kicked out. So In one of the custom modules of this drupal I have add the following hook:
function mymodule_user_login(&$edit, $account) {
if(blacklist(&$edit, $account)) {
drupal_goto("/user/logout/");
drupal_set_message('Acces denied','error');
}
}
However, my code doesn't work. What I'm doing wrong? I'm sure the hook is executed because I checked this using a watchdog. However, I also discovered the hook only gets executed if admin is the user who is logging in.
Are there any alternatives (maybe using Context module)? Any suggestions would be aprreciated!
Thanks!

Your code does not work because drupal_goto() calls drupal_exit() so the rest of your script does not get executed.
What you are trying to do will not work because user_logout() itself calls drupal_goto() to the front page.
You can however add an extra validation callback in the user login form so you can prevent users from logging in altogether.
<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch ($form_id) {
case 'user_login':
case 'user_login_block':
$form['#validate'][] = 'mymodule_custom_user_validation',
break;
}
}
function mymodule_custom_user_valudation($form, &$form_state) {
if (// Add your blacklist conditons here. ) {
form_set_error('', t('Your account is blacklisted, therefore you cannot login to this account'))
}
}
?>

If your interested in using the Login Destination Module, then you could create some type of rule to hook into your blacklist check and then redirect that user to the "user/logout" path. Let me know if you have any questions.
What exactly is your "blacklist()" function doing?

Related

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

How can I redirect a Drupal user after they create new content

I want to redirect my users after they create a piece of new content, instead of directing them to the 'view' page for the content.
I have seen mention of using hook_alter or something, but I'm really new to drupal and not even sure what that means?
Thanks!
As you mention that you are new to Drupal, I'd suggest to take a look at the Rules module. You can add a trigger on for content has been saved/updated and add an action, to redirect the user to a specific page.
You can however do the same in a light weight custom module using a form_alter hook.
First, find the form ID of the form. For node forms, it's the [node-type]_node_form.
Then, you can add a new submit function to be executed when the form is submitted. In this submit handler, set the redirect path.
See this guide on a basic how-to on creating a module.
Your module code would be something like belows:
<?php
function mymodule_mytype_node_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'mymodule_node_submit_do_redirect';
}
function mymodule_node_submit_do_redirect($form, &$form_state) {
$form_state['redirect'] = 'my_custom_destination';
}
A much much simpler approach is to set the destination in the node form's URL.
For example, if you opened http://example.com/node/add/mytype?destination=my_custom_destination, you will be redirected to that URL instead of the node view page.
This works for me using Drupal 7, after creating a new module!
Put into the .module file the following PHP code:
<?php
function custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == '[node-type]_node_form') {
$form['actions']['submit']['#submit'][]= 'my_custom_submit_handler';
}
}
function my_custom_submit_handler($form, &$form_state) {
$form_state['redirect'] = 'http://www.expamle.eu/?q=node/2';
}
You just need to change [node-type]_node_form with your node type name (e.g.: example_node_form) and the http://www.expamle.eu/?q=node/2 with the correct URL.

Session Login in wordpress

I have a site www.example.com and a wordpress blog at www.example.com/blog.
I want my user to login at www.example.com and $_SESSION['USERNAME'] is passed to www.example.com/blog.
I just want to know, how can i automatically login the end user to wordpress blog, once they login to my main site.
For now i am passing $_SESSION['USERNAME'] and using a plugin external database login, which I have linked to my main site database.
Any function that is used to login to wordpress using session username will be helpful.
Wordpress don't use $_SESSIONyou need to code a plugin or to add some code to your functions.php to do this - cf http://www.frank-verhoeven.com/using-session-in-wordpress/
You should add something like this :
function init_sessions() {
if (!session_id()) {
session_start();
}
}
add_action('init', 'init_sessions');
NB: there is also a plugin to manage Sessions with Wordpress, maybe you can use/hack this http://wordpress.org/plugins/wp-session-manager/
I have been reading on this. Here is what I have found as I am in the same issue. In your wp-config.php file at the top add:
add_action('init', 'myStartSession', 1);
function myStartSession() {
if(!session_id()) {
session_start();
}
}
HOWEVER, " The data stored in the session doesn’t go away when the user logs out or logs into a different account. For that you need to destroy the session. ..." (exerpt from this nice write up). It explains you also need to:
function myEndSession() {
session_destroy ();
}
if you can get wp user_id you could do something like this:
function init_sessions() {
if (user_logged_on_site) {
#session_start();
$user_id = $_SESSION['wp_user_id']; //this has to be set when the user logs on your website
wp_set_auth_cookie( $user_id); //log in the user on wordpress
}
}
add_action('init', 'init_sessions');
Documentation on wp_set_auth_cookie http://codex.wordpress.org/Function_Reference/wp_set_auth_cookie

Drupal - page route - content profile

I can't seem to get the page route module to move to the next page, after it creates the profile page.
1. I created a route called Registration
2. Inside the route I have two pages,
a) Content Profile Editing Form
b) Node Add form
The page route should take the user to the profile create page, and then route to the create a group node page.
Problem is after the user is directed to the content profile editing form and clicks next, it redirects back to the profile form instead of going to the next page.
Any ideas, this does not seem normal at all.
Charles
thanks for the help. I found the solution. I thought it might be a module or a coding change I have made so I decided to create a new drupal install and only install the modules needed.
I still had the same problem, very strange indeed. I eventually found the this article, with someone whom has had the same issues.
http://drupal.org/node/699458
The following needs to be added to the pageroute.module
<?php
/**
* Submit function for all pageroute forms, except submit-like tab buttons
* Redirect to the set target.
*/
function pageroute_page_form_submit($form, &$form_state) {
$page = &$form_state['page'];
$route = &$form_state['storage']['route'];
/* hack saturnino part */
if(!empty( $page->options['neighbours']['forward']) )
{
drupal_redirect_form($form, $route->path.'/'.$page->options['neighbours']['forward']);
return;
}
/* hack saturnino part */
// no page access -> try redirect
if (!$route->checkPageAccess($page->name, $form_state['target'])) {
unset($form_state['storage']);
$form_state['rebuild'] = FALSE;
if ($route->options['redirect_path']) {
drupal_redirect_form($form, pageroute_get_redirect_path($page));
return;
}
drupal_not_found();
pageroute_exit_now();
return;
}
$form_state['rebuild'] = TRUE;
}
?>
thanks for your help

How to redirect user to a specific page after they login if they belong to a certain role?

We have certain users in our member list that have a role "vendor" attached to them. All such members are to be redirected to a certain page upon login. How can this be accomplished?
There is more than one way to skin this cat...
This is my preferred Drupal 7 method:
function hook_user_login(&$edit, $account) {
$edit['redirect'] = 'node/123';
}
For Drupal 7
Action --> admin/config/system/actions - Redirect to URL
then enable your trigger module
Trigger --> /admin/structure/trigger/node
if your are trying to login redirect just follow this(select user tab in the page)
go to --> admin/structure/trigger/user
then
Trigger: After a user has logged in
choose an action -->Redirect to URL and assign.
Then clear the cache.
It will work for you!
You can define actions and triggers in Drupal:
Action(admin/settings/actions)
- Redirect to a specific page
Trigger (admin/build/trigger/user)
- After user has logged in
Try this.
EDIT (see comments):
Create a small module to check on a user's login process what role he has and then redirect if neccesary.
drupal_goto => redirect-function in drupal
hook_user =>triggers on user operations
And for the user's roles:
GLOBAL $user;
$roles = $user->roles;
$vendor = in_array('vendor', $roles);
$vendor then holds a true/false value will decide to redirect or not.
If you don't know how to do this, just post here and I'll write the module for you. But this would be a good practice for writing future drupa modules for you perhaps. :)
There are 2 ways in DRUPAL 7
1) Using action and trigger
see this http://drupal.org/node/298506
2)if using custom module
function YOURMODULE_user_login(&$edit, $account) {
if (!isset($_POST['form_id']) || $_POST['form_id'] != 'user_pass_reset' || variable_get('login_destination_immediate_redirect', FALSE)) {
if(in_array('THE-ROLE-WANTED-TO-REDIRECT',$account->roles)):
drupal_goto('PATH');
else: drupal_goto('user/'.$account->uid);
endif;
}
}
You can use Rules
Events: User has logged in.
Condition: User has role
Actions: Page redirect
There are modules that do this (besides Trigger+Actions), such as LoginDestination: http://drupal.org/project/login_destination. This Drupal forum post has a bit more info about it as well.
following condition for hook_user
if($op =='login') drupal_goto("your path");
This can be achieved by using a combination of content access and login toboggan modules. You'll be able to restrict pages and prompt user to login to access them.
First set the conditions in form preprocess (for example I want to redirect only users that logged in using the form at node page)
function YOURMODULE_form_user_login_alter(&$form, &$form_state, $form_id)
{
$pathArguments = explode('/', current_path());
if (count($pathArguments) == 2 && $pathArguments[0] === 'node' && is_numeric($pathArguments[1])) {
$form_state['nodepath'] = current_path();
}
}
than define redirect:
function YOURMODULE_user_login(&$edit, $account)
{
if (isset($edit['nodepath']) && !empty($edit['nodepath'])) {
drupal_goto($edit['nodepath']);
}
}

Resources