Index Forums Developer’s Forum Q2A & Drupal 6 integration

This topic contains 1 reply, has 2 voices, and was last updated by  MifX 1 year, 12 months ago.

  • Author
    Posts
  • #1231

    Towhid
    Keymaster

    Here is a code that I wrote(Or improved to use for drupal, can’t remember which) for Q2A’s Single Sign-On(SSO) with Drupal 6. I don’t remember how it worked, so if you used it and it had any problems let me know.
    Step 1
    in drupal open “\sites\default\setting.php” and add:

    $cookie_domain = '.example.com';

    Step 2

    now add this code to your “qa-config.php” file:

    	 // Database (for login/stats integration)
    	define('EXT_WEBADDRESS', 'http-link-to-your-drupal-homepage.com');
    
    	//define('DRUPAL_ROOT', dirname(getcwd() ).'\\');
    	define('DRUPAL_ROOT', '/drupal-directory/');
    
    	//$current_path = getcwd();
    	//define('DRUPAL_ROOT', dirname(getcwd() ).'\\');
    	chdir(DRUPAL_ROOT);
    	require_once DRUPAL_ROOT . 'includes/bootstrap.inc';
    	drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
    	//chdir($current_path);

    it should let you use authentication features in Drupal. don’t forget to set right value for DRUPAL_ROOT and EXT_WEBADDRESS.

    Step 3

    Now add this code to your “qa-external-users.php” file.

    <?php
    
      if (!defined('QA_VERSION')) { // don't allow this page to be requested directly from browser
    		header('Location: ../');
    		exit;
    	}
    
    	function qa_get_mysql_user_column_type()
    	{
    		//users > uid > int(10)
    		return 'INT';
    	}
    
    	function qa_get_login_links($relative_url_prefix, $redirect_back_to_url)
    	{
    		return array(
    			'login' => EXT_WEBADDRESS.'user/',
    			'register' => EXT_WEBADDRESS.'user/register',
    			'logout' => EXT_WEBADDRESS.'user/logout'
    		);
    	}
    
    	function qa_get_logged_in_user()
    	{	
    		global $user;
    
    		foreach($user -> roles as $key => $value)
    			if ($value=='administrator')
    				$user_is_admin=true;
    		if ($user -> uid==0)
    			return null;
    		else {
    			if ($user_is_admin)
    				$level=QA_USER_LEVEL_ADMIN;
    			else
    				$level=QA_USER_LEVEL_BASIC;
    			/*
    			elseif (current_user_can('editor'))
    				$level=QA_USER_LEVEL_EDITOR;
    			elseif (current_user_can('contributor'))
    				$level=QA_USER_LEVEL_EXPERT;
    			else
    				$level=QA_USER_LEVEL_BASIC;
    			*/
    			return array(
    				'userid' => $user->uid,
    				'publicusername' => $user->name,
    				'email' => $user->mail,
    				'level' => $level,
    			);
    		}
    	}
    
    	function qa_get_user_email($userid)
    	{
    		$thisUser = user_load(array('uid' => $userid));
    		return $thisUser->mail;
    	}
    
    	function qa_get_userids_from_public($publicusernames)
    	{
    		if (count($publicusernames))
    			return qa_db_read_all_assoc(qa_db_query_sub(
    				'SELECT name, uid FROM users WHERE name IN ($)',
    				$publicusernames
    			), 'name', 'uid');
    		else
    			return array();
    
    	}
    
    	function qa_get_public_from_userids($userids)
    	{
    		if (count($userids)) {
    			$useridtopublic=array();
    			$qa_user_emails=array();
    
    			$userfields=qa_db_read_all_assoc(qa_db_query_sub(
    				'SELECT uid, name, mail FROM users WHERE uid IN (#)',
    				$userids
    			), 'uid');
    
    			foreach ($userfields as $id => $fields) {
    				$useridtopublic[$id]=$fields['name'];
    				$qa_user_emails[$id]=$fields['mail'];
    			}
    
    			return $useridtopublic;
    
    		} else
    			return array();
    
    	}
    
    	function qa_get_logged_in_user_html($logged_in_user, $relative_url_prefix)
    	{
    		$publicusername=$logged_in_user['publicusername'];
    		return '<A HREF="'.htmlspecialchars($relative_url_prefix.'user/'.urlencode($publicusername)).
    			'" CLASS="qa-user-link">'.htmlspecialchars($publicusername).'</A>';
    	}
    
    	function qa_get_users_html($userids, $should_include_link, $relative_url_prefix)
    	{
    
    		$useridtopublic=qa_get_public_from_userids($userids);
    		$usershtml=array();
    
    		foreach ($userids as $userid) {
    			$publicusername=$useridtopublic[$userid];
    
    			$usershtml[$userid]=htmlspecialchars($publicusername);
    
    			if ($should_include_link)
    				$usershtml[$userid]='<A HREF="'.htmlspecialchars($relative_url_prefix.'user/'.urlencode($publicusername)).
    					'" CLASS="qa-user-link">'.$usershtml[$userid].'</A>';
    		}
    
    		return $usershtml;
    	}
    
    	function qa_avatar_html_from_userid($userid, $size, $padding)
    	{
    		return null; // show no avatars by default
    	}
    
    	function qa_user_report_action($userid, $action)
    	{
    
    	}
    
    /*
    	Omit PHP closing tag to help avoid accidental output
    */

     

  • #1296

    MifX
    Participant

    Hi Towhid,

    Thank you for posting the script for Q2A to Drupal SSO.

    I tried the steps as described above but got error messages on Q2A:

    Notice: Trying to get property of non-object in drupal/qa/qa-external/qa-external-users.php on line 27
    Warning: Invalid argument supplied for foreach() in drupal/qa/qa-external/qa-external-users.php on line 27
    Notice: Trying to get property of non-object in drupal/qa/qa-external/qa-external-users.php on line 30

    I am using Drupal 6.36 and Q2A 1.7. Is there a step I am missing or some setting I should setup on Drupal first (I followed the steps for Q2A SSO)? Any help or point in the right direction is greatly appreciated!

You must be logged in to reply to this topic.