Skip to content
Browse files

Allow setting a user as admin from the web

  • Loading branch information...
1 parent 4680d14 commit 481b4f72b12801250e7fbdc50ca7ba71f05c7364 @courtenay courtenay committed Aug 4, 2009
View
8 app/controllers/users_controller.rb
@@ -83,6 +83,14 @@ def purge
@user.destroy
redirect_to users_path
end
+
+ def make_admin
+ redirect_back_or_default('/') and return unless admin?
+ @user = find_user
+ @user.admin = (params[:user][:admin] == "1")
+ @user.save
+ redirect_to @user
+ end
protected
def find_user
View
18 app/views/users/edit.html.erb
@@ -1,8 +1,20 @@
<% content_for :right do %>
-<h5><%= 'Avatars'[:avatars_title] %></h5>
-
-<p><%= 'To have your very own avatar displayed on this forum visit {gravatar} and sign up for a free gravatar.'[:gravatar_notice, %(<a href="http://www.gravatar.com/">gravatar.com</a>)] %></p>
+ <h5><%= 'Avatars'[:avatars_title] %></h5>
+ <p><%= 'To have your very own avatar displayed on this forum visit {gravatar} and sign up for a free gravatar.'[:gravatar_notice, %(<a href="http://www.gravatar.com/">gravatar.com</a>)] %></p>
+
+ <% if current_user.admin? %>
+ <% form_for @user, :url => make_admin_user_path(@user) do |f| %>
+ <h5>Admin</h5>
+ <p>
+ <%= f.check_box :admin %>
+ <label for="user_admin"><%= 'User is an administrator'[] %></label>
+ </p>
+ <p>
+ <%= submit_tag 'Save'[:save] %>
+ </p>
+ <% end %>
+ <% end %>
<% end %>
View
1 config/routes.rb
@@ -15,6 +15,7 @@
map.resources :users, :member => { :suspend => :put,
:settings => :get,
+ :make_admin => :put,
:unsuspend => :put,
:purge => :delete },
:has_many => [:posts]
View
27 spec/controllers/users_controller_spec.rb
@@ -120,6 +120,33 @@ def create_user(options = {})
end
end
+describe UsersController, "PUT #make_admin" do
+ before do
+ login_as :admin
+ current_site :default
+ @attributes = {'login' => "Default"}
+ end
+
+ describe UsersController, "(as admin, successful)" do
+ define_models :users
+
+ it "sets admin" do
+ user = users(:default)
+ user.admin.should be_false
+ put :make_admin, :id => users(:default).to_param, :user => { :admin => "1" }
+ user.reload.admin.should be_true
+ end
+
+ it "unsets admin" do
+ user = users(:default)
+ user.update_attribute :admin, true
+ user.admin.should be_true
+ put :make_admin, :id => users(:default).to_param, :user => { }
+ user.reload.admin.should be_false
+ end
+ end
+end
+
describe UsersController, "PUT #update" do
before do
login_as :default
View
2 spec/model_stubs.rb
@@ -44,7 +44,7 @@ module ModelStubbing
define_models :users do
model User do
- stub :admin, :login => 'admin-user', :email => 'admin-user@example.com', :remember_token => 'blah'
+ stub :admin, :login => 'admin-user', :email => 'admin-user@example.com', :remember_token => 'blah', :admin => true
stub :pending, :login => 'pending-user', :email => 'pending-user@example.com', :state => 'pending', :activated_at => nil, :remember_token => 'asdf'
stub :suspended, :login => 'suspended-user', :email => 'suspended-user@example.com', :state => 'suspended', :remember_token => 'dfdfd'
end

0 comments on commit 481b4f7

Please sign in to comment.
Something went wrong with that request. Please try again.