Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Arcath/Adauth
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Sep 5, 2013
2 parents 384f643 + a257020 commit 007e61f
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
adauth (2.0.1)
adauth (2.0.2)
expects (~> 0.0.2)
net-ldap

Expand Down
1 change: 1 addition & 0 deletions lib/adauth.rb
Expand Up @@ -10,6 +10,7 @@
require 'adauth/authenticate'
require 'adauth/config'
require 'adauth/connection'
require 'adauth/search_results'
# AdObjects
require 'adauth/ad_objects/computer'
require 'adauth/ad_objects/folder'
Expand Down
35 changes: 25 additions & 10 deletions lib/adauth/ad_object.rb
Expand Up @@ -16,11 +16,33 @@ def self.add_field(object, adauth_method, ldap_method)
# Provides all the common functions for Active Directory.
class AdObject
include Expects


def self.method_missing(method, *args)
return super unless method =~ /^find_by_/
method_field = method.to_s.split("_").last
field = self::Fields[method_field.to_sym]
return super unless field
self.where(field, args.first)
end

def method_missing(method, *args)
field = self.class::Fields[method]
return handle_field(field) if field
return super
end

def self.reverse_field(search)
hash = {}
self::Fields.each do |k, v|
hash[v] = k
end
return hash[search]
end

# Returns all objects which have the ObjectClass of the inherited class
def self.all
Adauth.logger.info(self.class.inspect) { "Searching for all objects matching filter \"#{self::ObjectFilter}\"" }
self.filter(self::ObjectFilter)
Adauth::SearchResults.new(self.filter(self::ObjectFilter))
end

# Returns all the objects which match the supplied query
Expand All @@ -29,7 +51,7 @@ def self.all
def self.where(field, value)
search_filter = Net::LDAP::Filter.eq(field, value)
Adauth.logger.info(self.class.inspect) { "Searching for all \"#{self::ObjectFilter}\" where #{field} = #{value}" }
filter(add_object_filter(search_filter))
Adauth::SearchResults.new(filter(add_object_filter(search_filter)))
end

# Returns all LDAP objects that match the given filter
Expand Down Expand Up @@ -65,13 +87,6 @@ def ldap_object
@ldap_object
end

# Over ride method missing to see if the object has a field by that name
def method_missing(method, *args)
field = self.class::Fields[method]
return handle_field(field) if field
return super
end

# Handle the output for the given field
def handle_field(field)
case field
Expand Down
18 changes: 18 additions & 0 deletions lib/adauth/search_results.rb
@@ -0,0 +1,18 @@
module Adauth
class SearchResults < Array
def limit(x)
return self[0..(x-1)]
end

def order(field, direction = :asc)
case direction
when :asc
return sort! { |x, y| x.send(field) <=> y.send(field) }
when :desc
return order(field, :asc).reverse!
else
raise "Invalid Order Provided, please use :asc or :desc"
end
end
end
end
10 changes: 8 additions & 2 deletions spec/adauth_ad_object_user_spec.rb
Expand Up @@ -46,9 +46,9 @@
it "should allow for additional methods" do
default_config
Adauth.add_field(Adauth::AdObjects::User, :description, :description)
administrator.description.should be_a String
user.description.should be_a String
Adauth.add_field(Adauth::AdObjects::User, :objectguid, :objectguid)
administrator.objectguid.should be_a String
user.objectguid.should be_a String
end

it "should allow you to reset the password" do
Expand Down Expand Up @@ -86,4 +86,10 @@
rq_user.member_of?("Adauth Test Group").should be_false
new_group.delete
end

it "should have find_by methods (and not break method_missing)" do
default_config
lambda { Adauth::AdObjects::User.fooooooooo }.should raise_exception
Adauth::AdObjects::User.find_by_login(test_data("domain", "breakable_user")).should be_a Adauth::SearchResults
end
end
8 changes: 6 additions & 2 deletions spec/adauth_rails_model_bridge_spec.rb
Expand Up @@ -21,17 +21,21 @@ def save
end

describe Adauth::Rails::ModelBridge do
let(:user) do
Adauth::AdObjects::User.where('sAMAccountName', test_data("domain", "breakable_user")).first
end

it "should extend", :no_ad => true do
TestUserModel.should respond_to :create_from_adauth
end

it "should create the model" do
default_config
TestUserModel.create_from_adauth(administrator)
TestUserModel.create_from_adauth(user)
end

it "should return and create the model" do
default_config
TestUserModel.return_and_create_from_adauth(administrator)
TestUserModel.return_and_create_from_adauth(user)
end
end
41 changes: 41 additions & 0 deletions spec/adauth_search_results_spec.rb
@@ -0,0 +1,41 @@
require 'spec_helper'

describe Adauth::SearchResults, :no_ad => true do
let(:test_array) do
[OpenStruct.new({name: "foo"}), OpenStruct.new({name: "bar"}), OpenStruct.new({name: "widget"})]
end

let(:sorted_array) do
[OpenStruct.new({name: "bar"}), OpenStruct.new({name: "foo"}), OpenStruct.new({name: "widget"})]
end

let(:search_results) do
Adauth::SearchResults.new(test_array)
end

it "should create self from_array" do
Adauth::SearchResults.new(test_array).should be_a Adauth::SearchResults
end

it "should have the limit function" do
search_results.limit(2).length.should eq 2
search_results.limit(2).last.should_not eq test_array.last
search_results.limit(2).should be_a Adauth::SearchResults
end

it "should have the order function" do
search_results.order(:name, :asc).should eq sorted_array
search_results.order(:name, :asc).should be_a Adauth::SearchResults
search_results.order(:name, :desc).should eq sorted_array.reverse
search_results.order(:name, :desc).should be_a Adauth::SearchResults
end

it "should handle having a wrong direction passed to it" do
lambda { search_results.order(:name, :foo) }.should raise_exception
end

it "should default to :asc for order" do
search_results.order(:name).should eq sorted_array
search_results.order(:name).should be_a Adauth::SearchResults
end
end
12 changes: 0 additions & 12 deletions spec/spec_helper.rb
Expand Up @@ -23,15 +23,3 @@ def test_data(set, key)
@yaml ||= YAML::load(File.open('spec/test_data.yml'))
@yaml[set][key]
end

def administrator
Adauth::AdObjects::User.where('sAMAccountName', "administrator").first
end

#def breakable_user
# Adauth::AdObjects::User.where('sAMAccountName', test_data("domain", "breakable_user")).first
#end

def query_user
Adauth::AdObjects::User.where('sAMAccountName', test_data("domain", "query_user")).first
end

0 comments on commit 007e61f

Please sign in to comment.