Skip to content

Commit 167eb75

Browse files
committed
bcrypt未完成版
1 parent 71ac661 commit 167eb75

16 files changed

Lines changed: 181 additions & 14 deletions

Gemfile

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ gem 'uglifier', '>= 1.3.0'
2525
gem 'coffee-rails', '~> 4.2'
2626

2727
# Turbolinks
28-
# 详情参考链接: https://github.com/turbolinks/turbolinks
28+
# 详情参考链接: https://github.com/turbolinks/turbolinks 或者参考docs/Turbolinks.md
2929
gem 'turbolinks', '~> 5'
3030

31-
# 使用生成器生成JSON对象,常见如views/**/xxx.json.jbuilder文件 .详细见: /docs/jbuilder
31+
# 使用生成器生成JSON对象,常见如views/**/xxx.json.jbuilder文件 .详细见: /docs/Jbuilder.md
3232
gem 'jbuilder', '~> 2.5'
3333

3434
# Use ActiveModel has_secure_password
@@ -38,6 +38,7 @@ gem 'bcrypt', '~> 3.1.7'
3838
# gem 'mini_magick', '~> 4.8'
3939

4040
# Reduces boot times through caching; required in config/boot.rb
41+
# 使用缓存的方式加快rails应用的启动速度。缓存到tmp/cache目录下,一般是该目录下的bootsnap*文件夹
4142
gem 'bootsnap', '>= 1.1.0', require: false
4243

4344
# 是用于编译和服务assets的Ruby库
@@ -100,13 +101,21 @@ gem 'bootstrap', '~> 4.3.0'
100101
# 可以为我们的应用加入操作权限控制。
101102
# gem "pundit"
102103

104+
# elasticsearch中的apm瓶颈监控工具
105+
gem 'elastic-apm'
106+
gem "elasticsearch-model"
107+
gem "elasticsearch-rails"
108+
103109
# 拒绝低版本浏览器的访问,并给其返回一个让其升级的页面。
104110
gem 'browser_warrior', '>= 0.8.0'
105111

106112
# 异常监控
107113
gem 'exception_notification'
108114
gem 'slack-notifier'
109115

116+
# RPC远程过程调用
117+
gem 'grpc'
118+
110119
group :development, :test do
111120
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
112121
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]

Gemfile.lock

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,24 @@ GEM
149149
descendants_tracker (0.0.4)
150150
thread_safe (~> 0.3, >= 0.3.1)
151151
diff-lcs (1.3)
152+
domain_name (0.5.20190701)
153+
unf (>= 0.0.5, < 1.0.0)
154+
elastic-apm (3.2.0)
155+
concurrent-ruby (~> 1.0)
156+
http (>= 3.0)
157+
elasticsearch (7.4.0)
158+
elasticsearch-api (= 7.4.0)
159+
elasticsearch-transport (= 7.4.0)
160+
elasticsearch-api (7.4.0)
161+
multi_json
162+
elasticsearch-model (7.0.0)
163+
activesupport (> 3)
164+
elasticsearch (> 1)
165+
hashie
166+
elasticsearch-rails (7.0.0)
167+
elasticsearch-transport (7.4.0)
168+
faraday
169+
multi_json
152170
equalizer (0.0.11)
153171
erubi (1.8.0)
154172
erubis (2.7.0)
@@ -163,7 +181,12 @@ GEM
163181
railties (>= 4.2.0)
164182
faker (1.9.3)
165183
i18n (>= 0.7)
184+
faraday (0.17.0)
185+
multipart-post (>= 1.2, < 3)
166186
ffi (1.11.1)
187+
ffi-compiler (1.0.1)
188+
ffi (>= 1.0.0)
189+
rake
167190
figaro (1.1.1)
168191
thor (~> 0.14)
169192
flay (2.12.0)
@@ -178,9 +201,30 @@ GEM
178201
get_process_mem (0.2.3)
179202
globalid (0.4.2)
180203
activesupport (>= 4.2.0)
204+
google-protobuf (3.11.2)
205+
google-protobuf (3.11.2-universal-darwin)
206+
googleapis-common-protos-types (1.0.4)
207+
google-protobuf (~> 3.0)
208+
grpc (1.26.0)
209+
google-protobuf (~> 3.8)
210+
googleapis-common-protos-types (~> 1.0)
211+
grpc (1.26.0-universal-darwin)
212+
google-protobuf (~> 3.8)
213+
googleapis-common-protos-types (~> 1.0)
181214
hashdiff (0.3.9)
215+
hashie (4.0.0)
182216
heapy (0.1.4)
183217
hiredis (0.6.3)
218+
http (4.2.0)
219+
addressable (~> 2.3)
220+
http-cookie (~> 1.0)
221+
http-form_data (~> 2.0)
222+
http-parser (~> 1.2.0)
223+
http-cookie (1.0.3)
224+
domain_name (~> 0.5)
225+
http-form_data (2.1.1)
226+
http-parser (1.2.1)
227+
ffi-compiler (>= 1.0, < 2.0)
184228
i18n (0.9.5)
185229
concurrent-ruby (~> 1.0)
186230
ice_nine (0.11.2)
@@ -215,6 +259,8 @@ GEM
215259
mini_portile2 (2.4.0)
216260
minitest (5.11.3)
217261
msgpack (1.2.10)
262+
multi_json (1.14.1)
263+
multipart-post (2.1.1)
218264
mysql2 (0.5.2)
219265
net-scp (2.0.0)
220266
net-ssh (>= 2.6.5, < 6.0.0)
@@ -435,6 +481,9 @@ GEM
435481
thread_safe (~> 0.1)
436482
uglifier (4.1.20)
437483
execjs (>= 0.3.0, < 3)
484+
unf (0.1.4)
485+
unf_ext
486+
unf_ext (0.0.7.6)
438487
unicode-display_width (1.6.0)
439488
uniform_notifier (1.12.1)
440489
virtus (1.0.5)
@@ -490,10 +539,14 @@ DEPENDENCIES
490539
database_cleaner
491540
derailed
492541
derailed_benchmarks
542+
elastic-apm
543+
elasticsearch-model
544+
elasticsearch-rails
493545
exception_notification
494546
factory_bot_rails
495547
faker
496548
figaro
549+
grpc
497550
hiredis (~> 0.6.1)
498551
i18n
499552
jbuilder (~> 2.5)

app/controllers/users_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def edit
2424
# POST /users
2525
# POST /users.json
2626
def create
27+
binding.pry
2728
@user = User.new(user_params)
2829

2930
respond_to do |format|
@@ -41,6 +42,7 @@ def create
4142
# PATCH/PUT /users/1
4243
# PATCH/PUT /users/1.json
4344
def update
45+
binding.pry
4446
respond_to do |format|
4547
if @user.update(user_params)
4648
format.html { redirect_to @user, notice: 'User was successfully updated.' }
@@ -70,6 +72,6 @@ def set_user
7072

7173
# Never trust parameters from the scary internet, only allow the white list through.
7274
def user_params
73-
params.require(:user).permit(:name, :email, :age, :sex)
75+
params.require(:user).permit(:name, :email, :age, :sex, :password, :password_confirmation)
7476
end
7577
end

app/models/user.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,29 @@
1+
# == Schema Information
2+
#
3+
# Table name: users
4+
#
5+
# id :bigint not null, primary key
6+
# age(年龄) :string(255)
7+
# email(邮箱) :string(255)
8+
# name(用户名) :string(255)
9+
# password_digest(密码) :string(255)
10+
# sex(性别) :boolean
11+
# created_at :datetime not null
12+
# updated_at :datetime not null
13+
#
14+
115
class User < ApplicationRecord
16+
# 在rails 3.0以上版本中,ActiveModel::SecurePassword就使用bcrypt密码验证。不过也需要在gemfile中引入 gem 'bcrypt'
17+
# 需要在users表中添加 password_digest 字段,如果是password字段也是会报错的,只能是password_digest字段。
18+
# 当 has_secure_password 使用的时候就会载入bcrypt gem,并且该方法会给user添加两个虚拟属性password和password_confirmation,
19+
# 创建user的时候会执行存在性验证和匹配验证,同时会获得authenticate方法(根据密码验证),如果密码正确,返回对应的用户对象,否则返回false
20+
# 例如:
21+
# user = User.new(name: 'david', password: 'mUc3m00RsqyRe', password_confirmation: 'mUc3m00RsqyRe')
22+
# user.save # 实际是对虚拟属性password进行加密,把加密后的内容保存到password_digest字段中。
23+
# user.authenticate('notright') # => false
24+
# user.authenticate('mUc3m00RsqyRe') # => user
25+
# user.password # => "mUc3m00RsqyRe" 对password_digest解密后的密码
26+
# user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4." 对password加密后的密码
27+
has_secure_password
28+
229
end

app/views/users/_form.html.erb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<% binding.pry %>
12
<%= form_with(model: user, local: true) do |form| %>
23
<% if user.errors.any? %>
34
<div id="error_explanation">
@@ -16,6 +17,16 @@
1617
<%= form.text_field :name %>
1718
</div>
1819

20+
<div class="field">
21+
<%= form.label :password %>
22+
<%= form.password_field :password %>
23+
</div>
24+
25+
<div class="field">
26+
<%= form.label :password_confirmation %>
27+
<%= form.password_field :password_confirmation %>
28+
</div>
29+
1930
<div class="field">
2031
<%= form.label :email %>
2132
<%= form.text_field :email %>

app/views/users/index.html.erb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22

33
<h1>Users</h1>
44

5+
<div>
6+
<%= select_date(Time.now + 6.days) %>
7+
</div>
8+
<div>
9+
<%= distance_of_time_in_words(Time.now, Time.now + 15.seconds, include_seconds: true) %>
10+
</div>
11+
<div>
12+
<%= select_time(Time.now) %>
13+
</div>
514
<table>
615
<thead>
716
<tr>

config/elastic_apm.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
server_url: http://localhost:8200
2+
secret_token: ''

config/routes.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
55

66
# sidekiq的路由相关开始
7+
78
# 注意该路由(/sidekiq/stats)可以看到json格式sidekiq的一些状态信息。
89
require 'sidekiq/web'
910

@@ -23,6 +24,7 @@
2324
# 使用该api后可以通过路由/queue-status去查询'default'队列积压的情况。如果响应为“uhoh”,就会触发一封电子邮件。
2425
require 'sidekiq/api'
2526
match "queue-status" => proc { [200, {"Content-Type" => "text/plain"}, [Sidekiq::Queue.new.size < 100 ? "OK" : "UHOH" ]] }, via: :get
27+
2628
# sidekiq的路由相关结束
2729

2830
end

db/migrate/20190424015656_create_users.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class CreateUsers < ActiveRecord::Migration[5.2]
22
def change
33
create_table :users do |t|
4-
t.string :name
5-
t.string :email
6-
t.string :age
7-
t.boolean :sex
4+
t.string :name, comment: "用户名"
5+
t.string :email, comment: "邮箱"
6+
t.string :age, comment: "年龄"
7+
t.boolean :sex, comment: "性别"
88

99
t.timestamps
1010
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPasswordDigestToUsers < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :users, :password_digest, :string, comment: '密码', after: 'name'
4+
end
5+
end

0 commit comments

Comments
 (0)