Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

key :organization_id not found #263

Closed
thantez opened this issue Aug 26, 2019 · 7 comments
Closed

key :organization_id not found #263

thantez opened this issue Aug 26, 2019 · 7 comments

Comments

@thantez
Copy link

thantez commented Aug 26, 2019

hi
I tried to install invitation extension for my project according to its README but when I entered email for inviting an user I got this error:
key :organization_id not found in: %Project.Account.User{__meta__: #Ecto.Schema.Metadata<:loaded, "users">, confirm_password: nil, current_password: nil, email: "[email protected]", email_confirmation_token: "ef6e1f6e-beeb-43e4-b8e3-ae43cf685d83", email_confirmed_at: #DateTime<2019-08-26 14:02:43Z>, id: 5, inserted_at: ~N[2019-08-26 14:02:33], invitation_accepted_at: nil, invitation_token: nil, invited_by: #Ecto.Association.NotLoaded<association :invited_by is not loaded>, invited_by_id: nil, invited_users: #Ecto.Association.NotLoaded<association :invited_users is not loaded>, password: nil, password_hash: "$pbkdf2-sha512$100000$X3UILb2A8IL6sCDzgmzyNA==$zVjYxeaQpkYjNxeFXl4De5wyVtFX2qyxQ4P0ad7UblEI4A6r69jCEiWlcEMKstwzZW8vV9hzWcb63HcVLex54g==", unconfirmed_email: nil, updated_at: ~N[2019-08-26 14:02:43]}

and that was predictable. this is my user module:

defmodule Project.Account.User do
  use Ecto.Schema
  use Pow.Ecto.Schema
  use Pow.Extension.Ecto.Schema,
    extensions: [PowResetPassword, PowEmailConfirmation, PowPersistentSession, PowInvitation]

  schema "users" do
    pow_user_fields()
    timestamps()
  end

  def changeset(user_or_changeset, attrs) do
    user_or_changeset
    |> pow_changeset(attrs)
    |> pow_extension_changeset(attrs)
  end

  def invite_changeset(user_or_changeset, invited_by, attrs) do
    user_or_changeset
    |> pow_invite_changeset(invited_by, attrs)
    |> changeset_organization(invited_by)
  end

  defp changeset_organization(changeset, invited_by) do
    Ecto.Changeset.change(changeset, organization_id: invited_by.organization_id)
  end
end

so what should I do?
thanks.

@danschultzer
Copy link
Collaborator

You have to add a belongs to association first:

  # Schema

  schema "users" do
    belongs_to Project.Account.Organization

    pow_user_fields()
    timestamps()
  end

  # ...
  # Migration

  def change do
    alter table(:users) do
      add :organization_id, references(:organizations)
    end
  end

  # ...

@thantez
Copy link
Author

thantez commented Aug 26, 2019

I wonder what organization is, is it about administration and managing users levels?

@danschultzer
Copy link
Collaborator

danschultzer commented Aug 26, 2019

Organization is just the scope for your user. A common structure is that you have teams on your platform that users belongs to, e.g. how slack works. The example is to show how you can make sure that the user that gets invited will belong to the same team as the inviter. You wouldn't need to use the above unless you have some scope that invited users should belong to.

@thantez
Copy link
Author

thantez commented Aug 28, 2019

wow.
I thought inviting is just for advertising by users,
for example bob invite jack for ad site to him.
thanks a lot.

@thantez thantez closed this as completed Aug 28, 2019
@danschultzer
Copy link
Collaborator

It can be used that way too, PowInvitation was made to just expose a basic invitation system. It can be used/customized for tons of things.

@meoyawn
Copy link

meoyawn commented Mar 6, 2020

man it would be so cool to have a guide on adding organization_id in Pow. I'm still a beginner, that would help immensely

@danschultzer
Copy link
Collaborator

danschultzer commented Mar 6, 2020

I’ve it planned for https://powauth.com. Here’s a draft: pow-auth/pow_site#11 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants