One of the projects I needed to do was to create an image gallery that allowed users to login using their existing phpBB login/password. Doing it with RoR is fairly simple.
Follow up:
First thing we do is add the phpbb database entry into our database.yml file:
phpbb:
adapter: mysql
database: phpbb
username: root
password:
host: localhost
Then we create a new model that connects to the phpBB database:
class User < ActiveRecord::Base
establish_connection :phpbb
set_primary_key "user_id"
set_table_name "phpbb_users"
def self.authenticate(username, password)
user = User.find(:first, :conditions => ["username = ?", username])
if user.blank? || Digest::MD5.hexdigest(password) != user.user_password
raise "Username or password invalid"
end
user
end
end
The key lines that make this work are the first three. We first tell this model to use the phpbb entry we put in the database.yml file. Then we set the column for primary key to be user_id instead of the default id. Finally we set the table name to be phpbb_users instead of the default users.
Because phpBB uses a simple MD5 of the password, we have an authenticate method that does an MD5 of the password and compares it with what is in the database.
Now you can get an authenticated user object with the authenticate method:
myUser = User.authenticate(params[:user]["username"],params[:user]["password"])
EDIT: It has been pointed out that this quite handily breaks tests. As soon as I solve that issue, I will update.
EDIT2: See my new post on how I solved this here
What happens when you run 'rake test'? (Hint: the database connection used for User doesn't change....)
I will be working on all my test in the future and get this thing reasonably tested as pretty much all I had was the scaffold generated tests unchanged.