Feature #1838 » bulk_import_ldap.patch
| app/views/auth_sources/list.rhtml (working copy) | ||
|---|---|---|
|
<td align="center"><%= source.auth_method_name %></td>
|
||
|
<td align="center"><%= source.host %></td>
|
||
|
<td align="center"><%= link_to l(:button_test), :action => 'test_connection', :id => source %></td>
|
||
|
<td align="center"><%= link_to l(:button_import_ldap), :action => 'import', :id => source %></td>
|
||
|
<td align="center"><%= button_to l(:button_delete), { :action => 'destroy', :id => source }, :confirm => l(:text_are_you_sure), :class => "button-small" %></td>
|
||
|
|
||
|
</tr>
|
||
|
<% end %>
|
||
|
</tbody>
|
||
| app/controllers/auth_sources_controller.rb (working copy) | ||
|---|---|---|
|
end
|
||
|
end
|
||
|
|
||
|
def import
|
||
|
@auth_source = AuthSource.find(params[:id])
|
||
|
begin
|
||
|
@auth_source.import
|
||
|
flash[:notice] = l(:notice_successful_import)
|
||
|
rescue => err
|
||
|
flash[:error] = "Unable to import (#{err})"
|
||
|
end
|
||
|
redirect_to :action => 'list'
|
||
|
end
|
||
|
|
||
|
def test_connection
|
||
|
@auth_method = AuthSource.find(params[:id])
|
||
|
@auth_source = AuthSource.find(params[:id])
|
||
|
begin
|
||
|
@auth_method.test_connection
|
||
|
@auth_source.test_connection
|
||
|
flash[:notice] = l(:notice_successful_connection)
|
||
|
rescue => text
|
||
|
flash[:error] = "Unable to connect (#{text})"
|
||
| lang/en.yml (working copy) | ||
|---|---|---|
|
button_annotate: Annotate
|
||
|
button_update: Update
|
||
|
button_configure: Configure
|
||
|
button_import_ldap: Bulk import
|
||
|
|
||
|
status_active: active
|
||
|
status_registered: registered
|
||
| db/migrate/096_add_auth_sources_user_filter.rb (revision 0) | ||
|---|---|---|
|
class AddAuthSourcesUserFilter < ActiveRecord::Migration
|
||
|
def self.up
|
||
|
add_column :auth_sources, :user_filter, :string, :default => '(objectClass=*)', :null => true
|
||
|
end
|
||
|
|
||
|
def self.down
|
||
|
remove_column :auth_sources, :tls
|
||
|
end
|
||
|
end
|
||
| app/models/auth_source.rb (working copy) | ||
|---|---|---|
|
"Abstract"
|
||
|
end
|
||
|
|
||
|
def import
|
||
|
# bulk create AR User classes from auth_source so
|
||
|
# users from external auth sources don't need to login
|
||
|
# just to be visible in the UI
|
||
|
end
|
||
|
|
||
|
# Try to authenticate a user not yet registered against available sources
|
||
|
def self.authenticate(login, password)
|
||
|
AuthSource.find(:all, :conditions => ["onthefly_register=?", true]).each do |source|
|
||
| app/models/auth_source_ldap.rb (working copy) | ||
|---|---|---|
|
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, :user_filter, :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
|
||
|
|
||
| ... | ... | |
|
raise "LdapError: " + text
|
||
|
end
|
||
|
|
||
|
def import
|
||
|
ldap_con = initialize_ldap_con(self.account, self.account_password)
|
||
|
if self.user_filter
|
||
|
search_filter = self.user_filter
|
||
|
else
|
||
|
search_filter = Net::LDAP::Filter.eq("objectClass", "*")
|
||
|
end
|
||
|
found = created = 0
|
||
|
skipped = []
|
||
|
ldap_con.search(
|
||
|
:base => self.base_dn,
|
||
|
:filter => search_filter,
|
||
|
:attributes => ['dn', self.attr_firstname, self.attr_lastname, self.attr_mail, self.attr_login]
|
||
|
) do | entry |
|
||
|
logger.debug("Found entry with DN: #{entry.dn}") if logger
|
||
|
found += 1
|
||
|
attrs = [:firstname => (AuthSourceLdap.get_attr(entry, self.attr_firstname) != nil ? \
|
||
|
AuthSourceLdap.get_attr(entry, self.attr_firstname) : "Unknown"),
|
||
|
:lastname => AuthSourceLdap.get_attr(entry, self.attr_lastname),
|
||
|
:mail => AuthSourceLdap.get_attr(entry, self.attr_mail),
|
||
|
:auth_source_id => self.id ]
|
||
|
begin
|
||
|
logger.debug("Trying to create user with attrs: %s" % attrs.to_s) if logger
|
||
|
login = AuthSourceLdap.get_attr(entry, self.attr_login)
|
||
|
if User.find(:first, :conditions => ["login=?", login])
|
||
|
logger.debug("User #{login} already there, skipping...") if logger
|
||
|
next
|
||
|
end
|
||
|
u = User.create(*attrs)
|
||
|
u.login = login
|
||
|
u.language = Setting.default_language
|
||
|
if u.save
|
||
|
created += 1
|
||
|
else
|
||
|
skipped.push(login)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
logger.info("Found #{found} users, imported #{created}.")
|
||
|
logger.info("Skipped users: #{skipped.join(" ")}")
|
||
|
end
|
||
|
|
||
|
# test the connection to the LDAP
|
||
|
def test_connection
|
||
|
ldap_con = initialize_ldap_con(self.account, self.account_password)
|
||