从rails访问活动目录时,观察中的Encoding :: CompatibilityError

时间:2016-05-02 15:34:17

标签: ruby-on-rails ldap

我通过ldap / net建立了与Active Directory的连接。我试图拉取AD属性和值。

如果我使用以下代码(使用values.inspect),查询将起作用。

<% @temp_search.each do |user| %>
  TS = <%= user.sn %> <br>
    <% user.each do |attribute, values| %>
        <%= attribute %> <br>
        <% values.each do |value| %>
            <%= value.inspect %><br>
        <% end %>
        <hr style="border-color: red">
    <% end %>
<% end %>

我的控制器执行ldap查询的部分是s

   filter = Net::LDAP::Filter.eq( "sn", "mendla" )
   treebase = "dc=ccttapes1,dc=com"
   @temp_search = ldap.search( :base => treebase, :filter => filter )
   p ldap.get_operation_result

我似乎无法获得一个特定属性的值 - 例如,givenname。

我看到的结果如

cn 
"Chris G. Mendla"
sn 
"Mendla"
description 
"Test account 1 for rails apps - DO NOT CHANGE PW"
givenname 
"Chris"
initials 
"G"
distinguishedname 
"CN=Chris G. Mendla,OU=Users CCT,DC=CCTTAPES1,DC=com"

但是,如果我将<%= value.inspect %><br>更改为<%= value.givenname %><br>,则会收到错误

NoMethodError in Observations#index
Showing C:/Users/cmendla/RubymineProjects/employee_observations/app/views/observations/index.html.erb where line #61 raised:

undefined method `givenname' for "CN=Christopher Mendla,OU=Users CCT,DC=CCTTAPES1,DC=com":Net::BER::BerIdentifiedString
Rails.root: C:/Users/cmendla/RubymineProjects/employee_observations

Application Trace | Framework Trace | Full Trace
app/views/observations/index.html.erb:61:in `block (3 levels) in _app_views_observations_index_html_erb__474218211_78240600'
app/views/observations/index.html.erb:60:in `each'
app/views/observations/index.html.erb:60:in `block (2 levels) in _app_views_observations_index_html_erb__474218211_78240600'
app/views/observations/index.html.erb:58:in `block in _app_views_observations_index_html_erb__474218211_78240600'
app/views/observations/index.html.erb:56:in `each'
app/views/observations/index.html.erb:56:in `_app_views_observations_index_html_erb__474218211_78240600'

如果我尝试``&lt;%= givenname.value%&gt;`我得

NameError in Observations#index
Showing C:/Users/cmendla/RubymineProjects/employee_observations/app/views/observations/index.html.erb where line #61 raised:

undefined local variable or method `givenname' for #<#<Class:0x9cd0230>:0x9fadaf0>
Rails.root: C:/Users/cmendla/RubymineProjects/employee_observations

Application Trace | Framework Trace | Full Trace
app/views/observations/index.html.erb:61:in `block (3 levels) in _app_views_observations_index_html_erb__474218211_83715540'
app/views/observations/index.html.erb:60:in `each'
app/views/observations/index.html.erb:60:in `block (2 levels) in _app_views_observations_index_html_erb__474218211_83715540'
app/views/observations/index.html.erb:58:in `block in _app_views_observations_index_html_erb__474218211_83715540'
app/views/observations/index.html.erb:56:in `each'
app/views/observations/index.html.erb:56:in `_app_views_observations_index_html_erb__474218211_83715540'
Request

我的目标是能够通过名字和姓氏搜索AD记录,然后为诸如mail或memberof之类的属性提取值。

(我正在使用ldap-net

1 个答案:

答案 0 :(得分:2)

在您的示例中,@temp_search应该是符合搜索条件的用户数组。这些用户中的每一个都是Net::LDAP::Entry对象。在那些对象上,您可以调用与users属性对应的方法。

如果只返回一个用户,则Net::LDAP::Entry对象仍将在数组中。在这种情况下,你可以调用类似的东西:

@temp_search.first.cn

您也可以致电:

@temp_search.first.attribute_names查看该对象的所有可用属性。

例如,您可以执行以下操作:

<% @temp_search.each do |user| %>
   #call user attributes
   user.cn
   user.memberof
   #etc, other attributes
<% end %>