diff --git a/.gitignore b/.gitignore index d88beef..a43cb4b 100755 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/config/initializers/omniauth_secrets.rb # See http://help.github.com/ignore-files/ for more about ignoring files. # # If you find yourself ignoring temporary files generated by your text editor diff --git a/Gemfile b/Gemfile index 7f63ecb..0ad052a 100755 --- a/Gemfile +++ b/Gemfile @@ -53,6 +53,8 @@ gem "paper_trail" , '>=2.7.0' # User management gem "devise" ,'~>2.2.3' +gem "omniauth" +gem "omniauth-facebook" # Roles for users gem "rolify" @@ -80,4 +82,4 @@ gem "simple_calendar", "~> 0.1.9" gem 'rmagick' gem 'bootstrap-addons-rails' -gem "jquery-fileupload-rails" \ No newline at end of file +gem "jquery-fileupload-rails" diff --git a/Gemfile.lock b/Gemfile.lock index 797788d..e7fc484 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -67,6 +67,8 @@ GEM factory_girl_rails (4.2.1) factory_girl (~> 4.2.0) railties (>= 3.0.0) + faraday (0.8.8) + multipart-post (~> 1.2.0) formtastic (2.2.1) actionpack (>= 3.0) formtastic-bootstrap (2.0.0) @@ -79,7 +81,9 @@ GEM paper_trail (~> 2) haml (4.0.0) tilt + hashie (2.0.5) hike (1.2.1) + httpauth (0.2.0) i18n (0.6.1) journey (1.0.4) jquery-fileupload-rails (0.4.1) @@ -89,6 +93,8 @@ GEM railties (>= 3.0, < 5.0) thor (>= 0.14, < 2.0) json (1.7.7) + jwt (0.1.8) + multi_json (>= 1.5) libv8 (3.11.8.13) mail (2.4.4) i18n (>= 0.4.0) @@ -96,6 +102,21 @@ GEM treetop (~> 1.4.8) mime-types (1.21) multi_json (1.5.1) + multipart-post (1.2.0) + oauth2 (0.8.1) + faraday (~> 0.8) + httpauth (~> 0.1) + jwt (~> 0.1.4) + multi_json (~> 1.0) + rack (~> 1.2) + omniauth (1.1.4) + hashie (>= 1.2, < 3) + rack + omniauth-facebook (1.4.1) + omniauth-oauth2 (~> 1.1.0) + omniauth-oauth2 (1.1.1) + oauth2 (~> 0.8.0) + omniauth (~> 1.0) orm_adapter (0.4.0) paper_trail (2.7.0) activerecord (~> 3.0) @@ -199,6 +220,8 @@ DEPENDENCIES haml jquery-fileupload-rails jquery-rails + omniauth + omniauth-facebook paper_trail (>= 2.7.0) paperclip (~> 3.4.0) rails (= 3.2.9) diff --git a/app/controllers/memberships_controller.rb b/app/controllers/memberships_controller.rb index 0052c24..199fa8d 100644 --- a/app/controllers/memberships_controller.rb +++ b/app/controllers/memberships_controller.rb @@ -19,7 +19,7 @@ class MembershipsController < ApplicationController # POST /memberships.json def create @membership = Membership.new(params[:membership]) - @membership.fetprofile= Fetprofle.find(params[:fetprofile_id]) + @membership.fetprofile= Fetprofile.find(params[:fetprofile_id]) respond_to do |format| if @membership.save format.html { redirect_to @membership.fetprofile, notice: 'Membership was successfully created.' } diff --git a/app/controllers/users/omniauth_callbacks_controller.rb b/app/controllers/users/omniauth_callbacks_controller.rb new file mode 100644 index 0000000..a1abe98 --- /dev/null +++ b/app/controllers/users/omniauth_callbacks_controller.rb @@ -0,0 +1,15 @@ + +class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController + def facebook + # You need to implement the method below in your model (e.g. app/models/user.rb) + @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user) + + if @user.persisted? + sign_in_and_redirect @user, :event => :authentication #this will throw if @user is not activated + set_flash_message(:notice, :success, :kind => "Facebook") if is_navigational_format? + else + session["devise.facebook_data"] = request.env["omniauth.auth"] + redirect_to new_user_registration_url + end + end +end diff --git a/app/models/fetprofile.rb b/app/models/fetprofile.rb index fba5c01..a97f35e 100644 --- a/app/models/fetprofile.rb +++ b/app/models/fetprofile.rb @@ -15,12 +15,13 @@ # class Fetprofile < ActiveRecord::Base - attr_accessible :active, :desc, :fetmailalias, :nachname, :picture, :short, :vorname + attr_accessible :active, :desc, :fetmailalias, :nachname, :picture, :short, :vorname,:memberships_attributes has_many :memberships has_many :gremien, :through=> :membership mount_uploader :picture, PictureUploader def name [vorname, nachname, "(",short,")"].join(" ") end + accepts_nested_attributes_for :memberships scope :active, -> { where(:active=>:true).order(:vorname) } end diff --git a/app/models/user.rb b/app/models/user.rb index 3995a3d..92719ab 100755 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -30,10 +30,29 @@ class User < ActiveRecord::Base # :token_authenticatable, :confirmable, # :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable,:confirmable, - :recoverable, :rememberable, :trackable, :validatable + :recoverable, :rememberable, :trackable, :validatable,:omniauthable, :omniauth_providers => [:facebook] # Setup accessible (or protected) attributes for your model - attr_accessible :email, :password, :password_confirmation, :remember_me + attr_accessible :email, :password, :password_confirmation, :remember_me, :provider, :uid, :name # attr_accessible :title, :body - + def self.find_for_facebook_oauth(auth, signed_in_resource=nil) + user = User.where(:provider => auth.provider, :uid => auth.uid).first + unless user + user = User.create(name:auth.extra.raw_info.name, + provider:auth.provider, + uid:auth.uid, + email:auth.info.email, + password:Devise.friendly_token[0,20] + ) + end + user + end + def self.new_with_session(params, session) + super.tap do |user| + if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"] + user.email = data["email"] if user.email.blank? + end + end + end + end diff --git a/app/views/fetprofiles/_form.html.erb b/app/views/fetprofiles/_form.html.erb index dcd19db..034eb5e 100644 --- a/app/views/fetprofiles/_form.html.erb +++ b/app/views/fetprofiles/_form.html.erb @@ -17,20 +17,19 @@