Skip to content

Commit

Permalink
Make search_users API a bit more friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
blt04 committed Jun 28, 2012
1 parent 8acd4ac commit 001571f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 13 deletions.
37 changes: 28 additions & 9 deletions lib/simple_crowd/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,27 @@ def find_user_by_email email

# Partial email match
def search_users_by_email email
search_users({'principal.email' => email})
end

def search_users restrictions
soap_restrictions = prepare_search_restrictions restrictions
search_users({'email' => email})
end

# Search Crowd users using the given criteria.
#
# critieria should be a hash of SimpleCrowd::User properties or attributes.
# Not all properties are supported, see (https://developer.atlassian.com/display/CROWDDEV/Using+the+Search+API)
#
# NOTE: Atlassian Crowd contains a bug that ignores the limit and start
# parameters
#
# For example:
# client.search_users(:email => 'foo', :display_name => 'bar')
def search_users criteria, limit=0, start=0
# Convert search criteria to Crowd search restrictions
restrictions = criteria.inject({}) do |h, (key, val)|
key = User.search_restriction_for(key).to_s
h[key] = val
h
end
soap_restrictions = add_soap_namespace(prepare_search_restrictions(restrictions, limit, start))
users = simple_soap_call :search_principals, soap_restrictions rescue []
return [] if users.nil? || users[:soap_principal].nil?
users = users[:soap_principal].is_a?(Array) ? users[:soap_principal] : [users[:soap_principal]]
Expand Down Expand Up @@ -324,10 +340,13 @@ def prepare_validation_factors factors
}
end

def prepare_search_restrictions restrictions
{'int:searchRestriction' =>
restrictions.inject([]) {|arr, restrict| arr << {'int:name' => restrict[0], 'int:value' => restrict[1]}}
}
def prepare_search_restrictions restrictions, limit=0, start=0
restrictions = restrictions.inject([]) do |arr, (key, val)|
arr << {'name' => key, 'value' => val}
end
restrictions << {'name' => 'search.max.results', 'value' => limit.to_i} if limit.to_i > 0
restrictions << {'name' => 'search.index.start', 'value' => start.to_i} if start.to_i > 0
{'searchRestriction' => restrictions}
end

def hash_authenticated_token name = @options[:app_name], token = nil
Expand Down
13 changes: 13 additions & 0 deletions lib/simple_crowd/crowd_entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ def #{property_name}=(val)
@property_to_soap_map[property_name] = v
end

if opts[:search_restriction]
@property_to_search_restriction_map ||= {}
@property_to_search_restriction_map[property_name] = opts[:search_restriction]
end

if opts[:default]
@defaults ||= {}
@defaults[property_name] = opts[:default]
Expand Down Expand Up @@ -166,5 +171,13 @@ def self.soap_key_for(property_key)
end
property_key
end

def self.search_restriction_for(property_key)
property_key = :"#{property_key}"
if @property_to_search_restriction_map && @property_to_search_restriction_map.has_key?(property_key)
return @property_to_search_restriction_map[property_key]
end
property_key
end
end
end
8 changes: 4 additions & 4 deletions lib/simple_crowd/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ class User < CrowdEntity
# Immutable properties
property :id, :immutable => true
property :username, :immutable => true, :map_soap => :name
property :directory_id, :immutable => true
property :directory_id, :immutable => true, :search_restriction => 'principal.directory.id'

property :active, :default => true
property :active, :default => true, :search_restriction => 'principal.active'
property :description

# Assumed available attributes (with soap aliases)
attribute :first_name, :map_soap => :givenName
attribute :last_name, :map_soap => :sn
attribute :display_name, :map_soap => :displayName
attribute :email, :map_soap => :mail
attribute :display_name, :map_soap => :displayName, :search_restriction => 'principal.fullname'
attribute :email, :map_soap => :mail, :search_restriction => 'principal.email'
end
end

0 comments on commit 001571f

Please sign in to comment.