<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Matt Fawcett : Tag auth, everything about auth</title>
    <link>http://matthewfawcett.co.uk</link>
    <atom:link type="application/rss+xml" rel="self" href="http://matthewfawcett.co.uk/tag/auth.rss"/>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>Use a PHPBB forum to handle user authentication in your rails app</title>
      <description>&lt;p&gt;Im currently in the process of rebuilding &lt;a href="http://www.realalehunter.co.uk"&gt;www.realalehunter.co.uk&lt;/a&gt; in rails. Its currently uses PHPBB to handle logging in and authentication so to make the rebuild simpler I thought keep it working in the same way instead of trying to migrate all the users/passwords to a separate system.&lt;/p&gt;


&lt;p&gt;So I wrote a rails gem / plugin to make this a bit easier. It currently works on Rails &amp;gt; 2.3.3. I decided that I still wanted to have a users table in my applications database to make relationships easier but I would just store the stuff that&amp;#8217;s important to the app itself (not passwords etc). So if you visit the rails site and are logged in to the forum a user will also be created in your rails app database if one does not exist.&lt;/p&gt;


&lt;p&gt;So here is how to get started:&lt;/p&gt;


&lt;p&gt;&lt;div class="CodeRay"&gt;&lt;pre&gt;# install the gem
gem install mattfawcett-phpbb-auth&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;p&gt;Tell rails that you want to use the gem it by adding the following to your environment.rb&lt;/p&gt;


&lt;p&gt;&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;config.gem &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;mattfawcett-phpbb-auth&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:lib&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;phpbb_auth&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;, &lt;span class="sy"&gt;:source&lt;/span&gt; =&amp;gt; &lt;span class="s"&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;span class="k"&gt;http://gems.github.com&lt;/span&gt;&lt;span class="dl"&gt;&amp;quot;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;


&lt;br/&gt;


&lt;p&gt;Add an entries to database.yml for phpbb_database_development, phpbb_database_production and  phpbb_database_development.&lt;/p&gt;


&lt;p&gt;In your config directory create a file called phpbb_auth_settings.rb, copy whats below into the file and change to  match your setup.&lt;/p&gt;


&lt;p&gt;
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="co"&gt;PHPBB_AUTH_FORUM_DATABASE_TABLE_PREFIX&lt;/span&gt; = &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;phpbb_&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;
&lt;span class="co"&gt;PHPBB_AUTH_COOKIE_NAME&lt;/span&gt; = &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;phpbb3_7uah4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt;; 
&lt;span class="co"&gt;PHPBB_AUTH_LOCAL_USER_MODEL_NAME&lt;/span&gt; =&lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;User&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="c"&gt;#this is the name of the model that you will use to store information about  users in your rails app &lt;/span&gt;
&lt;span class="co"&gt;PHPBB_AUTH_REMOTE_REMOTE_IDENTIFIER&lt;/span&gt; = &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;user_email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="c"&gt;#I use email to identify somebody, you may want to use the username  instead &lt;/span&gt;
&lt;span class="co"&gt;PHPBB_AUTH_REMOTE_LOCAL_IDENTIFIER&lt;/span&gt; = &lt;span class="s"&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="k"&gt;email&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;/span&gt; &lt;span class="c"&gt;#this is the name of a field on your local user model that will be used to  lookup the value of remote identifier.&lt;/span&gt;
&lt;span class="co"&gt;PHPBB_AUTH_COLUMNS_TO_DUPLICATE&lt;/span&gt; = {&lt;span class="sy"&gt;:user_website&lt;/span&gt; =&amp;gt; &lt;span class="sy"&gt;:website&lt;/span&gt;} &lt;span class="c"&gt;#specify any additional fields that you would like copying  to your local user model &lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;




&lt;p&gt;In application_controller.rb, include the module&lt;/p&gt;


&lt;p&gt;&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;include &lt;span class="co"&gt;PhpbbAuth&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;




&lt;p&gt;How you handle the rest is up to you but here&amp;#8217;s what I did. Create a method called set_current_user.&lt;p&gt;
&lt;p&gt;
&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;&lt;span class="r"&gt;def&lt;/span&gt; &lt;span class="fu"&gt;set_current_user&lt;/span&gt;   
  &lt;span class="iv"&gt;@current_user&lt;/span&gt; = current_user 
&lt;span class="r"&gt;end&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;
&lt;/p&gt;

&lt;p&gt;Add a before filter to the controller to run the method.&lt;/p&gt;
&lt;p&gt;&lt;div class="CodeRay"&gt;&lt;pre&gt;&lt;span class="CodeRay"&gt;before_filter &lt;span class="sy"&gt;:set_current_user&lt;/span&gt; &lt;/span&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/p&gt;
&lt;p&gt;You will then have access to the @current_user variable in your controllers and views. This will be either nil if the  user is not logged in, or an instance of your local User.&lt;/p&gt;
&lt;p&gt;The source is up on &lt;a href="http://github.com/mattfawcett/phpbb-auth/tree/master"&gt;Github&lt;/a&gt;.&lt;/p&gt;
</description>
      <pubDate>Mon, 31 Aug 2009 12:12:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b41b76c2-51b4-43a4-9995-2801fe3616a6</guid>
      <comments>http://matthewfawcett.co.uk/2009/08/31/use-a-phpbb-forum-to-handle-users-authentication-in-your-rails-app#comments</comments>
      <category>ruby</category>
      <category>rails</category>
      <category>phpbb</category>
      <category>authentication</category>
      <category>auth</category>
      <category>users</category>
      <link>http://matthewfawcett.co.uk/2009/08/31/use-a-phpbb-forum-to-handle-users-authentication-in-your-rails-app</link>
    </item>
  </channel>
</rss>

