initial import boxroom 0.6.2
[boxroom-stian.git] / app / controllers / user_controller.rb
blobf992560edd6bf304c85880322a4e3da6294f3ccd
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.
15   def index
16     list
17     render :action => 'list'
18   end
20   # List all the users.
21   def list
22     @users = User.find(:all, :order => 'name')
23   end
25   # Show a form to enter data for a new user.
26   def new
27     @user = User.new
28   end
30   # Create a new user using the posted data from the 'new' view.
31   def create
32     if request.post?
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])
39       if @user.save
40         begin
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'
45         rescue Exception => e
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.'
48           else
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."
50           end
51           redirect_to :action => 'list'
52         end
53       else
54         render :action => 'new'
55       end
56     end
57   end
59   # Show a form in which the data of a user can be edited.
60   def edit
61     render
62   end
64   # Update the user attributes with the posted variables from the 'edit' view.
65   def update
66     if request.post?
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]
76         else
77           redirect_to :action => 'list'
78         end
79       else
80         render :action => 'edit'
81       end
82     end
83   end
85   # Delete a user.
86   def destroy
87     @user.destroy
88     redirect_to :action => 'list'
89   end
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
95   private
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
111           end
112         end
113       end
114     end
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
122       end
123     end
125     # Check if a user exists before executing an action.
126     # If it doesn't exist: redirect to 'list' and show an error message
127     def does_user_exist
128       # only admins can edit other users's data
129       if @logged_in_user.is_admin?
130         @user = User.find(params[:id])
131       else
132         @user = @logged_in_user
133       end
134     rescue
135       flash.now[:user_error] = 'Someone else deleted the user. Your action was cancelled.'
136       redirect_to :action => 'list' and return false
137     end