|
| 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 |
0 commit comments