1 # The user controller contains the following actions:
2 # [#index] the default action, redirects to list
3 # [#list] list all the users
4 # [#new] shows the form for creating a new user
5 # [#create] create a new user
6 # [#edit] show the form for adjusting the attributes of a user
7 # [#update] updates the attributes of a user
8 # [#destroy] delete a user
9 class UserController < ApplicationController
10 before_filter :authorize_admin, :except => [:edit, :update]
11 before_filter :does_user_exist, :only => [:edit, :update, :destroy]
12 before_filter :do_not_destroy_admin_user, :only => :destroy
14 # The default action, redirects to list.
17 render :action => 'list'
22 @users = User.find(:all, :order => 'name')
25 # Show a form to enter data for a new user.
30 # Create a new user using the posted data from the 'new' view.
33 @user = User.new(params[:user])
34 @user.password_required = true
36 # add the user to the selected groups
37 add_user_to_groups(@user, params[:belongs_to_group])
41 # send an e-mail to the new user; informing him about his account
42 PasswordMailer.deliver_new_user(@user.name, @user.email, params[:user][:password])
43 flash[:user_confirmation] = "The new user's account information has been e-mailed to " + @user.email
44 redirect_to :action => 'list'
46 if e.message.match('getaddrinfo: No address associated with nodename')
47 flash[:user_error] = 'The mail server settings in the environment file are incorrect. Check the installation instructions to solve this problem. The user was created nevertheless.'
49 flash[:user_error] = e.message + ".<br /><br />This means either the user's e-mail address or Boxroom's configuration for e-mailing is invalid. Please contact the administrator or check the installation instructions. The user was created nevertheless."
51 redirect_to :action => 'list'
54 render :action => 'new'
59 # Show a form in which the data of a user can be edited.
64 # Update the user attributes with the posted variables from the 'edit' view.
67 # add the user to the selected groups
68 add_user_to_groups(@user, params[:belongs_to_group])
70 if @user.update_attributes(params[:user])
71 # If a user edited his/her own settings: show a confirmation in the edit screen
72 # else: redirect to the list of users
73 if @user == @logged_in_user
74 flash[:user_confirmation] = 'You saved your settings successfully'
75 redirect_to :action => 'edit', :id => params[:id]
77 redirect_to :action => 'list'
80 render :action => 'edit'
88 redirect_to :action => 'list'
91 # These methods are private:
92 # [#add_user_to_groups] Add the user to the groups that are checked in the view
93 # [#do_not_destroy_admin_user] Via before_filter: make sure admin is not deleted
94 # [#does_user_exist] Check if a user exists
96 # Add the user to the groups that are checked in the view
97 def add_user_to_groups(user, group_check_box_list)
98 if group_check_box_list and @logged_in_user.is_admin?
99 user.groups.clear # remove the user from all groups
101 # admins is not in the list cause it's disabled;
102 # add it hardcodedly (is that a word?!?) in case of
103 # <i>the administrator</i>
104 user.groups.push(Group.find_by_is_the_administrators_group(true)) if user.is_the_administrator?
106 # iteratively add the user to the selected groups
107 group_check_box_list.each do |group_id, belongs_to|
108 if belongs_to == 'yes'
109 group = Group.find_by_id(group_id)
110 user.groups.push(group) if group # add user to the selected group
116 # The admin user can not be deleted.
117 # By calling this method via a before_filter,
118 # you makes sure this doesn't happen.
119 def do_not_destroy_admin_user
120 if @user and @user.is_the_administrator?
121 redirect_to :action => 'list' and return false
125 # Check if a user exists before executing an action.
126 # If it doesn't exist: redirect to 'list' and show an error message
128 # only admins can edit other users's data
129 if @logged_in_user.is_admin?
130 @user = User.find(params[:id])
132 @user = @logged_in_user
135 flash.now[:user_error] = 'Someone else deleted the user. Your action was cancelled.'
136 redirect_to :action => 'list' and return false