File #ubfirz26-7741 - RAILS - Sourcecode

Uploaded by AuthServicesController problem - 22/06/2012 11:52 - 104 Views
Source code
  1. class ServicesController < ApplicationController
  2.   before_filter :authenticate_user!, :except => [:create]
  3.  
  4. def index
  5.   # get all authentication services assigned to the current user
  6.   @services = current_user.services.all
  7. end
  8.  
  9. def destroy
  10.   # remove an authentication service linked to the current user
  11.   @service = current_user.services.find(params[:id])
  12.   @service.destroy
  13.  
  14.   redirect_to services_path
  15. end
  16.  
  17. def create
  18.   # get the service parameter from the Rails router
  19.   params[:service] ? service_route = params[:service] : service_route = 'no service (invalid callback)'
  20.  
  21.   # get the full hash from omniauth
  22.   omniauth = request.env['omniauth.auth']
  23.  
  24.   # continue only if hash and parameter exist
  25.   if omniauth and params[:service]
  26.  
  27.     # map the returned hashes to our variables first - the hashes differ for every service
  28.     # omniauth['extra']['raw_info'] o omniauth['extra']['raw_info'] . Aquest últim és el més modern
  29.     if service_route == 'facebook'
  30.       omniauth['extra']['raw_info']['email'] ? email =  omniauth['extra']['raw_info']['email'] : email = ''
  31.       omniauth['extra']['raw_info']['name'] ? name =  omniauth['extra']['raw_info']['name'] : name = ''
  32.       omniauth['extra']['raw_info']['id'] ?  uid =  omniauth['extra']['raw_info']['id'] : uid = ''
  33.       omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
  34.     elsif service_route == 'github'
  35.       omniauth['user_info']['email'] ? email =  omniauth['user_info']['email'] : email = ''
  36.       omniauth['user_info']['name'] ? name =  omniauth['user_info']['name'] : name = ''
  37.       omniauth['extra']['raw_info']['id'] ?  uid =  omniauth['extra']['raw_info']['id'] : uid = ''
  38.       omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
  39.     elsif service_route == 'twitter'
  40.       email = ''    # Twitter API never returns the email address
  41.       #omniauth['user_info']['name'] ? name =  omniauth['user_info']['name'] : name = ''
  42.       omniauth['info']['name'] ? name =  omniauth['info']['name'] : name = ''
  43.       omniauth['uid'] ?  uid =  omniauth['uid'] : uid = ''
  44.       omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
  45.     #elsif service_route == 'google'
  46.     #   omniauth['user_info']['email'] ? email =  omniauth['user_info']['email'] : email = ''
  47.    #    omniauth['user_info']['name'] ? name =  omniauth['user_info']['name'] : name = ''
  48.     #   omniauth['uid'] ? uid =  omniauth['uid'] : uid = ''
  49.    #    omniauth['provider'] ? provider =  omniauth['provider'] : provider = ''
  50.     else
  51.  
  52.       # we have an unrecognized service, just output the hash that has been returned
  53.       render :text => omniauth.to_yaml
  54.       #render :text => uid.to_s + " - " + name + " - " + email + " - " + provider
  55.       return
  56.     end
  57.  
  58.     # continue only if provider and uid exist
  59.     if uid != '' and provider != ''
  60.       # nobody can sign in twice, nobody can sign up while being signed in (this saves a lot of trouble)
  61.       if !user_signed_in?
  62.         # check if user has already signed in using this service provider and continue with sign in process if yes
  63.         auth = Service.find_by_provider_and_uid(provider, uid)
  64.         if auth
  65.           flash[:notice] = 'Signed in successfully via ' + provider.capitalize + '.'
  66.           sign_in_and_redirect(:user, auth.user)
  67.         else
  68.           # check if this user is already registered with this email address; get out if no email has been provided
  69.           if email != ''
  70.             # search for a user with this email address
  71.             existinguser = User.find_by_email(email)
  72.             if existinguser
  73.               # map this new login method via a service provider to an existing account if the email address is the same
  74.               existinguser.services.create(:provider => provider, :uid => uid, :uname => name, :uemail => email)
  75.               flash[:notice] = 'Sign in via ' + provider.capitalize + ' has been added to your account ' + existinguser.email + '. Signed in successfully!'
  76.               sign_in_and_redirect(:user, existinguser)
  77.             else
  78.               # let's create a new user: register this user and add this authentication method for this user
  79.               name = name[0, 39] if name.length > 39             # otherwise our user validation will hit us
  80.  
  81.               # new user, set email, a random password and take the name from the authentication service
  82.               #user = User.new :email => email, :password => SecureRandom.hex(10), :fullname => name
  83.               user = User.new :email => email, :password => SecureRandom.hex(10), :name => name, :haslocalpw => false
  84.               # add this authentication service to our new user
  85.              # user.services.build(:provider => provider, :uid => uid, :uname => name, :uemail => email)
  86.               user.services.build(:provider => provider, :uid => uid, :uname => name, :uemail => email)
  87.  
  88.               # do not send confirmation email, we directly save and confirm the new record
  89.               user.skip_confirmation!
  90.               user.save!
  91.               user.confirm!
  92.  
  93.               # flash and sign in
  94.               flash[:myinfo] = 'Your account on CommunityGuides has been created via ' + provider.capitalize + '. In your profile you can change your personal information and add a local password.'
  95.               sign_in_and_redirect(:user, user)
  96.             end
  97.           else
  98.             flash[:error] =  service_route.capitalize + ' can not be used to sign-up on CommunityGuides as no valid email address has been provided. Please use another authentication provider or use local sign-up. If you already have an account, please sign-in and add ' + service_route.capitalize + ' from your profile.'
  99.             redirect_to new_user_session_path
  100.           end
  101.         end
  102.       else
  103.         # the user is currently signed in
  104.  
  105.         # check if this service is already linked to his/her account, if not, add it
  106.         auth = Service.find_by_provider_and_uid(provider, uid)
  107.         if !auth
  108.           current_user.services.create(:provider => provider, :uid => uid, :uname => name, :uemail => email)
  109.           flash[:notice] = 'Sign in via ' + provider.capitalize + ' has been added to your account.'
  110.           redirect_to services_path
  111.         else
  112.           flash[:notice] = service_route.capitalize + ' is already linked to your account.'
  113.           redirect_to services_path
  114.         end  
  115.       end  
  116.     else
  117.       flash[:error] =  service_route.capitalize + ' returned invalid data for the user id.'
  118.       redirect_to new_user_session_path
  119.     end
  120.   else
  121.     flash[:error] = 'Error while authenticating via ' + service_route.capitalize + '.'
  122.     redirect_to new_user_session_path
  123.   end
  124. end
  125. end