-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
794b848
commit 2407fa8
Showing
13 changed files
with
198 additions
and
10 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
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
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
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
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,59 @@ | ||
defmodule ExternalSignIn do | ||
alias Core.Accounts | ||
alias Core.Repo | ||
import Ecto.Query, warn: false | ||
|
||
def sign_in(conn, organisation, external_id) do | ||
user = | ||
if user = get_user_by_external_id(external_id) do | ||
user | ||
else | ||
register_user(organisation, external_id) | ||
end | ||
|
||
CoreWeb.UserAuth.log_in_user_without_redirect(conn, user) | ||
end | ||
|
||
def get_user_by_external_id(external_id) do | ||
external_user_query = | ||
from(ex in ExternalSignIn.User, | ||
where: ex.external_id == ^external_id, | ||
select: ex.user_id | ||
) | ||
|
||
from(u in Accounts.User, where: u.id in subquery(external_user_query)) | ||
|> Repo.one() | ||
end | ||
|
||
def register_user(organisation, external_id) when is_atom(organisation) do | ||
register_user(Atom.to_string(organisation), external_id) | ||
end | ||
|
||
def register_user(organisation, external_id) do | ||
name = "#{organisation}_#{external_id}" | ||
|
||
user = | ||
Accounts.User.sso_changeset(%Accounts.User{}, %{ | ||
email: "external_#{name}@eyra.co", | ||
researcher: false, | ||
student: false, | ||
displayname: name, | ||
profile: %{ | ||
fullname: name | ||
} | ||
}) | ||
|
||
external_user = | ||
ExternalSignIn.User.changeset(%ExternalSignIn.User{}, %{ | ||
external_id: external_id, | ||
organisation: organisation | ||
}) | ||
|
||
{:ok, result} = | ||
external_user | ||
|> Ecto.Changeset.put_assoc(:user, user) | ||
|> Repo.insert() | ||
|
||
result.user | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
defmodule ExternalSignIn.User do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
@fields ~w(external_id organisation)a | ||
@required_fields @fields | ||
|
||
schema "external_users" do | ||
belongs_to(:user, Core.Accounts.User) | ||
field(:external_id, :string) | ||
field(:organisation, :string) | ||
|
||
timestamps() | ||
end | ||
|
||
def changeset(%ExternalSignIn.User{} = user, attrs) do | ||
user | ||
|> cast(attrs, @fields) | ||
|> validate_required(@required_fields) | ||
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
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
20 changes: 20 additions & 0 deletions
20
core/priv/repo/migrations/20231120165201_add_external_user.exs
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,20 @@ | ||
defmodule Core.Repo.Migrations.AddExternalUser do | ||
use Ecto.Migration | ||
|
||
def up do | ||
create table(:external_users) do | ||
add(:user_id, references(:users, on_delete: :delete_all), null: false) | ||
add(:organisation, :string) | ||
add(:external_id, :string) | ||
|
||
timestamps() | ||
end | ||
|
||
create(unique_index(:external_users, [:organisation, :external_id])) | ||
end | ||
|
||
def down do | ||
drop(index(:external_users, [:organisation, :external_id])) | ||
drop(table(:external_users)) | ||
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
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
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
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