Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: MitinPavel/current_user
base: bafd1711d1
...
head fork: MitinPavel/current_user
compare: ed4cdf5d82
Checking mergeability… Don't worry, you can still create the pull request.
  • 3 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
17 app/controllers/current_user/sessions_controller.rb
@@ -5,8 +5,8 @@ class SessionsController < ApplicationController
before_filter :check_key
def new
- @users = ::User.order 'email'
- @sign_in_url = key_sign_in_url ::CurrentUser::Config.instance.key
+ @users = users
+ @sign_in_url = key_sign_in_url ::CurrentUser.authentication_key
end
def create
@@ -17,9 +17,20 @@ def create
private
def check_key
- if ::CurrentUser::Config.instance.key != params[:key_id]
+ if ::CurrentUser.authentication_key != params[:key_id]
render_unauthorized
end
end
+
+ # TODO: move it from the controller
+ def users
+ identifier = ::CurrentUser.identifier
+
+ if identifier.respond_to? :call
+ ::User.all.sort { |x,y| identifier.call(y) <=> identifier.call(x) }
+ else
+ ::User.order identifier.to_s
+ end
+ end
end
end
View
2  app/views/current_user/sessions/new.html.erb
@@ -32,6 +32,6 @@
<% @users.each do |user| %>
<%= form_tag @sign_in_url do %>
<%= hidden_field_tag 'user_id', user.id %>
- <%= button_tag user.email, :type => 'text', :class => 'current_user_signin_button' %>
+ <%= submit_tag ::CurrentUser.identifier_for(user), :class => 'current_user_signin_button' %>
<% end %>
<% end %>
View
6 config/initializers/read_key_from_file.rb
@@ -1,6 +0,0 @@
-key_file_path = File.expand_path('config/current_user/key', Rails.root)
-
-if File.exist? key_file_path
- key = File.open(key_file_path) { |f| f.readline }
- ::CurrentUser::Config.instance.key = key
-end
View
30 lib/current_user.rb
@@ -1,4 +1,32 @@
-require "current_user/engine"
+require 'current_user/constants'
+require 'current_user/controller/helpers'
+require 'current_user/engine'
module CurrentUser
+ def self.setup
+ yield self
+ end
+
+ mattr_accessor :authentication_key
+
+ mattr_accessor :identifier
+
+ def identifier_for(user)
+ identifier = ::CurrentUser.identifier
+
+ if identifier.respond_to? :call
+ identifier.call user
+ else
+ user.send identifier
+ end
+ end
+ module_function :identifier_for
+
+ def read_authentication_key
+ key_file_path = File.expand_path 'config/current_user/key', Rails.root
+ if File.exist? key_file_path
+ File.open(key_file_path) { |f| f.readline }
+ end
+ end
+ module_function :read_authentication_key
end
View
4 lib/current_user/engine.rb
@@ -1,7 +1,3 @@
-require 'current_user/constants'
-require 'current_user/controller/helpers'
-require 'current_user/config'
-
module CurrentUser
class Engine < ::Rails::Engine
isolate_namespace CurrentUser
View
16 lib/generators/current_user/install_generator.rb
@@ -3,9 +3,21 @@
module CurrentUser
module Generators
class InstallGenerator < Rails::Generators::Base
- def create_initializer_file
+ source_root File.expand_path("../../templates", __FILE__)
+
+ desc "Creates a CurrentUser initializer and a authentication key file to your application."
+
+ def create_authentication_key
create_file "config/current_user/key", ::SecureRandom.hex(20)
end
+
+ def copy_initializer
+ template "current_user.rb", "config/initializers/current_user.rb"
+ end
+
+ def show_readme
+ readme "README" if behavior == :invoke
+ end
end
end
-end
+end
View
3  lib/generators/templates/README
@@ -0,0 +1,3 @@
+TODO
+
+Add README here :)
View
13 lib/generators/templates/current_user.rb
@@ -0,0 +1,13 @@
+::CurrentUser.setup do |config|
+
+ # Setup an authentication key.
+ # If you want to have a custom key, it might look like:
+ # config.authentication_key = '4242424242424242'
+ config.authentication_key = ::CurrentUser.read_authentication_key
+
+ # Setup how users might be distinguished via UI.
+ # It might be a symbol, a string or a lambda.
+ # For the last case it might be:
+ # config.identifier = -> { |u| "User with email #{u.email}" }
+ config.identifier = :email
+end
View
13 test/dummy/config/initializers/current_user.rb
@@ -0,0 +1,13 @@
+::CurrentUser.setup do |config|
+
+ # Setup an authentication key.
+ # If you want to have a custom key, it might look like:
+ # config.authentication_key = '4242424242424242'
+ config.authentication_key = ::CurrentUser.read_authentication_key
+
+ # Setup how users might be distinguished via UI.
+ # It might be a symbol, a string or a lambda.
+ # For the last case it might be:
+ # config.identifier = -> { |u| "User with email #{u.email}" }
+ config.identifier = :email
+end
View
5 test/integration/login_test.rb
@@ -11,8 +11,9 @@ class LoginTest < ActionDispatch::IntegrationTest
test "repeated successful login process" do
visit sing_in_page_path
- assert_equal 200, page.status_code, 'Responds with the OK http status code'
- page.click_link_or_button 'admin@my.app.com'
+ assert_equal 200, page.status_code, 'Sign in responds with the OK http status code'
+ page.click_button 'admin@my.app.com'
+ assert_equal 200, page.status_code, 'Root responds with the OK http status code'
assert page.has_content?('Welcome admin@my.app.com'), 'Contains the welcome message for the first user'
visit sing_in_page_path

No commit comments for this range

Something went wrong with that request. Please try again.