2
votes

I am at my wits end with this issue, I have been trying to figure it out for about a week now so I hope someone can tell me what im doing wrong. I am writing an LDAP application using activeldap but I can not seem to get the LDAP modify method to work correctly.

I am locating the user with the _modify partial and then passing the params into the controller to load the instance variable for another form so I can view/edit the users profile.

zion_users/_modify.html.haml

= simple_form_for :modify_entry, :url => modify_entry_zion_users_path, :method => :post, :html => { :class => 'form-horizontal' } do |f|
  = f.error_notification

  .control-group
    .control-label
      = f.label :un, "Username"
    .controls
      = f.select :un, options_for_select(ZionUser.form_array.sort), include_blank: true

  .control-group
    .controls
      = f.submit 'Search', :class => 'btn btn-primary'
      = link_to "Cancel", zion_users_path, :class => "btn btn-danger", 'data-no-turbolink' => true

zion_users_controller.rb

  def modify_user
    values = params[:modify_entry]
    @modify_user = ZionUser.modify_user(values[:cn],values[:gidnumber],values[:shadowlastchange],values[:uid],values[:loginshell],values[:sn],values[:gecos],values[:homedirectory],values[:uidnumber])

    if @modify_user
      redirect_to :back, :flash => { :success => 'Zion user was successfully changed'}
    else
      redirect_to :back,  :flash => { :error => 'ERROR: Zion user was NOT created because the username already exists'}
    end
  end

  def modify_entry
    values = params[:modify_entry]
    @zion_users = ZionUser.find(values[:un])
  end

zion_users/_modify_entry.html.haml

= simple_form_for :modify_user, :url => url_for(:controller => 'zion_users', :action => 'modify_user'), :method => :post, :html => { :class => 'form-horizontal' } do |f|
  = f.error_notification

  %dl.dl-horizontal
    %dt
      dn
    %dd
      = @zion_users.dn

  .control-group
    .control-label
      = f.label :uidnumber, "uidnumber"
    .controls
      = f.input_field :uidnumber, :value => @zion_users.uidnumber, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :uid, "uid"
    .controls
      = f.input_field :uid, :value => @zion_users.uid, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :cn, "cn"
    .controls
      = f.input_field :cn, :value => @zion_users.cn, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :sn, "sn"
    .controls
      = f.input_field :sn, :value => @zion_users.sn, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :gidnumber, "gidnumber"
    .controls
      = f.input_field :gidnumber, :value => @zion_users.gidnumber, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :loginshell, "loginshell"
    .controls
      = f.input_field :loginshell, :value => @zion_users.loginshell, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :homedirectory, "homedirectory"
    .controls
      = f.input_field :homedirectory, :value => @zion_users.homedirectory, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :gecos, "gecos"
    .controls
      = f.input_field :gecos, :value => @zion_users.gecos, :label => false, :class => 'required'
  .control-group
    .control-label
      = f.label :shadowlastchange, "shadowlastchange"
    .controls
      = f.input_field :shadowlastchange, :value => @zion_users.shadowlastchange, :label => false, :class => 'required'

  .control-group
    .controls
      = f.submit 'Submit', :class => 'btn btn-primary'
      = link_to "Cancel", zion_users_path, :class => "btn btn-danger", 'data-no-turbolink' => true

routes.rb

  resources :zion_users do
    match 'delete', :on => :collection, :via => [:get, :post]
    match 'password', :on => :collection, :via => [:get, :post]
    match 'modify', :on => :collection, :via => [:get, :post]
    match 'modify_entry', :on => :collection, :via => [:get, :post]
    match 'modify_user', :on => :collection, :via => [:get, :post]
  end

Now here is where I get lost. I get the form loaded with the users profile as expected but when submitting this form I get this error and I don't understand what it is trying to tell me. The request parameter hash looks good and it is going to the correct action, the controller method ZionUser.modify_user is defined in the model and I have confirmed it is working correctly. Any help would be appreciated.

Started POST "/zion_users/modify_user" for 127.0.0.1 at 2015-08-11 11:31:48 -0600
Processing by ZionUsersController#modify_user as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"tPEW3wVD367iJCKsLeSAflWJ0Z6ROCzFhusW6FRhyVg8bx4nHGx6SG9QJUyGpNfauRSeMqy5SO4OyTEqR9ybQg==", "modify_user"=>{"uidnumber"=>"1237", "uid"=>"abby.brown", "cn"=>"abby.brown", "sn"=>"abby.brown", "gidnumber"=>"100", "loginshell"=>"/bin/bash", "homedirectory"=>"/opt/ltsp-users/abby.brown", "gecos"=>"ltsp", "shadowlastchange"=>"15096"}, "commit"=>"Submit"}
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 23ms (ActiveRecord: 1.0ms)

NoMethodError - undefined method `[]' for nil:NilClass:
  • I changed my controller method as suggested below
  def modify_user
    values = params[:modify_user]
    @modify_user = ZionUser.modify_user(values[:cn],values[:gidnumber],values[:shadowlastchange],values[:uid],values[:loginshell],values[:sn],values[:gecos],values[:homedirectory],values[:uidnumber])

    if @modify_user
      redirect_to :back, :flash => { :success => 'Zion user was successfully changed'}
    else
      redirect_to :back,  :flash => { :error => 'ERROR: Zion user was NOT created because the username already exists'}
    end
  end
Started POST "/zion_users/modify_user" for 127.0.0.1 at 2015-08-11 12:40:01 -0600
Processing by ZionUsersController#modify_user as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"3NCseVnbPVag0gwRkNc4CVMcMFwN48H48Mn3MGD8rmhUTqSBQPSYsC2mC/E7l2+tv4F/8DBipdN469Dyc0H8cg==", "modify_user"=>{"uidnumber"=>"1237", "uid"=>"abby.brown", "cn"=>"abby.brown", "sn"=>"abby.brown", "gidnumber"=>"100", "loginshell"=>"/bin/sh", "homedirectory"=>"/opt/ltsp-users/abby.brown", "gecos"=>"ltsp", "shadowlastchange"=>"15096"}, "commit"=>"Submit"}
  User Load (0.4ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
  LDAP: search_full (9.3ms) {:base=>"ou=people,dc=zion,dc=in", :scope=>:sub, :filter=>"(&(uid=abby.brown)(objectClass=top))", :attributes=>["*", "objectClass"], :limit=>1}
Redirected to http://localhost:3000/zion_users/modify_entry
Completed 302 Found in 23ms (ActiveRecord: 0.4ms)


Started GET "/zion_users/modify_entry" for 127.0.0.1 at 2015-08-11 12:40:01 -0600
Processing by ZionUsersController#modify_entry as HTML
  User Load (0.2ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = ?  ORDER BY "users"."id" ASC LIMIT 1  [["id", 1]]
Completed 500 Internal Server Error in 3ms (ActiveRecord: 0.2ms)

NoMethodError - undefined method `[]' for nil:NilClass:
  app/controllers/zion_users_controller.rb:54:in `modify_entry'
  actionpack (4.2.3) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
1
i guess you need to change values = params[:modify_entry] to values = params[:modify_user] this is what you get from paramsAthar
I tried this a few times and all it manages to do is direct to the wrong controller action. It goes too "/zion_users/modify_entry"lnxman
it should not.actually in your modify_user action you do this values = params[:modify_entry] but the params has modify_user in it so when you do values[:cn] it gives this undefined method []' for nil:NilClass:` bcoz values are already nil. i think you just need to change this values = params[:modify_entry] to values = params[:modify_user] and it should not go to other action, bcoz you are already in the actionAthar
you are absolutely correct I was troubleshooting the wrong thing this whole time, thank you for all the help. I redirected back to the index page and it is working perfectly. You Rock!!!lnxman
apparently I can not accept my own answer for 2 days, if you would not mind adding your answer so I can close this discussion out and give you the proper amount of reputation :) Thanks againlnxman

1 Answers

0
votes

so the first issue can be simply resolved by doing changing the fetching of params like this

values = params[:modify_user]

and then the second issue which we discussed comes upon redirection. on redirect it goes to Redirected to http://localhost:3000/zion_users/modify_entry

which expect params[:modify_entry] but as params dont have modify_entry in it, it crashes giving nil class error. if you redirect to index path or root path everything will work fine.