diff options
| author | hukl <contact@smyck.org> | 2009-10-07 21:03:28 +0200 |
|---|---|---|
| committer | hukl <contact@smyck.org> | 2009-10-07 21:03:28 +0200 |
| commit | 9dadc61cae2c4c01a97880e89ca86a0e760fc8d1 (patch) | |
| tree | 4c50bccc0339429d669a04e5dc3f2e735dfcb515 /app | |
| parent | a57fb0c2084885c35b7ba89917c37696e5df3b3f (diff) | |
implemented complete restful user management interface including functional tests. this enables basic user operation. note that only admins are allowed to create, edit, destroy other users
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/users_controller.rb | 30 | ||||
| -rw-r--r-- | app/helpers/users_helper.rb | 7 | ||||
| -rw-r--r-- | app/models/user.rb | 2 | ||||
| -rw-r--r-- | app/views/users/_admin_user_item.html.erb | 12 | ||||
| -rw-r--r-- | app/views/users/_user_item.html.erb | 4 | ||||
| -rw-r--r-- | app/views/users/create.html.erb | 2 | ||||
| -rw-r--r-- | app/views/users/destroy.html.erb | 2 | ||||
| -rw-r--r-- | app/views/users/edit.html.erb | 32 | ||||
| -rw-r--r-- | app/views/users/index.html.erb | 15 | ||||
| -rw-r--r-- | app/views/users/new.html.erb | 32 | ||||
| -rw-r--r-- | app/views/users/show.html.erb | 22 | ||||
| -rw-r--r-- | app/views/users/update.html.erb | 2 |
12 files changed, 141 insertions, 21 deletions
diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 19f6b2d..b15f83b 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb | |||
| @@ -3,29 +3,57 @@ class UsersController < ApplicationController | |||
| 3 | # Private | 3 | # Private |
| 4 | 4 | ||
| 5 | before_filter :login_required | 5 | before_filter :login_required |
| 6 | before_filter :verify_admin_status, :except => [:index, :show] | ||
| 6 | 7 | ||
| 7 | layout 'admin' | 8 | layout 'admin' |
| 8 | 9 | ||
| 9 | def index | 10 | def index |
| 10 | @users = User.all | 11 | @users = User.all(:order => "login ASC") |
| 11 | end | 12 | end |
| 12 | 13 | ||
| 13 | def new | 14 | def new |
| 15 | @user = User.new( params[:user] ) | ||
| 14 | end | 16 | end |
| 15 | 17 | ||
| 16 | def create | 18 | def create |
| 19 | @user = User.new params[:user] | ||
| 20 | |||
| 21 | if @user.save | ||
| 22 | redirect_to user_path(@user) | ||
| 23 | else | ||
| 24 | render :new | ||
| 25 | end | ||
| 17 | end | 26 | end |
| 18 | 27 | ||
| 19 | def edit | 28 | def edit |
| 29 | @user = User.find(params[:id]) | ||
| 20 | end | 30 | end |
| 21 | 31 | ||
| 22 | def update | 32 | def update |
| 33 | @user = User.find(params[:id]) | ||
| 34 | |||
| 35 | if @user.update_attributes(params[:user]) | ||
| 36 | redirect_to user_path(@user) | ||
| 37 | else | ||
| 38 | render :edit | ||
| 39 | end | ||
| 23 | end | 40 | end |
| 24 | 41 | ||
| 25 | def show | 42 | def show |
| 43 | @user = User.find(params[:id]) | ||
| 26 | end | 44 | end |
| 27 | 45 | ||
| 28 | def destroy | 46 | def destroy |
| 47 | user = User.find(params[:id]) | ||
| 48 | user.destroy if user | ||
| 49 | redirect_to users_path | ||
| 29 | end | 50 | end |
| 30 | 51 | ||
| 52 | private | ||
| 53 | def verify_admin_status | ||
| 54 | unless current_user.admin | ||
| 55 | flash[:notice] = "Sorry, you need to be an admin for this action" | ||
| 56 | redirect_to users_path | ||
| 57 | end | ||
| 58 | end | ||
| 31 | end | 59 | end |
diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 2310a24..ff03138 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb | |||
| @@ -1,2 +1,9 @@ | |||
| 1 | module UsersHelper | 1 | module UsersHelper |
| 2 | def user_list_by_admin_status | ||
| 3 | if current_user && current_user.admin | ||
| 4 | render :partial => 'admin_user_item', :collection => @users | ||
| 5 | else | ||
| 6 | render :partial => 'user_item', :collection => @users | ||
| 7 | end | ||
| 8 | end | ||
| 2 | end | 9 | end |
diff --git a/app/models/user.rb b/app/models/user.rb index 26ebf45..035a145 100644 --- a/app/models/user.rb +++ b/app/models/user.rb | |||
| @@ -21,7 +21,7 @@ class User < ActiveRecord::Base | |||
| 21 | validates_format_of :email, :with => Authentication.email_regex, | 21 | validates_format_of :email, :with => Authentication.email_regex, |
| 22 | :message => Authentication.bad_email_message | 22 | :message => Authentication.bad_email_message |
| 23 | 23 | ||
| 24 | attr_accessible :login, :email, :password, :password_confirmation | 24 | attr_accessible :login, :email, :password, :password_confirmation, :admin |
| 25 | 25 | ||
| 26 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. | 26 | # Authenticates a user by their login name and unencrypted password. Returns the user or nil. |
| 27 | def self.authenticate(login, password) | 27 | def self.authenticate(login, password) |
diff --git a/app/views/users/_admin_user_item.html.erb b/app/views/users/_admin_user_item.html.erb new file mode 100644 index 0000000..d882dc4 --- /dev/null +++ b/app/views/users/_admin_user_item.html.erb | |||
| @@ -0,0 +1,12 @@ | |||
| 1 | <tr> | ||
| 2 | <td><%= admin_user_item.login %></td> | ||
| 3 | <td> | ||
| 4 | <%= link_to "Show", user_path(admin_user_item) %> | ||
| 5 | <%= link_to( | ||
| 6 | "Destroy", | ||
| 7 | user_path(admin_user_item), | ||
| 8 | :method => "delete", | ||
| 9 | :confirm => "Are you sure to delete user: #{admin_user_item.login}?" | ||
| 10 | ) %> | ||
| 11 | </td> | ||
| 12 | </tr> \ No newline at end of file | ||
diff --git a/app/views/users/_user_item.html.erb b/app/views/users/_user_item.html.erb new file mode 100644 index 0000000..c3389ba --- /dev/null +++ b/app/views/users/_user_item.html.erb | |||
| @@ -0,0 +1,4 @@ | |||
| 1 | <tr> | ||
| 2 | <td><%= user_item.login %></td> | ||
| 3 | <td></td> | ||
| 4 | </tr> \ No newline at end of file | ||
diff --git a/app/views/users/create.html.erb b/app/views/users/create.html.erb deleted file mode 100644 index 48ea02e..0000000 --- a/app/views/users/create.html.erb +++ /dev/null | |||
| @@ -1,2 +0,0 @@ | |||
| 1 | <h1>Users#create</h1> | ||
| 2 | <p>Find me in app/views/users/create.html.erb</p> | ||
diff --git a/app/views/users/destroy.html.erb b/app/views/users/destroy.html.erb deleted file mode 100644 index de4bd26..0000000 --- a/app/views/users/destroy.html.erb +++ /dev/null | |||
| @@ -1,2 +0,0 @@ | |||
| 1 | <h1>Users#destroy</h1> | ||
| 2 | <p>Find me in app/views/users/destroy.html.erb</p> | ||
diff --git a/app/views/users/edit.html.erb b/app/views/users/edit.html.erb index 1881fbd..5b73242 100644 --- a/app/views/users/edit.html.erb +++ b/app/views/users/edit.html.erb | |||
| @@ -1,2 +1,30 @@ | |||
| 1 | <h1>Users#edit</h1> | 1 | <h1>Edit existing user</h1> |
| 2 | <p>Find me in app/views/users/edit.html.erb</p> | 2 | |
| 3 | <% form_for @user do |f| %> | ||
| 4 | <table id="new_node"> | ||
| 5 | <tr> | ||
| 6 | <td class="description">Login</td> | ||
| 7 | <td><%= f.text_field :login %></td> | ||
| 8 | </tr> | ||
| 9 | <tr> | ||
| 10 | <td class="description">E-Mail</td> | ||
| 11 | <td><%= f.text_field :email %></td> | ||
| 12 | </tr> | ||
| 13 | <tr> | ||
| 14 | <td class="description">Password</td> | ||
| 15 | <td><%= f.text_field :password %></td> | ||
| 16 | </tr> | ||
| 17 | <tr> | ||
| 18 | <td class="description">Confirm</td> | ||
| 19 | <td><%= f.text_field :password_confirmation %></td> | ||
| 20 | </tr> | ||
| 21 | <tr> | ||
| 22 | <td class="description">Admin?</td> | ||
| 23 | <td><%= f.check_box :admin %></td> | ||
| 24 | </tr> | ||
| 25 | <tr> | ||
| 26 | <td class="description"></td> | ||
| 27 | <td class="right"><%= f.submit "Create" %></td> | ||
| 28 | </tr> | ||
| 29 | </table> | ||
| 30 | <% end %> \ No newline at end of file | ||
diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index f0b5b28..8526d84 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb | |||
| @@ -1,12 +1,13 @@ | |||
| 1 | <h1>Users</h1> | 1 | <h1>Users</h1> |
| 2 | 2 | ||
| 3 | <table> | 3 | <table id="user_list"> |
| 4 | <tr> | 4 | <tr> |
| 5 | <td>login</td> | 5 | <td>login</td> |
| 6 | </tr> | 6 | </tr> |
| 7 | <% @users.each do |user| %> | 7 | <%= user_list_by_admin_status %> |
| 8 | <tr> | 8 | </table> |
| 9 | <td><%= user.login %></td> | 9 | |
| 10 | </tr> | 10 | |
| 11 | <% end %> | 11 | <% content_for :subnavigation do %> |
| 12 | </table> \ No newline at end of file | 12 | <%= link_to "create", new_user_path %> |
| 13 | <% end %> \ No newline at end of file | ||
diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index c21a1ad..0629641 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb | |||
| @@ -1,2 +1,30 @@ | |||
| 1 | <h1>Users#new</h1> | 1 | <h1>Create new user</h1> |
| 2 | <p>Find me in app/views/users/new.html.erb</p> | 2 | |
| 3 | <% form_for @user do |f| %> | ||
| 4 | <table id="new_node"> | ||
| 5 | <tr> | ||
| 6 | <td class="description">Login</td> | ||
| 7 | <td><%= f.text_field :login %></td> | ||
| 8 | </tr> | ||
| 9 | <tr> | ||
| 10 | <td class="description">E-Mail</td> | ||
| 11 | <td><%= f.text_field :email %></td> | ||
| 12 | </tr> | ||
| 13 | <tr> | ||
| 14 | <td class="description">Password</td> | ||
| 15 | <td><%= f.text_field :password %></td> | ||
| 16 | </tr> | ||
| 17 | <tr> | ||
| 18 | <td class="description">Confirm</td> | ||
| 19 | <td><%= f.text_field :password_confirmation %></td> | ||
| 20 | </tr> | ||
| 21 | <tr> | ||
| 22 | <td class="description">Admin?</td> | ||
| 23 | <td><%= f.check_box :admin %></td> | ||
| 24 | </tr> | ||
| 25 | <tr> | ||
| 26 | <td class="description"></td> | ||
| 27 | <td class="right"><%= f.submit "Create" %></td> | ||
| 28 | </tr> | ||
| 29 | </table> | ||
| 30 | <% end %> \ No newline at end of file | ||
diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index e5fa3ad..3055d24 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb | |||
| @@ -1,2 +1,20 @@ | |||
| 1 | <h1>Users#show</h1> | 1 | <h1>User: <%= @user.login %></h1> |
| 2 | <p>Find me in app/views/users/show.html.erb</p> | 2 | |
| 3 | <table id="new_node"> | ||
| 4 | <tr> | ||
| 5 | <td class="description">Login</td> | ||
| 6 | <td><%= @user.login %></td> | ||
| 7 | </tr> | ||
| 8 | <tr> | ||
| 9 | <td class="description">E-Mail</td> | ||
| 10 | <td><%= @user.email %></td> | ||
| 11 | </tr> | ||
| 12 | <tr> | ||
| 13 | <td class="description">Admin?</td> | ||
| 14 | <td><%= @user.admin ? "yes" : "no" %></td> | ||
| 15 | </tr> | ||
| 16 | </table> | ||
| 17 | |||
| 18 | <% content_for :subnavigation do %> | ||
| 19 | <%= link_to 'Edit', edit_user_path(@user) %> | ||
| 20 | <% end %> | ||
diff --git a/app/views/users/update.html.erb b/app/views/users/update.html.erb deleted file mode 100644 index cabbde1..0000000 --- a/app/views/users/update.html.erb +++ /dev/null | |||
| @@ -1,2 +0,0 @@ | |||
| 1 | <h1>Users#update</h1> | ||
| 2 | <p>Find me in app/views/users/update.html.erb</p> | ||
