-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dynamic selects to Sharepoint block
- Loading branch information
1 parent
4ac75b9
commit fab929a
Showing
5 changed files
with
313 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
202 changes: 202 additions & 0 deletions
202
apps/api/lib/buildel_web/controllers/organizations/tools/sharepoint_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,202 @@ | ||
defmodule BuildelWeb.OrganizationToolSharepointController do | ||
use BuildelWeb, :controller | ||
use OpenApiSpex.ControllerSpecs | ||
|
||
import BuildelWeb.UserAuth | ||
|
||
alias Buildel.Organizations | ||
|
||
action_fallback(BuildelWeb.FallbackController) | ||
|
||
plug(:fetch_current_user) | ||
plug(:require_authenticated_user) | ||
|
||
plug OpenApiSpex.Plug.CastAndValidate, | ||
json_render_error_v2: true, | ||
render_error: BuildelWeb.ErrorRendererPlug | ||
|
||
tags ["sharepoint"] | ||
|
||
operation :list_sites, | ||
summary: "List sites", | ||
parameters: [ | ||
organization_id: [ | ||
in: :path, | ||
description: "Organization ID", | ||
type: :integer, | ||
required: true | ||
], | ||
client_id: [ | ||
in: :query, | ||
description: "Client ID", | ||
type: :string, | ||
required: true | ||
], | ||
secret_name: [ | ||
in: :query, | ||
description: "Client secret name", | ||
type: :string, | ||
required: true | ||
], | ||
tenant_id: [ | ||
in: :query, | ||
description: "Tenant ID", | ||
type: :string, | ||
required: true | ||
] | ||
], | ||
request_body: nil, | ||
responses: [ | ||
ok: {"organizations", "application/json", BuildelWeb.Schemas.Sharepoint.ListSitesResponse}, | ||
unauthorized: | ||
{"unauthorized", "application/json", BuildelWeb.Schemas.Errors.UnauthorizedResponse}, | ||
forbidden: {"forbidden", "application/json", BuildelWeb.Schemas.Errors.ForbiddenResponse} | ||
], | ||
security: [%{"authorization" => []}] | ||
|
||
def list_sites(conn, _) do | ||
%{ | ||
client_id: client_id, | ||
secret_name: secret_name, | ||
tenant_id: tenant_id, | ||
organization_id: organization_id | ||
} = conn.params | ||
|
||
user = conn.assigns.current_user | ||
|
||
with {:ok, organization} <- Organizations.get_user_organization(user, organization_id), | ||
{:ok, %{value: client_secret}} <- | ||
Buildel.Organizations.get_organization_secret(organization, secret_name), | ||
{:ok, access_token} <- get_access_token(client_id, client_secret, tenant_id), | ||
{:ok, sites} <- get_sites(access_token) do | ||
render(conn, :list_sites, sites: sites["value"]) | ||
end | ||
end | ||
|
||
operation :list_drives, | ||
summary: "List drives", | ||
parameters: [ | ||
organization_id: [ | ||
in: :path, | ||
description: "Organization ID", | ||
type: :integer, | ||
required: true | ||
], | ||
client_id: [ | ||
in: :query, | ||
description: "Client ID", | ||
type: :string, | ||
required: true | ||
], | ||
secret_name: [ | ||
in: :query, | ||
description: "Client secret name", | ||
type: :string, | ||
required: true | ||
], | ||
tenant_id: [ | ||
in: :query, | ||
description: "Tenant ID", | ||
type: :string, | ||
required: true | ||
], | ||
site_id: [ | ||
in: :query, | ||
description: "Site ID", | ||
type: :string, | ||
required: true | ||
] | ||
], | ||
request_body: nil, | ||
responses: [ | ||
ok: {"organizations", "application/json", BuildelWeb.Schemas.Sharepoint.ListSitesResponse}, | ||
unauthorized: | ||
{"unauthorized", "application/json", BuildelWeb.Schemas.Errors.UnauthorizedResponse}, | ||
forbidden: {"forbidden", "application/json", BuildelWeb.Schemas.Errors.ForbiddenResponse} | ||
], | ||
security: [%{"authorization" => []}] | ||
|
||
def list_drives(conn, _) do | ||
%{ | ||
client_id: client_id, | ||
secret_name: secret_name, | ||
tenant_id: tenant_id, | ||
organization_id: organization_id, | ||
site_id: site_id | ||
} = conn.params | ||
|
||
user = conn.assigns.current_user | ||
|
||
with {:ok, organization} <- Organizations.get_user_organization(user, organization_id), | ||
{:ok, %{value: client_secret}} <- | ||
Buildel.Organizations.get_organization_secret(organization, secret_name), | ||
{:ok, access_token} <- get_access_token(client_id, client_secret, tenant_id), | ||
{:ok, drives} <- get_drives(access_token, site_id) do | ||
render(conn, :list_drives, drives: drives["value"]) | ||
end | ||
end | ||
|
||
defp get_sites(access_token) do | ||
url = | ||
"https://graph.microsoft.com/v1.0/sites?search=*" | ||
|
||
headers = [ | ||
{"Authorization", "Bearer #{access_token}"} | ||
] | ||
|
||
case Req.new(url: url) |> Req.get(headers: headers) do | ||
{:ok, %Req.Response{status: 200, body: body}} -> | ||
{:ok, body} | ||
|
||
{:error, %Req.Response{body: reason}} -> | ||
{:ok, reason} | ||
|
||
{:error, reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
defp get_drives(access_token, site_id) do | ||
url = | ||
"https://graph.microsoft.com/v1.0/sites/#{site_id}/drives" | ||
|
||
headers = [ | ||
{"Authorization", "Bearer #{access_token}"} | ||
] | ||
|
||
case Req.new(url: url) |> Req.get(headers: headers) do | ||
{:ok, %Req.Response{status: 200, body: body}} -> | ||
{:ok, body} | ||
|
||
{:error, %Req.Response{body: reason}} -> | ||
{:ok, reason} | ||
|
||
{:error, reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
|
||
defp get_access_token(client_id, client_secret, tenant_id) do | ||
url = "https://login.microsoftonline.com/#{tenant_id}/oauth2/v2.0/token" | ||
|
||
body = %{ | ||
"client_id" => client_id, | ||
"client_secret" => client_secret, | ||
"scope" => "https://graph.microsoft.com/.default", | ||
"grant_type" => "client_credentials" | ||
} | ||
|
||
headers = [{"Content-Type", "application/x-www-form-urlencoded"}] | ||
|
||
case Req.new(url: url) |> Req.post(form: body, headers: headers) do | ||
{:ok, %Req.Response{status: 200, body: body}} -> | ||
{:ok, body["access_token"]} | ||
|
||
{:ok, %Req.Response{body: reason}} -> | ||
{:error, reason} | ||
|
||
{:error, reason} -> | ||
{:error, reason} | ||
end | ||
end | ||
end |
23 changes: 23 additions & 0 deletions
23
apps/api/lib/buildel_web/controllers/organizations/tools/sharepoint_json.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
defmodule BuildelWeb.OrganizationToolSharepointJSON do | ||
def list_sites(%{sites: sites}) do | ||
%{data: for(site <- sites, do: site(site))} | ||
end | ||
|
||
def list_drives(%{drives: drives}) do | ||
%{data: for(drive <- drives, do: drive(drive))} | ||
end | ||
|
||
defp drive(drive) do | ||
%{ | ||
id: drive["id"], | ||
name: drive["name"] | ||
} | ||
end | ||
|
||
defp site(site) do | ||
%{ | ||
id: site["id"], | ||
name: site["displayName"] | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
apps/api/lib/buildel_web/schemas/organizations/tools/sharepoint.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
defmodule BuildelWeb.Schemas.Sharepoint do | ||
alias OpenApiSpex.Schema | ||
|
||
defmodule ListSitesResponse do | ||
require OpenApiSpex | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "SharepointListSitesResponse", | ||
type: :object, | ||
properties: %{ | ||
id: %Schema{type: :string, description: "Site ID"}, | ||
name: %Schema{type: :string, description: "Site name"} | ||
}, | ||
required: [:id, :name] | ||
}) | ||
end | ||
|
||
defmodule ListDrivesResponse do | ||
require OpenApiSpex | ||
|
||
OpenApiSpex.schema(%{ | ||
title: "SharepointListDrivesResponse", | ||
type: :object, | ||
properties: %{ | ||
id: %Schema{type: :string, description: "Drive ID"}, | ||
name: %Schema{type: :string, description: "Drive name"} | ||
}, | ||
required: [:id, :name] | ||
}) | ||
end | ||
end |