Class: Caboose::UsersController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

#add_ga_event, #admin_bulk_add, #admin_bulk_delete, #admin_bulk_update, #before_before_action, #hashify_query_string, #init_cart, #logged_in?, #logged_in_user, #login_user, #logout_user, #parse_url_params, #reject_param, #under_construction_or_forwarding_domain?, #user_is_allowed, #user_is_allowed_to, #validate_cookie, #validate_token, #var, #verify_logged_in

Instance Method Details

#admin_addObject

POST /admin/users



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/caboose/users_controller.rb', line 151

def admin_add
  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
  user.site_id = @site.id
  
  if user.email.length == 0
    resp.error = "Please enter a valid email address."
  elsif User.where(:site_id => @site.id, :email => user.email).exists?
    resp.error = "That email is already in the system for this site."
  else
    user.save
    resp.redirect = "/admin/users/#{user.id}"
  end
  
  render :json => resp
end

#admin_add_to_roleObject

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



241
242
243
244
245
246
247
# File 'app/controllers/caboose/users_controller.rb', line 241

def admin_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_deleteObject

DELETE /admin/users/:id



229
230
231
232
233
234
235
236
237
238
# File 'app/controllers/caboose/users_controller.rb', line 229

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

#admin_editObject

GET /admin/users/:id



63
64
65
66
67
68
# File 'app/controllers/caboose/users_controller.rb', line 63

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

#admin_edit_passwordObject

GET /admin/users/:id/edit-password



71
72
73
74
# File 'app/controllers/caboose/users_controller.rb', line 71

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

#admin_importObject

POST /admin/users/import



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/controllers/caboose/users_controller.rb', line 87

def admin_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),
        :site_id    => @site.id
      )
      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

#admin_import_formObject

GET /admin/users/import



77
78
79
# File 'app/controllers/caboose/users_controller.rb', line 77

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

#admin_indexObject

GET /admin/users



22
23
24
# File 'app/controllers/caboose/users_controller.rb', line 22

def admin_index
  return if !user_is_allowed('users', 'view')            
end

#admin_jsonObject

GET /admin/users/json



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

def admin_json
  return if !user_is_allowed('users', 'view')
  
  pager = PageBarGenerator.new(params, {
      'site_id'         => @site.id,
		  '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
	})    	    	      
	render :json => {
	  :pager => pager,
	  :models => pager.items.as_json(:include => :roles)    	  
	}
end

#admin_json_singleObject

GET /admin/users/:id/json



50
51
52
53
54
# File 'app/controllers/caboose/users_controller.rb', line 50

def admin_json_single
  return if !user_is_allowed('users', 'view')    
  u = User.find(params[:id])      
  render :json => u.as_json(:include => :roles)
end

#admin_newObject

GET /admin/users/new



57
58
59
60
# File 'app/controllers/caboose/users_controller.rb', line 57

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

#admin_optionsObject

GET /admin/users/options



257
258
259
260
261
262
# File 'app/controllers/caboose/users_controller.rb', line 257

def admin_options
  return if !user_is_allowed('users', 'view')
  @users = User.where(:site_id => @site.id).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

#admin_remove_from_roleObject

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



250
251
252
253
254
# File 'app/controllers/caboose/users_controller.rb', line 250

def admin_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

#admin_suObject

GET /admin/users/:id/su



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'app/controllers/caboose/users_controller.rb', line 265

def admin_su
  return if !user_is_allowed('users', 'sudo')
  user = User.find(params[:id])
                              
  # See if we're on the default domain               
  d = Caboose::Domain.where(:domain => request.host_with_port).first      
        
  if d.primary == true
    logout_user
    (user, false) # Login the new user      
    redirect_to "/"
  end
           
  # Set a random token for the user
  user.token = (0...20).map { ('a'..'z').to_a[rand(26)] }.join
  user.save
  redirect_to "http://#{d.site.primary_domain.domain}/admin/users/#{params[:id]}/su/#{user.token}"                    
end

#admin_su_tokenObject

GET /admin/users/:id/su/:token



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'app/controllers/caboose/users_controller.rb', line 285

def admin_su_token
  return if params[:token].nil?
  user = User.find(params[:id])
  
  token = params[:token]      
  if user.token == params[:token]
    if logged_in? || logged_in_user.id == User::LOGGED_OUT_USER_ID
      Caboose.log(logged_in_user.id)          
      redirect_to "/logout?return_url=/admin/users/#{params[:id]}/su/#{user.token}"
      return
    end
    
    user.token = nil
    user.save                                
    (user)
    redirect_to '/'
  else
    render :json => false     
  end                    
end

#admin_updateObject

PUT /admin/users/:id



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'app/controllers/caboose/users_controller.rb', line 176

def admin_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 'site_id'              then user.site_id             = value
      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 'locked'               then user.locked              = value
	  	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 'role_ids'             then user.toggle_roles(value[0], value[1])
	  	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

#admin_update_picObject

POST /admin/users/:id/update-pic



223
224
225
226
# File 'app/controllers/caboose/users_controller.rb', line 223

def admin_update_pic
  @edituser = User.find(params[:id])
  @new_value = "Testing"
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

#random_string(length) ⇒ Object



81
82
83
84
# File 'app/controllers/caboose/users_controller.rb', line 81

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