Skip to content

Commit

Permalink
Added more code to bootstrap task to deal with upgrade cases.
Browse files Browse the repository at this point in the history
Added add_admin task
Added activate and suspend methods to users
  • Loading branch information
gabceb committed Feb 26, 2013
1 parent f8b1a39 commit 7adc29e
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
16 changes: 16 additions & 0 deletions app/models/user.rb
Expand Up @@ -68,4 +68,20 @@ def self.deleted_user
return dummy_user
end

def activate
self.registration_status = "active"
end

def activate!
self.activate && self.save!
end

def suspend
self.registration_status = "suspended"
end

def suspend!
self.suspend && self.save!
end

end
54 changes: 54 additions & 0 deletions lib/tasks/kandan.rake
Expand Up @@ -18,6 +18,26 @@ namespace :kandan do
user.password_confirmation = "kandanappadmin"
user.is_admin = true
user.save!
else
# Doing some cleanup if this is an upgrade instead of a new DB

# Users that were already in the database with no registration status will be added as active
User.where("registration_status IS NULL").each do |user|
user.activate!
end

# If there is no admin we will try to find a user with username admin and make it an admin. Otherwise we will
# alert the user
if User.where(:is_admin => true).count == 0
admin = User.where(:username => "admin").first

if admin
admin.is_admin = true && admin.save!
else
puts "\e[31mIt looks like there are no admins in your database. Run rake kandan:add_admin_user\e[0m"
end
end

end

channel = Channel.first
Expand Down Expand Up @@ -77,4 +97,38 @@ namespace :kandan do
puts "There's not hubot account. Run rake kandan:boot_hubot to create a bot account."
end
end

desc "Adds an admin based on a username if there are no admins in kandan"
task :add_admin_user => :environment do
if User.count == 0
puts "\e[31mThere are no users on your kandan DB. Try running rake kandan:bootstrap first\e[0m"
elsif User.where(:is_admin => true).count != 0
puts "\e[32mLooks like you already have an admin. Nothing to do here.\e[0m"
else
username = ""
exit_word = "EXIT"
done = false

while not done
puts "Enter the email address of your admin user or type '#{exit_word}' to cancel this script"
answer = (STDIN.gets).delete("\n")

if answer == exit_word
puts "Ok. We forgive you. Carry on....."
done = true
elsif user = User.where(:username => answer).first
user.is_admin = true
user.save!

puts "Done. #{user.full_name} is now admin!"

done = true
else
puts "User not found. Let's try that again."
end

end

end
end
end

0 comments on commit 7adc29e

Please sign in to comment.