From bbf6adef38798c813bbc31d480eb6e70a8099d3b Mon Sep 17 00:00:00 2001 From: benji Date: Thu, 19 Mar 2015 18:46:41 +0100 Subject: [PATCH 1/3] remove text field for search --- app/views/orders/overview.html.erb | 1 - 1 file changed, 1 deletion(-) diff --git a/app/views/orders/overview.html.erb b/app/views/orders/overview.html.erb index c9239e8..95aa1bb 100644 --- a/app/views/orders/overview.html.erb +++ b/app/views/orders/overview.html.erb @@ -1,5 +1,4 @@ <%= render partial: 'flash' %> -<%= text_field_tag :search %>
<% @users.each do |user| %> From c9a24e81e2c6ab334ddc7d62db54f07b4f67e937 Mon Sep 17 00:00:00 2001 From: benji Date: Thu, 19 Mar 2015 19:03:17 +0100 Subject: [PATCH 2/3] Add tab omniauth --- Gemfile | 2 +- Gemfile.lock | 18 +++++++++++++++++- config/initializers/tab.rb | 39 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 config/initializers/tab.rb diff --git a/Gemfile b/Gemfile index 170b1a2..26435ea 100644 --- a/Gemfile +++ b/Gemfile @@ -75,4 +75,4 @@ gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw] gem 'coveralls', require: false -gem 'omniauth' +gem 'omniauth-oauth2' diff --git a/Gemfile.lock b/Gemfile.lock index 4c7f373..fb566ce 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -108,6 +108,8 @@ GEM execjs (2.3.0) faker (1.4.2) i18n (~> 0.5) + faraday (0.9.1) + multipart-post (>= 1.2, < 3) ffi (1.9.6-x64-mingw32) globalid (0.3.2) activesupport (>= 4.1.0) @@ -122,6 +124,7 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.2) + jwt (1.4.1) launchy (2.4.3) addressable (~> 2.3) loofah (2.0.1) @@ -132,6 +135,8 @@ GEM mini_portile (0.6.2) minitest (5.5.1) multi_json (1.10.1) + multi_xml (0.5.5) + multipart-post (2.0.0) mysql2 (0.3.17) net-scp (1.2.1) net-ssh (>= 2.6.5) @@ -141,9 +146,20 @@ GEM mini_portile (~> 0.6.0) nokogiri (1.6.6.2-x64-mingw32) mini_portile (~> 0.6.0) + oauth2 (1.0.0) + faraday (>= 0.8, < 0.10) + jwt (~> 1.0) + multi_json (~> 1.3) + multi_xml (~> 0.5) + rack (~> 1.2) omniauth (1.2.2) hashie (>= 1.2, < 4) rack (~> 1.0) + omniauth-oauth2 (1.2.0) + faraday (>= 0.8, < 0.10) + multi_json (~> 1.3) + oauth2 (~> 1.0) + omniauth (~> 1.2) orm_adapter (0.5.0) paper_trail (4.0.0.beta2) activerecord (>= 3.0, < 6.0) @@ -267,7 +283,7 @@ DEPENDENCIES jquery-rails launchy mysql2 - omniauth + omniauth-oauth2 paper_trail (~> 4.0.0.beta) paperclip rails (= 4.2) diff --git a/config/initializers/tab.rb b/config/initializers/tab.rb new file mode 100644 index 0000000..1c73d89 --- /dev/null +++ b/config/initializers/tab.rb @@ -0,0 +1,39 @@ +require 'omniauth-oauth2' + +module OmniAuth + module Strategies + class Tab < OmniAuth::Strategies::OAuth2 + # Give your strategy a name. + option :name, "tab" + + # This is where you pass the options you would pass when + # initializing your consumer from the OAuth gem. + option :client_options, { + :site => "http://kelder.zeus.ugent.be/oauth/oauth2/authorize/" + } + + # These are called after authentication has succeeded. If + # possible, you should try to set the UID without making + # additional calls (if the user id is returned with the token + # or as a URI parameter). This may not be possible with all + # providers. + uid{ raw_info['id'] } + + info do + { + :name => raw_info['name'], + } + end + + extra do + { + 'raw_info' => raw_info + } + end + + def raw_info + @raw_info ||= access_token.get('/me').parsed + end + end + end +end From eaa076b50826585ce2c78bd9786c01fedac4dbfd Mon Sep 17 00:00:00 2001 From: benji Date: Thu, 19 Mar 2015 22:37:16 +0100 Subject: [PATCH 3/3] Add oauth --- app/assets/javascripts/callbacks.js.coffee | 3 +++ app/assets/stylesheets/callbacks.css.scss | 3 +++ app/controllers/callbacks_controller.rb | 7 +++++++ app/helpers/callbacks_helper.rb | 2 ++ app/models/user.rb | 9 ++++++++- app/views/devise/sessions/new.html.erb | 1 - config/initializers/devise.rb | 3 ++- config/initializers/{tab.rb => zeuswpi.rb} | 17 +++++++++++------ config/routes.rb | 2 +- config/secrets.yml | 5 +++++ test/controllers/callbacks_controller_test.rb | 7 +++++++ 11 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 app/assets/javascripts/callbacks.js.coffee create mode 100644 app/assets/stylesheets/callbacks.css.scss create mode 100644 app/controllers/callbacks_controller.rb create mode 100644 app/helpers/callbacks_helper.rb rename config/initializers/{tab.rb => zeuswpi.rb} (62%) create mode 100644 test/controllers/callbacks_controller_test.rb diff --git a/app/assets/javascripts/callbacks.js.coffee b/app/assets/javascripts/callbacks.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/callbacks.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/callbacks.css.scss b/app/assets/stylesheets/callbacks.css.scss new file mode 100644 index 0000000..e4c4d53 --- /dev/null +++ b/app/assets/stylesheets/callbacks.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the callbacks controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/callbacks_controller.rb b/app/controllers/callbacks_controller.rb new file mode 100644 index 0000000..bbab5e8 --- /dev/null +++ b/app/controllers/callbacks_controller.rb @@ -0,0 +1,7 @@ +class CallbacksController < Devise::OmniauthCallbacksController + def zeuswpi + @user = User.from_omniauth(request.env["omniauth.auth"]) + @user.save + sign_in_and_redirect @user + end +end diff --git a/app/helpers/callbacks_helper.rb b/app/helpers/callbacks_helper.rb new file mode 100644 index 0000000..6c9550c --- /dev/null +++ b/app/helpers/callbacks_helper.rb @@ -0,0 +1,2 @@ +module CallbacksHelper +end diff --git a/app/models/user.rb b/app/models/user.rb index bd7ace0..6a4a158 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,7 +27,7 @@ # class User < ActiveRecord::Base - devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable + devise :database_authenticatable, :registerable, :rememberable, :trackable, :validatable, :omniauthable, :omniauth_providers => [:zeuswpi] has_paper_trail only: [:debt_cents, :admin, :orders_count, :koelkast] @@ -44,6 +44,13 @@ class User < ActiveRecord::Base scope :members, -> { where koelkast: false } + def self.from_omniauth(auth) + where(provider: auth.provider, uid: auth.uid).first_or_create do |user| + user.provider = auth.provider + user.uid = auth.uid + end + end + def debt self.debt_cents / 100.0 end diff --git a/app/views/devise/sessions/new.html.erb b/app/views/devise/sessions/new.html.erb index 7f01101..222f8a5 100644 --- a/app/views/devise/sessions/new.html.erb +++ b/app/views/devise/sessions/new.html.erb @@ -14,5 +14,4 @@ <% end %>
- <%= render "devise/shared/links" %> diff --git a/config/initializers/devise.rb b/config/initializers/devise.rb index 1f4e17f..447d856 100644 --- a/config/initializers/devise.rb +++ b/config/initializers/devise.rb @@ -1,5 +1,6 @@ # Use this hook to configure devise mailer, warden hooks and so forth. # Many of these configuration options can be set straight in your model. +# require 'yaml' Devise.setup do |config| # The secret key used by Devise. Devise uses this key to generate # random tokens. Changing this key will render invalid all existing @@ -232,7 +233,7 @@ Devise.setup do |config| # ==> OmniAuth # Add a new OmniAuth provider. Check the wiki for more information on setting # up on your models and hooks. - # config.omniauth :github, 'APP_ID', 'APP_SECRET', scope: 'user,public_repo' + config.omniauth :zeuswpi, Rails.application.secrets.omniauth_client_id, Rails.application.secrets.omniauth_client_secret # ==> Warden configuration # If you want to use other strategies, that are not supported by Devise, or diff --git a/config/initializers/tab.rb b/config/initializers/zeuswpi.rb similarity index 62% rename from config/initializers/tab.rb rename to config/initializers/zeuswpi.rb index 1c73d89..8b16a5b 100644 --- a/config/initializers/tab.rb +++ b/config/initializers/zeuswpi.rb @@ -2,14 +2,19 @@ require 'omniauth-oauth2' module OmniAuth module Strategies - class Tab < OmniAuth::Strategies::OAuth2 + class Zeuswpi < OmniAuth::Strategies::OAuth2 + + option :provider_ignores_state, true + # Give your strategy a name. - option :name, "tab" + option :name, "zeuswpi" # This is where you pass the options you would pass when # initializing your consumer from the OAuth gem. option :client_options, { - :site => "http://kelder.zeus.ugent.be/oauth/oauth2/authorize/" + site: "http://kelder.zeus.ugent.be", + authorize_url: "/oauth/oauth2/authorize/", + token_url: "/oauth/oauth2/token/", } # These are called after authentication has succeeded. If @@ -17,11 +22,11 @@ module OmniAuth # additional calls (if the user id is returned with the token # or as a URI parameter). This may not be possible with all # providers. - uid{ raw_info['id'] } + uid{ raw_info['username'] } info do { - :name => raw_info['name'], + # :nickname => raw_info['username'], } end @@ -32,7 +37,7 @@ module OmniAuth end def raw_info - @raw_info ||= access_token.get('/me').parsed + @raw_info ||= access_token.get('/oauth/api/current_user/').parsed end end end diff --git a/config/routes.rb b/config/routes.rb index 2b91e32..7e68b34 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,5 @@ Rails.application.routes.draw do - devise_for :users + devise_for :users, controllers: { omniauth_callbacks: "callbacks" } devise_scope :user do unauthenticated :user do diff --git a/config/secrets.yml b/config/secrets.yml index 90d7301..226185a 100644 --- a/config/secrets.yml +++ b/config/secrets.yml @@ -12,6 +12,8 @@ development: secret_key_base: 5d40610321e19e4f71ee2ba8af4f426fe15096c405da3800c6b33bed6779f2d11f55a0edc455974b19a01fd71f6cd508dba980305dbc55ff82521a2d12f891d8 + omniauth_client_id: "client_id" + omniauth_client_secret: "client_secret" test: secret_key_base: 961437e28e7d6055ffaad9cf1f8d614354f57f10cb2d7601c9d6ede72a03b9c9535ad9e63507e3eb31252c4895970a63117493408f2e9a46c7a0c4a5a7836b81 @@ -20,3 +22,6 @@ test: # instead read values from the environment. production: secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> + omniauth_client_id: "" + omniauth_client_secret: "" + diff --git a/test/controllers/callbacks_controller_test.rb b/test/controllers/callbacks_controller_test.rb new file mode 100644 index 0000000..6d160fc --- /dev/null +++ b/test/controllers/callbacks_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class CallbacksControllerTest < ActionController::TestCase + # test "the truth" do + # assert true + # end +end