Commit c468862a authored by o's avatar o
Browse files

Merge branch 'whoisApi' into 'master'

whois api for identifying type of mail objects

See merge request !60
parents cae9da19 bf5a32f8
Pipeline #9335 passed with stage
in 3 minutes and 11 seconds
......@@ -181,6 +181,7 @@ class Authenticator
:admin_api_token => {
'get' => [
'/users_admin/info',
'/users_admin/whois',
'/users_admin/list',
'/users_admin/list_forwards',
'/domains/list',
......
......@@ -13,6 +13,10 @@ class ResourceManager
@@resource_type_ ||=
Hash[Resource.descendants.map{|r| [r.resource_name, r]}]
end
def resource_name
@@resource_name_ ||=
Hash[Resource.descendants.map{|r| [r.data_kind, r.resource_name]}]
end
def by_uid(uid)
r = resource_type[Resource.uid_type(uid)]
throw :invalid_resource_type unless r
......
......@@ -29,6 +29,17 @@ class EmailIaddress < ActiveRecord::Base
by_email_assoc(local_part, domain, scope).first
end
def self.search(search_string)
local_part, domain = search_string.split('@', 2)
if domain
where('email_iaddresses.local_part like ?', "#{local_part}")
.joins(:email_domain)
.where('email_domains.domain like ?', "#{domain}%")
else
where('email_iaddresses.local_part like ?', "#{local_part}%")
end
end
def self.active_by_email(local_part, domain = nil)
by_email(local_part, domain, active)
end
......
......@@ -40,13 +40,22 @@ class IApi < Sinatra::Base
# admin api
get '/list' do
kind = params['resource_type']
filter = params['find']
if res = ResourceManager.resource_type[kind]
data = {result: 'success'}
if params['deleted'] == 'true' || params['deleted'] == 'both'
data[:deleted] = ResourceManager.deleted(kind)
if filter.present?
data[:deleted].select!{|u| u['uid'] =~ /:#{filter}/}
end
end
if params['deleted'] != 'true' || params['deleted'] == 'both'
data[:data] = res.all.map{|k,v| v.to_api}
if filter.present?
# TODO: some support for filtering in the DB instead of filtering
# all results. Needs to be implemented in the resource manager.
data[:data].select!{|u| u[:uid] =~ /:#{filter}/}
end
end
return json data
end
......
......@@ -14,6 +14,19 @@ class IApi < Sinatra::Base
client_error('failed')
end
get '/whois' do
res = EmailIaddress.search(params['search']).all.map do |e|
{
email: e.email,
deleted: e.deleted_at || false,
email_object: (e.email_object.class.to_s if e.email_object),
resource: (ResourceManager.resource_name[e.email_object.class] if e.email_object),
forwards: e.email_forwards.all.map{|f| f.target},
}
end
return json result: 'success', matches: res
end
post '/update' do
email = validate_email(parsed_body['email'])
action = parsed_body['action']
......
......@@ -180,4 +180,38 @@ describe "iApi users admin route" do
end
end
end
context '/whois' do
it 'queries users' do
get_as('admin', '/users_admin/whois', search: 'user1@example.com')
expect(last_response.parsed_body["matches"].size).to eq(1)
expect(last_response.parsed_body["matches"][0]["email_object"]).to eq("EmailUser")
get_as('admin', '/users_admin/whois', search: 'user1@')
expect(last_response.parsed_body["matches"].size).to eq(1)
expect(last_response.parsed_body["matches"][0]["email"]).to eq("user1@example.com")
expect(last_response.parsed_body["matches"][0]["email_object"]).to eq("EmailUser")
end
it 'queries forwards' do
get_as('admin', '/users_admin/whois', search: 'fwd3@')
expect(last_response.parsed_body["matches"].size).to eq(1)
expect(last_response.parsed_body["matches"][0]["email_object"]).to eq("EmailAlias")
expect(last_response.parsed_body["matches"][0]["resource"]).to eq("mail_alias")
expect(last_response.parsed_body["matches"][0]["forwards"]).to eq(["user1@example.com"])
get_as('admin', '/users_admin/whois', search: 'fwd5@')
expect(last_response.parsed_body["matches"].size).to eq(1)
expect(last_response.parsed_body["matches"][0]["email_object"]).to be_nil
expect(last_response.parsed_body["matches"][0]["forwards"]).to eq(["external@test.com"])
end
it 'queries resources' do
u = EmailUser.active.all.sample
ResourceManager.create!(
"schleuder",
u.email,
{name: "testcreation",
domain: "example.com",
schleuder_host: "schleuder1"})
get_as('admin', '/users_admin/whois', search: 'testcreation@example.com')
expect(last_response.parsed_body["matches"].size).to eq(1)
expect(last_response.parsed_body["matches"][0]["resource"]).to eq("schleuder")
end
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment