You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@directory.apache.org by Peter Neubauer <ne...@gmail.com> on 2007/05/23 20:30:57 UTC

Fwd: How to make a WAR application in JRuby against LDAP for user registration

Hi all,
Andreas made up a small JRuby app to register users, think this is
pretty amazing!

Just for your information.

/peter

---------- Forwarded message ----------
From: Andreas Ronge <an...@jayway.se>




1. install jruby, http://dist.codehaus.org/jruby/
2. Check/Update environment vars:
   set JAVA_HOME (or just have "java" executable in PATH)
   add $JRUBY_HOME/bin to PATH
3. install rails: gem install rails
4. install ldap ruby library: gem install ruby-net-ldap
5. create a rails project: rails demo
6. cd demo
7. create a controller: jruby script/generate controller UserAdmin
8. edit the app/controllers/upser_admin_controller:
require 'rubygems'
require 'net/ldap'

class UserAdminController < ApplicationController
        def registration
        end

        def add_user
                @username = params[:username]
                pwd = params[:password]
                ldap = Net::LDAP.new
                ldap.host = 'localhost'
                ldap.port = 10389
                ldap.auth "uid=admin,ou=system", "secret"

                dn = "uid=#@username,ou=users,ou=system"
                attributes = {
                        :cn => @username,
                        :objectclass => ["top", "person",
"inetorgperson", "organizationalPerson"],
                        :sn => @username
                }

                success = ldap.add(:dn => dn, :attributes => attributes)
                puts "Success #{success}"
        end


end

9. create rhtml files:
app/views/user_admin/add_user.rhtml
<html>
        <head>
                <title>Signed Up</title>
        </head>

        <body>
                <h1>You have now registered with username <%=@username%></h1>
        </body>
</html>

app/views/user_admin/registration.rhtml:
<html>
        <head>
                <title>Registration</title>
        </head>

        <body>
                <h1>Registration</h1>

                <h3>Please enter user name and password</h3>
                <% form_tag(:action => :add_user) do %>
                  <label>Username:</label>
                  <%= text_field_tag :username %>
                  <br/>
                  <label>Password:</label>
                  <%= password_field_tag :password %>
                  <p>
                  <%=submit_tag 'Add'%>
                <% end %>
        </body>
</html>

10. test it in WEBrick: jruby script/server
You can now add a user in the ldap by filling in the html form at
http://localhost:3000/user_admin/registration


11. configure which gems should be included in the WAR file
you need to tell which gems should be packed in the WAR file by adding
file config/war.rb:
add_gem 'ruby-net-ldap'

12. create WAR
rake war:standalone:create

13. run it in jetty
rake war:standalone:run