authem 1.5.0 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (44) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +3 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.ruby-version +1 -0
  6. data/.travis.yml +6 -0
  7. data/Appraisals +12 -0
  8. data/CHANGELOG.md +42 -0
  9. data/Gemfile +10 -0
  10. data/README.markdown +15 -1
  11. data/Rakefile +11 -5
  12. data/authem.gemspec +25 -0
  13. data/gemfiles/rails_4.0.gemfile +16 -0
  14. data/gemfiles/rails_4.1.gemfile +15 -0
  15. data/lib/authem.rb +4 -10
  16. data/lib/authem/controller.rb +50 -0
  17. data/lib/authem/errors/ambigous_role.rb +8 -0
  18. data/lib/authem/errors/unknown_role.rb +7 -0
  19. data/lib/authem/railtie.rb +12 -0
  20. data/lib/authem/role.rb +62 -0
  21. data/lib/authem/session.rb +41 -0
  22. data/lib/authem/support.rb +129 -0
  23. data/lib/authem/token.rb +5 -5
  24. data/lib/authem/user.rb +27 -13
  25. data/lib/authem/version.rb +1 -1
  26. data/lib/generators/authem/session/session_generator.rb +12 -0
  27. data/lib/generators/authem/session/templates/create_sessions.rb +15 -0
  28. data/lib/generators/authem/user/templates/create_table_migration.rb +22 -0
  29. data/lib/generators/authem/user/templates/model.rb +11 -0
  30. data/lib/generators/authem/user/user_generator.rb +13 -0
  31. data/spec/controller_spec.rb +413 -0
  32. data/spec/session_spec.rb +52 -0
  33. data/spec/spec_helper.rb +4 -0
  34. data/spec/support/active_record.rb +45 -0
  35. data/spec/support/i18n.rb +1 -0
  36. data/spec/support/time.rb +1 -0
  37. data/spec/token_spec.rb +10 -0
  38. data/spec/user_spec.rb +115 -0
  39. metadata +42 -112
  40. data/lib/authem/base_user.rb +0 -54
  41. data/lib/authem/config.rb +0 -21
  42. data/lib/authem/controller_support.rb +0 -51
  43. data/lib/authem/sorcery_user.rb +0 -24
  44. data/lib/generators/authem/model/model_generator.rb +0 -23
@@ -1,54 +0,0 @@
1
- module Authem::BaseUser
2
- extend ::ActiveSupport::Concern
3
-
4
- included do
5
- validates_uniqueness_of :email
6
- validates_format_of :email, with: /\A\S+@\S+\z/
7
- end
8
-
9
- module ClassMethods
10
- def find_by_email(email)
11
- find_by("lower(email) = ?", email.downcase)
12
- end
13
- end
14
-
15
- def remember_token
16
- self[:remember_token] || generate_token(:remember)
17
- end
18
-
19
- def session_token
20
- self[:session_token] || generate_token(:session)
21
- end
22
-
23
- def reset_password(password, confirmation)
24
- if password.blank?
25
- self.errors.add :password, :blank
26
- return false
27
- end
28
-
29
- reset_password_token = self.reset_password_token
30
-
31
- self.password = password
32
- self.password_confirmation = confirmation
33
- self.reset_password_token = nil
34
-
35
- return true if save
36
-
37
- self.reset_password_token = reset_password_token
38
- false
39
- end
40
-
41
- def reset_password_token!
42
- generate_token :reset_password
43
- end
44
-
45
- def reset_session_token!
46
- generate_token :session
47
- end
48
-
49
- private
50
-
51
- def generate_token(type)
52
- Authem::Token.generate.tap { |token| update_column("#{type}_token", token) }
53
- end
54
- end
@@ -1,21 +0,0 @@
1
- module Authem
2
- module Config
3
- extend self
4
-
5
- attr_accessor :sign_in_path
6
-
7
- Authem::Config.sign_in_path ||= :sign_in
8
-
9
- def configure
10
- yield self
11
- end
12
-
13
- def user_class
14
- @user_class.constantize
15
- end
16
-
17
- def user_class=(user_class)
18
- @user_class = user_class.to_s
19
- end
20
- end
21
- end
@@ -1,51 +0,0 @@
1
- module Authem::ControllerSupport
2
- extend ActiveSupport::Concern
3
-
4
- protected
5
-
6
- def sign_in(user, remember_me=true)
7
- cookies.permanent.signed[:remember_token] = user.remember_token if remember_me
8
- session[:session_token] = user.session_token
9
- end
10
-
11
- def sign_out
12
- cookies[:remember_token] = nil
13
- session[:session_token] = nil
14
- reset_session
15
- current_user.reset_session_token! if current_user
16
- @current_user = nil
17
- end
18
-
19
- def current_user
20
- @current_user ||= if session[:session_token]
21
- Authem::Config.user_class.where(session_token: session[:session_token].to_s).first
22
- elsif cookies[:remember_token].present?
23
- Authem::Config.user_class.where(remember_token: cookies.signed[:remember_token].to_s).first.tap do |user|
24
- session[:session_token] = user.session_token if user
25
- end
26
- end
27
- end
28
-
29
- def require_user
30
- unless signed_in?
31
- session[:return_to_url] = request.url unless request.xhr?
32
- redirect_to Authem::Config.sign_in_path
33
- end
34
- end
35
-
36
- def signed_in?
37
- current_user.present?
38
- end
39
-
40
- def redirect_back_or_to(url, flash_hash = {})
41
- url = session[:return_to_url] || url
42
- session[:return_to_url] = nil
43
- redirect_to url, flash: flash_hash
44
- end
45
-
46
- included do
47
- helper_method :current_user
48
- helper_method :signed_in?
49
- end
50
-
51
- end
@@ -1,24 +0,0 @@
1
- module Authem::SorceryUser
2
- extend ::ActiveSupport::Concern
3
- include Authem::BaseUser
4
-
5
- included do
6
- attr_accessor :password, :password_confirmation
7
- validates_presence_of :password, on: :create
8
- validates_presence_of :password_confirmation, if: ->{ password.present? }
9
- validates_confirmation_of :password, if: ->{ password.present? }
10
-
11
- before_save :encrypt_password
12
- end
13
-
14
- def authenticate(password)
15
- self if crypted_password.present? && ::BCrypt::Password.new(crypted_password) == [password, salt].join
16
- end
17
-
18
- def encrypt_password
19
- if password.present?
20
- self.salt = ::BCrypt::Engine.generate_salt
21
- self.crypted_password = ::BCrypt::Password.create([password, salt].join)
22
- end
23
- end
24
- end
@@ -1,23 +0,0 @@
1
- require 'rails/generators/base'
2
-
3
- module Authem
4
- class ModelGenerator < Rails::Generators::Base
5
-
6
- argument :model_name, type: :string, default: "user"
7
-
8
- def generate_model
9
- generate "model #{model_name} email:string password_digest:string reset_password_token:string session_token:string remember_token:string"
10
- end
11
-
12
- def update_model_to_include_authem
13
- insert_into_file "app/models/#{model_name}.rb", "\n include Authem::User\n\n", after: "class #{model_name.camelize} < ActiveRecord::Base\n"
14
- end
15
-
16
- def add_initializer
17
- create_file 'config/initializers/authem.rb' do
18
- "Authem.configure do |config|\n config.user_class = #{model_name.camelize}\nend"
19
- end
20
- end
21
-
22
- end
23
- end