Skip to content

Commit

Permalink
Merge pull request #116 from gabceb/kandan_113
Browse files Browse the repository at this point in the history
Added default status for users. Migration from 1.0 works.
  • Loading branch information
donthorp committed Feb 26, 2013
2 parents e03e9d9 + b367ccf commit 0da443c
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 31 deletions.
2 changes: 1 addition & 1 deletion app/assets/javascripts/admin/admin.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ act_on_user = (obj)->

btn_text = _.str.titleize(new_css_class)

$row.find("td.status").text(_.str.titleize(data.status))
$row.find("td.registration_status").text(_.str.titleize(data.registration_status))

# Change the look of the buttons by removing and adding classes
$el.text(btn_text).removeClass("#{old_btn_class} #{old_css_class}").addClass("#{new_btn_class} #{new_css_class}")
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/admin/admin_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def index
@approved_users = []

# Iterate over the array to get approved and non-approved users
@all_users.each{|user| user.status.waiting_approval? ? @waiting_for_approval_users.push(user) : @approved_users.push(user) }
@all_users.each{|user| user.registration_status.waiting_approval? ? @waiting_for_approval_users.push(user) : @approved_users.push(user) }
end

def update
Expand All @@ -30,13 +30,11 @@ def update_user

case action
when "activate", "approve"
user.status = "active"
user.activate!
when "suspend"
user.status = "suspended"
user.suspend!
end

user.save! if user.changed?

render :json => user, :status => 200
end

Expand Down
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class ApplicationController < ActionController::Base

def force_approved_account
# We will redirect to the approval page if a user is signed in, is not an admin and is marked as waiting for approval
redirect = user_signed_in? && !current_user.is_admin? && current_user.status.waiting_approval?
redirect = user_signed_in? && !current_user.is_admin? && current_user.registration_status.waiting_approval?

redirect_to approval_path if redirect
end

def redirect_suspended_account
# We will redirect to suspended if a user is singed in and its marked as suspended
redirect = user_signed_in? && current_user.status.suspended?
redirect = user_signed_in? && current_user.registration_status.suspended?

redirect_to suspended_path if redirect
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ class PagesController < ApplicationController
skip_filter :redirect_suspended_account, :only => :suspended

def approval
redirect_to(root_path) && return unless current_user.status.waiting_approval?
redirect_to(root_path) && return unless current_user.registration_status.waiting_approval?
end

def suspended
redirect_to(root_path) && return unless current_user.status.suspended?
redirect_to(root_path) && return unless current_user.registration_status.suspended?
end

def about
Expand Down
8 changes: 4 additions & 4 deletions app/helpers/admin/admin_helper.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module Admin::AdminHelper
def user_status user
"<div class='#{user.status}'>#{user.status.titlecase}</div>".html_safe
def user_registration_status user
"<div class='#{user.registration_status}'>#{user.registration_status.titlecase}</div>".html_safe
end

def user_action user
action, css = if user.status.waiting_approval?
action, css = if user.registration_status.waiting_approval?
["Approve", "btn-success"]
elsif user.status.suspended?
elsif user.registration_status.suspended?
["Activate", "btn-success"]
else
["Suspend", "btn-danger"]
Expand Down
31 changes: 22 additions & 9 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,23 @@ class User < ActiveRecord::Base
extend Enumerize

# Being pesimistic here and making the default waiting for approval for security reasons
enumerize :status, in: [:active, :suspended, :waiting_approval], :default => :waiting_approval
enumerize :registration_status, in: [:active, :suspended, :waiting_approval], :default => :waiting_approval

has_many :activities
before_save :ensure_authentication_token
before_save :ensure_gravatar_hash
before_create :mark_status_depending_on_app_settings
before_create :mark_registration_status_depending_on_app_settings

after_create :ensure_at_least_one_admin
after_destroy :ensure_at_least_one_admin

validates :username, :presence => true, :uniqueness => true
validates :first_name, :presence => true
validates :last_name, :presence => true


# Kandan.devise_modules is defined in config/initializers/kandan.rb
devise devise *Kandan.devise_modules

# Setup accessible (or protected) attributes for your model
attr_accessible :id, :username, :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :locale, :gravatar_hash, :status
attr_accessible :id, :username, :email, :password, :password_confirmation, :remember_me, :first_name, :last_name, :locale, :gravatar_hash, :registration_status

def full_name
"#{self.first_name.to_s} #{self.last_name.to_s}".titlecase
Expand All @@ -38,10 +35,10 @@ def cloudfuji_extra_attributes(extra_attributes)
self.locale = extra_attributes["locale"]
end

# Callback to mark the user status depending on the settings of the app
def mark_status_depending_on_app_settings
# Callback to mark the user registration status depending on the settings of the app
def mark_registration_status_depending_on_app_settings
# If the site is public we will make the user active. Otherwise we will make the user as waiting_approval
self.status = Setting.my_settings.public_site? ? :active : :waiting_approval
self.registration_status = Setting.my_settings.public_site? ? :active : :waiting_approval
end

def ensure_gravatar_hash
Expand All @@ -68,4 +65,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
4 changes: 2 additions & 2 deletions app/views/admin/admin/_user_table.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
</thead>
<tbody>
<% users.each do |user| %>
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>" data-full-name="<%= user.full_name %>">
<tr class="<%= cycle('odd', 'even')%>" data-user-id="<%= user.id%>" data-full-name="<%= user.full_name_or_username %>">
<td class="username">
<%= user.username %>
</td>
Expand All @@ -27,7 +27,7 @@
<%= user.email %>
</td>
<td class="status">
<%= user_status(user) %>
<%= user_registration_status(user) %>
</td>
<td class="action">
<%= user_action(user) %>
Expand Down
11 changes: 11 additions & 0 deletions db/migrate/20130224150724_add_status_as_default_for_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class AddStatusAsDefaultForUsers < ActiveRecord::Migration
def up
rename_column :users, :status, :registration_status
change_column :users, :registration_status, :string, :default => "active"
end

def down
change_column :users, :registration_status, :string, :default => nil
rename_column :users, :registration_status, :status
end
end
12 changes: 6 additions & 6 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.

ActiveRecord::Schema.define(:version => 20130221173650) do
ActiveRecord::Schema.define(:version => 20130224150724) do

create_table "activities", :force => true do |t|
t.text "content"
Expand Down Expand Up @@ -57,8 +57,8 @@
end

create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "email", :default => "", :null => false
t.string "encrypted_password", :limit => 128, :default => "", :null => false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
Expand All @@ -72,13 +72,13 @@
t.text "last_name"
t.text "ido_id"
t.string "locale"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.text "gravatar_hash"
t.boolean "active", :default => true
t.string "username"
t.boolean "is_admin"
t.string "status"
t.string "registration_status", :default => "active"
end

add_index "users", ["authentication_token"], :name => "index_users_on_authentication_token", :unique => true
Expand Down
55 changes: 55 additions & 0 deletions lib/tasks/kandan.rake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ 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 +98,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_or_username} is now admin!"

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

end

end
end
end

0 comments on commit 0da443c

Please sign in to comment.