venus 0.3.3 → 0.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -18,6 +18,12 @@ end
18
18
  Usage
19
19
  =====
20
20
 
21
+ * Setup Omniauth for multiple omniauth (Facebook, Twitter, Github) in model `User`.
22
+
23
+ ```
24
+ rails generate venus:omniauth
25
+ ```
26
+
21
27
  * Setup gem 'capistrano' for deloyment.
22
28
 
23
29
  ```
@@ -55,12 +61,6 @@ Usage
55
61
  rails generate venus:devise
56
62
  ```
57
63
 
58
- * Setup Omniauth for Facebook login (model `User`).
59
-
60
- ```
61
- rails generate venus:fbauth
62
- ```
63
-
64
64
  * Setup Rspec testing framework
65
65
 
66
66
  ```
@@ -53,6 +53,10 @@ module Venus
53
53
  bundle_install
54
54
  end
55
55
 
56
+ def run_magic_encoding
57
+ run 'magic_encoding'
58
+ end
59
+
56
60
  end
57
61
  end
58
- end
62
+ end
@@ -0,0 +1,63 @@
1
+ module Venus
2
+ module Generators
3
+ class OmniauthGenerator < Base
4
+ desc "Setup gem 'omniauth' with multi providers"
5
+
6
+ def name
7
+ "Omniauth"
8
+ end
9
+
10
+ def asks
11
+ say 'checking dependent gems "settinglogic"...'
12
+ generate 'venus:settingslogic' unless has_gem?('settingslogic')
13
+ say 'checking dependent gems "devise"...'
14
+ generate 'venus:devise' unless has_gem?('devise')
15
+
16
+ @settinglogic_class = ask?("Your settinglogic class name?", 'Setting')
17
+ @settinglogic_yml = ask?("Your settinglogic yaml file in config/ ?", 'setting.yml')
18
+
19
+ @providers = {}
20
+ [:facebook, :github, :twitter].each do |provider|
21
+ if ask?("Use '#{provider}'?", true)
22
+ token = ask?("#{provider.capitalize} App ID?", '267188576687915')
23
+ secret = ask?("#{provider.capitalize} App Secret?", '84f72292e1f6b299f4a668f12ed1a7f2')
24
+ @providers[provider] = {:token => token, :secret => secret}
25
+ end
26
+ end
27
+ end
28
+
29
+ def gemfile
30
+ add_gem('omniauth')
31
+ @providers.each do |provider, |
32
+ add_gem("omniauth-#{provider}")
33
+ end
34
+ bundle_install
35
+ end
36
+
37
+ def controller
38
+ template 'omniauth_callbacks_controller.rb', 'app/controllers/users/omniauth_callbacks_controller.rb', :force => true
39
+ end
40
+
41
+ def configs
42
+ insert_template('config/routes.rb', 'routes.erb', :after => "routes.draw do\n")
43
+ template 'omniauth.rb', 'config/initializers/omniauth.rb'
44
+ ["config/#{@settinglogic_yml}", "config/#{@settinglogic_yml}.example"].each do |to_file|
45
+ insert_template(to_file, "setting.yml.erb", :after => "&defaults\n")
46
+ end
47
+ end
48
+
49
+ def model
50
+ generate "model authorization provider:string uid:string auth_type:string auth_id:integer auth_data:text"
51
+ insert_template 'app/models/authorization.rb', 'authorization.rb', :after => "ActiveRecord::Base\n"
52
+ template 'omniauthable.rb', 'app/lib/omniauthable.rb'
53
+ insert_template("app/models/user.rb", "user.erb", :before => "\nend\n")
54
+ sleep(1)
55
+ migration_template "migrations.rb", "db/migrate/add_index_for_authorizations_and_add_column_for_users"
56
+ end
57
+
58
+ def msg
59
+ bundle_exec('rake db:migrate') if ask?("Run 'bundle exec rake db:migrate'?", true)
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,5 @@
1
+
2
+ belongs_to :auth, :polymorphic => true
3
+ validates_presence_of :provider, :uid, :auth_type, :auth_id
4
+ attr_accessible :provider, :uid, :auth_type, :auth_id, :auth_data
5
+ serialize :auth_data, Hash
@@ -0,0 +1,7 @@
1
+ class AddIndexForAuthorizationsAndAddColumnForUsers < ActiveRecord::Migration
2
+ def change
3
+ add_column :users, :name, :string, :after => :email
4
+ add_index :authorizations, [:provider, :uid]
5
+ add_index :authorizations, [:auth_type, :auth_id]
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ Rails.application.config.middleware.use OmniAuth::Builder do
2
+ Setting.providers.each do |provider_name|
3
+ provider provider_name, Setting.send("#{provider_name}_token"), Setting.send("#{provider_name}_secret")
4
+ end
5
+ end
6
+
7
+ OmniAuth.config.path_prefix = '/users/auth'
@@ -0,0 +1,15 @@
1
+ class Users::OmniauthCallbacksController < ApplicationController
2
+
3
+ def callback
4
+ provider = params[:provider]
5
+ if not current_user.blank?
6
+ current_user.bind_service(env["omniauth.auth"])
7
+ sign_in_and_redirect(current_user, :notice => "Successfully binded to #{provider}.")
8
+ else
9
+ @user = User.find_or_create_by_omniauth(env["omniauth.auth"])
10
+ flash[:notice] = "Sign in with #{provider.to_s.titleize} successfully."
11
+ sign_in_and_redirect @user, :event => :authentication, :notice => "Login successfully."
12
+ end
13
+ end
14
+
15
+ end
@@ -0,0 +1,59 @@
1
+ module Omniauthable
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ has_many :authorizations, :dependent => :destroy, :as => :auth
6
+ end
7
+
8
+ module ClassMethods
9
+
10
+ def find_or_create_by_omniauth(authhash)
11
+ authorization = Authorization.find_by_provider_and_uid(authhash['provider'], authhash['uid'])
12
+ return authorization.auth if authorization
13
+ user = send "initialize_from_omniauth_#{authhash['provider']}", authhash
14
+ if User.find_by_email(user.email)
15
+ user = _
16
+ else
17
+ user.save
18
+ user.bind_service(authhash)
19
+ end
20
+ user
21
+ end
22
+
23
+ private
24
+
25
+ def initialize_from_omniauth_facebook(authhash)
26
+ User.new(:email => authhash['info']['email'], :name => authhash['info']['name'])
27
+ end
28
+
29
+ def initialize_from_omniauth_twitter(authhash)
30
+ User.new(:email => authhash['info']['email'], :name => authhash['info']['name'])
31
+ end
32
+
33
+ def initialize_from_omniauth_github(authhash)
34
+ User.new(:email => authhash['info']['email'], :name => authhash['info']['name'])
35
+ end
36
+
37
+ end
38
+
39
+ module InstanceMethods
40
+
41
+ def bind_service(authhash)
42
+ authorization = Authorization.find_by_provider_and_uid(authhash['provider'], authhash['uid'])
43
+ unless authorization
44
+ authorization = authorizations.create! :uid => authhash['uid'], :provider => authhash['provider'], :auth_data => authhash
45
+ end
46
+ authorization
47
+ end
48
+
49
+ def can_bind_to
50
+ Setting.providers - (authorizations.map {|auth| auth.provider})
51
+ end
52
+
53
+ def password_required?
54
+ false
55
+ end
56
+
57
+ end
58
+
59
+ end
@@ -0,0 +1,7 @@
1
+
2
+ match '/users/auth/:provider/callback', :to => "users/omniauth_callbacks#callback"
3
+ match '/users/auth/failure' => 'sessions#failure', :as => :auth_failure
4
+
5
+ Setting.providers.each do |provider|
6
+ match "/users/auth/#{provider}", :as => "user_auth_#{provider}"
7
+ end
@@ -0,0 +1,5 @@
1
+ providers: <%= (@providers.map {|p| p[0].to_s}).to_s %>
2
+ <%- @providers.each do |name, info| -%>
3
+ <%=name%>_token: "<%=info[:token] %>"
4
+ <%=name%>_secret: "<%=info[:secret] %>"
5
+ <%- end -%>
@@ -0,0 +1,3 @@
1
+
2
+ attr_accessible :name
3
+ include Omniauthable
@@ -21,11 +21,11 @@ module Venus
21
21
 
22
22
  def kaminari_views
23
23
  if @kaminari_views
24
- haml = @kaminari_haml != 'n' ? ' -e haml' : ''
24
+ haml = @kaminari_haml ? ' -e haml' : ''
25
25
  generate 'kaminari:views default' + haml
26
26
  end
27
27
  end
28
28
 
29
29
  end
30
30
  end
31
- end
31
+ end
data/lib/venus/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Venus
2
- VERSION = "0.3.3"
2
+ VERSION = "0.4.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: venus
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.4.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-02 00:00:00.000000000 Z
12
+ date: 2013-01-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: railties
@@ -80,17 +80,20 @@ files:
80
80
  - lib/generators/venus/deploy/templates/gemfile.erb
81
81
  - lib/generators/venus/deploy/templates/stage.erb
82
82
  - lib/generators/venus/devise/devise_generator.rb
83
- - lib/generators/venus/fbauth/fbauth_generator.rb
84
- - lib/generators/venus/fbauth/templates/migration.rb
85
- - lib/generators/venus/fbauth/templates/omniauth.erb
86
- - lib/generators/venus/fbauth/templates/routes.rb
87
- - lib/generators/venus/fbauth/templates/sessions_controller.rb
88
- - lib/generators/venus/fbauth/templates/user.rb
89
83
  - lib/generators/venus/helpers.rb
90
84
  - lib/generators/venus/init/init_generator.rb
91
85
  - lib/generators/venus/init/templates/gem_developments.erb
92
86
  - lib/generators/venus/mysql/mysql_generator.rb
93
87
  - lib/generators/venus/mysql/templates/database.yml.erb
88
+ - lib/generators/venus/omniauth/omniauth_generator.rb
89
+ - lib/generators/venus/omniauth/templates/authorization.rb
90
+ - lib/generators/venus/omniauth/templates/migrations.rb
91
+ - lib/generators/venus/omniauth/templates/omniauth.rb
92
+ - lib/generators/venus/omniauth/templates/omniauth_callbacks_controller.rb
93
+ - lib/generators/venus/omniauth/templates/omniauthable.rb
94
+ - lib/generators/venus/omniauth/templates/routes.erb
95
+ - lib/generators/venus/omniauth/templates/setting.yml.erb
96
+ - lib/generators/venus/omniauth/templates/user.erb
94
97
  - lib/generators/venus/paginate/paginate_generator.rb
95
98
  - lib/generators/venus/rspec/rspec_generator.rb
96
99
  - lib/generators/venus/rspec/templates/config_application.rb
@@ -1,56 +0,0 @@
1
- module Venus
2
- module Generators
3
- class FbauthGenerator < Base
4
- desc "Setup gem 'omniauth-fb'"
5
-
6
- def asks
7
- say 'checking dependent gems "settinglogic"...'
8
- generate 'venus:settingslogic' unless has_gem?('settingslogic')
9
- say 'checking dependent gems "devise"...'
10
- generate 'venus:devise' unless has_gem?('devise')
11
- @fb_app_id = ask?("Facebook App ID?", '267188576687915')
12
- @fb_secret = ask?("Facebook App Secret?", '84f72292e1f6b299f4a668f12ed1a7f2')
13
- @settinglogic_class = ask?("Your settinglogic class name?", 'Setting')
14
- @settinglogic_yml = ask?("Your settinglogic yaml file in config/ ?", 'setting.yml')
15
- end
16
-
17
- def name
18
- "Omniauth FB"
19
- end
20
-
21
- def gemfile
22
- add_gem('omniauth')
23
- add_gem('omniauth-facebook')
24
- bundle_install
25
- end
26
-
27
- def controller
28
- generate "controller sessions"
29
- template 'sessions_controller.rb', 'app/controllers/sessions_controller.rb', :force => true
30
- end
31
-
32
- def routes
33
- insert_template("config/routes.rb", "routes.rb", :after => "routes.draw do\n")
34
- end
35
-
36
- def model
37
- migration_template "migration.rb", "db/migrate/add_columns_for_fblogin_to_users"
38
- insert_template("app/models/user.rb", "user.rb", :before => "\nend\n")
39
- replace_in_file("app/models/user.rb", " attr_accessible ", " attr_accessible :name, :facebook_id, ")
40
- end
41
-
42
- def config
43
- template 'omniauth.erb', 'config/initializers/omniauth.rb'
44
- ["config/#{@settinglogic_yml}", "config/#{@settinglogic_yml}.example"].each do |to_file|
45
- replace_in_file(to_file, "facebook_app_id: ", "facebook_app_id: '#{@fb_app_id}'")
46
- replace_in_file(to_file, "facebook_secret: ", "facebook_secret: '#{@fb_secret}'")
47
- end
48
- end
49
-
50
- def msg
51
- bundle_exec('rake db:migrate') if ask?("Run 'bundle exec rake db:migrate'?", true)
52
- end
53
-
54
- end
55
- end
56
- end
@@ -1,7 +0,0 @@
1
- class AddColumnsForFbloginToUsers < ActiveRecord::Migration
2
- def change
3
- add_column :users, :name, :string, :after => :email
4
- add_column :users, :facebook_id, :string, :after => :email
5
- add_index :users, [:facebook_id]
6
- end
7
- end
@@ -1,3 +0,0 @@
1
- Rails.application.config.middleware.use OmniAuth::Builder do
2
- provider :facebook, <%= @settinglogic_class %>.facebook_app_id, <%= @settinglogic_class %>.facebook_secret, :scope => <%= @settinglogic_class %>.facebook_perms
3
- end
@@ -1,4 +0,0 @@
1
-
2
- match '/auth/:provider/callback', :to => 'sessions#create'
3
- match '/auth/failure' => 'sessions#failure', :as => :auth_failure
4
- match '/auth/facebook', :as => :facebook_login
@@ -1,16 +0,0 @@
1
- class SessionsController < ApplicationController
2
-
3
- def create
4
- if current_user = User.create_by_omniauth(request.env['omniauth.auth'], current_user)
5
- flash[:notice] = "Login success"
6
- sign_in_and_redirect :user, current_user
7
- else
8
- redirect_to new_user_session_path, :alert => "Login fail"
9
- end
10
- end
11
-
12
- def failure
13
- redirect_to new_user_session_path, :alert => params[:message]
14
- end
15
-
16
- end
@@ -1,15 +0,0 @@
1
-
2
- def self.create_by_omniauth(hash, current_user)
3
- hash = ActiveSupport::HashWithIndifferentAccess.new hash
4
- user = User.send("find_by_#{hash[:provider]}_id", hash[:uid])
5
- unless user
6
- user = current_user || User.find_by_email(hash[:info][:email]) || User.new( :email => hash[:info][:email], :name => hash[:info][:name] )
7
- user.send("#{hash[:provider]}_id=", hash[:uid])
8
- user.save!
9
- end
10
- user
11
- end
12
-
13
- def password_required?
14
- facebook_id.present? ? false : true
15
- end