Browse Source

Registration captcha

pull/33/head
Stephen 8 months ago
parent
commit
7aa3a63c26
10 changed files with 147 additions and 8 deletions
  1. +2
    -0
      Gemfile
  2. +3
    -0
      Gemfile.lock
  3. +3
    -2
      app/controllers/application_controller.rb
  4. +6
    -0
      app/controllers/user_controller.rb
  5. +17
    -1
      app/views/user/new.html.erb
  6. +31
    -0
      config/initializers/simple_captcha.rb
  7. +2
    -1
      config/locales/en.yml
  8. +15
    -0
      db/migrate/20200126015317_create_simple_captcha_data.rb
  9. +10
    -2
      db/schema.rb
  10. +58
    -2
      db/structure.sql

+ 2
- 0
Gemfile View File

@@ -75,3 +75,5 @@ gem 'pg_search'
gem 'simplecov', require: false, group: :test

gem 'rails-controller-testing'

gem 'simple_captcha2', require: 'simple_captcha'

+ 3
- 0
Gemfile.lock View File

@@ -193,6 +193,8 @@ GEM
selenium-webdriver (3.142.4)
childprocess (>= 0.5, < 3.0)
rubyzip (~> 1.2, >= 1.2.2)
simple_captcha2 (0.5.0)
rails (>= 4.1)
simplecov (0.17.1)
docile (~> 1.1)
json (>= 1.8, < 3)
@@ -265,6 +267,7 @@ DEPENDENCIES
rails-controller-testing
sass-rails (~> 5)
selenium-webdriver
simple_captcha2
simplecov
spring
spring-watcher-listen (~> 2.0.0)


+ 3
- 2
app/controllers/application_controller.rb View File

@@ -1,4 +1,5 @@
class ApplicationController < ActionController::Base
include SimpleCaptcha::ControllerHelpers
#Makes method available from views
helper_method :current_user
@@ -12,6 +13,8 @@ class ApplicationController < ActionController::Base
helper_method :user_fname
helper_method :path_from_post

add_flash_types :alerts

def current_user
@current_user ||= User.find(session[:user_id]) if session[:user_id]
end
@@ -104,6 +107,4 @@ html
def default_fg_color
'#555555'
end

add_flash_types :alerts
end

+ 6
- 0
app/controllers/user_controller.rb View File

@@ -5,6 +5,12 @@ class UserController < ApplicationController

def create
@user = User.new(user_params)
unless simple_captcha_valid?
flash.now[:alert] = "Captcha was not entered correctly"
render :new
return
end

if @user.save
StatsD.increment("user.signup.success")


+ 17
- 1
app/views/user/new.html.erb View File

@@ -21,7 +21,23 @@
<td><%= f.label :password_confirmation %></td>
<td><%= f.password_field :password_confirmation %></td>
</tr>
<tr>
<td></td>
<td style="padding: 0px;">
<%= simple_captcha_options[:image] %>
</td>
</tr>
<tr>
<td>
<%= "Captcha" %>
(<%= simple_captcha_options(refresh_button_html: {
tabindex: -1})[:refresh_button] %>)
</td>
<td>
<%= simple_captcha_options[:field] %>
</td>
</tr>
</table>
<%= f.submit "Sign Up" %>
<%= f.submit "Sign Up" %>
</div>
<% end %>

+ 31
- 0
config/initializers/simple_captcha.rb View File

@@ -0,0 +1,31 @@
SimpleCaptcha.setup do |sc|
# default: 100x28
sc.image_size = '120x40'

# default: 5
sc.length = 6

# default: simply_blue
# possible values:
# 'embosed_silver',
# 'simply_red',
# 'simply_green',
# 'simply_blue',
# 'distorted_black',
# 'all_black',
# 'charcoal_grey',
# 'almost_invisible'
# 'random'
sc.image_style = 'simply_green'

# default: low
# possible values: 'low', 'medium', 'high', 'random'
sc.distortion = 'high'

# default: medium
# possible values: 'none', 'low', 'medium', 'high'
sc.implode = 'high'

# No jquery
sc.refresh_format = :plain_javascript
end

+ 2
- 1
config/locales/en.yml View File

@@ -30,4 +30,5 @@
# available at https://guides.rubyonrails.org/i18n.html.

en:
hello: "Hello world"
simple_captcha:
placeholder: ""

+ 15
- 0
db/migrate/20200126015317_create_simple_captcha_data.rb View File

@@ -0,0 +1,15 @@
class CreateSimpleCaptchaData < ActiveRecord::Migration[4.2]
def self.up
create_table :simple_captcha_data do |t|
t.string :key, :limit => 40
t.string :value, :limit => 6
t.timestamps
end

add_index :simple_captcha_data, :key, :name => "idx_key"
end

def self.down
drop_table :simple_captcha_data
end
end

+ 10
- 2
db/schema.rb View File

@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2019_12_12_184641) do
ActiveRecord::Schema.define(version: 2020_01_26_015317) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -59,8 +59,8 @@ ActiveRecord::Schema.define(version: 2019_12_12_184641) do
t.integer "priority", default: -> { "nextval('page_priority_sequence'::regclass)" }, null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "is_draft", default: false, null: false
t.boolean "hidden", default: false, null: false
t.boolean "is_draft", default: false, null: false
t.index ["blog_id"], name: "index_pages_on_blog_id"
t.index ["hidden"], name: "index_pages_on_hidden"
end
@@ -84,6 +84,14 @@ ActiveRecord::Schema.define(version: 2019_12_12_184641) do
t.index ["blog_id"], name: "index_posts_on_blog_id"
end

create_table "simple_captcha_data", id: :serial, force: :cascade do |t|
t.string "key", limit: 40
t.string "value", limit: 6
t.datetime "created_at"
t.datetime "updated_at"
t.index ["key"], name: "idx_key"
end

create_table "users", force: :cascade do |t|
t.string "username"
t.string "display_name"


+ 58
- 2
db/structure.sql View File

@@ -11,7 +11,7 @@ SET row_security = off;

SET default_tablespace = '';

SET default_with_oids = false;
SET default_table_access_method = heap;

--
-- Name: ar_internal_metadata; Type: TABLE; Schema: public; Owner: -
@@ -257,6 +257,39 @@ CREATE TABLE public.schema_migrations (


--
-- Name: simple_captcha_data; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE public.simple_captcha_data (
id integer NOT NULL,
key character varying(40),
value character varying(6),
created_at timestamp without time zone,
updated_at timestamp without time zone
);


--
-- Name: simple_captcha_data_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE public.simple_captcha_data_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;


--
-- Name: simple_captcha_data_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE public.simple_captcha_data_id_seq OWNED BY public.simple_captcha_data.id;


--
-- Name: users; Type: TABLE; Schema: public; Owner: -
--

@@ -366,6 +399,13 @@ ALTER TABLE ONLY public.posts ALTER COLUMN id SET DEFAULT nextval('public.posts_


--
-- Name: simple_captcha_data id; Type: DEFAULT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.simple_captcha_data ALTER COLUMN id SET DEFAULT nextval('public.simple_captcha_data_id_seq'::regclass);


--
-- Name: users id; Type: DEFAULT; Schema: public; Owner: -
--

@@ -444,6 +484,14 @@ ALTER TABLE ONLY public.schema_migrations


--
-- Name: simple_captcha_data simple_captcha_data_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

ALTER TABLE ONLY public.simple_captcha_data
ADD CONSTRAINT simple_captcha_data_pkey PRIMARY KEY (id);


--
-- Name: users users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--

@@ -460,6 +508,13 @@ ALTER TABLE ONLY public.video_meta


--
-- Name: idx_key; Type: INDEX; Schema: public; Owner: -
--

CREATE INDEX idx_key ON public.simple_captcha_data USING btree (key);


--
-- Name: index_blogs_on_urlName; Type: INDEX; Schema: public; Owner: -
--

@@ -565,6 +620,7 @@ INSERT INTO "schema_migrations" (version) VALUES
('20191203020942'),
('20191205023946'),
('20191207213541'),
('20191212184641');
('20191212184641'),
('20200126015317');



Loading…
Cancel
Save