-
-
Notifications
You must be signed in to change notification settings - Fork 536
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created an admin controller that allows the admin user (only) to view…
… the users on the system and destroy and signup new users. Destroying a user deletes all of their actions, contexts, projects and notes. The link to the admin page only appears when an admin user is logged in, and the signup link is now on the admin page, rather than appearing in the mini-links at the top of the page. The page also lists some statistics associated with each user (the number of actions, contexts, projects etc. each user has). git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@371 a4c988fc-2ded-0310-b66e-134b36920a42
- Loading branch information
bsag
committed
Dec 10, 2006
1 parent
74ad2b9
commit 26c7a1e
Showing
13 changed files
with
180 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
class AdminController < ApplicationController | ||
|
||
before_filter :login_required | ||
before_filter :admin_login_required | ||
layout 'standard' | ||
|
||
def index | ||
@user_pages, @users = paginate :users, :order => 'login ASC', :per_page => 10 | ||
@total_users = User.find(:all).size | ||
# When we call login/signup from the admin page | ||
# we store the URL so that we get returned here when signup is successful | ||
store_location | ||
end | ||
|
||
def destroy | ||
@deleted_user = User.find_by_id(params[:id]) | ||
@saved = @deleted_user.destroy | ||
@total_users = User.find(:all).size | ||
|
||
respond_to do |wants| | ||
|
||
wants.html do | ||
if @saved | ||
notify :notice, "Successfully deleted user #{@deleted_user.login}", 2.0 | ||
redirect_to :action => 'index' | ||
else | ||
notify :error, "Failed to delete user #{@deleted_user.login}", 2.0 | ||
redirect_to :action => 'index' | ||
end | ||
end | ||
|
||
wants.js do | ||
render | ||
end | ||
|
||
wants.xml { render :text => '200 OK. User deleted.', :status => 200 } | ||
|
||
end | ||
end | ||
|
||
protected | ||
|
||
def admin_login_required | ||
unless User.find_by_id_and_is_admin(session['user_id'], true) | ||
notify :error, "Only admin users are allowed access to this function" | ||
redirect_to :controller => 'todo', :action => 'index' | ||
return false | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module AdminHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Admin#create</h1> | ||
<p>Find me in app/views/admin/create.rhtml</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
if @saved | ||
page["user-#{@deleted_user.id}"].remove | ||
page['user_count'].replace_html @total_users.to_s | ||
page.notify :notice, "User #{@deleted_user.login} was successfully destroyed", 2.0 | ||
else | ||
page.notify :error, "There was an error deleting the user #{@deleted_user.login}", 8.0 | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
page.notify :error, @error_message || "An error occurred on the server.", 8.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<h1>Manage users</h1> | ||
|
||
<p>You have a total of <span id="user_count"><%= @total_users %></span> users</p> | ||
|
||
<table class="users_table"> | ||
<tr> | ||
<th>Login</th> | ||
<th>Full name</th> | ||
<th>Authorization type</th> | ||
<th>Open ID URL</th> | ||
<th>Total actions</th> | ||
<th>Total contexts</th> | ||
<th>Total projects</th> | ||
<th>Total notes</th> | ||
<th> </th> | ||
</tr> | ||
<% for user in @users %> | ||
<tr <%= "class=\"highlight\"" if user.is_admin? %> id="user-<%= user.id %>"> | ||
<td><%=h user.login %></td> | ||
<td><%=h user.last_name? ? user.display_name : '-' %></td> | ||
<td><%= h user.auth_type %></td> | ||
<td><%= h user.open_id_url || '-' %></td> | ||
<td><%= h user.todos.size %></td> | ||
<td><%= h user.contexts.size %></td> | ||
<td><%= h user.projects.size %></td> | ||
<td><%= h user.notes.size %></td> | ||
<td><%= !user.is_admin? ? link_to_remote( image_tag("blank.png", :title =>"Destroy user", :class=>"delete_item"), {:url => { :controller => 'admin', :action => 'destroy', :id => user.id }, :confirm => "Warning: this will delete user \'#{user.login}\', all their actions, contexts, project and notes. Are you sure that you want to continue?" }, { :class => "icon" } ) : " " %></td> | ||
</tr> | ||
<% end %> | ||
</table> | ||
<p> | ||
<%= link_to "« Previous page", { :page => @user_pages.current.previous } if @user_pages.current.previous %> | ||
<%= link_to "Next page »", { :page => @user_pages.current.next } if @user_pages.current.next %> | ||
</p> | ||
|
||
<p><%= link_to 'Signup new user', :controller => 'login', :action => 'signup' %></p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<h1>Admin#update</h1> | ||
<p>Find me in app/views/admin/update.rhtml</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require File.dirname(__FILE__) + '/../test_helper' | ||
require 'admin_controller' | ||
|
||
# Re-raise errors caught by the controller. | ||
class AdminController; def rescue_action(e) raise e end; end | ||
|
||
class AdminControllerTest < Test::Unit::TestCase | ||
fixtures :users, :preferences, :projects, :contexts, :todos | ||
|
||
def setup | ||
@controller = AdminController.new | ||
@request = ActionController::TestRequest.new | ||
@response = ActionController::TestResponse.new | ||
end | ||
|
||
def test_get_index_when_not_logged_in | ||
get :index | ||
assert_redirected_to :controller => 'login', :action => 'login' | ||
end | ||
|
||
def test_get_index_by_nonadmin | ||
@request.session['user_id'] = users(:other_user).id | ||
get :index | ||
assert_redirected_to :controller => 'todo', :action => 'index' | ||
end | ||
|
||
def test_get_index_by_admin | ||
@request.session['user_id'] = users(:admin_user).id | ||
get :index | ||
assert_response :success | ||
end | ||
|
||
def test_destroy_user | ||
@no_users_before = User.find(:all).size | ||
@request.session['user_id'] = users(:admin_user).id | ||
xhr :post, :destroy, :id => 3 | ||
assert_rjs :page, "user-3", :remove | ||
assert_equal @no_users_before-1, User.find(:all).size | ||
end | ||
|
||
end |