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')
29 g_err, usercount = '', 0
31 params[:users][:textfield].each do |line|
33 name, email, *groupnames = line.chomp.split(",")
36 err << "User #{name} does not exist.<br>" unless (user = User.find_by_name(name.strip))
37 p "#{user} going well"
39 groupnames.each do |g|
40 op, group = g.split(//, 2)
41 err << "Group #{group} does not exist.<br>" unless (grp = Group.find_by_name(group.strip))
42 p "#{group} going well with #{op}"
49 tmp.delete_if {|x| x.id == grp.id }.each {|x| user.groups.push(x)}
51 err << "Invalid operator #{op}, must be + or -.<br>"
57 err << "User #{name} already exists.<br>" if User.find_by_name(name)
58 err << "Email #{email} already exists.<br>" if User.find_by_email(email)
59 err << "Email #{email} is invalid.<br>" unless email.strip.match(VALID_EMAIL)
62 groupnames.each do |g|
63 err << "Group #{g} does not exist.<br>" if (groups << Group.find_by_name(g.strip)) == [nil]
67 password = User.random_password(8)
70 :email => email.strip,
73 groups.compact.each {|g| user.groups.push(g) }
76 PasswordMailer.deliver_new_user(user.name, user.email, password)
82 flash[g_err.empty? ? :user_confirmation : :user_error] = g_err + "#{usercount} users added/changed."
83 redirect_to :action => 'list'
86 # Show a form to enter data for a new user.
91 # Create a new user using the posted data from the 'new' view.
94 @user = User.new(params[:user])
95 @user.password_required = true
97 # add the user to the selected groups
98 add_user_to_groups(@user, params[:belongs_to_group])
102 # send an e-mail to the new user; informing him about his account
103 PasswordMailer.deliver_new_user(@user.name, @user.email, params[:user][:password])
104 flash[:user_confirmation] = "The new user's account information has been e-mailed to " + @user.email
105 redirect_to :action => 'list'
106 rescue Exception => e
107 if e.message.match('getaddrinfo: No address associated with nodename')
108 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.'
110 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."
112 redirect_to :action => 'list'
115 render :action => 'new'
120 # Show a form in which the data of a user can be edited.
125 # Update the user attributes with the posted variables from the 'edit' view.
128 # add the user to the selected groups
129 add_user_to_groups(@user, params[:belongs_to_group])
131 if @user.update_attributes(params[:user])
132 # If a user edited his/her own settings: show a confirmation in the edit screen
133 # else: redirect to the list of users
134 if @user == @logged_in_user
135 flash[:user_confirmation] = 'You saved your settings successfully'
136 redirect_to :action => 'edit', :id => params[:id]
138 redirect_to :action => 'list'
141 render :action => 'edit'
149 redirect_to :action => 'list'
152 # These methods are private:
153 # [#add_user_to_groups] Add the user to the groups that are checked in the view
154 # [#do_not_destroy_admin_user] Via before_filter: make sure admin is not deleted
155 # [#does_user_exist] Check if a user exists
157 # Add the user to the groups that are checked in the view
158 def add_user_to_groups(user, group_check_box_list)
159 if group_check_box_list and @logged_in_user.is_admin?
160 user.groups.clear # remove the user from all groups
162 # admins is not in the list cause it's disabled;
163 # add it hardcodedly (is that a word?!?) in case of
164 # <i>the administrator</i>
165 user.groups.push(Group.find_by_is_the_administrators_group(true)) if user.is_the_administrator?
167 # iteratively add the user to the selected groups
168 group_check_box_list.each do |group_id, belongs_to|
169 if belongs_to == 'yes'
170 group = Group.find_by_id(group_id)
171 user.groups.push(group) if group # add user to the selected group
177 # The admin user can not be deleted.
178 # By calling this method via a before_filter,
179 # you makes sure this doesn't happen.
180 def do_not_destroy_admin_user
181 if @user and @user.is_the_administrator?
182 redirect_to :action => 'list' and return false
186 # Check if a user exists before executing an action.
187 # If it doesn't exist: redirect to 'list' and show an error message
189 # only admins can edit other users's data
190 if @logged_in_user.is_admin?
191 @user = User.find(params[:id])
193 @user = @logged_in_user
196 flash.now[:user_error] = 'Someone else deleted the user. Your action was cancelled.'
197 redirect_to :action => 'list' and return false