From e6e1187fdc44766aa4336e05aa12d4e74db65d1d Mon Sep 17 00:00:00 2001 From: ankonym Date: Wed, 5 Aug 2015 16:55:39 +0200 Subject: Adding invite code field to signup with validation for hardcoded invite code --- app/models/user.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/user.rb b/app/models/user.rb index d44df40..5510470 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,7 @@ class User < CouchRest::Model::Base property :password_salt, String, :accessible => true property :contact_email, String, :accessible => true property :contact_email_key, String, :accessible => true - + property :invite_code, String, :accessible => true property :enabled, TrueClass, :default => true # these will be null by default but we shouldn't ever pull them directly, but only via the methods that will return the full ServiceLevel @@ -39,6 +39,8 @@ class User < CouchRest::Model::Base :email => true, :mx_with_fallback => true + validates :invite_code, inclusion: { in: ["testcode"], message: "%{value} is not a valid invite code" } + timestamps! design do -- cgit v1.2.3 From 393291de60c4f29bb03bad596bb60c1e0648e6e7 Mon Sep 17 00:00:00 2001 From: ankonym Date: Thu, 13 Aug 2015 15:28:02 +0200 Subject: Add invite code model --- app/models/invite_code.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 app/models/invite_code.rb (limited to 'app/models') diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb new file mode 100644 index 0000000..b9f6f33 --- /dev/null +++ b/app/models/invite_code.rb @@ -0,0 +1,10 @@ +class InviteCode < CouchRest::Model::Base + use_database 'invite_codes' + property :invite_code, String + timestamps! + + design do + view :by__id + end +end + -- cgit v1.2.3 From bd98a2cbb8796039efbffb7039a9bda1cde22dae Mon Sep 17 00:00:00 2001 From: ankonym Date: Thu, 13 Aug 2015 15:29:04 +0200 Subject: Add validation of invite code in user object based on codes in couch db --- app/models/invite_code_validator.rb | 16 ++++++++++++++++ app/models/user.rb | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 app/models/invite_code_validator.rb (limited to 'app/models') diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb new file mode 100644 index 0000000..55b7a74 --- /dev/null +++ b/app/models/invite_code_validator.rb @@ -0,0 +1,16 @@ +class InviteCodeValidator < ActiveModel::Validator + def validate(user) + if not_existent?(user.invite_code) + add_error_to_user("This is not a valid code", user) + end + end + + private + def not_existent?(code) + InviteCode.find_by__id(code) == nil + end + + def add_error_to_user(error, user) + user.errors[:invite_code] << error + end +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 5510470..06e1a0f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -39,7 +39,7 @@ class User < CouchRest::Model::Base :email => true, :mx_with_fallback => true - validates :invite_code, inclusion: { in: ["testcode"], message: "%{value} is not a valid invite code" } + validates_with InviteCodeValidator timestamps! -- cgit v1.2.3 From 8b9b2a0c3c8457024d99d0794416c59cb245a513 Mon Sep 17 00:00:00 2001 From: ankonym Date: Thu, 13 Aug 2015 17:24:02 +0200 Subject: Changed invite code query to look for invite_code string instead of id --- app/models/invite_code.rb | 2 +- app/models/invite_code_validator.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index b9f6f33..751b28e 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -4,7 +4,7 @@ class InviteCode < CouchRest::Model::Base timestamps! design do - view :by__id + view :by_invite_code end end diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index 55b7a74..978d3f5 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -7,7 +7,7 @@ class InviteCodeValidator < ActiveModel::Validator private def not_existent?(code) - InviteCode.find_by__id(code) == nil + InviteCode.find_by_invite_code(code) == nil end def add_error_to_user(error, user) -- cgit v1.2.3 From 0543217b433a8f4809f08018c1a11c20119fa85d Mon Sep 17 00:00:00 2001 From: ankonym Date: Fri, 21 Aug 2015 17:49:36 +0200 Subject: assign random invite code when creating new invite codes --- app/models/invite_code.rb | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'app/models') diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index 751b28e..d52436f 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -1,10 +1,19 @@ +require 'coupon_code' + class InviteCode < CouchRest::Model::Base use_database 'invite_codes' - property :invite_code, String + property :invite_code, String, :read_only => true timestamps! design do view :by_invite_code end + + def initialize(attributes = {}, options = {}) + super(attributes, options) + write_attribute('invite_code', CouponCode.generate) if new? + end + end + -- cgit v1.2.3 From 45c3fadd930a474951bd918a50e1ea2b00af75c7 Mon Sep 17 00:00:00 2001 From: ankonym Date: Tue, 25 Aug 2015 14:11:00 +0200 Subject: Make sure codes can only be used once, fix validations We introduced a count on invite codes to make sure that (at the moment) codes can only be used once. (The code will also allow multi-use codes in the future.) Also, some of our validations weren't validating against the correct data, which is now fixed. --- app/models/account.rb | 5 +++-- app/models/invite_code.rb | 3 +++ app/models/invite_code_validator.rb | 20 ++++++++++++++++++-- app/models/user.rb | 2 ++ 4 files changed, 26 insertions(+), 4 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index af470ed..a57e3f7 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,7 +1,7 @@ # -# The Account model takes care of the livecycle of a user. +# The Account model takes care of the lifecycle of a user. # It composes a User record and it's identity records. -# It also allows for other engines to hook into the livecycle by +# It also allows for other engines to hook into the lifecycle by # monkeypatching the create, update and destroy methods. # There's an ActiveSupport load_hook at the end of this file to # make this more easy. @@ -19,6 +19,7 @@ class Account identity = nil user = nil user = User.new(attrs) + user.save if !user.tmp? && user.persisted? identity = user.identity diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index d52436f..30a6498 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -3,10 +3,13 @@ require 'coupon_code' class InviteCode < CouchRest::Model::Base use_database 'invite_codes' property :invite_code, String, :read_only => true + property :invite_count, Integer, :default => 0, :accessible => true + timestamps! design do view :by_invite_code + view :by_invite_count end def initialize(attributes = {}, options = {}) diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index 978d3f5..73c333a 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -1,7 +1,17 @@ class InviteCodeValidator < ActiveModel::Validator def validate(user) - if not_existent?(user.invite_code) + + user_invite_code = InviteCode.find_by_invite_code user.invite_code + + if not_existent?(user_invite_code.invite_code) add_error_to_user("This is not a valid code", user) + + elsif count_greater_than_zero?(user_invite_code.invite_count) + add_error_to_user("This code has already been used", user) + + else + user_invite_code.invite_count += 1 + user_invite_code.save end end @@ -10,7 +20,13 @@ class InviteCodeValidator < ActiveModel::Validator InviteCode.find_by_invite_code(code) == nil end + def count_greater_than_zero?(code) + code > 0 + end + def add_error_to_user(error, user) user.errors[:invite_code] << error end -end \ No newline at end of file +end + + diff --git a/app/models/user.rb b/app/models/user.rb index 06e1a0f..a4dd132 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -39,8 +39,10 @@ class User < CouchRest::Model::Base :email => true, :mx_with_fallback => true + validates_with InviteCodeValidator + timestamps! design do -- cgit v1.2.3 From a8f07fb18518fd95fd701e8c0e550a3cd70520b0 Mon Sep 17 00:00:00 2001 From: ankonym Date: Mon, 31 Aug 2015 17:15:27 +0200 Subject: Fixes for the invite code validator Validation should only happen for new records User invite code was nil for invalid invite codes Adding missing tests --- app/models/invite_code_validator.rb | 3 ++- app/models/user.rb | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'app/models') diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index 73c333a..d03d510 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -3,7 +3,7 @@ class InviteCodeValidator < ActiveModel::Validator user_invite_code = InviteCode.find_by_invite_code user.invite_code - if not_existent?(user_invite_code.invite_code) + if not_existent?(user.invite_code) add_error_to_user("This is not a valid code", user) elsif count_greater_than_zero?(user_invite_code.invite_count) @@ -18,6 +18,7 @@ class InviteCodeValidator < ActiveModel::Validator private def not_existent?(code) InviteCode.find_by_invite_code(code) == nil + end def count_greater_than_zero?(code) diff --git a/app/models/user.rb b/app/models/user.rb index a4dd132..c0079d5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,7 +40,7 @@ class User < CouchRest::Model::Base :mx_with_fallback => true - validates_with InviteCodeValidator + validates_with InviteCodeValidator, on: :create timestamps! -- cgit v1.2.3 From 2ce3d14cfa77f985b6849dd4431db65e9abd0226 Mon Sep 17 00:00:00 2001 From: Aya Jaff Date: Fri, 4 Sep 2015 14:01:49 +0200 Subject: Fixed the signup bug that wrongly consumes the invite code. --- app/models/account.rb | 5 ++++- app/models/invite_code_validator.rb | 4 ---- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index a57e3f7..c398d93 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -19,8 +19,8 @@ class Account identity = nil user = nil user = User.new(attrs) - user.save + if !user.tmp? && user.persisted? identity = user.identity identity.user_id = user.id @@ -28,6 +28,9 @@ class Account identity.errors.each do |attr, msg| user.errors.add(attr, msg) end + user_invite_code = InviteCode.find_by_invite_code user.invite_code + user_invite_code.invite_count += 1 + user_invite_code.save end rescue StandardError => ex user.errors.add(:base, ex.to_s) if user diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index d03d510..127dc57 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -8,10 +8,6 @@ class InviteCodeValidator < ActiveModel::Validator elsif count_greater_than_zero?(user_invite_code.invite_count) add_error_to_user("This code has already been used", user) - - else - user_invite_code.invite_count += 1 - user_invite_code.save end end -- cgit v1.2.3 From ca591b482870c93674aaf454e90f56796da7d87d Mon Sep 17 00:00:00 2001 From: ankonym Date: Thu, 10 Sep 2015 18:10:28 +0200 Subject: Cleaned up code in invite_code_validator.rb --- app/models/invite_code_validator.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'app/models') diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index 127dc57..f96ca4a 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -3,22 +3,21 @@ class InviteCodeValidator < ActiveModel::Validator user_invite_code = InviteCode.find_by_invite_code user.invite_code - if not_existent?(user.invite_code) + if not_existent?(user_invite_code) add_error_to_user("This is not a valid code", user) - elsif count_greater_than_zero?(user_invite_code.invite_count) + elsif count_greater_than_zero?(user_invite_code) add_error_to_user("This code has already been used", user) end end private def not_existent?(code) - InviteCode.find_by_invite_code(code) == nil - + code == nil end def count_greater_than_zero?(code) - code > 0 + code.invite_count > 0 end def add_error_to_user(error, user) -- cgit v1.2.3 From 9adbde13619de8b2c300056b062d12f0961cb710 Mon Sep 17 00:00:00 2001 From: ankonym Date: Mon, 21 Sep 2015 18:34:04 +0200 Subject: Make invite code configurable Through the config param 'invite_required', providers can decide whether users need to provide an invite code upon signup. The default setting is false. --- app/models/account.rb | 9 ++++++--- app/models/user.rb | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) (limited to 'app/models') diff --git a/app/models/account.rb b/app/models/account.rb index c398d93..a5cd833 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -28,9 +28,12 @@ class Account identity.errors.each do |attr, msg| user.errors.add(attr, msg) end - user_invite_code = InviteCode.find_by_invite_code user.invite_code - user_invite_code.invite_count += 1 - user_invite_code.save + + if APP_CONFIG[:invite_required] + user_invite_code = InviteCode.find_by_invite_code user.invite_code + user_invite_code.invite_count += 1 + user_invite_code.save + end end rescue StandardError => ex user.errors.add(:base, ex.to_s) if user diff --git a/app/models/user.rb b/app/models/user.rb index c0079d5..3daee0f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -40,7 +40,7 @@ class User < CouchRest::Model::Base :mx_with_fallback => true - validates_with InviteCodeValidator, on: :create + validates_with InviteCodeValidator, on: :create, if: -> {APP_CONFIG[:invite_required]} timestamps! -- cgit v1.2.3