Skip to content

Commit abdeb48

Browse files
committed
init
0 parents  commit abdeb48

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+10357
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.bundle
2+
db/*.sqlite3
3+
log/*.log
4+
tmp/

.rspec

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--colour

Gemfile

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
source 'http://rubygems.org'
2+
3+
gem 'rails', '3.0.9'
4+
5+
# Bundle edge Rails instead:
6+
# gem 'rails', :git => 'git://github.com/rails/rails.git'
7+
8+
gem 'mysql2', '< 0.3'
9+
10+
# Use unicorn as the web server
11+
# gem 'unicorn'
12+
13+
# Deploy with Capistrano
14+
# gem 'capistrano'
15+
16+
# To use debugger (ruby-debug for Ruby 1.8.7+, ruby-debug19 for Ruby 1.9.2+)
17+
# gem 'ruby-debug'
18+
# gem 'ruby-debug19', :require => 'ruby-debug'
19+
20+
# Bundle the extra gems:
21+
# gem 'bj'
22+
# gem 'nokogiri'
23+
# gem 'sqlite3-ruby', :require => 'sqlite3'
24+
# gem 'aws-s3', :require => 'aws/s3'
25+
26+
# Bundle gems for the local environment. Make sure to
27+
# put test-only gems in this group so their generators
28+
# and rake tasks are available in development mode:
29+
# group :development, :test do
30+
# gem 'webrat'
31+
# end
32+
gem "haml", ">= 3.0.0"
33+
gem "haml-rails"
34+
gem "omniauth", "0.2.0"
35+
gem "rspec-rails", ">= 2.0.1", :group => [:development, :test]

Gemfile.lock

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
GEM
2+
remote: http://rubygems.org/
3+
specs:
4+
abstract (1.0.0)
5+
actionmailer (3.0.9)
6+
actionpack (= 3.0.9)
7+
mail (~> 2.2.19)
8+
actionpack (3.0.9)
9+
activemodel (= 3.0.9)
10+
activesupport (= 3.0.9)
11+
builder (~> 2.1.2)
12+
erubis (~> 2.6.6)
13+
i18n (~> 0.5.0)
14+
rack (~> 1.2.1)
15+
rack-mount (~> 0.6.14)
16+
rack-test (~> 0.5.7)
17+
tzinfo (~> 0.3.23)
18+
activemodel (3.0.9)
19+
activesupport (= 3.0.9)
20+
builder (~> 2.1.2)
21+
i18n (~> 0.5.0)
22+
activerecord (3.0.9)
23+
activemodel (= 3.0.9)
24+
activesupport (= 3.0.9)
25+
arel (~> 2.0.10)
26+
tzinfo (~> 0.3.23)
27+
activeresource (3.0.9)
28+
activemodel (= 3.0.9)
29+
activesupport (= 3.0.9)
30+
activesupport (3.0.9)
31+
addressable (2.2.4)
32+
arel (2.0.10)
33+
builder (2.1.2)
34+
diff-lcs (1.1.2)
35+
erubis (2.6.6)
36+
abstract (>= 1.0.0)
37+
faraday (0.5.7)
38+
addressable (~> 2.2.4)
39+
multipart-post (~> 1.1.0)
40+
rack (>= 1.1.0, < 2)
41+
haml (3.1.2)
42+
haml-rails (0.3.4)
43+
actionpack (~> 3.0)
44+
activesupport (~> 3.0)
45+
haml (~> 3.0)
46+
railties (~> 3.0)
47+
i18n (0.5.0)
48+
mail (2.2.19)
49+
activesupport (>= 2.3.6)
50+
i18n (>= 0.4.0)
51+
mime-types (~> 1.16)
52+
treetop (~> 1.4.8)
53+
mime-types (1.16)
54+
multi_json (0.0.5)
55+
multipart-post (1.1.2)
56+
mysql2 (0.2.11)
57+
net-ldap (0.1.1)
58+
nokogiri (1.4.7)
59+
oa-basic (0.2.0)
60+
multi_json (~> 0.0.2)
61+
nokogiri (~> 1.4.2)
62+
oa-core (= 0.2.0)
63+
rest-client (~> 1.6.0)
64+
oa-core (0.2.0)
65+
rack (~> 1.1)
66+
oa-enterprise (0.2.0)
67+
net-ldap (~> 0.1.1)
68+
nokogiri (~> 1.4.2)
69+
oa-core (= 0.2.0)
70+
pyu-ruby-sasl (~> 0.0.3.1)
71+
rubyntlm (~> 0.1.1)
72+
oa-more (0.2.0)
73+
multi_json (~> 0.0.2)
74+
oa-core (= 0.2.0)
75+
rest-client (~> 1.6.0)
76+
oa-oauth (0.2.0)
77+
multi_json (~> 0.0.2)
78+
nokogiri (~> 1.4.2)
79+
oa-core (= 0.2.0)
80+
oauth (~> 0.4.0)
81+
oauth2 (~> 0.1.1)
82+
oa-openid (0.2.0)
83+
oa-core (= 0.2.0)
84+
rack-openid (~> 1.2.0)
85+
ruby-openid-apps-discovery
86+
oauth (0.4.5)
87+
oauth2 (0.1.1)
88+
faraday (~> 0.5.0)
89+
multi_json (~> 0.0.4)
90+
omniauth (0.2.0)
91+
oa-basic (= 0.2.0)
92+
oa-core (= 0.2.0)
93+
oa-enterprise (= 0.2.0)
94+
oa-more (= 0.2.0)
95+
oa-oauth (= 0.2.0)
96+
oa-openid (= 0.2.0)
97+
polyglot (0.3.1)
98+
pyu-ruby-sasl (0.0.3.3)
99+
rack (1.2.3)
100+
rack-mount (0.6.14)
101+
rack (>= 1.0.0)
102+
rack-openid (1.2.0)
103+
rack (>= 1.1.0)
104+
ruby-openid (>= 2.1.8)
105+
rack-test (0.5.7)
106+
rack (>= 1.0)
107+
rails (3.0.9)
108+
actionmailer (= 3.0.9)
109+
actionpack (= 3.0.9)
110+
activerecord (= 3.0.9)
111+
activeresource (= 3.0.9)
112+
activesupport (= 3.0.9)
113+
bundler (~> 1.0)
114+
railties (= 3.0.9)
115+
railties (3.0.9)
116+
actionpack (= 3.0.9)
117+
activesupport (= 3.0.9)
118+
rake (>= 0.8.7)
119+
rdoc (~> 3.4)
120+
thor (~> 0.14.4)
121+
rake (0.9.2)
122+
rdoc (3.8)
123+
rest-client (1.6.3)
124+
mime-types (>= 1.16)
125+
rspec (2.6.0)
126+
rspec-core (~> 2.6.0)
127+
rspec-expectations (~> 2.6.0)
128+
rspec-mocks (~> 2.6.0)
129+
rspec-core (2.6.4)
130+
rspec-expectations (2.6.0)
131+
diff-lcs (~> 1.1.2)
132+
rspec-mocks (2.6.0)
133+
rspec-rails (2.6.1)
134+
actionpack (~> 3.0)
135+
activesupport (~> 3.0)
136+
railties (~> 3.0)
137+
rspec (~> 2.6.0)
138+
ruby-openid (2.1.8)
139+
ruby-openid-apps-discovery (1.2.0)
140+
ruby-openid (>= 2.1.7)
141+
rubyntlm (0.1.1)
142+
thor (0.14.6)
143+
treetop (1.4.9)
144+
polyglot (>= 0.3.1)
145+
tzinfo (0.3.29)
146+
147+
PLATFORMS
148+
ruby
149+
150+
DEPENDENCIES
151+
haml (>= 3.0.0)
152+
haml-rails
153+
mysql2 (< 0.3)
154+
omniauth (= 0.2.0)
155+
rails (= 3.0.9)
156+
rspec-rails (>= 2.0.1)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
== Buffset

Rakefile

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Add your own tasks in files placed in lib/tasks ending in .rake,
2+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3+
4+
require File.expand_path('../config/application', __FILE__)
5+
require 'rake'
6+
7+
Buffset::Application.load_tasks
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class ApplicationController < ActionController::Base
2+
protect_from_forgery
3+
4+
helper_method :current_user
5+
helper_method :user_signed_in?
6+
7+
private
8+
def current_user
9+
@current_user ||= User.find_by_id(session[:user_id]) if session[:user_id]
10+
end
11+
12+
def user_signed_in?
13+
return 1 if current_user
14+
end
15+
16+
def authenticate_user!
17+
if !current_user
18+
flash[:error] = 'You need to sign in before accessing this page!'
19+
redirect_to signin_services_path
20+
end
21+
end
22+
end
+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
class ServicesController < ApplicationController
2+
before_filter :authenticate_user!, :except => [:create, :signin, :signup, :newaccount, :failure]
3+
protect_from_forgery :except => :create
4+
5+
# GET all authentication services assigned to the current user
6+
def index
7+
@services = current_user.services.order('provider asc')
8+
end
9+
10+
def signin
11+
redirect_to users_path if current_user.present?
12+
end
13+
14+
# POST to remove an authentication service
15+
def destroy
16+
# remove an authentication service linked to the current user
17+
@service = current_user.services.find(params[:id])
18+
19+
if session[:service_id] == @service.id
20+
flash[:error] = 'You are currently signed in with this account!'
21+
else
22+
@service.destroy
23+
end
24+
25+
redirect_to services_path
26+
end
27+
28+
# POST from signup view
29+
def newaccount
30+
if params[:commit] == "Cancel"
31+
session[:authhash] = nil
32+
session.delete :authhash
33+
redirect_to root_url
34+
else # create account
35+
@newuser = User.new
36+
@newuser.name = session[:authhash][:name]
37+
@newuser.email = session[:authhash][:email]
38+
@newuser.services.build(:provider => session[:authhash][:provider], :uid => session[:authhash][:uid], :uname => session[:authhash][:name], :uemail => session[:authhash][:email])
39+
40+
if @newuser.save!
41+
# signin existing user
42+
# in the session his user id and the service id used for signing in is stored
43+
session[:user_id] = @newuser.id
44+
session[:service_id] = @newuser.services.first.id
45+
46+
flash[:notice] = 'Your account has been created and you have been signed in!'
47+
redirect_to root_url
48+
else
49+
flash[:error] = 'This is embarrassing! There was an error while creating your account from which we were not able to recover.'
50+
redirect_to root_url
51+
end
52+
end
53+
end
54+
55+
# Sign out current user
56+
def signout
57+
if current_user
58+
session[:user_id] = nil
59+
session[:service_id] = nil
60+
session.delete :user_id
61+
session.delete :service_id
62+
flash[:notice] = 'You have been signed out!'
63+
end
64+
redirect_to root_url
65+
end
66+
67+
# callback: success
68+
# This handles signing in and adding an authentication service to existing accounts itself
69+
# It renders a separate view if there is a new user to create
70+
def create
71+
# get the service parameter from the Rails router
72+
params[:service] ? service_route = params[:service] : service_route = 'No service recognized (invalid callback)'
73+
74+
# get the full hash from omniauth
75+
omniauth = request.env['omniauth.auth']
76+
77+
# continue only if hash and parameter exist
78+
if omniauth and params[:service]
79+
80+
# map the returned hashes to our variables first - the hashes differs for every service
81+
82+
# create a new hash
83+
@authhash = Hash.new
84+
85+
if service_route == 'facebook'
86+
omniauth['extra']['user_hash']['email'] ? @authhash[:email] = omniauth['extra']['user_hash']['email'] : @authhash[:email] = ''
87+
omniauth['extra']['user_hash']['name'] ? @authhash[:name] = omniauth['extra']['user_hash']['name'] : @authhash[:name] = ''
88+
omniauth['extra']['user_hash']['id'] ? @authhash[:uid] = omniauth['extra']['user_hash']['id'].to_s : @authhash[:uid] = ''
89+
omniauth['provider'] ? @authhash[:provider] = omniauth['provider'] : @authhash[:provider] = ''
90+
elsif service_route == 'github'
91+
omniauth['user_info']['email'] ? @authhash[:email] = omniauth['user_info']['email'] : @authhash[:email] = ''
92+
omniauth['user_info']['name'] ? @authhash[:name] = omniauth['user_info']['name'] : @authhash[:name] = ''
93+
omniauth['extra']['user_hash']['id'] ? @authhash[:uid] = omniauth['extra']['user_hash']['id'].to_s : @authhash[:uid] = ''
94+
omniauth['provider'] ? @authhash[:provider] = omniauth['provider'] : @authhash[:provider] = ''
95+
elsif ['google', 'yahoo', 'twitter', 'myopenid', 'open_id'].index(service_route) != nil
96+
omniauth['user_info']['email'] ? @authhash[:email] = omniauth['user_info']['email'] : @authhash[:email] = ''
97+
omniauth['user_info']['name'] ? @authhash[:name] = omniauth['user_info']['name'] : @authhash[:name] = ''
98+
omniauth['uid'] ? @authhash[:uid] = omniauth['uid'].to_s : @authhash[:uid] = ''
99+
omniauth['provider'] ? @authhash[:provider] = omniauth['provider'] : @authhash[:provider] = ''
100+
else
101+
# debug to output the hash that has been returned when adding new services
102+
render :text => omniauth.to_yaml
103+
return
104+
end
105+
106+
if @authhash[:uid] != '' and @authhash[:provider] != ''
107+
108+
auth = Service.find_by_provider_and_uid(@authhash[:provider], @authhash[:uid])
109+
110+
# if the user is currently signed in, he/she might want to add another account to signin
111+
if user_signed_in?
112+
if auth
113+
flash[:notice] = 'Your account at ' + @authhash[:provider].capitalize + ' is already connected with this site.'
114+
redirect_to services_path
115+
else
116+
current_user.services.create!(:provider => @authhash[:provider], :uid => @authhash[:uid], :uname => @authhash[:name], :uemail => @authhash[:email])
117+
flash[:notice] = 'Your ' + @authhash[:provider].capitalize + ' account has been added for signing in at this site.'
118+
redirect_to services_path
119+
end
120+
else
121+
if auth
122+
# signin existing user
123+
# in the session his user id and the service id used for signing in is stored
124+
session[:user_id] = auth.user.id
125+
session[:service_id] = auth.id
126+
127+
flash[:notice] = 'Signed in successfully via ' + @authhash[:provider].capitalize + '.'
128+
redirect_to root_url
129+
else
130+
# this is a new user; show signup; @authhash is available to the view and stored in the sesssion for creation of a new user
131+
session[:authhash] = @authhash
132+
render signup_services_path
133+
end
134+
end
135+
else
136+
flash[:error] = 'Error while authenticating via ' + service_route + '/' + @authhash[:provider].capitalize + '. The service returned invalid data for the user id.'
137+
redirect_to signin_path
138+
end
139+
else
140+
flash[:error] = 'Error while authenticating via ' + service_route.capitalize + '. The service did not return valid data.'
141+
redirect_to signin_path
142+
end
143+
end
144+
145+
# callback: failure
146+
def failure
147+
flash[:error] = 'There was an error at the remote authentication service. You have not been signed in.'
148+
redirect_to root_url
149+
end
150+
end
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class SessionsController < ApplicationController
2+
def callback
3+
auth # Do what you want with the auth hash!
4+
end
5+
6+
def auth; request.env['omniauth.auth'] end
7+
end

0 commit comments

Comments
 (0)