Skip to content

Commit

Permalink
feat: PPT-1470 Add lookup by email endpoint in domains controller (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
naqvis authored Oct 14, 2024
1 parent 3bb8d8a commit ca1895b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 4 deletions.
75 changes: 73 additions & 2 deletions OPENAPI_DOC.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5477,6 +5477,77 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
/api/engine/v2/domains/lookup/{email}:
get:
summary: Find the domain name by looking into domain registerd email domains.
tags:
- Domains
operationId: PlaceOS::Api::Domains_lookup
parameters:
- name: email
in: path
description: User email to lookup domain for
example: [email protected]
required: true
schema:
type: string
responses:
200:
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/String'
409:
description: Conflict
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
401:
description: Unauthorized
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
403:
description: Forbidden
404:
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
408:
description: Request Timeout
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__CommonError'
400:
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
422:
description: Unprocessable Entity
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ParameterError'
406:
description: Not Acceptable
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
415:
description: Unsupported Media Type
content:
application/json:
schema:
$ref: '#/components/schemas/PlaceOS__Api__Application__ContentError'
/api/engine/v2/domains/{id}:
get:
summary: show the selected domain
Expand Down Expand Up @@ -21976,6 +22047,8 @@ components:
id:
type: string
nullable: true
String:
type: string
PlaceOS__Model__Driver:
type: object
properties:
Expand Down Expand Up @@ -22536,8 +22609,6 @@ components:
id:
type: string
nullable: true
String:
type: string
Array_JSON__Any_:
type: array
items:
Expand Down
14 changes: 14 additions & 0 deletions spec/controllers/domains_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require "../helper"

module PlaceOS::Api
describe Domains do
it "Lookup domain via user email" do
authority = Model::Generator.authority("https://www.dev-placeos.com", ["placeos.com", "dev-placeos.com"]).save!
email = URI.encode_www_form("[email protected]")
path = "#{Domains.base_route}lookup/#{email}"
result = client.get(path)
result.status_code.should eq(200)
result.body.strip('"').should eq(authority.domain)
end
end
end
19 changes: 17 additions & 2 deletions src/placeos-rest-api/controllers/domains.cr
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ module PlaceOS::Api
before_action :can_read, only: [:index, :show]
before_action :can_write, only: [:create, :update, :destroy, :remove]

before_action :check_admin, except: [:index, :show]
before_action :check_admin, except: [:index, :show, :lookup]
before_action :check_support, only: [:index, :show]

###############################################################################################

@[AC::Route::Filter(:before_action, except: [:index, :create])]
@[AC::Route::Filter(:before_action, except: [:index, :lookup, :create])]
def find_current_domain(id : String)
Log.context.set(authority_id: id)
# Find will raise a 404 (not found) if there is an error
Expand All @@ -35,6 +35,21 @@ module PlaceOS::Api
paginate_results(elastic, query)
end

# skip authentication for the lookup
skip_action :authorize!, only: :lookup
skip_action :set_user_id, only: :lookup

# Find the domain name by looking into domain registerd email domains.
@[AC::Route::GET("/lookup/:email")]
def lookup(
@[AC::Param::Info(name: "email", description: "User email to lookup domain for", example: "[email protected]")]
email : String
) : String
authority = ::PlaceOS::Model::Authority.find_by_email(email)
raise Error::NotFound.new("No matching domain found") unless authority
authority.domain
end

# show the selected domain
@[AC::Route::GET("/:id")]
def show : ::PlaceOS::Model::Authority
Expand Down

0 comments on commit ca1895b

Please sign in to comment.