Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
117 lines (77 sloc) 3.03 KB
== About
This is a port of the standard LoginGenerator to use OpenID for
authentication. It is distributed with the Ruby OpenID library.
Read more at:
* http://openidenabled.com
* http://openidenabled.com/openid/libraries/ruby
* http://openid.net
== Installation
If you are reading this, then you have installed the openid_login
system, but there are still a few things you have to do
manually. First open your app/controllers/application.rb and add
require_dependency "openid_login_system"
to the top of the file and include the login system with
include OpenidLoginSystem
The beginning of your ApplicationController.
It should look something like this :
require_dependency "openid_login_system"
class ApplicationController < ActionController::Base
include OpenidLoginSystem
model :user
After you have done the modifications the the AbstractController you can import
the user model into the database. This model is meant as an example and you
should extend it.
The model :user is required when you are hitting problems to the degree of
"Session could not be restored becuase not all items in it are known"
== Requirements
You need a database table corresponding to the User model.
mysql syntax:
CREATE TABLE users (
id int(11) NOT NULL auto_increment,
openid_url varchar(256) default NULL,
PRIMARY KEY (id)
);
 
postgres :
CREATE TABLE "users" (
 "id" SERIAL NOT NULL UNIQUE,
 "openid_url" VARCHAR(256),
 PRIMARY KEY("id")
) WITH OIDS;
sqlite:
CREATE TABLE 'users' (
'id' INTEGER PRIMARY KEY NOT NULL,
'openid_url' VARCHAR(256) DEFAULT NULL
);
Of course your user model can have any amount of extra fields. This is just a
starting point
== How to use it
Now you can go around and happily add "before_filter :login_required" to the
controllers which you would like to protect.
After integrating the login system with your rails application
navigate to your new controller's login method. There you may login
which will create a new User object if you've never logged in
before. After you are done you should have a look at your DB, and
you'll see the record for your User with the openid_url you entered.
== Tips & Tricks
How do I...
... access the user who is currently logged in
A: You can get the user id from the session using @session[:user_id]
Example:
@session[:user_id]
To get the User object:
user = User.find(@session[:user_id])
The OpenidController also has a find_user method
which will return the User object of the logged in user, or nil
if no user is logged in.
... restrict access to only a few methods?
A: Use before_filters build in scoping.
Example:
before_filter :login_required, :only => [:myaccount, :changepassword]
before_filter :login_required, :except => [:index]
... check if a user is logged-in in my views?
A: @session[:user_id] will tell you. Here is an example helper which you can use to make this more pretty:
Example:
def user?
!@session[:user_id].nil?
end