diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index ff6bcbdc06..9fa29ed125 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,6 +10,7 @@ def new def create @user = User.new(params[:user]) + @user.status = 1 using_recaptcha = !params[:spamaway] && Rails.env == "production" recaptcha = verify_recaptcha(model: @user) if using_recaptcha @spamaway = Spamaway.new(params[:spamaway]) unless using_recaptcha diff --git a/app/models/drupal_user.rb b/app/models/drupal_user.rb index b9ec83f041..0adacb47e7 100644 --- a/app/models/drupal_user.rb +++ b/app/models/drupal_user.rb @@ -47,6 +47,7 @@ def role def moderate self.status = 5 save + update_user_status(5) # user is logged out next time they access current_user in a controller; see application controller self end @@ -54,6 +55,7 @@ def moderate def unmoderate self.status = 1 save + update_user_status(1) self end @@ -61,6 +63,7 @@ def ban self.status = 0 decrease_likes_banned save + update_user_status(0) # user is logged out next time they access current_user in a controller; see application controller self end @@ -69,6 +72,7 @@ def unban self.status = 1 increase_likes_unbanned save + update_user_status(1) self end @@ -76,6 +80,12 @@ def email mail end + def update_user_status(status) + u = self.user + u.status = status + u.save! + end + def first_time_poster user.first_time_poster end diff --git a/app/models/user.rb b/app/models/user.rb index ce4f46ed59..ef0f735749 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -8,7 +8,7 @@ def validate(record) class User < ActiveRecord::Base self.table_name = 'rusers' - attr_accessible :username, :email, :password, :password_confirmation, :openid_identifier, :key, :photo, :photo_file_name, :bio + attr_accessible :username, :email, :password, :password_confirmation, :openid_identifier, :key, :photo, :photo_file_name, :bio, :status alias_attribute :name, :username acts_as_authentic do |c| diff --git a/db/migrate/20180128162224_sync_user_status_with_drupal_user_status.rb b/db/migrate/20180128162224_sync_user_status_with_drupal_user_status.rb new file mode 100644 index 0000000000..979fb8b30d --- /dev/null +++ b/db/migrate/20180128162224_sync_user_status_with_drupal_user_status.rb @@ -0,0 +1,12 @@ +class SyncUserStatusWithDrupalUserStatus < ActiveRecord::Migration + def up + DrupalUser.all.each do |du| + user = du.user + user.status = du.status + user.save({}) + end + end + + def down + end +end diff --git a/db/schema.rb.example b/db/schema.rb.example index 3ef06318a0..ff655cf91d 100644 --- a/db/schema.rb.example +++ b/db/schema.rb.example @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180119204200) do +ActiveRecord::Schema.define(version: 20180128162224) do create_table "answer_selections", force: true do |t| t.integer "user_id" diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index a365e082b8..dcceeefa97 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -142,4 +142,22 @@ class UserTest < ActiveSupport::TestCase assert_equal 1, user.errors[:email].count end + test 'user status changes when drupal user is banned or unbanned' do + drupal_user = drupal_users(:bob) + assert_equal 1, drupal_user.user.status + drupal_user.ban + assert_equal 0, drupal_user.user.status + drupal_user.unban + assert_equal 1, drupal_user.user.status + end + + test 'user status changes when drupal user is moderated or unmoderated' do + drupal_user = drupal_users(:bob) + assert_equal 1, drupal_user.user.status + drupal_user.moderate + assert_equal 5, drupal_user.user.status + drupal_user.unmoderate + assert_equal 1, drupal_user.user.status + end + end