Darryl L. Pierce
2008-May-07 21:03 UTC
[Ovirt-devel] [PATCH 1/4] Added to enable LDAP support via the ActiveLdap gem.
--- wui/src/app/helpers/ldap_connection.rb | 46 +++++++++++++ wui/src/app/models/account.rb | 24 +++++++ wui/src/vendor/plugins/active_ldap/README | 54 +++++++++++++++ .../active_ldap/generators/model_active_ldap/USAGE | 17 +++++ .../model_active_ldap_generator.rb | 70 ++++++++++++++++++++ .../model_active_ldap/templates/fixtures.yml | 11 +++ .../templates/model_active_ldap.rb | 3 + .../model_active_ldap/templates/unit_test.rb | 10 +++ .../scaffold_active_ldap_generator.rb | 7 ++ .../scaffold_al/scaffold_al_generator.rb | 20 ++++++ wui/src/vendor/plugins/active_ldap/init.rb | 64 ++++++++++++++++++ 11 files changed, 326 insertions(+), 0 deletions(-) create mode 100644 wui/src/app/helpers/ldap_connection.rb create mode 100644 wui/src/app/models/account.rb create mode 100644 wui/src/vendor/plugins/active_ldap/README create mode 100644 wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/USAGE create mode 100644 wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/model_active_ldap_generator.rb create mode 100644 wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/fixtures.yml create mode 100644 wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/model_active_ldap.rb create mode 100644 wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/unit_test.rb create mode 100644 wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/scaffold_active_ldap_generator.rb create mode 100644 wui/src/vendor/plugins/active_ldap/generators/scaffold_al/scaffold_al_generator.rb create mode 100644 wui/src/vendor/plugins/active_ldap/init.rb diff --git a/wui/src/app/helpers/ldap_connection.rb b/wui/src/app/helpers/ldap_connection.rb new file mode 100644 index 0000000..53256fa --- /dev/null +++ b/wui/src/app/helpers/ldap_connection.rb @@ -0,0 +1,46 @@ +# +# Copyright (C) 2008 Red Hat, Inc. +# Written by Darryl L. Pierce <dpierce at redhat.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. A copy of the GNU General Public License is +# also available at http://www.gnu.org/copyleft/gpl.html. + +# +LDAPConnection+ handles establishing, returning and closing +# connections with an LDAP server. +# +class LDAPConnection + @@hostname = nil + @@port = 389 + + # Connects the LDAP server. + def LDAPConnection.connect( + base, + hostname = LDAPConnection.hostname, + port = LDAPConnection.port + ) + ActiveLdap::Base.establish_connection(:host => hostname + end + + # Returns whether a connection already exists to the LDAP server. + def LDAPConnection.connected? + return ActiveLdap::Base.connected? + end + + # Disconnects from the LDAP server. + def LDAPConnection.disconnected + ActiveLdap::Base.remove_connection if LDAPConnection.connected? + end + +end diff --git a/wui/src/app/models/account.rb b/wui/src/app/models/account.rb new file mode 100644 index 0000000..94c3bb6 --- /dev/null +++ b/wui/src/app/models/account.rb @@ -0,0 +1,24 @@ +# +# Copyright (C) 2008 Red Hat, Inc. +# Written by Darryl L. Pierce <dpierce at redhat.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. A copy of the GNU General Public License is +# also available at http://www.gnu.org/copyleft/gpl.html. + +# +Account+ represents a single user's account from the LDAP server. +# +class Account < ActiveLdap::Base + ldap_mapping :dn_attribute => 'uid', :classes => ['person', 'posixAccount'] +end diff --git a/wui/src/vendor/plugins/active_ldap/README b/wui/src/vendor/plugins/active_ldap/README new file mode 100644 index 0000000..8e40086 --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/README @@ -0,0 +1,54 @@ += ActiveLdap plugin for Ruby on Rails + +== Setup + +You need to write RAILS_ROOT/config/ldap.yml like the following: + + development: + host: 127.0.0.1 + port: 389 + base: dc=devel,dc=local,dc=net + bind_dn: cn=admin,dc=local,dc=net + password: secret + + test: + host: 127.0.0.1 + port: 389 + base: dc=test,dc=local,dc=net + bind_dn: cn=admin,dc=local,dc=net + password: secret + + production: + host: 127.0.0.1 + port: 389 + base: dc=production,dc=local,dc=net + bind_dn: cn=admin,dc=local,dc=net + password: secret + +== Model + +Here is some examples. + +app/model/member.rb: + class Member < ActiveLdap::Base + ldap_mapping :dn_attribute => 'uid', + :classes => ['person', 'posixAccount'] + belongs_to :primary_group, :class => "Group", + :foreign_key => "gidNumber", :primary_key => "gidNumber" + belongs_to :groups, :many => 'memberUid' + end + +app/model/group.rb: + class Group < ActiveLdap::Base + ldap_mapping :dn_attribute => "cn", :classes => ['posixGroup'] + has_many :members, :wrap => "memberUid" + has_many :primary_members, + :foreign_key => 'gidNumber', + :primary_key => 'gidNumber' + end + +app/model/ou.rb: + class Ou < ActiveLdap::Base + ldap_mapping :prefix => "", + :classes => ["top", "organizationalUnit"] + end diff --git a/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/USAGE b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/USAGE new file mode 100644 index 0000000..a86c5dd --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/USAGE @@ -0,0 +1,17 @@ +Description: + The model_activeldap generator creates stubs for a new model. + + The generator takes a model name as its argument. The model name may be given in CamelCase or under_score and + should not be suffixed with 'Model'. + + The generator creates a model class in app/models, a test suite in test/unit, and test fixtures in + test/fixtures/singular_name.yml. It will not create a migration. + +Examples: + ./script/generate model_activeldap user + + This will create a User model: + Model: app/models/user.rb + Test: test/unit/user_test.rb + Fixtures: test/fixtures/users.yml + diff --git a/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/model_active_ldap_generator.rb b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/model_active_ldap_generator.rb new file mode 100644 index 0000000..f8435a3 --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/model_active_ldap_generator.rb @@ -0,0 +1,70 @@ +class ModelActiveLdapGenerator < Rails::Generator::NamedBase + include ActiveLdap::GetTextSupport + + default_options :dn_attribute => "cn", :classes => nil + + def manifest + record do |m| + # Check for class naming collisions. + m.class_collisions class_path, class_name, "#{class_name}Test" + + # Model, test, and fixture directories. + m.directory File.join('app/models', class_path) + m.directory File.join('test/unit', class_path) + m.directory File.join('test/fixtures', class_path) + + # Model class, unit test, and fixtures. + m.template('model_active_ldap.rb', + File.join('app/models', class_path, "#{file_name}.rb"), + :assigns => {:ldap_mapping => ldap_mapping}) + m.template('unit_test.rb', + File.join('test/unit', class_path, "#{file_name}_test.rb")) + m.template('fixtures.yml', + File.join('test/fixtures', class_path, "#{table_name}.yml")) + end + end + + private + def add_options!(opt) + opt.separator '' + opt.separator 'Options:' + opt.on("--dn-attribute=ATTRIBUTE", + _("Use ATTRIBUTE as default DN attribute for " \ + "instances of this model"), + _("(default: %s)") % options[:dn_attribute]) do |attribute| + options[:dn_attribute] = attribute + end + + opt.on("--prefix=PREFIX", + _("Use PREFIX as prefix for this model"), + _("(default: %s)") % default_prefix) do |prefix| + options[:prefix] = prefix + end + + opt.on("--classes=CLASS,CLASS,...", + Array, + "Use CLASSES as required objectClass for instances of this model", + "(default: %s)" % options[:classes]) do |classes| + options[:classes] = classes + end + end + + def prefix + options[:prefix] || default_prefix + end + + def default_prefix + "ou=#{Inflector.pluralize(Inflector.demodulize(name))}" + end + + def ldap_mapping(indent=' ') + mapping = "ldap_mapping " + mapping_options = [":dn_attribute => #{options[:dn_attribute].dump}"] + mapping_options << ":prefix => #{prefix.dump}" + if options[:classes] + mapping_options << ":classes => #{options[:classes].inspect}" + end + mapping_options = mapping_options.join(",\n#{indent}#{' ' * mapping.size}") + "#{indent}#{mapping}#{mapping_options}" + end +end diff --git a/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/fixtures.yml b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/fixtures.yml new file mode 100644 index 0000000..9f5ae29 --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/fixtures.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 +<% for attribute in attributes -%> + <%= attribute.name %>: <%= attribute.default %> +<% end -%> +two: + id: 2 +<% for attribute in attributes -%> + <%= attribute.name %>: <%= attribute.default %> +<% end -%> \ No newline at end of file diff --git a/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/model_active_ldap.rb b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/model_active_ldap.rb new file mode 100644 index 0000000..cdfa66b --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/model_active_ldap.rb @@ -0,0 +1,3 @@ +class <%= class_name %> < ActiveLdap::Base +<%= ldap_mapping %> +end diff --git a/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/unit_test.rb b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/unit_test.rb new file mode 100644 index 0000000..b464de4 --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/model_active_ldap/templates/unit_test.rb @@ -0,0 +1,10 @@ +require File.dirname(__FILE__) + '<%= '/..' * class_nesting_depth %>/../test_helper' + +class <%= class_name %>Test < Test::Unit::TestCase + fixtures :<%= table_name %> + + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/scaffold_active_ldap_generator.rb b/wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/scaffold_active_ldap_generator.rb new file mode 100644 index 0000000..35ad937 --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/scaffold_active_ldap_generator.rb @@ -0,0 +1,7 @@ +class ScaffoldActiveLdapGenerator < Rails::Generator::Base + def manifest + record do |m| + m.template("ldap.yml", File.join("config", "ldap.yml")) + end + end +end diff --git a/wui/src/vendor/plugins/active_ldap/generators/scaffold_al/scaffold_al_generator.rb b/wui/src/vendor/plugins/active_ldap/generators/scaffold_al/scaffold_al_generator.rb new file mode 100644 index 0000000..3fa365c --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/scaffold_al/scaffold_al_generator.rb @@ -0,0 +1,20 @@ +class ScaffoldAlGenerator < Rails::Generator::Base + include ActiveLdap::GetTextSupport + + def initialize(*args) + duped_args = args.collect {|arg| arg.dup} + super + logger.warning(_("scaffold_al is deprecated. " \ + "Use scaffold_active_ldap instead.")) + generator_class = self.class.lookup("scaffold_active_ldap").klass + @generator = generator_class.new(duped_args) + end + + def manifest + @generator.manifest + end + + def source_path(*args) + @generator.source_path(*args) + end +end diff --git a/wui/src/vendor/plugins/active_ldap/init.rb b/wui/src/vendor/plugins/active_ldap/init.rb new file mode 100644 index 0000000..35e19d4 --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/init.rb @@ -0,0 +1,64 @@ +require_library_or_gem 'active_ldap' +ActiveLdap::Base.logger ||= RAILS_DEFAULT_LOGGER + +required_version = ["0", "9", "1"] +if (ActiveLdap::VERSION.split(".") <=> required_version) < 0 + ActiveLdap::Base.class_eval do + format = _("You need ActiveLdap %s or later") + logger.error(format % required_version.join(".")) + end +end + +ldap_configuration_file = File.join(RAILS_ROOT, 'config', 'ldap.yml') +if File.exist?(ldap_configuration_file) + configurations = YAML.load(ERB.new(IO.read(ldap_configuration_file)).result) + ActiveLdap::Base.configurations = configurations + ActiveLdap::Base.establish_connection +else + ActiveLdap::Base.class_eval do + format = _("You should run 'script/generator scaffold_active_ldap' to make %s.") + logger.error(format % ldap_configuration_file) + end +end + +class ::ActionView::Base + include ActiveLdap::Helper +end + +module ::ActionController + module LdapBenchmarking + def self.included(base) + base.class_eval do + alias_method_chain :render, :active_ldap_benchmark + alias_method_chain :rendering_runtime, :active_ldap + end + end + + protected + def render_with_active_ldap_benchmark(*args, &block) + if logger + @ldap_runtime_before_render = ActiveLdap::Base.reset_runtime + result = render_without_active_ldap_benchmark(*args, &block) + @ldap_runtime_after_render = ActiveLdap::Base.reset_runtime + @rendering_runtime -= @ldap_runtime_after_render + result + else + render_without_active_ldap_benchmark(*args, &block) + end + end + + private + def rendering_runtime_with_active_ldap(runtime) + result = rendering_runtime_without_active_ldap(runtime) + ldap_runtime = ActiveLdap::Base.reset_runtime + ldap_runtime += @ldap_runtime_before_render || 0 + ldap_runtime += @ldap_runtime_after_render || 0 + ldap_percentage = ldap_runtime * 100 / runtime + result + (" | LDAP: %.5f (%d%%)" % [ldap_runtime, ldap_percentage]) + end + end + + class Base + include LdapBenchmarking + end +end -- 1.5.4.1
Darryl L. Pierce
2008-May-07 21:03 UTC
[Ovirt-devel] [PATCH 2/4] A basically working LDAP base is started.
--- wui/src/config/ldap.yml | 4 +++ .../scaffold_active_ldap/templates/ldap.yml | 21 ++++++++++++++++++++ wui/src/vendor/plugins/active_ldap/init.rb | 2 +- 3 files changed, 26 insertions(+), 1 deletions(-) create mode 100644 wui/src/config/ldap.yml create mode 100644 wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/templates/ldap.yml diff --git a/wui/src/config/ldap.yml b/wui/src/config/ldap.yml new file mode 100644 index 0000000..c8b167e --- /dev/null +++ b/wui/src/config/ldap.yml @@ -0,0 +1,4 @@ +development: + hostname: ldap.rdu.redhat.com + port: 389 + base: dc=redhat,dc=com diff --git a/wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/templates/ldap.yml b/wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/templates/ldap.yml new file mode 100644 index 0000000..720ec39 --- /dev/null +++ b/wui/src/vendor/plugins/active_ldap/generators/scaffold_active_ldap/templates/ldap.yml @@ -0,0 +1,21 @@ +development: + host: 127.0.0.1 + port: 389 + base: dc=devel,dc=local,dc=net + bind_dn: cn=admin,dc=local,dc=net + password: secret + +test: + host: 127.0.0.1 + port: 389 + base: dc=test,dc=local,dc=net + bind_dn: cn=admin,dc=local,dc=net + password: secret + +production: + host: 127.0.0.1 + port: 389 + method: :tls + base: dc=production,dc=local,dc=net + bind_dn: cn=admin,dc=local,dc=net + password: secret diff --git a/wui/src/vendor/plugins/active_ldap/init.rb b/wui/src/vendor/plugins/active_ldap/init.rb index 35e19d4..fa1484a 100644 --- a/wui/src/vendor/plugins/active_ldap/init.rb +++ b/wui/src/vendor/plugins/active_ldap/init.rb @@ -1,7 +1,7 @@ require_library_or_gem 'active_ldap' ActiveLdap::Base.logger ||= RAILS_DEFAULT_LOGGER -required_version = ["0", "9", "1"] +required_version = ["0", "10", "0"] if (ActiveLdap::VERSION.split(".") <=> required_version) < 0 ActiveLdap::Base.class_eval do format = _("You need ActiveLdap %s or later") -- 1.5.4.1