Commit 570a5959 authored by o@immerda.ch's avatar o@immerda.ch
Browse files

whois api for identifying type of mail objects

parent 07e1ebf9
Pipeline #9127 passed with stage
in 2 minutes and 43 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.by_prefix(search_string)
local_part, domain = search_string.split('@', 2)
if domain
where('email_iaddresses.local_part' => 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.by_prefix(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']
......
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