Commit 9a5190a5cecb611a73e45fc93bbd7c4e1395c540
1 parent
fa46a8f5
Adds OAuth login without creating a Noosfero user
Signed-off-by: Gabriel Silva <gabriel93.silva@gmail.com> Signed-off-by: Sabryna Sousa <sabryna.sousa1323@gmail.com>
Showing
2 changed files
with
24 additions
and
7 deletions
Show diff stats
plugins/oauth_client/controllers/public/oauth_client_plugin_public_controller.rb
| @@ -5,7 +5,13 @@ class OauthClientPluginPublicController < PublicController | @@ -5,7 +5,13 @@ class OauthClientPluginPublicController < PublicController | ||
| 5 | def callback | 5 | def callback |
| 6 | auth = request.env["omniauth.auth"] | 6 | auth = request.env["omniauth.auth"] |
| 7 | auth_user = environment.users.where(email: auth.info.email).first | 7 | auth_user = environment.users.where(email: auth.info.email).first |
| 8 | - if auth_user then login auth_user.person else signup auth end | 8 | + |
| 9 | + oauth_params = request.env["omniauth.params"] | ||
| 10 | + if oauth_params && oauth_params["action"] == "external_login" | ||
| 11 | + login(auth) | ||
| 12 | + else | ||
| 13 | + signup(auth) | ||
| 14 | + end | ||
| 9 | end | 15 | end |
| 10 | 16 | ||
| 11 | def failure | 17 | def failure |
| @@ -20,12 +26,23 @@ class OauthClientPluginPublicController < PublicController | @@ -20,12 +26,23 @@ class OauthClientPluginPublicController < PublicController | ||
| 20 | 26 | ||
| 21 | protected | 27 | protected |
| 22 | 28 | ||
| 23 | - def login person | 29 | + def login auth |
| 24 | provider = OauthClientPlugin::Provider.find(session[:provider_id]) | 30 | provider = OauthClientPlugin::Provider.find(session[:provider_id]) |
| 25 | - auth = person.oauth_auths.where(provider_id: provider.id).first | ||
| 26 | - auth ||= person.oauth_auths.create! profile: person, provider: provider, enabled: true | ||
| 27 | - if auth.enabled? && provider.enabled? | ||
| 28 | - self.current_user = person.user | 31 | + |
| 32 | + if provider.enabled? | ||
| 33 | + user = User.new | ||
| 34 | + user.email = auth.info.email | ||
| 35 | + user.login = auth.info.name.to_slug | ||
| 36 | + | ||
| 37 | + webfinger = OpenStruct.new( | ||
| 38 | + identifier: user.login, | ||
| 39 | + name: auth.info.name, | ||
| 40 | + created_at: Time.now, | ||
| 41 | + domain: auth.provider, | ||
| 42 | + email: user.email) | ||
| 43 | + user.external_person_id = ExternalPerson.get_or_create(webfinger).id | ||
| 44 | + user.store_oauth_providers | ||
| 45 | + self.current_user = user | ||
| 29 | else | 46 | else |
| 30 | session[:notice] = _("Can't login with %s") % provider.name | 47 | session[:notice] = _("Can't login with %s") % provider.name |
| 31 | end | 48 | end |
plugins/oauth_client/views/auth/_oauth_login.html.erb
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | <% end %> | 4 | <% end %> |
| 5 | <% providers.each do |provider| %> | 5 | <% providers.each do |provider| %> |
| 6 | <span class="provider"> | 6 | <span class="provider"> |
| 7 | - <%= link_to provider.image ? image_tag(provider.image.public_filename) : provider.name, "/plugin/oauth_client/#{provider.strategy}?id=#{provider.id}", :class => provider.strategy, :title => provider.name %> | 7 | + <%= link_to provider.image ? image_tag(provider.image.public_filename) : provider.name, "/plugin/oauth_client/#{provider.strategy}?id=#{provider.id}&action=external_login", :class => provider.strategy, :title => provider.name %> |
| 8 | </span> | 8 | </span> |
| 9 | <% end %> | 9 | <% end %> |
| 10 | 10 |