/
auth_generator.rb
193 lines (145 loc) · 5.9 KB
/
auth_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
require 'rails/generators'
require_relative '../../diesel/actions'
module Diesel
module Generators
class AuthGenerator < ::Rails::Generators::Base
include ::Diesel::Actions
desc "Login/Logout Stuff"
source_root ::File.expand_path("../templates", __FILE__)
class_option :template_engine
def initialize(args = [], options = {}, config = {})
super(args, options, config)
end
def create_auth_folders
log :create_auth_folders, ""
in_root do
mkdir_p "app/views/sessions"
mkdir_p "app/views/users"
mkdir_p "test/support"
end
end
def generate_auth_model
log :generate_auth_model, ""
generate :model, "user", "first_name:string",
"last_name:string",
"email:string",
"password_digest:string"
inject_into_file 'app/models/user.rb',
" has_secure_password\n",
after: "class User < ActiveRecord::Base\n"
end
def copy_auth_files
log :copy_auth_files, ""
# Controllers
copy_file "controllers/sessions_controller.rb", "app/controllers/sessions_controller.rb"
copy_file "controllers/users_controller.rb", "app/controllers/users_controller.rb"
# Add Concern to Application Controller
inject_into_file 'app/controllers/application_controller.rb',
"\n include SessionAuthentication\n",
after: "protect_from_forgery with: :exception\n"
# Views
copy_file "views/sessions_new.html.erb", "app/views/sessions/new.html.erb"
copy_file "views/users_edit.html.erb", "app/views/users/edit.html.erb"
copy_file "views/users_show.json.jbuilder", "app/views/users/show.json.jbuilder"
# Concerns
copy_file "controllers/session_authentication.rb",
'app/controllers/concerns/session_authentication.rb'
copy_file "controllers/api_authentication.rb",
'app/controllers/concerns/api_authentication.rb'
copy_file "controllers/api_versioning.rb",
'app/controllers/concerns/api_versioning.rb'
# Tests
copy_file "test/session_auth_test_helper.rb",
'test/support/session_auth_test_helper.rb'
copy_file "test/api_helper.rb",
'test/support/api_helper.rb'
copy_file "test/authorization_helper.rb",
'test/support/authorization_helper.rb'
# Test Helper
inject_into_file 'test/test_helper.rb',
"Dir[Rails.root.join('test/support/**/*.rb')].each { |file| require file }\n",
after: "require 'rails/test_help'\n"
inject_into_file 'test/test_helper.rb',
"\nclass ActionController::TestCase\n include ::SessionAuthTestHelper, ::ApiHelper\nend",
after: "end\n"
copy_file "test/models/user_test.rb",
"test/models/user_test.rb"
copy_file "test/controllers/sessions_controller_test.rb",
"test/controllers/sessions_controller_test.rb"
copy_file "test/controllers/users_controller_test.rb",
"test/controllers/users_controller_test.rb"
copy_file "test/controllers/sessions_api_test.rb",
"test/controllers/sessions_api_test.rb"
copy_file "test/controllers/users_api_test.rb",
"test/controllers/users_api_test.rb"
copy_file "test/fixtures/users.yml",
"test/fixtures/users.yml", force: true
end
def add_auth_routes
log :add_auth_routes, ""
auth_routes = <<-ROUTE
# User Resource
resources :users
# Login & Logout
delete "/logout", to: "sessions#destroy", as: :logout
post "/login", to: "sessions#create", as: :perform_login
get "/login", to: "sessions#new", as: :login
# Default Route should go to the login page
root "sessions#new"
ROUTE
route auth_routes
end
# Adds password_confirmation to the filtered parameters array
def filter_password_confirmation
log :filter_password_confirmation, ""
replace_in_file 'config/initializers/filter_parameter_logging.rb',
'Rails.application.config.filter_parameters += [:password]',
'Rails.application.config.filter_parameters += [:password, :password_confirmation]'
end
# def create_login_template
# log :create_login_template, ""
# copy_file "views/login_layout.html.erb",
# 'app/views/layouts/login.html.erb'
# mkdir_p "app/views/application"
# touch "app/views/application/_doctype.html.erb"
# touch "app/views/application/_nav.html.erb"
# touch "app/views/application/_flashes.html.erb"
# touch "app/views/application/_footer.html.erb"
# end
def include_session_auth_concern
log :include_session_auth_concern, ""
content = <<-EOF
include SessionAuthentication,
ApiAuthentication,
ApiVersioning
def json_requested?
request.format.json?
end
def render_unauthorized
self.headers['WWW-Authenticate'] = "Token realm=\"\#{api_config['company_name']}\""
render json: 'Bad credentials', status: 401
end
private
def api_config
::Rails.application.config_for(:api)
end
EOF
insert_into_file("app/controllers/application_controller.rb",
content,
after: "protect_from_forgery with: :exception\n\n")
end
def copy_locale
log :copy_locale, ""
copy_file "config/locales/login.en.yml", "config/locales/login.en.yml"
end
def add_bcrypt_gem
log :add_bcrypt_gem, ""
gem 'bcrypt'
end
def execute_bundle_install
log :execute_bundle_install, ""
in_root { run "bundle" }
end
end
end
end