people_user_generator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (124) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/javascripts/people/account.js +2 -0
  5. data/app/assets/javascripts/people/application.js +13 -0
  6. data/app/assets/stylesheets/people/account.css +7 -0
  7. data/app/assets/stylesheets/people/application.css +15 -0
  8. data/app/assets/stylesheets/scaffold.css +56 -0
  9. data/app/controllers/people/account_controller.rb +86 -0
  10. data/app/controllers/people/api/v1/application_controller.rb +5 -0
  11. data/app/controllers/people/api/v1/authentication_controller.rb +140 -0
  12. data/app/controllers/people/api/v1/users_controller.rb +120 -0
  13. data/app/controllers/people/application_controller.rb +4 -0
  14. data/app/helpers/people/account_helper.rb +4 -0
  15. data/app/helpers/people/application_helper.rb +4 -0
  16. data/app/helpers/people/users_helper.rb +4 -0
  17. data/app/mailers/people/account_mailer.rb +68 -0
  18. data/app/models/people/email_hash.rb +44 -0
  19. data/app/models/people/user.rb +9 -0
  20. data/app/models/people/v1/user.rb +54 -0
  21. data/app/serializers/people/v1/user_serializer.rb +61 -0
  22. data/app/views/layouts/people/application.html.erb +14 -0
  23. data/app/views/layouts/people/default/application.html.erb +14 -0
  24. data/app/views/people/account/changed_password.html.erb +3 -0
  25. data/app/views/people/account/email_confirm.html.erb +3 -0
  26. data/app/views/people/account/forgotten_password.html.erb +12 -0
  27. data/app/views/people/account/new_password.html.erb +25 -0
  28. data/app/views/people/account/posted_email_sent.html.erb +3 -0
  29. data/app/views/people/account_mailer/email_confirmation.html.erb +14 -0
  30. data/app/views/people/account_mailer/email_confirmation.text.erb +5 -0
  31. data/app/views/people/account_mailer/forgot_password.html.erb +15 -0
  32. data/app/views/people/account_mailer/forgot_password.text.erb +8 -0
  33. data/app/views/people/account_mailer/password_reset.html.erb +10 -0
  34. data/app/views/people/account_mailer/password_reset.text.erb +3 -0
  35. data/app/views/people/account_mailer/unlock_account.html.erb +14 -0
  36. data/app/views/people/account_mailer/unlock_account.text.erb +7 -0
  37. data/app/views/people/account_mailer/welcome_email.html.erb +15 -0
  38. data/app/views/people/account_mailer/welcome_email.text.erb +6 -0
  39. data/app/views/people/account_mailer/you_confirmed_email.html.erb +10 -0
  40. data/app/views/people/account_mailer/you_confirmed_email.text.erb +3 -0
  41. data/config/routes.rb +29 -0
  42. data/db/migrate/20141021150908_create_people_users.rb +13 -0
  43. data/db/migrate/20141022145945_create_people_email_hashes.rb +11 -0
  44. data/db/migrate/20141022161442_add_confirmed_to_people_users.rb +5 -0
  45. data/db/migrate/20141022161524_add_locked_to_people_users.rb +5 -0
  46. data/db/migrate/20141022161543_add_attempts_to_people_users.rb +5 -0
  47. data/db/migrate/20141023203747_remove_hash_from_people_email_hash.rb +5 -0
  48. data/db/migrate/20141023203806_add_token_to_people_email_hash.rb +5 -0
  49. data/db/migrate/20141024042651_remove_type_from_people_email_hash.rb +5 -0
  50. data/db/migrate/20141024042708_add_email_type_to_people_email_hash.rb +5 -0
  51. data/lib/generators/people/admin/USAGE +8 -0
  52. data/lib/generators/people/admin/admin_generator.rb +17 -0
  53. data/lib/generators/people/admin/templates/active_admin.rb +100 -0
  54. data/lib/generators/people/admin/templates/email_hash.rb +82 -0
  55. data/lib/generators/people/all/USAGE +8 -0
  56. data/lib/generators/people/all/all_generator.rb +18 -0
  57. data/lib/generators/people/authorizations/USAGE +8 -0
  58. data/lib/generators/people/authorizations/authorizations_generator.rb +19 -0
  59. data/lib/generators/people/authorizations/templates/authentication_authorization.rb +25 -0
  60. data/lib/generators/people/authorizations/templates/authorization.rb +106 -0
  61. data/lib/generators/people/tests/USAGE +8 -0
  62. data/lib/generators/people/tests/templates/account_routing.rb +37 -0
  63. data/lib/generators/people/tests/templates/authentication_requests.rb +166 -0
  64. data/lib/generators/people/tests/templates/authentication_routing.rb +37 -0
  65. data/lib/generators/people/tests/templates/email_hash_factory.rb +7 -0
  66. data/lib/generators/people/tests/templates/email_hash_model.rb +14 -0
  67. data/lib/generators/people/tests/templates/email_hash_request.rb +174 -0
  68. data/lib/generators/people/tests/templates/factory.rb +17 -0
  69. data/lib/generators/people/tests/templates/models.rb +61 -0
  70. data/lib/generators/people/tests/templates/requests.rb +180 -0
  71. data/lib/generators/people/tests/templates/routing.rb +47 -0
  72. data/lib/generators/people/tests/tests_generator.rb +25 -0
  73. data/lib/people.rb +4 -0
  74. data/lib/people/engine.rb +5 -0
  75. data/lib/people/version.rb +3 -0
  76. data/lib/tasks/people_tasks.rake +4 -0
  77. data/test/controllers/people/account_controller_test.rb +21 -0
  78. data/test/controllers/people/users_controller_test.rb +51 -0
  79. data/test/dummy/README.rdoc +28 -0
  80. data/test/dummy/Rakefile +6 -0
  81. data/test/dummy/app/assets/javascripts/application.js +13 -0
  82. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  83. data/test/dummy/app/controllers/application_controller.rb +5 -0
  84. data/test/dummy/app/helpers/application_helper.rb +2 -0
  85. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  86. data/test/dummy/bin/bundle +3 -0
  87. data/test/dummy/bin/rails +4 -0
  88. data/test/dummy/bin/rake +4 -0
  89. data/test/dummy/config.ru +4 -0
  90. data/test/dummy/config/application.rb +23 -0
  91. data/test/dummy/config/boot.rb +5 -0
  92. data/test/dummy/config/database.yml +25 -0
  93. data/test/dummy/config/environment.rb +5 -0
  94. data/test/dummy/config/environments/development.rb +29 -0
  95. data/test/dummy/config/environments/production.rb +80 -0
  96. data/test/dummy/config/environments/test.rb +36 -0
  97. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  98. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  99. data/test/dummy/config/initializers/inflections.rb +16 -0
  100. data/test/dummy/config/initializers/mime_types.rb +5 -0
  101. data/test/dummy/config/initializers/secret_token.rb +12 -0
  102. data/test/dummy/config/initializers/session_store.rb +3 -0
  103. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  104. data/test/dummy/config/locales/en.yml +23 -0
  105. data/test/dummy/config/routes.rb +4 -0
  106. data/test/dummy/db/development.sqlite3 +0 -0
  107. data/test/dummy/db/schema.rb +38 -0
  108. data/test/dummy/log/development.log +122 -0
  109. data/test/dummy/public/404.html +58 -0
  110. data/test/dummy/public/422.html +58 -0
  111. data/test/dummy/public/500.html +57 -0
  112. data/test/dummy/public/favicon.ico +0 -0
  113. data/test/fixtures/people/email_hashes.yml +11 -0
  114. data/test/fixtures/people/users.yml +11 -0
  115. data/test/helpers/people/account_helper_test.rb +6 -0
  116. data/test/helpers/people/users_helper_test.rb +6 -0
  117. data/test/integration/navigation_test.rb +10 -0
  118. data/test/mailers/people/account_mailer_test.rb +9 -0
  119. data/test/mailers/previews/people/account_mailer_preview.rb +6 -0
  120. data/test/models/people/email_hash_test.rb +9 -0
  121. data/test/models/people/user_test.rb +9 -0
  122. data/test/people_test.rb +7 -0
  123. data/test/test_helper.rb +15 -0
  124. metadata +353 -0
@@ -0,0 +1,4 @@
1
+ module People
2
+ class ApplicationController < ::ApplicationController
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module People
2
+ module AccountHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module People
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module People
2
+ module UsersHelper
3
+ end
4
+ end
@@ -0,0 +1,68 @@
1
+ module People
2
+ class AccountMailer < ActionMailer::Base
3
+ before_action :set_host
4
+ #default from: "from@example.com"
5
+
6
+ #Need to send an email saying welcome
7
+ def welcome_email(user)
8
+ @user = user
9
+ mail(to: @user.email, subject: 'Welcome!')
10
+ end
11
+
12
+ #Need to send an email saying confirm email address
13
+ def email_confirmation(user)
14
+ #Need to send an email with a link to account_controller#email_confirm
15
+ @user = user
16
+ @email_hash = @user.email_hashes.new
17
+ @email_hash.email_type = ::People::EmailHash.email_confirm
18
+ @email_hash.save
19
+ mail(to: @user.email, subject: 'Confirm your email')
20
+ end
21
+
22
+ def you_confirmed_email(user)
23
+ @user = user
24
+ mail(to: @user.email, subject: 'Email confirmed')
25
+ end
26
+
27
+ #Need to send an email saying your account is locked
28
+ def unlock_account(user)
29
+ #Need to send an email with a link to account_controller#new_password
30
+ @user = user
31
+ @email_hash = @user.email_hashes.new
32
+ @email_hash.email_type = ::People::EmailHash.forgotten_password
33
+ @email_hash.save
34
+ mail(to: @user.email, subject: 'Account locked')
35
+ end
36
+
37
+ #Need to send an email saying click to change password
38
+ def forgot_password(user)
39
+ #Need to send an email with a link to account_controller#new_password
40
+ @user = user
41
+ @email_hash = @user.email_hashes.new
42
+ @email_hash.email_type = ::People::EmailHash.forgotten_password
43
+ @email_hash.save
44
+ mail(to: @user.email, subject: 'Reset your password')
45
+ end
46
+
47
+ def password_reset(user)
48
+ @user = user
49
+ mail(to: @user.email, subject: 'Password reset')
50
+ end
51
+
52
+ private
53
+
54
+ def set_host
55
+ @host = ""
56
+ if Rails.env.development?
57
+ @host = "http://localhost:3000"
58
+ end
59
+ if Rails.env.test?
60
+ @host = "http://localhost:3000"
61
+ end
62
+ if Rails.env.production?
63
+ @host = "https://www.example.com"
64
+ end
65
+ end
66
+
67
+ end
68
+ end
@@ -0,0 +1,44 @@
1
+ require 'type_cartographer'
2
+
3
+ module People
4
+ class EmailHash < ActiveRecord::Base
5
+
6
+ before_validation :setup_email_hash
7
+
8
+ belongs_to :user, class_name: "::People::V1::User", foreign_key: "user_id"
9
+
10
+ validates :user_id, presence: true
11
+ validates :token, presence: true
12
+ validates :email_type, presence: true
13
+
14
+ def setup_email_hash
15
+ if self.token.nil? || self.token.blank?
16
+ self.token = ::Devise.friendly_token
17
+ end
18
+ return true
19
+ end
20
+
21
+ def self.email_confirm
22
+ return 1
23
+ end
24
+
25
+ def self.forgotten_password
26
+ return 2
27
+ end
28
+
29
+ def self.expiration_minutes
30
+ return 360
31
+ end
32
+
33
+ def self.expired?(email_hash)
34
+ date = email_hash.created_at.utc + ::People::EmailHash.expiration_minutes.minutes
35
+ if ::Time.now.utc > date
36
+ self.destroy
37
+ return true
38
+ else
39
+ return false
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -0,0 +1,9 @@
1
+ module People
2
+ class User < ActiveRecord::Base
3
+
4
+ def self.recent(num)
5
+ ::People::User.order("created_at DESC").limit(num)
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,54 @@
1
+ require 'type_cartographer'
2
+
3
+ module People
4
+ class V1::User < ActiveRecord::Base
5
+ self.table_name = 'people_users'
6
+
7
+ before_save { self.email = email.downcase }
8
+ before_save { self.username = username.downcase }
9
+ before_validation :sanitize_attributes
10
+ after_create :setup_user
11
+
12
+ has_secure_password
13
+
14
+ has_many :tokens, :as => :imageable, :class_name => "::Arcadex::Token", dependent: :destroy
15
+ has_many :email_hashes, :class_name => "::People::EmailHash", dependent: :destroy
16
+
17
+ validates :username, presence: true, :uniqueness => { :case_sensitive => false }
18
+ #validates_format_of :username, :with => /\A[A-Za-z0-9\d]+\Z/i
19
+ validates :email, presence: true, :uniqueness => { :case_sensitive => false }
20
+ #Might need a regex for emails, or just rather confirm them
21
+
22
+ def sanitize_attributes
23
+ if self.locked.nil?
24
+ self.locked = false
25
+ end
26
+ if self.confirmed.nil?
27
+ self.confirmed = false
28
+ end
29
+ if self.attempts.nil?
30
+ self.attempts = 0
31
+ end
32
+ return true
33
+ end
34
+
35
+ def setup_user
36
+ create_token
37
+ return true
38
+ end
39
+
40
+ def create_token
41
+ self.tokens.create!
42
+ return true
43
+ end
44
+
45
+ def self.over_max_attempts?(attempts)
46
+ if attempts > 20
47
+ return true
48
+ else
49
+ return false
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,61 @@
1
+ require 'authorization'
2
+
3
+ module People
4
+ class V1::UserSerializer < ActiveModel::Serializer
5
+ embed :ids, include: true
6
+
7
+ attributes :id
8
+ attributes :email
9
+ attributes :username
10
+ attributes :password_digest
11
+ attributes :confirmed
12
+ attributes :locked
13
+ attributes :attempts
14
+ attributes :created_at
15
+ attributes :updated_at
16
+
17
+ has_many :tokens
18
+
19
+
20
+ def include_id?
21
+ return ::Authorization::People::V1::User.include_id?(current_user,object,@options)
22
+ end
23
+
24
+ def include_email?
25
+ return ::Authorization::People::V1::User.include_email?(current_user,object,@options)
26
+ end
27
+
28
+ def include_username?
29
+ return ::Authorization::People::V1::User.include_username?(current_user,object,@options)
30
+ end
31
+
32
+ def include_password_digest?
33
+ return ::Authorization::People::V1::User.include_password_digest?(current_user,object,@options)
34
+ end
35
+
36
+ def include_confirmed?
37
+ return ::Authorization::People::V1::User.include_confirmed?(current_user,object,@options)
38
+ end
39
+
40
+ def include_locked?
41
+ return ::Authorization::People::V1::User.include_locked?(current_user,object,@options)
42
+ end
43
+
44
+ def include_attempts?
45
+ return ::Authorization::People::V1::User.include_attempts?(current_user,object,@options)
46
+ end
47
+
48
+ def include_created_at?
49
+ return ::Authorization::People::V1::User.include_created_at?(current_user,object,@options)
50
+ end
51
+
52
+ def include_updated_at?
53
+ return ::Authorization::People::V1::User.include_updated_at?(current_user,object,@options)
54
+ end
55
+
56
+ def include_associations!
57
+ include! :tokens if ::Authorization::People::V1::User.include_tokens?(current_user,object,@options)
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Account</title>
5
+ <%= stylesheet_link_tag "people/application", media: "all" %>
6
+ <%= javascript_include_tag "people/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Account</title>
5
+ <%= stylesheet_link_tag "people/application", media: "all" %>
6
+ <%= javascript_include_tag "people/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,3 @@
1
+ <div class="text-center">
2
+ <h2>Password is changed!</h2>
3
+ </div>
@@ -0,0 +1,3 @@
1
+ <div class="text-center">
2
+ <h2>Account email is confirmed!</h2>
3
+ </div>
@@ -0,0 +1,12 @@
1
+ <div class="text-center">
2
+ <h1>Forgotten Password</h1>
3
+ <h4>Enter your email and we will send a password reset link</h4>
4
+ </div>
5
+
6
+ <div class="text-center">
7
+ <%= form_tag({controller: "people/account", action: "posted_email"}, method: "post") do %>
8
+ <%= label_tag(:email, "Enter your email") %><br>
9
+ <%= text_field_tag(:email) %><br><br>
10
+ <%= submit_tag("Password Reset") %>
11
+ <% end %>
12
+ </div>
@@ -0,0 +1,25 @@
1
+ <div class="text-center">
2
+ <h1>New password form</h1>
3
+ <h4>Enter both password and password confirmation</h4>
4
+ </div>
5
+
6
+ <div class="text-center">
7
+ <%= form_tag({controller: "people/account", action: "change_password", eh_id: "#{@email_hash.id}", token: "#{@email_hash.token}"}, method: "post") do %>
8
+ <% if @user.errors.any? %>
9
+ <div id="error_explanation">
10
+ <h4><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h4>
11
+
12
+ <ul>
13
+ <% @user.errors.full_messages.each do |message| %>
14
+ <li><%= message %></li>
15
+ <% end %>
16
+ </ul>
17
+ </div>
18
+ <% end %>
19
+ <%= label_tag(:password, "New Password") %><br>
20
+ <%= password_field_tag(:password) %><br>
21
+ <%= label_tag(:password_confirmation, "Confirmation") %><br>
22
+ <%= password_field_tag(:password_confirmation) %><br><br>
23
+ <%= submit_tag("Password Reset") %>
24
+ <% end %>
25
+ </div>
@@ -0,0 +1,3 @@
1
+ <div class="text-center">
2
+ <h2>Email sent! Click the link in your email to begin resetting your password</h2>
3
+ </div>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
5
+ </head>
6
+ <body>
7
+ <h1>Email Confirmed!</h1>
8
+ <p>
9
+ Your email is now confirmed.<br>
10
+ </p>
11
+
12
+ <p>Thanks for joining and have a great day!</p>
13
+ </body>
14
+ </html>
@@ -0,0 +1,5 @@
1
+ Email Confirmed!
2
+
3
+ Your email is now confirmed.
4
+
5
+ Thanks for joining and have a great day!
@@ -0,0 +1,15 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
5
+ </head>
6
+ <body>
7
+ <h1>Password reset!</h1>
8
+ <p>
9
+ Our records show you were recently having problems logging in.
10
+ To change your password follow the link below. Do not share this
11
+ link.<br><br>
12
+ <a href="<%= @host %><%= url_for(:controller => 'people/account', :action => 'new_password', :eh_id => "#{@email_hash.id}", :token => "#{@email_hash.token}").html_safe %>">Password Reset</a>
13
+ </p>
14
+ </body>
15
+ </html>
@@ -0,0 +1,8 @@
1
+ Password reset!
2
+
3
+ Our records show you were recently having problems logging in.
4
+ To change your password follow the link below. Do not share this
5
+ link.
6
+
7
+
8
+ <%= @host %><%= url_for(:controller => 'people/account', :action => 'new_password', :eh_id => "#{@email_hash.id}", :token => "#{@email_hash.token}").html_safe %>
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
5
+ </head>
6
+ <body>
7
+ <h1>Password reset!</h1><br>
8
+ <p>Log in with <%= @user.email %> and your new password.</p>
9
+ </body>
10
+ </html>
@@ -0,0 +1,3 @@
1
+ Password reset!
2
+
3
+ Log in with <%= @user.email %> and your new password.
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
5
+ </head>
6
+ <body>
7
+ <h1>Account locked!</h1>
8
+ <p>
9
+ There were too many failed login attempts on your account. To unlock your account,
10
+ follow the link and change your password. Do not share this link.<br><br>
11
+ <a href="<%= @host %><%= url_for(:controller => 'people/account', :action => 'new_password',:eh_id => "#{@email_hash.id}", :token => "#{@email_hash.token}").html_safe %>">Reset password</a>
12
+ </p>
13
+ </body>
14
+ </html>
@@ -0,0 +1,7 @@
1
+ Account locked!
2
+
3
+ There were too many failed login attempts on your account. To unlock your account,
4
+ follow the link and change your password. Do not share this link.
5
+
6
+
7
+ <%= @host %><%= url_for(:controller => 'people/account', :action => 'new_password',:eh_id => "#{@email_hash.id}", :token => "#{@email_hash.token}").html_safe %>