Class: Caboose::UsersController

Inherits:
ApplicationController show all
Defined in:
app/controllers/caboose/users_controller.rb

Instance Method Summary collapse

Methods inherited from ApplicationController

#admin_add, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #admin_delete, #admin_edit, #admin_index, #admin_json, #admin_json_single, #admin_update, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#add_to_roleObject

POST /admin/users/:id/roles/:role_id



274
275
276
277
278
279
280
# File 'app/controllers/caboose/users_controller.rb', line 274

def add_to_role
  return if !user_is_allowed('users', 'edit')
  if !RoleMembership.where(:user_id => params[:id], :role_id => params[:role_id]).exists?
    RoleMembership.create(:user_id => params[:id], :role_id => params[:role_id])
  end
  render :json => true
end

#admin_suObject

GET /admin/users/:id/su



298
299
300
301
302
303
304
305
306
307
308
309
# File 'app/controllers/caboose/users_controller.rb', line 298

def admin_su
  return if !user_is_allowed('users', 'sudo')
  user = User.find(params[:id])
  
  # Log out the current user
  cookies.delete(:caboose_user_id)
  reset_session
  
  # Login the new user
  (user, false)      
  redirect_to "/"      
end

#before_actionObject



7
8
9
# File 'app/controllers/caboose/users_controller.rb', line 7

def before_action
  @page = Page.page_with_uri(request.host_with_port, '/admin')
end

#createObject

POST /admin/users



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# File 'app/controllers/caboose/users_controller.rb', line 188

def create
  return if !user_is_allowed('users', 'add')
  
  resp = StdClass.new({
      'error' => nil,
      'redirect' => nil
  })
  
  user = User.new()
  user.email = params[:email] ? params[:email].strip.downcase : nil
  
  if user.email.length == 0
    resp.error = "Please enter a valid email address."
  elsif User.where(:email => user.email).exists?
    resp.error = "That email is already in the system."
  else
    user.save
    resp.redirect = "/admin/users/#{user.id}"
  end
  
  render :json => resp
end

#destroyObject

DELETE /admin/users/1



262
263
264
265
266
267
268
269
270
271
# File 'app/controllers/caboose/users_controller.rb', line 262

def destroy
  return if !user_is_allowed('users', 'delete')
  user = User.find(params[:id])
  user.destroy
  
  resp = StdClass.new({
    'redirect' => '/admin/users'
  })
  render :json => resp
end

#editObject

GET /admin/users/1/edit



101
102
103
104
105
106
# File 'app/controllers/caboose/users_controller.rb', line 101

def edit
  return if !user_is_allowed('users', 'edit')
  @edituser = User.find(params[:id])    
  @all_roles = Role.tree
  @roles = Role.roles_with_user(@edituser.id)
end

#edit_passwordObject

GET /admin/users/1/edit-password



109
110
111
112
# File 'app/controllers/caboose/users_controller.rb', line 109

def edit_password
  return if !user_is_allowed('users', 'edit')
  @edituser = User.find(params[:id])
end

#importObject

POST /admin/users/import



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/caboose/users_controller.rb', line 125

def import
  return if !user_is_allowed('users', 'add')
  
  resp = StdClass.new
  csv_data = params[:csv_data]
  arr = []
  good_count = 0
  bad_count = 0            
  csv_data.strip.split("\n").each do |line|        
    data = CSV.parse_line(line)

    if data.count < 3
      arr << [line, true, "Too few columns"] 
      bad_count = bad_count + 1
      next
    end
    
    first_name = data[0].nil? ? nil : data[0].strip
    last_name  = data[1].nil? ? nil : data[1].strip
    email      = data[2].nil? ? nil : data[2].strip.downcase
    username   = data.count >= 4 && !data[3].nil? ? data[3].strip.downcase : nil
    password   = data.count >= 5 && !data[4].nil? ? data[4].strip : random_string(8)
    
    first_name = data[0]
    last_name  = data[1]
    email      = data[2]
    username   = data.count >= 4 ? data[3] : nil
    password   = data.count >= 5 ? data[4] : random_string(8)

    if first_name.nil? || first_name.length == 0
      arr << [line, false, "Missing first name."]
      bad_count = bad_count + 1
    elsif last_name.nil? || last_name.length == 0
      arr << [line, false, "Missing last name."]
      bad_count = bad_count + 1          
    elsif email.nil? || email.length == 0 || !email.include?('@')
      arr << [line, false, "Email is invalid."]
      bad_count = bad_count + 1          
    elsif Caboose::User.where(:email => email).exists?
      arr << [line, false, "Email already exists."]
      bad_count = bad_count + 1                    
    else                  
      Caboose::User.create(
        :first_name => first_name,
        :last_name  => last_name,
        :email      => email,
        :username   => username,          
        :password   => Digest::SHA1.hexdigest(Caboose::salt + password)
      )
      good_count = good_count + 1
    end
  end
  
  resp.success = "#{good_count} user#{good_count == 1 ? '' : 's'} were added successfully."     
  if bad_count > 0
    resp.success << "<br />#{bad_count} user#{bad_count == 1 ? '' : 's'} were skipped."
    resp.success << "<br /><br />Please check the log below for more details."
    resp.log = arr
  end      
  render :json => resp
end

#import_formObject

GET /admin/users/import



115
116
117
# File 'app/controllers/caboose/users_controller.rb', line 115

def import_form
  return if !user_is_allowed('users', 'edit')      
end

#indexObject

GET /admin/users



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/caboose/users_controller.rb', line 57

def index
  return if !user_is_allowed('users', 'view')
  
  @gen = PageBarGenerator.new(params, {
		  'first_name_like' => '',
		  'last_name_like'	=> '',
		  'username_like'	  => '',
		  'email_like' 		  => '',
		},{
		  'model'          => 'Caboose::User',
	    'sort'			     => 'last_name, first_name',
		  'desc'			     => false,
		  'base_url'		   => '/admin/users',
		  'use_url_params' => false
	})
	@users = @gen.items
end

#my_accountObject

GET /my-account



16
17
18
19
20
# File 'app/controllers/caboose/users_controller.rb', line 16

def 
  return if !logged_in?
  @user = logged_in_user
  render :layout => 'caboose/modal'
end

#newObject

GET /admin/users/new



95
96
97
98
# File 'app/controllers/caboose/users_controller.rb', line 95

def new
  return if !user_is_allowed('users', 'add')
  @newuser = User.new
end

#optionsObject

GET /admin/users/options



290
291
292
293
294
295
# File 'app/controllers/caboose/users_controller.rb', line 290

def options
  return if !user_is_allowed('users', 'view')
  @users = User.where('id <> 2').reorder('last_name, first_name').all
  options = @users.collect { |u| { 'value' => u.id, 'text' => "#{u.first_name} #{u.last_name} (#{u.email})"}}
  render json: options
end

#random_string(length) ⇒ Object



119
120
121
122
# File 'app/controllers/caboose/users_controller.rb', line 119

def random_string(length)
  o = [('a'..'z'),('A'..'Z'),('0'..'9')].map { |i| i.to_a }.flatten
  return (0...length).map { o[rand(o.length)] }.join
end

#remove_from_roleObject

DELETE /admin/users/:id/roles/:role_id



283
284
285
286
287
# File 'app/controllers/caboose/users_controller.rb', line 283

def remove_from_role
  return if !user_is_allowed('users', 'edit')
  RoleMembership.where(:user_id => params[:id], :role_id => params[:role_id]).destroy_all        
  render :json => true
end

#updateObject

PUT /admin/users/1



212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'app/controllers/caboose/users_controller.rb', line 212

def update
  return if !user_is_allowed('users', 'edit')

  resp = StdClass.new     
  user = User.find(params[:id])

  save = true
  params.each do |name,value|
    case name
      when 'first_name'           then user.first_name          = value     
      when 'last_name'            then user.last_name           = value 
      when 'username'             then user.username            = value 
      when 'email'                then user.email               = value         
      when 'address'              then user.address             = value
      when 'address2'             then user.address2            = value
      when 'city'                 then user.city                = value
      when 'state'                then user.state               = value
      when 'zip'                  then user.zip                 = value
      when 'phone'                then user.phone               = value
      when 'fax'                  then user.fax                 = value
      when 'utc_offset'           then user.utc_offset          = value.to_f        
	  	when "password"			  
	  	  confirm = params[:password2]
	  		if (value != confirm)			
	  		  resp.error = "Passwords do not match.";
	  		  save = false
	  		elsif (value.length < 8)
	  		  resp.error = "Passwords must be at least 8 characters.";
	  		  save = false
	  		else
	  		  user.password = Digest::SHA1.hexdigest(Caboose::salt + value)
	  		end
	  	when "roles"
	  	  user.roles = [];
	  	  value.each { |rid| user.roles << Role.find(rid) } unless value.nil?
	  	  resp.attribute = { 'text' => user.roles.collect{ |r| r.name }.join(', ') }    		  
	  end
	end
	
	resp.success = save && user.save
	render json: resp
end

#update_my_accountObject

PUT /my-account



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/controllers/caboose/users_controller.rb', line 23

def   
  return if !logged_in?
  
  resp = StdClass.new     
  user = logged_in_user

  save = true
  params.each do |name,value|
    case name
	  	when "first_name", "last_name", "username", "email", "phone"
	  	  user[name.to_sym] = value
	  	when "password"			  
	  	  confirm = params[:confirm]
	  		if (value != confirm)			
	  		  resp.error = "Passwords do not match.";
	  		  save = false
	  		elsif (value.length < 8)
	  		  resp.error = "Passwords must be at least 8 characters.";
	  		  save = false
	  		else
	  		  user.password = Digest::SHA1.hexdigest(Caboose::salt + value)
	  		end    	  	    		  
	  end
	end
	
	resp.success = save && user.save
	render json: resp
end

#update_picObject

POST /admin/users/1/update-pic



256
257
258
259
# File 'app/controllers/caboose/users_controller.rb', line 256

def update_pic
  @edituser = User.find(params[:id])
  @new_value = "Testing"
end