ninsho 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +15 -3
- data/app/controllers/ninsho/sessions_controller.rb +1 -0
- data/app/controllers/ninsho_controller.rb +5 -3
- data/lib/generators/active_record/ninsho_generator.rb +2 -1
- data/lib/generators/ninsho/views_generator.rb +0 -1
- data/lib/generators/templates/README +4 -0
- data/lib/ninsho.rb +22 -1
- data/lib/ninsho/authentication.rb +19 -13
- data/lib/ninsho/controllers/helpers.rb +66 -56
- data/lib/ninsho/interface.rb +3 -1
- data/lib/ninsho/omniauth.rb +1 -0
- data/lib/ninsho/omniauth/config.rb +2 -0
- data/lib/ninsho/rails.rb +3 -0
- data/lib/ninsho/rails/routes.rb +15 -17
- data/lib/ninsho/railtie.rb +3 -0
- data/lib/ninsho/routes_drawer.rb +2 -0
- data/lib/ninsho/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -123,7 +123,10 @@ en:
|
|
123
123
|
|
124
124
|
### Changelog
|
125
125
|
|
126
|
-
* Current gem version 0.0.
|
126
|
+
* Current gem version 0.0.2
|
127
|
+
* Add more documentation on code
|
128
|
+
* Add aouth token for Facebook friends
|
129
|
+
* Released gem version 0.0.1
|
127
130
|
|
128
131
|
### Devs
|
129
132
|
|
@@ -135,8 +138,7 @@ en:
|
|
135
138
|
* Add tests
|
136
139
|
* Support for Mongoid
|
137
140
|
* Add handy helpers
|
138
|
-
|
139
|
-
* Add more documentation on code
|
141
|
+
|
140
142
|
|
141
143
|
## Credits
|
142
144
|
Icalia Labs - weare@icalialabs.com
|
@@ -146,7 +148,17 @@ Icalia Labs - weare@icalialabs.com
|
|
146
148
|
|
147
149
|
[Like us on Facebook](https://www.facebook.com/icalialab "Like us on Facebook")
|
148
150
|
|
151
|
+
### Special thanks
|
152
|
+
|
153
|
+
Inspired by the plataformatec guys, and pieces of code based on you! Thanks!
|
154
|
+
|
155
|
+
https://github.com/plataformatec/
|
149
156
|
|
150
157
|
### License
|
151
158
|
|
152
159
|
MIT License. Copyright 2012-2013 IcaliaLabs. http://icalialabs.com
|
160
|
+
|
161
|
+
|
162
|
+
|
163
|
+
|
164
|
+
|
@@ -14,6 +14,7 @@ class NinshoController < Ninsho.parent_controller.constantize
|
|
14
14
|
instance_variable_get(:"@#{resource_name}")
|
15
15
|
end
|
16
16
|
|
17
|
+
# Omniauth hash for creating the records
|
17
18
|
def resource_params
|
18
19
|
env['omniauth.auth']
|
19
20
|
end
|
@@ -23,6 +24,8 @@ class NinshoController < Ninsho.parent_controller.constantize
|
|
23
24
|
Ninsho.resource_name #authentication
|
24
25
|
end
|
25
26
|
|
27
|
+
# Class name for the ninsho model
|
28
|
+
# commonly Authentication
|
26
29
|
def resource_class
|
27
30
|
Ninsho.resource_class
|
28
31
|
end
|
@@ -32,13 +35,12 @@ class NinshoController < Ninsho.parent_controller.constantize
|
|
32
35
|
instance_variable_set(:"@#{resource_name}", new_resource)
|
33
36
|
end
|
34
37
|
|
35
|
-
# Parent resource
|
38
|
+
# Parent resource, commonly user
|
36
39
|
def parent_resource
|
37
40
|
resource_class.reflect_on_all_associations(:belongs_to).first.name.to_s
|
38
41
|
end
|
39
42
|
|
40
|
-
# Build a ninsho resource
|
41
|
-
# Assignment bypasses attribute protection when :unsafe option is passed
|
43
|
+
# Build a ninsho resource, from the omniauth hash
|
42
44
|
def build_resource_from_omniauth
|
43
45
|
self.resource = resource_class.from_omniauth(resource_params)
|
44
46
|
end
|
@@ -6,5 +6,9 @@ Hey you, thanks for installing Ninsho, here are some tips:
|
|
6
6
|
|
7
7
|
rails g ninsho:views
|
8
8
|
|
9
|
+
2. Having trouble?, Check out the documentation at:
|
10
|
+
|
11
|
+
https://github.com/IcaliaLabs/ninsho
|
12
|
+
|
9
13
|
===============================================================================
|
10
14
|
|
data/lib/ninsho.rb
CHANGED
@@ -12,29 +12,46 @@ module Ninsho
|
|
12
12
|
autoload :Helpers, 'ninsho/controllers/helpers'
|
13
13
|
end
|
14
14
|
|
15
|
+
# The parent controller all Ninsho controllers inherits from.
|
16
|
+
# The Default is set to ApplicationController.
|
15
17
|
mattr_accessor :parent_controller
|
16
18
|
@@parent_controller = "ApplicationController"
|
17
19
|
|
20
|
+
# The name class of the resource generate by the Ninsho model generator
|
21
|
+
# Comonly Authentication
|
18
22
|
mattr_accessor :resource_class
|
19
23
|
@@resource_class = ""
|
20
24
|
|
25
|
+
# The resource name singularized and downcased
|
26
|
+
# Used as a proxy to map ninsho resource
|
27
|
+
# Used in routes on ninsho_on method
|
21
28
|
mattr_accessor :resource_name
|
22
29
|
@@resource_name = ""
|
23
30
|
|
31
|
+
# List all providers added in the initializer
|
32
|
+
# All of them are symbols
|
33
|
+
# [ :facebook, :twitter, :github ]
|
24
34
|
mattr_reader :providers
|
25
35
|
@@providers = []
|
26
36
|
|
37
|
+
# The class name for the resource relation
|
38
|
+
# Is commonly the class User
|
27
39
|
mattr_accessor :parent_resource_name
|
28
40
|
@@parent_resource_name = ''
|
29
41
|
|
30
|
-
|
42
|
+
# Hash which contains omniauth configurations
|
31
43
|
mattr_reader :omniauth_configs
|
32
44
|
@@omniauth_configs = ActiveSupport::OrderedHash.new
|
33
45
|
|
46
|
+
# Default setup for Ninsho.
|
47
|
+
# Run the rails g ninsho:install to create a fresh initializer
|
34
48
|
def self.setup
|
35
49
|
yield self
|
36
50
|
end
|
37
51
|
|
52
|
+
# It is used toe get the resource class and map it with
|
53
|
+
# ActiveSupport to get the class name
|
54
|
+
# :user will become User
|
38
55
|
class Getter
|
39
56
|
def initialize name
|
40
57
|
@name = name
|
@@ -54,6 +71,10 @@ module Ninsho
|
|
54
71
|
end
|
55
72
|
end
|
56
73
|
|
74
|
+
# Used to specify an omniauth provider
|
75
|
+
#
|
76
|
+
# config.omniauth :facebook, 'APPD_ID', 'APP_SECRET'
|
77
|
+
#
|
57
78
|
def self.omniauth(provider, *args)
|
58
79
|
config = Ninsho::OmniAuth::Config.new(provider, args)
|
59
80
|
@@providers << config.strategy_name.to_sym
|
@@ -1,11 +1,14 @@
|
|
1
1
|
module Ninsho
|
2
|
+
#
|
3
|
+
# Responsible for manage the authentication process with the
|
4
|
+
# omniauth hash
|
5
|
+
#
|
2
6
|
class Authentication
|
3
|
-
PARENT_RESOURCE_NAME = Ninsho.parent_resource_name.to_s.downcase
|
4
|
-
|
5
7
|
def initialize(omniauth = nil)
|
6
8
|
@omniauth = omniauth
|
7
9
|
@provider = omniauth['provider']
|
8
10
|
@uid = omniauth['uid']
|
11
|
+
@oauth_token = @omniauth.credentials.token
|
9
12
|
@email = omniauth['info']['email']
|
10
13
|
end
|
11
14
|
|
@@ -13,24 +16,27 @@ module Ninsho
|
|
13
16
|
user.present?
|
14
17
|
end
|
15
18
|
|
19
|
+
# Little method to check if the record is find by the provider and uid
|
16
20
|
def from_oauth
|
17
|
-
Ninsho.resource_class.
|
21
|
+
Ninsho.resource_class.where(@omniauth.slice(:provider, :uid)).first_or_initialize.tap do |resource|
|
22
|
+
resource.provider = @provider
|
23
|
+
resource.uid = @uid
|
24
|
+
resource.oauth_token = @oauth_token
|
25
|
+
resource.save if resource.respond_to?(Ninsho.parent_resource_name.to_s.downcase.to_sym) && !resource.new_record?
|
26
|
+
end
|
18
27
|
end
|
19
28
|
|
29
|
+
# Method to create an authentication record when user is find,
|
30
|
+
# otherwise creates a user with the authentication
|
20
31
|
def from_user
|
21
32
|
user = Ninsho.parent_resource_name.send :find_by_email, @email
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
else
|
27
|
-
user = Ninsho.parent_resource_name.send :new, { email: @email }
|
28
|
-
user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid)
|
29
|
-
user.send(:save)
|
30
|
-
user
|
31
|
-
end
|
33
|
+
user = Ninsho.parent_resource_name.send :new, { email: @email } unless user
|
34
|
+
user.send("#{Ninsho.resource_name.pluralize}").build(provider: @provider, uid: @uid, oauth_token: @oauth_token)
|
35
|
+
user.send(:save)
|
36
|
+
user
|
32
37
|
end
|
33
38
|
|
39
|
+
# Check if a parent record is returned
|
34
40
|
def user
|
35
41
|
from_oauth.try(Ninsho.parent_resource_name.to_s.downcase.to_sym) || from_user
|
36
42
|
end
|
@@ -3,40 +3,49 @@ module Ninsho
|
|
3
3
|
module Helpers
|
4
4
|
extend ActiveSupport::Concern
|
5
5
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
6
|
+
RESOURCE_NAME = Ninsho.resource_name.singularize
|
7
|
+
PARENT_RESOURCE_NAME = Ninsho.parent_resource_name.to_s.downcase
|
8
|
+
|
9
|
+
# Stores the location for better redirection
|
10
|
+
def store_location
|
11
|
+
session[:return_to] = request.fullpath
|
12
|
+
end
|
13
|
+
|
14
|
+
# Clears the session after redirection is done
|
15
|
+
def clear_return_to
|
16
|
+
session[:return_to] = nil
|
17
|
+
end
|
18
|
+
|
19
|
+
# Method which handles the redirection
|
20
|
+
def redirect_back_or(default)
|
21
|
+
redirect_to(session[:return_to] || default )
|
22
|
+
clear_return_to
|
23
|
+
end
|
24
|
+
|
25
|
+
# Destroy the user session
|
26
|
+
def sign_out
|
27
|
+
session["#{PARENT_RESOURCE_NAME}_id".to_sym] = nil
|
28
|
+
end
|
29
|
+
|
30
|
+
def sign_in_and_redirect(parent_id, path=nil)
|
31
|
+
session["#{PARENT_RESOURCE_NAME}_id".to_sym] = parent_id
|
32
|
+
redirect_to path
|
33
|
+
end
|
34
|
+
|
35
|
+
# Set the session for the authenticated user
|
36
|
+
def sign_in(parent_id)
|
37
|
+
session["#{PARENT_RESOURCE_NAME}_id".to_sym] = parent_id
|
38
|
+
end
|
39
|
+
|
40
|
+
# There be monsters!
|
41
|
+
# Creates several methods according to the parent resource
|
42
|
+
#
|
43
|
+
# authenticate_user!
|
44
|
+
#
|
45
|
+
# user_signed_in?
|
46
|
+
#
|
47
|
+
# current_user
|
48
|
+
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
40
49
|
def current_#{PARENT_RESOURCE_NAME}
|
41
50
|
@current_#{PARENT_RESOURCE_NAME} ||= #{Ninsho.parent_resource_name}.find(session[:#{PARENT_RESOURCE_NAME}_id]) if session[:#{PARENT_RESOURCE_NAME}_id]
|
42
51
|
end
|
@@ -49,31 +58,32 @@ module Ninsho
|
|
49
58
|
deny_access unless #{PARENT_RESOURCE_NAME}_signed_in?
|
50
59
|
end
|
51
60
|
|
52
|
-
|
53
|
-
|
54
|
-
define_method "link_#{RESOURCE_NAME}_with" do |provider|
|
55
|
-
link_to "Connect with #{provider.to_s.capitalize}", "auth/#{provider.to_s}"
|
56
|
-
end
|
57
|
-
|
61
|
+
METHODS
|
58
62
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
# By default it is the root_path.
|
63
|
-
def redirect_on_sign_out_path
|
64
|
-
redirect_to_root
|
65
|
-
end
|
63
|
+
define_method "link_#{RESOURCE_NAME}_with" do |provider|
|
64
|
+
link_to "Connect with #{provider.to_s.capitalize}", "auth/#{provider.to_s}"
|
65
|
+
end
|
66
66
|
|
67
|
-
def redirect_on_sign_in_path
|
68
|
-
redirect_to_root
|
69
|
-
end
|
70
|
-
def redirect_to_root
|
71
|
-
redirect_to respond_to?(:root_path) ? root_path : "/"
|
72
|
-
end
|
73
67
|
|
74
|
-
|
75
|
-
|
76
|
-
|
68
|
+
# Method used by sessions controller to sign out a user.
|
69
|
+
# You can overwrite it in your ApplicationController
|
70
|
+
#
|
71
|
+
# By default it is the root_path.
|
72
|
+
def redirect_on_sign_out_path
|
73
|
+
redirect_to_root
|
74
|
+
end
|
75
|
+
|
76
|
+
def redirect_on_sign_in_path
|
77
|
+
redirect_to_root
|
78
|
+
end
|
79
|
+
def redirect_to_root
|
80
|
+
redirect_to respond_to?(:root_path) ? root_path : "/"
|
81
|
+
end
|
82
|
+
|
83
|
+
# Redirection if user has no right to enter
|
84
|
+
def deny_access
|
85
|
+
redirect_to_root
|
86
|
+
end
|
77
87
|
end
|
78
88
|
end
|
79
89
|
end
|
data/lib/ninsho/interface.rb
CHANGED
@@ -20,7 +20,9 @@ module Ninsho
|
|
20
20
|
Ninsho.parent_resource_name = Ninsho.ref(associated_model.to_s.classify).get
|
21
21
|
end
|
22
22
|
end
|
23
|
-
|
23
|
+
|
24
|
+
# Responsible for creating or find the record with the
|
25
|
+
# omniauth hash
|
24
26
|
def from_omniauth(omniauth = nil)
|
25
27
|
Ninsho::Authentication.new(omniauth)
|
26
28
|
end
|
data/lib/ninsho/omniauth.rb
CHANGED
data/lib/ninsho/rails.rb
CHANGED
@@ -7,10 +7,13 @@ module Ninsho
|
|
7
7
|
# Force routes to be loaded if we are doing any eager load.
|
8
8
|
config.before_eager_load { |app| app.reload_routes! }
|
9
9
|
|
10
|
+
# Loads then Ninsho Helpers into the Ninsho Controllers
|
11
|
+
# for the ones whicn inherits from have access
|
10
12
|
ActiveSupport.on_load(:action_controller) do
|
11
13
|
include Ninsho::Controllers::Helpers
|
12
14
|
end
|
13
15
|
|
16
|
+
# Setup for omniauth strategies when initializer is run
|
14
17
|
initializer "ninsho.omniauth" do |app|
|
15
18
|
Ninsho.omniauth_configs.each do |provider, config|
|
16
19
|
app.middleware.use config.strategy_class, *config.args do |strategy|
|
data/lib/ninsho/rails/routes.rb
CHANGED
@@ -1,6 +1,5 @@
|
|
1
|
-
|
2
1
|
module ActionDispatch::Routing
|
3
|
-
|
2
|
+
|
4
3
|
|
5
4
|
class Mapper
|
6
5
|
#Includes the ninsho_on method for routes. This method is responsible
|
@@ -20,22 +19,21 @@ module ActionDispatch::Routing
|
|
20
19
|
# authentication_session_path POST /authentication/:provider/callback { controller: 'ninsho/sessions', action: 'create' }
|
21
20
|
# destroy_authentication_session_path DELETE /sign_out { controller: 'ninsho/sessions', action: 'destroy' }
|
22
21
|
#
|
22
|
+
def ninsho_on(resources_name)
|
23
|
+
drawer = Ninsho::RoutesDrawer.new resources_name
|
24
|
+
Ninsho.resource_class = drawer.to
|
25
|
+
Ninsho.resource_name = drawer.resource
|
26
|
+
ninsho_session(drawer.singular_name)
|
27
|
+
end
|
23
28
|
|
24
|
-
|
25
|
-
drawer = Ninsho::RoutesDrawer.new resources_name
|
26
|
-
Ninsho.resource_class = drawer.to
|
27
|
-
Ninsho.resource_name = drawer.resource
|
28
|
-
ninsho_session(drawer.singular_name)
|
29
|
-
end
|
30
|
-
|
31
|
-
protected
|
29
|
+
protected
|
32
30
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
31
|
+
def ninsho_session(name) #:nodoc:
|
32
|
+
resource :session, :only => [], :controller => 'ninsho/sessions', :path => "" do
|
33
|
+
get :new, :path => 'sign_in', as: "new_#{name}"
|
34
|
+
match :create, path: "auth/:provider/callback", as: "#{name}"
|
35
|
+
delete :destroy, path: 'sign_out', as: "destroy_#{name}"
|
36
|
+
end
|
37
|
+
end
|
40
38
|
end
|
41
39
|
end
|
data/lib/ninsho/railtie.rb
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
module Ninsho
|
2
|
+
# Responsible for loading and mounting the Ninsho::Interface method on
|
3
|
+
# ActiveRecord, which allows it to add the belongs_to_ninsho method
|
4
|
+
# inside models
|
2
5
|
class Railtie < Rails::Railtie
|
3
6
|
ActiveSupport.on_load :active_record do
|
4
7
|
ActiveRecord::Base.send :include, Ninsho::Interface
|
data/lib/ninsho/routes_drawer.rb
CHANGED
data/lib/ninsho/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ninsho
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
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-03-
|
12
|
+
date: 2013-03-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: orm_adapter
|