Meet SourceLevel, an All-in-one Data & Analytics for Engineering Teams

SourceLevel provides metrics and insights by collecting data from many sources such as GitHub and GitLab. Our product brings visibility over every corner of the delivery pipeline in a Data & Analytics Solution for Engineering Teams.

Get started for free
Go to line 1
defmodule Hexpm.Accounts.Organizations do
Go to line 2
  use Hexpm.Context
Go to line 4
  def all_by_user(user, preload \\ []) do
Go to line 5
    Repo.all(assoc(user, :organizations))
Go to line 6
    |> Repo.preload(preload)
Go to line 9
  def get(name, preload \\ []) do
Go to line 10
    Repo.get_by(Organization, name: name)
Go to line 11
    |> Repo.preload(preload)
Go to line 14
  def get_role(organization, user) do
Go to line 15
    org_user = Repo.get_by(OrganizationUser, organization_id: organization.id, user_id: user.id)
Go to line 16
    org_user && org_user.role
Go to line 19
  def preload(organization, preload) do
Go to line 20
    Repo.preload(organization, preload)
Go to line 23
  def access?(_organization, nil = _user, _role) do
Go to line 27
  def access?(organization, user, role) do
Go to line 28
    Repo.one!(Organization.access(organization, user, role))
Go to line 31
  def create(user, params, audit: audit_data) do
Go to line 33
      Multi.new()
Go to line 34
      |> Multi.insert(:organization, Organization.changeset(%Organization{}, params))
Go to line 35
      |> Multi.insert(:repository, fn %{organization: organization} ->
Go to line 36
        %Repository{name: organization.name, public: false, organization_id: organization.id}
Go to line 38
      |> Multi.insert(:user, &User.build_organization(&1.organization))
Go to line 39
      |> Multi.insert(:organization_user, fn %{organization: organization} ->
Go to line 40
        organization_user = %OrganizationUser{
Go to line 41
          organization_id: organization.id,
Go to line 42
          user_id: user.id,
Go to line 43
          role: "admin"
Go to line 46
        Organization.add_member(organization_user, %{})
Go to line 48
      |> audit(audit_data, "organization.create", & &1.organization)
Go to line 50
    case Repo.transaction(multi) do
Go to line 51
      {:ok, result} -> {:ok, result.organization}
Go to line 52
      {:error, :user, changeset, _} -> {:error, changeset}
Go to line 53
      {:error, :organization, changeset, _} -> {:error, changeset}
Go to line 57
  def create_from_user(organization_user, admin_user) do
Go to line 59
      Multi.new()
Go to line 60
      |> Multi.insert(:organization, Organization.build_from_user(organization_user))
Go to line 61
      |> Multi.insert(:repository, fn %{organization: organization} ->
Go to line 62
        %Repository{name: organization.name, public: false, organization_id: organization.id}
Go to line 64
      |> Multi.update(:user, &User.to_organization(organization_user, &1.organization))
Go to line 65
      |> Multi.insert(:organization_user, fn %{organization: organization} ->
Go to line 66
        organization_user = %OrganizationUser{
Go to line 67
          organization_id: organization.id,
Go to line 68
          user_id: admin_user.id,
Go to line 69
          role: "admin"
Go to line 72
        Organization.add_member(organization_user, %{})
Go to line 75
    Repo.transaction(multi)
Go to line 78
  def merge_with_user(
Go to line 79
        %Organization{name: name} = organization,
Go to line 80
        %User{username: name, organization_id: nil} = user
Go to line 82
    Repo.update(User.to_organization(user, organization))
Go to line 85
  def add_member(_organization, %User{organization_id: id}, _params, _opts) when is_integer(id) do
Go to line 86
    {:error, :organization_user}
Go to line 89
  def add_member(organization, %User{organization_id: nil} = user, params, audit: audit_data) do
Go to line 90
    organization_user = %OrganizationUser{organization_id: organization.id, user_id: user.id}
Go to line 93
      Multi.new()
Go to line 94
      |> Multi.insert(:organization_user, Organization.add_member(organization_user, params))
Go to line 95
      |> audit(audit_data, "organization.member.add", {organization, user})
Go to line 97
    case Repo.transaction(multi) do
Go to line 98
      {:ok, result} ->
Go to line 99
        send_invite_email(organization, user)
Go to line 100
        {:ok, result.organization_user}
Go to line 102
      {:error, :organization_user, changeset, _} ->
Go to line 103
        {:error, changeset}
Go to line 107
  def remove_member(organization, user, audit: audit_data) do
Go to line 108
    # TODO: Remove package ownerships to packages in organization repository
Go to line 110
    count = Repo.aggregate(assoc(organization, :organization_users), :count, :id)
Go to line 112
    if count == 1 do
Go to line 113
      {:error, :last_member}
Go to line 115
      organization_user = Repo.get_by(assoc(organization, :organization_users), user_id: user.id)
Go to line 117
      if organization_user do
Go to line 118
        {:ok, _result} =
Go to line 119
          Multi.new()
Go to line 120
          |> Multi.delete(:organization_user, organization_user)
Go to line 121
          |> audit(audit_data, "organization.member.remove", {organization, user})
Go to line 122
          |> Repo.transaction()
Go to line 129
  def change_role(organization, user, params, audit: audit_data) do
Go to line 130
    organization_users = Repo.all(assoc(organization, :organization_users))
Go to line 131
    organization_user = Enum.find(organization_users, &(&1.user_id == user.id))
Go to line 132
    number_admins = Enum.count(organization_users, &(&1.role == "admin"))
Go to line 135
      !organization_user ->
Go to line 136
        {:error, :unknown_user}
Go to line 138
      organization_user.role == "admin" and number_admins == 1 ->
Go to line 139
        {:error, :last_admin}
Go to line 143
          Multi.new()
Go to line 144
          |> Multi.update(:organization_user, Organization.change_role(organization_user, params))
Go to line 145
          |> audit(audit_data, "organization.member.role", {organization, user, params["role"]})
Go to line 147
        case Repo.transaction(multi) do
Go to line 148
          {:ok, result} ->
Go to line 149
            {:ok, result.organization_user}
Go to line 151
          {:error, :organization_user, changeset, _} ->
Go to line 152
            {:error, changeset}
Go to line 157
  def user_count(organization) do
Go to line 158
    Repo.aggregate(assoc(organization, :organization_users), :count, :id)
Go to line 161
  defp send_invite_email(organization, user) do
Go to line 162
    Emails.organization_invite(organization, user)
Go to line 163
    |> Mailer.deliver_now_throttled()