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



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

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



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

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



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

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



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

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



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

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

#admin_importObject



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
149
# File 'app/controllers/caboose/users_controller.rb', line 88

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



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

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

#admin_indexObject



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

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

#admin_jsonObject



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

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



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

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



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

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

#admin_optionsObject



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

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



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

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



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

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



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

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



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
221
# File 'app/controllers/caboose/users_controller.rb', line 177

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



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

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



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

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