Project

General

Profile

Feature #1113 » redmine_ldap_groups_import_0.9.0.patch

Natalia Lebedeva, 2010-01-26 11:03

View differences:

./app/models/auth_source_ldap.rb 2010-01-26 12:26:18.000000000 +0300
class AuthSourceLdap < AuthSource
validates_presence_of :host, :port, :attr_login
validates_length_of :name, :host, :account_password, :maximum => 60, :allow_nil => true
validates_length_of :account, :base_dn, :maximum => 255, :allow_nil => true
validates_length_of :account, :base_dn, :group_base_dn, :maximum => 255, :allow_nil => true
validates_length_of :attr_login, :attr_firstname, :attr_lastname, :attr_mail, :maximum => 30, :allow_nil => true
validates_numericality_of :port, :only_integer => true
......
return nil unless ldap_con.bind
# return user's attributes
logger.debug "Authentication successful for '#{login}'" if logger && logger.debug?
if self.group_base_dn != ""
# Search for ldap groups that the user is in
ldap_con.search( :base => self.group_base_dn,
:filter => Net::LDAP::Filter.eq("uniqueMember", dn),
:attributes => [ "cn" ]) do |entry|
# look to see if the group exists
# then add it if it doesn't
@matchinggroups = Group.find(:all, :conditions => "lastname = '#{entry.cn}'")
if @matchinggroups.length == 0
@group = Group.new(:lastname => "#{entry.cn}")
unless @group.save
logger.debug "group '#{entry.cn}' save didn't work" if logger && logger.debug?
end
end
# look to see if the user is a member of the group
# and add them if they are not
@currentgroup = Group.find(:first, :conditions => "lastname = '#{entry.cn}'")
@userdetails = User.find(:first, :conditions => "login = '#{login}'")
if !@currentgroup.user_ids.include?(@userdetails.id)
# add the user to the group
@currentgroup.user_ids = @currentgroup.user_ids + [@userdetails.id]
unless @currentgroup.save
logger.debug "user NOT added to group" if logger && logger.debug?
end
end
end
end
attrs
rescue Net::LDAP::LdapError => text
raise "LdapError: " + text
./app/views/auth_sources/_form.rhtml 2010-01-26 11:32:27.000000000 +0300
<p><label for="auth_source_base_dn"><%=l(:field_base_dn)%> <span class="required">*</span></label>
<%= text_field 'auth_source', 'base_dn', :size => 60 %></p>
<p><label for="auth_source_group_base_dn"><%=l(:field_group_base_dn)%></label>
<%= text_field 'auth_source', 'group_base_dn', :size => 60 %></p>
<p><label for="auth_source_onthefly_register"><%=l(:field_onthefly)%></label>
<%= check_box 'auth_source', 'onthefly_register' %></p>
</div>
./config/locales/en.yml 2010-01-26 12:28:29.000000000 +0300
field_attr_firstname: Firstname attribute
field_attr_lastname: Lastname attribute
field_attr_mail: Email attribute
field_group_base_dn: Base DN for groups
field_onthefly: On-the-fly user creation
field_start_date: Start
field_done_ratio: % Done
./db/migrate/20100125132612_add_group_base_dn_to_auth_sources.rb 2010-01-25 16:29:43.000000000 +0300
class AddGroupBaseDnToAuthSources < ActiveRecord::Migration
def self.up
add_column :auth_sources, :group_base_dn, :string, :limit => 255
end
def self.down
remove_column :auth_sources, :group_base_dn
end
end
(1-1/2)