-
-
Save shahryarjb/60706a6b949399dfcb6c069de6a1388d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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