Since we currently only want each user to be associated with one organization (either as an admin or an owner), we need to add a validation to make sure that a user cannot create or be associated with more than one account. It's easy to disable this endpoint on the frontend, but we also want to make sure that a feisty customer can't break his own account by using a curl command.
We're going to create another validation just like we did with our website validation above, using validate_change/3 (docs). In this case, we're creating a function called validate_new_owner_admin/2 to make sure the user trying to create an organization is not already associated with an existing account. Just like we did in our auth/me route, we're finding a user, preloading the associations, then checking to see if we get an organization. If we do, we return an error.
... def changeset(struct, params \\ %{}, association \\ false) do required_fields = if association, do: [:website], else: [:website, :owner_id] struct |> cast(params, [:website, :owner_id]) |> validate_required(required_fields) |> validate_change(:owner_id, &validate_new_owner_admin/2) |> update_change(:website, &set_uri_scheme/1) |> validate_change(:website, &validate_website/2) |> unique_constraint(:website) |> put_public_key() |> unique_constraint(:public_key) end ... defp validate_new_owner_admin(:owner_id, owner_id) do user = Repo.get! User, owner_id org = Repo.preload(user, :organization).organization || Repo.preload(user, :owned_organization).owned_organization if org do [owner_id: "user is owner or admin of existing organiation"] else [] end end ...Now that we can create an organization, we need to be able to return the public_key to the user in a consistent manner. Currently, when a user creates an organization we return the key in the HTTP response, but after that response the user has no way to access her key again. To solve this, we're going to update the auth/me route to return the public_key of the organization (if there is one) that the user owns or is an admin.
Although we currently only have owners and no admins, we want to check if the current user is either an owner or an admin of an existing organization. We check this by using Repo.preload/3 (docs) to load the associations of both :organization and :owned_organization. We then check to see if there is a match in either one. If there is no match, it returns nil so we want to render just the user data without the organization data.
... def me(conn, _params) do user = Guardian.Plug.current_resource(conn) org = Repo.preload(user, :organization).organization || Repo.preload(user, :owned_organization).owned_organization case org do nil -> render(conn, UserView, "show.json", user: user) org -> render(conn, UserView, "show.json", user: user, org: org) end end...Then we need to update our UserView to handle this new data.
...def render("show.json", %{user: user, org: org}) do %{data: render_one(user, UserView, "user_organization.json", org: org)}enddef render("show.json", %{user: user}) do %{data: render_one(user, UserView, "user.json")}end...def render("user_organization.json", %{user: user, org: org}) do %{email: user.email, id: user.id, username: user.username, website: org.website, public_key: org.public_key}endAnd now when we authorize a user, we check to see if that user is associated with an organization and return the organization's data along with the authorization request. The next step is to connect our frontend and allow our existing user to create an organization.
Previous: Controlling and Validating FormsNext: Create and Join an Organization on Signup