Skip to content

Commit

Permalink
first import
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason Cale committed Sep 11, 2009
0 parents commit 09bb725
Show file tree
Hide file tree
Showing 17 changed files with 462 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "vendor/sinatra"]
path = vendor/sinatra
url = git://github.com/sinatra/sinatra.git
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace :ecomo-wizzards do


end
8 changes: 8 additions & 0 deletions config.ru
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# To use with thin
# thin start -p PORT -R config.ru

require File.join(File.dirname(__FILE__), 'lib', 'ecomo-wizzards.rb')

disable :run
set :environment, :production
run EcomoWizzard
1 change: 1 addition & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
---
Binary file added db/development.db
Binary file not shown.
206 changes: 206 additions & 0 deletions lib/authinabox.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
# NAME: authinabox
# VERSION: 1.01 (Dec 27, 2008)
# AUTHOR: Peter Cooper [ http://www.rubyinside.com/ github:peterc twitter:peterc ]
# DESCRIPTION: An "all in one" Sinatra library containing a User model and authentication
# system for both session-based logins OR HTTP Basic auth (for APIs, etc).
# This is an "all in one" system so you will probably need to heavily tailor
# it to your own ideas, but it will work "out of the box" as-is.
# COMPATIBILITY: - Tested on 0.3.2 AND the latest rtomayko Hoboken build! (recommended for the latter though)
# - NEEDS DataMapper!
# - Less work needed if you use initializer library -- http://gist.github.com/40238
# (remember to turn sessions on!!)
# LICENSE: Use for what you want, just don't claim full credit unless you make significant changes
#
# INSTRUCTIONS: To come in full later..
# Basically, require in lib/authinabox from your Sinatra app
# Tie up login, logout, and signup methods as shown in example at bottom of this file
# Use current_user, login_required, etc, from your app (as shown in example)
# If you do NOT want .json, .xml, etc, requests going to HTTP Basic auth, head down to line 200.



# ====== DEFAULT OPTIONS FOR PLUGIN ======
module Sinatra
module Plugins
module AuthInABox
OPTIONS = {
:login_url => '/login',
:logout_url => '/logout',
:signup_url => '/signup',
:after_signup_url => '/dashboard',
:after_logout_url => '/',
:template_language => :erb
}
end
end
end


# ====== USER MODEL ======

# APP_ROOT/models/user.rb



# ====== LOGIC ======

module Sinatra
module Plugins
module AuthInABox
# ====== CONTROLLERS AND VIEWS ======

# Present login screen (these are really last resorts, you should code your own and call them from your app!)
def render_login
if Plugins::AuthInABox::OPTIONS[:template_language] == :haml
haml clean(<<-EOS)
%form{ :method => "post" }
%label
username or e-mail:
%input{ :id => "user_username", :name => "username", :size => 30, :type => "text" }
%label
password:
%input{ :id => "user_password", :name => "password", :size => 30, :type => "password" }
%input{ :type => "submit", :value => "login" }
EOS
else
erb clean(<<-EOS)
<form method='post'>
<label>
username or e-mail:
</label>
<input id='user_username' name='username' size='30' type='text' />
<label>
password:
</label>
<input id='user_password' name='password' size='30' type='password' />
<input type='submit' value='login' />
</form>
EOS
end
end

# Log in
def login
if user = User.authenticate(params[:username], params[:password])
session[:user] = user.id
redirect_to_stored
else
redirect Plugins::AuthInABox::OPTIONS[:login_url]
end
end

# Log out and delete session info
def logout
session[:user] = nil
redirect Plugins::AuthInABox::OPTIONS[:after_logout_url]
end

# Present signup page
def render_signup
if Plugins::AuthInABox::OPTIONS[:template_language] == :haml
haml clean(<<-EOS)
%form{ :action => "#{Plugins::AuthInABox::OPTIONS[:signup_url]}", :method => "post" }
%label
username:
%input{ :id => "user_username", :name => "username", :size => 30, :type => "text" }
%label
email:
%input{ :id => "user_email", :name => "email", :size => 30, :type => "text" }
%label
password:
%input{ :id => "user_password", :name => "password", :size => 30, :type => "password" }
%label
confirm:
%input{ :id => "user_password_confirmation", :name => "password_confirmation", :size => 30, :type => "password" }
%input{ :type => "submit", :value => "sign up" }
EOS
else
erb clean(<<-EOS)
<form action='#{Plugins::AuthInABox::OPTIONS[:signup_url]}' method='post'>
<label>
username:
</label>
<input id='user_username' name='username' size='30' type='text' />
<label>
email:
</label>
<input id='user_email' name='email' size='30' type='text' />
<label>
password:
</label>
<input id='user_password' name='password' size='30' type='password' />
<label>
confirm:
</label>
<input id='user_password_confirmation' name='password_confirmation' size='30' type='password' />
<input type='submit' value='sign up' />
</form>
EOS
end
end

def signup
@user = User.new(:email => params[:email], :username => params[:username], :password => params[:password], :password_confirmation => params[:password_confirmation])
if @user.save
session[:user] = @user.id
redirect Plugins::AuthInABox::OPTIONS[:after_signup_url]
else
puts @user.errors.full_messages
redirect Plugins::AuthInABox::OPTIONS[:signup_url]
end
end


# ====== HELPERS ======
helpers do
def login_required
if session[:user]
return true
elsif request.env['REQUEST_PATH'] =~ /(\.json|\.xml)$/ && request.env['HTTP_USER_AGENT'] !~ /Mozilla/
@auth ||= Rack::Auth::Basic::Request.new(request.env)
if @auth.provided? && @auth.basic? && @auth.credentials && User.authenticate(@auth.credentials.first, @auth.credentials.last)
session[:user] = User.first(:username => @auth.credentials.first).id
return true
else
status 401
halt("401 Unauthorized") rescue throw(:halt, "401 Unauthorized")
end
else
session[:return_to] = request.fullpath
redirect Plugins::AuthInABox::OPTIONS[:login_url]
pass rescue throw :pass
end
end

def admin_required
return true if login_required && current_user.account_type == 'admin'
redirect '/'
end

def current_user
User.get(session[:user])
end

def redirect_to_stored
if return_to = session[:return_to]
session[:return_to] = nil
redirect return_to
else
redirect '/'
end
end

# Cleans indentation for heredocs
def clean(str); str.gsub(/^\s{#{str[/\s+/].length}}/, ''); end
end

end
end
end

# Little hack to make inclusion work with both Sinatra 0.3.2 and latest experimental builds
(Sinatra::Base rescue Sinatra::EventContext).send(:include, Sinatra::Plugins::AuthInABox)

# Get database up to date
# DataMapper.auto_upgrade!
54 changes: 54 additions & 0 deletions lib/ecomo-wizzards.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

require 'rubygems'
$:.unshift File.join(APP_ROOT, 'vendor', 'sinatra', 'lib')
require 'sinatra'
require 'lib/initializer'
require 'lib/authinabox'

class EcomoWizzard < Sinatra::Application

set :root, APP_ROOT

get '/' do
haml :index
end

get '/login' do
render_login # or render your own equivalent!
end

post '/login' do
login
end

get '/signup' do
render_signup # or render your own equivalent!
end

post '/signup' do
signup
end

get '/logout' do
logout
end


# SEARCHING STUFF

get '/dashboard' do
login_required
haml :dashboard
end


# get '/api.json' do
# login_required
# content_type "text/json"
# "{ 'a': 'b' }"
# end


end

59 changes: 59 additions & 0 deletions lib/initializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# NAME: initializer
# VERSION: 1.0
# AUTHOR: Peter Cooper [ http://www.rubyinside.com/ github:peterc twitter:peterc ]
# DESCRIPTION: Sinatra library to perform initialization functions - oriented around DataMapper use
# COMPATIBILITY: All, in theory - tested on Hoboken
# LICENSE: Use for what you want
#
# INSTRUCTIONS:
# 1. Ensure _this_ file is lib/initializer.rb within your app's directory structure
# 2. Read through and customize this file to your taste and your app's requirements
# 3. Add require 'lib/initializer' to your Sinatra app


# Add the current app's /lib folder to the load path for convenience
$:.unshift('lib')

# Load any gems required for the app - database drivers, etc..
require 'rubygems'
require 'datamapper'
require 'dm-core'
require 'dm-timestamps'
require 'dm-validations'
require 'fileutils'
require 'haml'

# Establish base directory names
DATABASE_DIR = File.join(APP_ROOT, "db")

# If the db directory (for SQLite databases) doesn't exist, create it
FileUtils.mkdir(DATABASE_DIR) unless File.directory?(DATABASE_DIR)

# Establish environments and connect to database
configure :development do
# Turn on logging for DataMapper when in development environment
DataMapper::Logger.new(STDOUT, :debug)
DataMapper.setup(:default, "sqlite3://" + File.join(DATABASE_DIR, "development.db"))
puts File.join(DATABASE_DIR, "db", "development.db")
end

configure :production do
DataMapper.setup(:default, "sqlite3://" + File.join(DATABASE_DIR, "production.db"))
end

configure :test do
DataMapper.setup(:default, "sqlite3://" + File.join(DATABASE_DIR, "test.db"))
end


# Load plugins, if any
Dir[APP_ROOT + '/plugins/**/*.rb'].each { |plugin| load plugin } if File.directory?(File.join(APP_ROOT, "plugins"))

# Load models, if any
Dir[APP_ROOT + '/models/**/*.rb'].each { |model| load model } if File.directory?(File.join(APP_ROOT, "models"))

# Upgrade database schema from models
DataMapper.auto_upgrade!

# Enable sessions
enable :sessions
12 changes: 12 additions & 0 deletions models/item.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Item
include DataMapper::Resource

property :id, Serial, :writer => :protected, :key => true

property :title, String, :nullable => false
property :description, Text
property :created_at, DateTime

belongs_to :listing_source

end
15 changes: 15 additions & 0 deletions models/listing_source.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'uri'

class ListingSource
include DataMapper::Resource

property :id, Serial, :writer => :protected, :key => true
property :base_uri, String, :nullable => false

has n, :items

def permalink_for(item)
URI.join(base_uri, item.to_param)
end

end
Loading

0 comments on commit 09bb725

Please sign in to comment.