docbrown/app/services/user_role_service.rb
rhymes e588fa7ece Code cleanups (#659)
* Initial automatic cleanup with rubocop

* Fix syntax error introduced by rubocop

* Cleanup seeds file

* Cleanup lib folder

* Exclude bin folder because it contains auto generated files

* Make Rubocop a little bit more chatty

* Block length should not include comments in the count

* Cleanup config folder

* Cleanup specs

* Updated Rubocop version and generated a todo file

* Fix broken ArticlesApi spec

* Fix tests

* Restored rubocop pre-commit hook
2018-08-07 11:00:13 -04:00

103 lines
2.8 KiB
Ruby

class UserRoleService
def initialize(user)
@user = user
end
def check_for_roles(params)
if @user.banned && params[:banned] == "0"
unban
else
bannable?(params)
warnable?(params)
end
new_roles?(params)
end
def update_tag_moderators(user_ids, tag)
users = user_ids.map do |id|
User.find(id)
rescue StandardError
tag.errors[:moderator_ids] << ": user id #{id} was not found"
end
return false if !tag.errors[:moderator_ids].blank?
# Don't have to worry about comparing old and new values.
tag.tag_moderator_ids.each do |id|
User.find(id).remove_role(:tag_moderator, tag)
end
users.each do |user|
user.add_role(:tag_moderator, tag)
end
true
end
private
def new_roles?(params)
params[:trusted] == "1" ? @user.add_role(:trusted) : @user.remove_role(:trusted)
params[:analytics] == "1" ? @user.add_role(:analytics_beta_tester) : @user.remove_role(:analytics_beta_tester)
if params[:scholar] == "1"
@user.add_role(:workshop_pass)
@user.update(workshop_expiration: params[:workshop_expiration])
ScholarshipMailer.delay.scholarship_awarded_email(@user) if params[:scholar_email] == "1"
else
@user.remove_role(:workshop_pass)
end
end
def bannable?(params)
if params[:banned] == "0" && !params[:reason_for_ban].blank?
@user.errors[:banned] << "was not checked but had the reason filled out"
elsif params[:banned] == "1" && params[:reason_for_ban].blank?
@user.errors[:reason_for_ban] << "can't be blank if banned is checked"
elsif params[:banned] == "1"
ban(params[:reason_for_ban])
else
unban
end
end
def warnable?(params)
if params[:warned] == "0" && !params[:reason_for_warning].blank?
@user.errors[:warned] << "was not checked but had the reason filled out"
elsif params[:warned] == "1" && params[:reason_for_warning].blank?
@user.errors[:reason_for_warning] << "can't be blank if warned is checked"
elsif params[:warned] == "1"
give_warning(params[:reason_for_warning])
end
end
def create_or_update_note(reason, content)
note = Note.find_by(noteable_id: @user.id, noteable_type: "User", reason: reason)
if note.nil?
Note.create(
noteable_id: @user.id,
noteable_type: "User",
reason: reason,
content: content,
)
else
note.update(content: content)
end
end
def ban(content)
@user.add_role :banned
create_or_update_note("banned", content)
end
def unban
@user.remove_role :banned
end
# Only give warning method b/c no need to remove warnings
def give_warning(content)
@user.add_role :warned
create_or_update_note("warned", content)
end
def validate_user_ids(user_ids)
user_ids.each do |id|
User.find(id)
end
end
end