Skip to content

Instantly share code, notes, and snippets.

@shahryarjb
Created Oct 20, 2018
Embed
What would you like to do?
defmodule UsersInfo do
use Ecto.Schema
import Ecto.Changeset
alias TrangellUsersService.Login.Security.Security
@primary_key {:id, :binary_id, autogenerate: true}
schema "usersinfo" do
field :email, :string
field :password, :string, virtual: true
field :name, :string
field :lastname, :string
field :password_hash, :string
field :last_ip, :string
field :group_acl, :string
field :language, :string
field :country, :string
has_many :token_actions, TokenActions, foreign_key: :users_info_id
has_many :profile_actions, ProfileActions, foreign_key: :users_info_id
timestamps()
end
def changeset(struct, params \\ %{}) do
struct
|> cast(params, [:lastname, :name, :email, :password, :last_ip, :group_acl, :language, :country, :password_hash])
|> validate_required([:name, :lastname, :email, :password, :last_ip, :group_acl,:language, :country])
|> validate_length(:name, min: 3, max: 15)
|> validate_length(:lastname, min: 3, max: 15)
|> validate_length(:email, min: 8, max: 30)
|> validate_length(:password, min: 8, max: 120)
|> check_password(:password)
|> hash_password
|> validate_length(:last_ip, min: 6, max: 20)
|> validate_length(:group_acl, max: 50)
|> validate_length(:language, min: 2, max: 3)
|> validate_length(:country, max: 20)
|> validate_inclusion(:country, Security.country_list())
|> validate_inclusion(:group_acl, ["admin", "actived", "unactived", "blocked"])
|> validate_inclusion(:language, ["fa", "en"])
|> validate_format(:email, ~r/@/)
|> validate_format(:name, ~r/^[A-z]+$/)
|> validate_format(:lastname, ~r/^[A-z]+$/)
|> unique_constraint(:email, name: :index_of_users_unique_email, message: "email already exists.")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment