FreakAuth Vanilla integration an auth lib and a forum engine

Derek Jones edited this page Jul 5, 2012 · 16 revisions
Clone this wiki locally

Category:Core | Category:Core::Integration | Category:Library::Authorization

Summary

If you use Freakuth light authentication API (FAL), you'll be able to integrate the Vanilla forum engine. If you already use Vanilla, you won't find a way here to insert your users in FAL's tables. But we'll be glad if you explain how you managed to do so in a week or two.

Requirements

This integration of Vanilla needs to dip into CI, CI 1.5.1 or 1.5.2, Freakuth light authentication API and Vanilla. Check out the relative forum thread

The following steps are based on this article.

Vanilla Installation

First, you will run the Vanilla installation and put all of the Vanilla tables into your existing database. In the step 3 of the Vanilla Installation Wizard, you have to set up some administrative stuff. Since we now use FAL, this admin won't be used after integration has succeeded. WARNING: if testing with a weak local mysql account with a blank password, you'll have to be sure to have this line in /conf/database.php```php $Configuration['DATABASE_PASSWORD'] = '';


#### Database Mapping

In **/conf/database.php**, add this line:```php
include  FAL_integration_database.php';

Now create the FAL_integration_database.php, where we will tell Vanilla the right table and columns.

// Vanilla-FreakAuth light Integration

// Map to the FAL user table
$DatabaseTables['User'] = 'fa_user';

// Map existing and matching FAL columns
$DatabaseColumns['User']['UserID'] = 'id';
$DatabaseColumns['User']['Name'] = 'user_name';
$DatabaseColumns['User']['Password'] = 'password';
$DatabaseColumns['User']['Email'] = 'email';

// Also map the other FAL columns
$DatabaseColumns['User']['country_id'] = 'country_id';
$DatabaseColumns['User']['role'] = 'role';
$DatabaseColumns['User']['banned'] = 'banned';
$DatabaseColumns['User']['forgotten_password_code'] = 'forgotten_password_code';
$DatabaseColumns['User']['last_visit'] = 'last_visit';
$DatabaseColumns['User']['created'] = 'created';
$DatabaseColumns['User']['modified'] = 'modified';

Other Needed Columns

Now you have to add all of the fields from the Vanilla User table, that your fa_user table doesn't already have. VANILLA (LUM_User)

+-------------------------------+---------------+------+-----+---------------------+---------------+
| Field                         | Type          | Null | Key | Default             | Extra         |
+-------------------------------+---------------+------+-----+---------------------+---------------+
| UserID                        | int(10)       |      | PRI | NULL                | auto_increment|
| RoleID                        | int(2)        |      | MUL | 0                   |               |
| StyleID                       | int(3)        |      | MUL | 1                   |               |
| CustomStyle                   | varchar(255)  | YES  |     | NULL                |               |
| FirstName                     | varchar(50)   |      |     |                     |               |
| LastName                      | varchar(50)   |      |     |                     |               |
| Name                          | varchar(20)   |      | MUL |                     |               |
| Password                      | varchar(32)   | YES  |     | NULL                |               |
| VerificationKey               | varchar(50)   |      |     |                     |               |
| EmailVerificationKey          | varchar(50)   | YES  |     | NULL                |               |
| Email                         | varchar(200)  |      |     |                     |               |
| UtilizeEmail                  | enum('1','0') |      |     | 0                   |               |
| ShowName                      | enum('1','0') |      |     | 1                   |               |
| Icon                          | varchar(255)  | YES  |     | NULL                |               |
| Picture                       | varchar(255)  | YES  |     | NULL                |               |
| Attributes                    | text          | YES  |     | NULL                |               |
| CountVisit                    | int(8)        |      |     | 0                   |               |
| CountDiscussions              | int(8)        |      |     | 0                   |               |
| CountComments                 | int(8)        |      |     | 0                   |               |
| DateFirstVisit                | datetime      |      |     | 0000-00-00 00:00:00 |               |
| DateLastActive                | datetime      |      |     | 0000-00-00 00:00:00 |               |
| RemoteIp                      | varchar(100)  |      |     |                     |               |
| LastDiscussionPost            | datetime      | YES  |     | NULL                |               |
| DiscussionSpamCheck           | int(11)       |      |     | 0                   |               |
| LastCommentPost               | datetime      | YES  |     | NULL                |               |
| CommentSpamCheck              | int(11)       |      |     | 0                   |               |
| UserBlocksCategories          | enum('1','0') |      |     | 0                   |               |
| DefaultFormatType             | varchar(20)   | YES  |     | NULL                |               |
| Discovery                     | text          | YES  |     | NULL                |               |
| Preferences                   | text          | YES  |     | NULL                |               |
| SendNewApplicantNotifications | enum('1','0') |      |     | 0                   |               |
+-------------------------------+---------------+------+-----+---------------------+---------------+

FAL (fa_user)

+-------------------------+--------------+------+-----+---------------------+----------------+
| Field                   | Type         | Null | Key | Default             | Extra          |
+-------------------------+--------------+------+-----+---------------------+----------------+
| id                      | int(11)      |      | PRI | NULL                | auto_increment |
| user_name               | varchar(45)  |      |     |                     |                |
| country_id   *          | int(11)      | YES  | MUL | NULL                |                |
| password                | varchar(50)  |      |     |                     |                |
| email                   | varchar(120) |      |     |                     |                |
| role         *          | varchar(50)  |      |     | user                |                |
| banned       *          | tinyint(1)   |      |     | 0                   |                |
| forgotten_password_code*| varchar(50)  | YES  |     | NULL                |                |
| last_visit   *          | datetime     | YES  |     | NULL                |                |
| created      *          | timestamp    | YES  |     | CURRENT_TIMESTAMP   |                |
| modified     *          | timestamp    | YES  |     | 0000-00-00 00:00:00 |                |
+-------------------------+--------------+------+-----+---------------------+----------------+

Thanks to this script:

ALTER TABLE fa_user
 ADD RoleID int(2) NOT NULL default '3',
 ADD StyleID int(3) NOT NULL default '1',
 ADD CustomStyle varchar(255) default NULL,
 ADD FirstName varchar(50) NOT NULL default '',
 ADD LastName varchar(50) NOT NULL default '',
 ADD VerificationKey varchar(50) NOT NULL default '',
 ADD EmailVerificationKey varchar(50) default NULL,
 ADD UtilizeEmail enum('1','0') NOT NULL default '0',
 ADD ShowName enum('1','0') NOT NULL default '1',
 ADD Icon varchar(255) default NULL,
 ADD Picture varchar(255) default NULL,
 ADD Attributes text NULL,
 ADD CountVisit int(8) NOT NULL default '0',
 ADD CountDiscussions int(8) NOT NULL default '0',
 ADD CountComments int(8) NOT NULL default '0',
 ADD DateFirstVisit datetime NOT NULL default '0000-00-00 00:00:00',
 ADD DateLastActive datetime NOT NULL default '0000-00-00 00:00:00',
 ADD RemoteIp varchar(100) NOT NULL default '',
 ADD LastDiscussionPost datetime default NULL,
 ADD DiscussionSpamCheck int(11) NOT NULL default '0',
 ADD LastCommentPost datetime default NULL,
 ADD CommentSpamCheck int(11) NOT NULL default '0',
 ADD UserBlocksCategories enum('1','0') NOT NULL default '0',
 ADD DefaultFormatType varchar(20) default NULL,
 ADD Discovery text,
 ADD Preferences text,
 ADD SendNewApplicantNotifications enum('1','0') NOT NULL default '0',

 ADD KEY `user_role` (`RoleID`),
 ADD KEY `user_style` (`StyleID`),
 ADD KEY `user_name` (`user_name`);

Now you have to give to the FAL superadmin the settings of the Vanilla superadmin you just created.

update fa_user set
    RoleID=(select RoleID from LUM_User where id=1),
    StyleID=(select StyleID from LUM_User where id=1),
    CustomStyle=(select CustomStyle from LUM_User where id=1),
    FirstName=(select FirstName from LUM_User where id=1),
    LastName=(select LastName from LUM_User where id=1),
    VerificationKey=(select VerificationKey from LUM_User where id=1),
    EmailVerificationKey=(select EmailVerificationKey from LUM_User where id=1),
    UtilizeEmail=(select UtilizeEmail from LUM_User where id=1),
    ShowName=(select ShowName from LUM_User where id=1),
    Icon=(select Icon from LUM_User where id=1),
    Picture=(select Picture from LUM_User where id=1),
    Attributes=(select Attributes from LUM_User where id=1),
    CountVisit=(select CountVisit from LUM_User where id=1),
    CountDiscussions=(select CountDiscussions from LUM_User where id=1),
    CountComments=(select CountComments from LUM_User where id=1),
    DateFirstVisit=(select DateFirstVisit from LUM_User where id=1),
    DateLastActive=(select DateLastActive from LUM_User where id=1),
    RemoteIp=(select RemoteIp from LUM_User where id=1),
    LastDiscussionPost=(select LastDiscussionPost from LUM_User where id=1),
    DiscussionSpamCheck=(select DiscussionSpamCheck from LUM_User where id=1),
    LastCommentPost=(select LastCommentPost from LUM_User where id=1),
    CommentSpamCheck=(select CommentSpamCheck from LUM_User where id=1),
    UserBlocksCategories=(select UserBlocksCategories from LUM_User where id=1),
    DefaultFormatType=(select DefaultFormatType from LUM_User where id=1),
    Discovery=(select Discovery from LUM_User where id=1),
    Preferences=(select Preferences from LUM_User where id=1),
    SendNewApplicantNotifications=(select SendNewApplicantNotifications from LUM_User where id=1)
    where id=1;

Auth Class

Eventually, you will need to write a new Authenticator class, to make Vanilla use your existing session management code.

In conf/settings.php, please add:

// FAL integration
$Configuration['AUTHENTICATION_MODULE'] = 'People/People.Class.FAL_Authenticator.php';
$Configuration['SAFE_REDIRECT'] = '../index.php/auth/';

The file People.Class.FAL_Authenticator.php must be created in library/People/.

<?php
/*
* Copyright 2003 Mark O'Sullivan
* This file is part of People: The Lussumo User Management System.
* Lussumo's Software Library is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
* Lussumo's Software Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with Vanilla; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
* The latest source code is available at www.lussumo.com
* Contact Mark O'Sullivan at mark [at] lussumo [dot] com
*
* This class replaces the original one
* (see "AUTHENTICATION_MODULE" configuration setting)
*
* the only function that has been changed: GetIdentity()
* some others have been removed
*
* author: Christophe Gragnic (grahack) 2007

* Applications utilizing this file: Vanilla;
*/
class Authenticator {
   var $Context;

   function Authenticator(&$Context) {
      $this->Context = &$Context;
   }

   function GetIdentity() {
    // dip into Code Igniter Framework
       require($this->Context->Configuration['APPLICATION_PATH'].'../system/cidip/cidip_index.php');
       $obj =& get_instance();
       $UserID = $obj->db_session->userdata('id');
       if ($UserID=='') $UserID = 0;

        if (!session_id()) {
            session_start();
        }

      // If it has now been found, set up the session.
      $this->AssignSessionUserID($UserID);
      return $UserID;
   }

   // All methods below this point are specific to this authenticator and
   // should not be treated as interface methods. The only required interface
   // properties and methods appear above.

   function AssignSessionUserID($UserID) {
      if ($UserID > 0) {
         @$_SESSION[$this->Context->Configuration['SESSION_USER_IDENTIFIER']] = $UserID;
      }
   }

   function LogIp($UserID) {
      if ($this->Context->Configuration['LOG_ALL_IPS']) {
         $s = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
         $s->SetMainTable('IpHistory', 'i');
         $s->AddFieldNameValue('UserID', $UserID);
         $s->AddFieldNameValue('RemoteIp', GetRemoteIp(1));
         $s->AddFieldNameValue('DateLogged', MysqlDateTime());

         $this->Context->Database->Insert($s,
            'Authenticator',
            'LogIp',
            'An error occurred while logging your IP address.',
            false); // fail silently
      }
   }

   function SetCookieCredentials($CookieUserID, $VerificationKey) {
      // Note: 2592000 is 60*60*24*30 or 30 days
      setcookie($this->Context->Configuration['COOKIE_USER_KEY'],
         $CookieUserID,
         time()+2592000,
         $this->Context->Configuration['COOKIE_PATH'],
         $this->Context->Configuration['COOKIE_DOMAIN']);
      setcookie($this->Context->Configuration['COOKIE_VERIFICATION_KEY'],
         $VerificationKey,
         time()+2592000,
         $this->Context->Configuration['COOKIE_PATH'],
         $this->Context->Configuration['COOKIE_DOMAIN']);
   }

   function UpdateLastVisit($UserID, $VerificationKey = '') {
      $s = $this->Context->ObjectFactory->NewContextObject($this->Context, 'SqlBuilder');
      $s->SetMainTable('User', 'u');
      $s->AddFieldNameValue('DateLastActive', MysqlDateTime());
      if ($VerificationKey != '') $s->AddFieldNameValue('VerificationKey', $VerificationKey);
      $s->AddFieldNameValue('CountVisit', 'CountVisit + 1', 0);
      $s->AddWhere('u', 'UserID', '', $UserID, '=');

      $this->Context->Database->Update($s,
         'Authenticator',
         'UpdateLastVisit',
         'An error occurred while updating your profile.',
         false); // fail silently
   }
}
?>

Visual Stuff Do as the Vanilla docs suggest: [quote]1) Copy the “Vanilla” folder within the root “themes” folder and paste it with your new name, like themes/your_new_theme_name. Once this is complete, you have a skeleton of the folder structure, images, and css required for a theme.

2) Copy the template file(s) you want to change into this new directory. Then change the file(s) as much as you want. Only copy the template files you need to alter.

note: since People.Class.FAL_Authenticator.php doesn't provide every function, you'll have to hide some links or buttons (like in menu.php, see below).

3) Finally, change the css and image files in the styles/default folder as necessary to work with your new templates.[/quote]

What I personnaly did: [quote]- override menu.php (removed the

and used '<?php $obj =& get_instance();echo $obj->load->view('my_menu', null, true); ?>' to catch the menu I use everywhere on my site)
  • override head.php (hardcoded the css link because of a bug (the bug consists of the css sytle sheet path being saved in the vanilla database, in PREFIX_style. If you relocate your vanilla installation path, make sure you change this path too!) change path to favicon.ico)
  • override foot.php (added my footer with the same trick than for the menu)[/quote]

What's Next

Merging FAL and Vanilla accounts of existing installations is another story...

Discussions

Forum thread