Packages
boruta
2.1.3
3.0.0-beta.4
3.0.0-beta.3
3.0.0-beta.2
3.0.0-beta.1
2.3.6
2.3.5
2.3.4
2.3.3
2.3.2
2.3.1
2.3.0
2.2.2
2.2.1
2.2.0
2.1.5
2.1.4
2.1.3
2.1.2
2.1.1
2.1.0
2.0.1
2.0.0
2.0.0-rc.1
2.0.0-rc.0
1.2.1
1.2.0
1.1.0
1.0.3
1.0.2
1.0.1
1.0.0
1.0.0-rc.3
1.0.0-rc.2
1.0.0-rc.1
1.0.0-rc.0
0.2.1
0.2.0
0.1.1
0.1.0
0.1.0-rc.5
0.1.0-rc.4
0.1.0-rc.3
0.1.0-rc.2
0.1.0-rc.1
Core of an OAuth/OpenID Connect provider enabling authorization in your applications.
Current section
Files
Jump to
Current section
Files
lib/boruta/adapters/ecto/schemas/scope.ex
defmodule Boruta.Ecto.Scope do
@moduledoc """
Ecto Adapter Scope Schema
"""
use Ecto.Schema
import Ecto.Changeset
@type t :: %__MODULE__{
name: String.t(),
public: boolean()
}
@primary_key {:id, Ecto.UUID, autogenerate: true}
@foreign_key_type :binary_id
@timestamps_opts type: :utc_datetime
schema "oauth_scopes" do
field :name, :string, default: ""
field :label, :string
field :public, :boolean, default: false
timestamps()
end
def changeset(scope, attrs) do
scope
|> cast(attrs, [:label, :name, :public])
|> unique_constraint(:id)
|> unique_constraint(:name)
|> validate_required([:name])
|> validate_not_nil(:public)
|> validate_no_whitespace(:name)
end
def assoc_changeset(scope, attrs) do
scope
|> cast(attrs, [:id, :name])
|> validate_no_whitespace(:name)
end
defp validate_not_nil(changeset, field) do
if get_field(changeset, field) == nil do
add_error(changeset, field, "must not be null")
else
changeset
end
end
defp validate_no_whitespace(changeset, field) do
value = get_field(changeset, field)
if value && String.match?(value, ~r/\s/) do
add_error(changeset, field, "must not contain whitespace")
else
changeset
end
end
end