diff options
-rw-r--r-- | app/assets/stylesheets/leap.scss | 3 | ||||
-rw-r--r-- | app/controllers/sessions_controller.rb | 9 | ||||
-rw-r--r-- | app/controllers/users_controller.rb | 6 | ||||
-rw-r--r-- | app/models/invite_code.rb | 1 | ||||
-rw-r--r-- | app/models/invite_code_validator.rb | 7 | ||||
-rw-r--r-- | app/views/users/_user.html.haml | 34 | ||||
-rw-r--r-- | app/views/users/index.html.haml | 3 | ||||
-rw-r--r-- | config/locales/en/users.en.yml | 9 | ||||
-rw-r--r-- | config/locales/es.yml | 8 | ||||
-rw-r--r-- | lib/tasks/invite_code.rake | 17 | ||||
-rw-r--r-- | test/functional/users_controller_test.rb | 1 | ||||
-rw-r--r-- | test/unit/invite_code_test.rb | 42 | ||||
-rw-r--r-- | test/unit/invite_code_validator_test.rb | 56 |
13 files changed, 138 insertions, 58 deletions
diff --git a/app/assets/stylesheets/leap.scss b/app/assets/stylesheets/leap.scss index dddcc1a..9fe2195 100644 --- a/app/assets/stylesheets/leap.scss +++ b/app/assets/stylesheets/leap.scss @@ -307,3 +307,6 @@ html, body { margin: 0 2px; } } +.modal-footer form{ + margin: 0; +} diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 66eba40..34d4f53 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -32,4 +32,13 @@ class SessionsController < ApplicationController # throw :warden, response.finish #end + Warden::Manager.after_set_user do |user, auth, opts| + scope = opts[:scope] + unless user.enabled? + auth.logout(scope) + throw(:warden, scope: scope, reason: "User not active") + end + end + + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3943afc..446b726 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -49,13 +49,15 @@ class UsersController < ApplicationController def deactivate @user.enabled = false @user.save - respond_with @user + flash[:notice] = I18n.t("actions.user_disabled_message", username: @user.username) + redirect_to :back end def enable @user.enabled = true @user.save - respond_with @user + flash[:notice] = I18n.t("actions.user_enabled_message", username: @user.username) + redirect_to :back end def destroy diff --git a/app/models/invite_code.rb b/app/models/invite_code.rb index 30a6498..13c3a39 100644 --- a/app/models/invite_code.rb +++ b/app/models/invite_code.rb @@ -4,6 +4,7 @@ class InviteCode < CouchRest::Model::Base use_database 'invite_codes' property :invite_code, String, :read_only => true property :invite_count, Integer, :default => 0, :accessible => true + property :max_uses, Integer, :default => 1 timestamps! diff --git a/app/models/invite_code_validator.rb b/app/models/invite_code_validator.rb index f96ca4a..676e4fa 100644 --- a/app/models/invite_code_validator.rb +++ b/app/models/invite_code_validator.rb @@ -1,4 +1,5 @@ class InviteCodeValidator < ActiveModel::Validator + def validate(user) user_invite_code = InviteCode.find_by_invite_code user.invite_code @@ -6,7 +7,7 @@ class InviteCodeValidator < ActiveModel::Validator 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) + elsif has_no_uses_left?(user_invite_code) add_error_to_user("This code has already been used", user) end end @@ -16,8 +17,8 @@ class InviteCodeValidator < ActiveModel::Validator code == nil end - def count_greater_than_zero?(code) - code.invite_count > 0 + def has_no_uses_left?(code) + code.invite_count >= code.max_uses end def add_error_to_user(error, user) diff --git a/app/views/users/_user.html.haml b/app/views/users/_user.html.haml index 583d22f..1cabcf5 100644 --- a/app/views/users/_user.html.haml +++ b/app/views/users/_user.html.haml @@ -1,4 +1,32 @@ %tr - %td= link_to user.login, user - %td= l(user.created_at, :format => :short) - %td= l(user.updated_at, :format => :short) + %td + = link_to user.username, user + %td + = user.created_at.strftime("%d %b, %Y, %H:%M") + %td + = user.updated_at.strftime("%d %b, %Y, %H:%M") + %td + - if user.enabled + %button.btn.btn-default{:"data-toggle" => "modal", :"data-target" => "#user-form-#{user.id}", :type => "button"} + = t("actions.disable_user") + .modal.fade.hide{:id => "user-form-#{user.id}"} + .modal-dialog + .modal-content + .modal-header + %button.close{:"data-dismiss" => "modal"} × + = t("actions.confirm_user_deactivation", username: user.username) + .modal-footer + = form_tag deactivate_user_path(user) do + %input.btn.btn-default.btn-danger{:type => "submit", :value => "#{t("actions.disable_user")}"} + - else + %button.btn.btn-default{:"data-toggle" => "modal", :"data-target" => "#user-form-#{user.id}", :type => "button"} + = t("actions.enable_user") + .modal.fade.hide{:id => "user-form-#{user.id}"} + .modal-dialog + .modal-content + .modal-header + %button.close{:"data-dismiss" => "modal"} × + = t("actions.confirm_user_activation", username: user.username) + .modal-footer + = form_tag enable_user_path(user) do + %input.btn.btn-default.btn-danger{:type => "submit", :value => "#{t("actions.enable_user")}"} diff --git a/app/views/users/index.html.haml b/app/views/users/index.html.haml index 3ed8835..e1136d8 100644 --- a/app/views/users/index.html.haml +++ b/app/views/users/index.html.haml @@ -1,4 +1,5 @@ - @show_navigation = false = search :users -= table @users, %w(username, created, updated) += table @users, %w(username, created, updated, actions.toggle_user) + diff --git a/config/locales/en/users.en.yml b/config/locales/en/users.en.yml index 7158743..f2e60af 100644 --- a/config/locales/en/users.en.yml +++ b/config/locales/en/users.en.yml @@ -59,6 +59,14 @@ en: tickets: "Create and check support tickets." email: "Modify email settings." account: "Destroy your account." + actions: + toggle_user: "Enable / Disable User" + enable_user: "Enable User" + disable_user: "Disable User" + confirm_user_deactivation: "Are you sure you want to deactive user with username '%{username}'?" + confirm_user_activation: "Are you sure you want to activate user with username '%{username}'?" + user_disabled_message: "User with username '%{username}' has been disabled." + user_enabled_message: "User with username '%{username}' has been enabled." # # rails @@ -80,4 +88,3 @@ en: placeholders: user: email_forward: "my_other_email@domain.net" - diff --git a/config/locales/es.yml b/config/locales/es.yml index 3dc3dbe..1742be5 100644 --- a/config/locales/es.yml +++ b/config/locales/es.yml @@ -112,6 +112,14 @@ es: user: one: Usuario other: '%{count} usuarios' + actions: + toggle_user: "Activar / Desactivar Usuario" + enable_user: "Activar Usuario" + disable_user: "Desactivar Usuario" + confirm_user_deactivation: "Estás seguro que quieres desactivar al usuario con nombre '%{username}'?" + confirm_user_activation: "Estás seguro que quieres activar al usuario con nombre '%{username}'?" + user_disabled_message: "Usuario con nombre '%{username}' ha sido desactivado." + user_enabled_message: "Usuario con nombre '%{username}' ha sido activado." create_new_customer: Crear un nuevo Cliente de Braintree must_create_customer: Tiene que almacenar un cliente en Braintree antes de suscribirse a un plan subscribe: Suscribirse diff --git a/lib/tasks/invite_code.rake b/lib/tasks/invite_code.rake index f3bafac..d6e2b49 100644 --- a/lib/tasks/invite_code.rake +++ b/lib/tasks/invite_code.rake @@ -1,16 +1,21 @@ desc "Generate a batch of invite codes" -task :generate_invites, [:n] => :environment do |task, args| +task :generate_invites, [:n, :u] => :environment do |task, args| - codes = args.n - codes = codes.to_i + codes = args.n + codes = codes.to_i - codes.times do |x| + if args.u != nil + max_uses = args.u + end + + codes.times do |x| x = InviteCode.new + x.max_uses = max_uses x.save - puts "#{x.invite_code} Code generated." - + puts x.invite_code end + end diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb index 7d1745c..70f483e 100644 --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -158,6 +158,7 @@ class UsersControllerTest < ActionController::TestCase login :is_admin? => true + @request.env['HTTP_REFERER'] = 'http://test.com/sessions/new' post :deactivate, :id => user.id assert !assigns(:user).enabled? end diff --git a/test/unit/invite_code_test.rb b/test/unit/invite_code_test.rb index b17d1bc..fd93f2f 100644 --- a/test/unit/invite_code_test.rb +++ b/test/unit/invite_code_test.rb @@ -21,47 +21,5 @@ class InviteCodeTest < ActiveSupport::TestCase end - test "Invite count >0 is not accepted for new account signup" do - validator = InviteCodeValidator.new nil - - user_code = InviteCode.new - user_code.invite_count = 1 - user_code.save - - user = FactoryGirl.build :user - user.invite_code = user_code.invite_code - - validator.validate(user) - - assert_equal ["This code has already been used"], user.errors[:invite_code] - - end - - test "Invite count 0 is accepted for new account signup" do - validator = InviteCodeValidator.new nil - - user_code = InviteCode.create - - user = FactoryGirl.build :user - user.invite_code = user_code.invite_code - - validator.validate(user) - - assert_equal [], user.errors[:invite_code] - end - - test "There is an error message if the invite code does not exist" do - validator = InviteCodeValidator.new nil - - user = FactoryGirl.build :user - user.invite_code = "wrongcode" - - validator.validate(user) - - assert_equal ["This is not a valid code"], user.errors[:invite_code] - - end - - end diff --git a/test/unit/invite_code_validator_test.rb b/test/unit/invite_code_validator_test.rb index ee8f1b3..62eeae6 100644 --- a/test/unit/invite_code_validator_test.rb +++ b/test/unit/invite_code_validator_test.rb @@ -27,4 +27,60 @@ class InviteCodeValidatorTest < ActiveSupport::TestCase assert_equal errors, invalid_user.errors.messages end end + + + test "Invite count >= invite max uses is not accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.new + user_code.invite_count = 1 + user_code.save + + user = FactoryGirl.build :user + user.invite_code = user_code.invite_code + + validator.validate(user) + + assert_equal ["This code has already been used"], user.errors[:invite_code] + + end + + test "Invite count < invite max uses is accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.create + user_code.save + + user = FactoryGirl.build :user + user.invite_code = user_code.invite_code + + validator.validate(user) + + assert_equal [], user.errors[:invite_code] + end + + test "Invite count 0 is accepted for new account signup" do + validator = InviteCodeValidator.new nil + + user_code = InviteCode.create + + user = FactoryGirl.build :user + user.invite_code = user_code.invite_code + + validator.validate(user) + + assert_equal [], user.errors[:invite_code] + end + + test "There is an error message if the invite code does not exist" do + validator = InviteCodeValidator.new nil + + user = FactoryGirl.build :user + user.invite_code = "wrongcode" + + validator.validate(user) + + assert_equal ["This is not a valid code"], user.errors[:invite_code] + end + end
\ No newline at end of file |