Our local database will store user and ward names. It will not store passwords or any other personal information for the logged-in user.
However, it will cache the ward database for a few days. For this reason we will also log a timestamp for the user and the directory cache.
Logging Into LDS.org
LoginController:
private
def test_lds_account(username, password)
require 'mechanize'
#puts "Logging in..."
# Start up our "web browser"
agent = WWW::Mechanize.new
agent.user_agent_alias = 'Mac Safari'
# Start at the main LDS.org page
begin
page = agent.get('http://lds.org/')
rescue Exception
return false
#abort "Can't connect to LDS.org. Try again later."
end
page = page.links.find {|l| l.text =~ /ward.*site/i}.click # Go to the login page
form = page.forms.find {|f| f.name =~ /log/i} # Find the login form
form.fields.find {|f| f.name =~ /user/i}.value = username # Enter the username
form.fields.find {|f| f.name =~ /pass/i}.value = password # Enter the password
page = form.submit # Try to login
# Check for a failed login
if /login/i.match(page.title)
#abort "Unable to login! Incorrect username and/or password?"
return false
end
#puts "Navigating to the membership directory..."
page = page.links.find {|l| l.text =~ /member.*directory/i}.click # Go to the membership directory page
link = page.links.find {|l| l.text =~ /photo/i} # Find the link to the version of the directory with the photos
# The actual URL for the photo directory is embedded in javascript
# in the HREF field. So we have to dig it out ourselves.
# TODO Can we pull the base of the URI from the agent (or page)
# instead of hard coding it?
photo_directory_url = 'https://secure.lds.org' + /(\/.*\.html)/i.match(link.uri.to_s)[1]
page = agent.get(photo_directory_url)
#puts "Downloading the pictures (this can take several minutes)..."
# Yes, this page actually has _two_ HTML title elements!
ward_name = page.search('/html/head/title[1]').inner_text.strip
ward_name.slice!(' Membership Directory')
return ward_name
end
Overriding the default authentication of Authlogic_Example
User.find_by_login(params[:user_session][:login])
ldsorg = Ldsorg(params[:user_session][:login], params[:user_session][:password])
if ldsorg.ldslogin
UserSession.new(@user, true)
end
Updating the progress of the directory to the user requires using a generator.
Moving from the WEBrick test environment to a production apache server
http://www.modrails.com/documentation/Users%20guide.html#_deploying_a_ruby_on_rails_application