materia v0.1.2 Materia.Accounts

The Accounts context.

Link to this section Summary

Functions

Returns an %Ecto.Changeset{} for tracking grant changes

Creates a accounts

Creates a grant

Creates a user

Deletes a Account

Deletes a Grant

Deletes a User

Gets a single account

Gets a single grant

指定されらロールで実行可能な権限の一覧を返す。

Gets a single user

Returns the list of accounts

iex(1)> accounts = Materia.Accounts.list_accounts_by_params(%{“and” => [ %{“status” => 1}, %{“organization_id” => 1} ]}) iex(2)> length(accounts) 1

Returns the list of grants

Returns the list of users

User汎用検索

ユーザー本登録&サインイン

パスワードリセット要求

パスワードリセット

Updates a account

Updates a grant

Updates a user

Ecto.Mulit用ユーザー更新

Link to this section Functions

Link to this function change_grant(grant)

Returns an %Ecto.Changeset{} for tracking grant changes.

Examples


iex(1)> Materia.Accounts.change_grant(%Materia.Accounts.Grant{role: "admin", method: "GET", request_path: "/hogehoge"})
#Ecto.Changeset<action: nil, changes: %{}, errors: [], data: #Materia.Accounts.Grant<>, valid?: true>
Link to this function create_account(attrs \\ %{})

Creates a accounts.

Examples

iex(1)> {:ok, account} = Materia.Accounts.create_account(%{“external_code” => “craete_account_test001”}) iex(2)> MateriaWeb.AccountView.render(“show.json”, %{account: account}) |> Map.delete(:id) |> Map.delete(:start_datetime) %{ descriptions: nil, expired_datetime: nil, external_code: “craete_account_test001”, frozen_datetime: nil, lock_version: 0, main_user: nil, name: nil, organization: nil, status: 1 }

Link to this function create_grant(attrs \\ %{})

Creates a grant.

Examples


iex(1)> {:ok, grant} = Materia.Accounts.create_grant(%{role: "test_user", method: "GET", request_path: "/hogehoge"})
iex(2)> MateriaWeb.GrantView.render("show.json", %{grant: grant}) |> Map.delete(:id)
%{method: "GET", request_path: "/hogehoge", role: "test_user"}
Link to this function create_user(attrs \\ %{})

Creates a user.

Examples

only requred params and full params case


iex(1)> {:ok, user} = Materia.Accounts.create_user(%{name: "テスト01", email: "test01@example.com", password: "test01", role: "operator", organization_id: 1})
iex(2)> MateriaWeb.UserView.render("show.json", %{user: user}) |> Map.delete(:id)
%{
  addresses: [],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "test01@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  lock_version: 1,
  name: "テスト01",
  organization: [],
  phone_number: nil,
  role: "operator",
  status: 1
}
iex(3)> {:ok, user} = Materia.Accounts.create_user(%{name: "テスト02", email: "test02@example.com", password: "test01", role: "operator", back_ground_img_url: "https://test02.com/bg_img.jpg", icon_img_url: "https://test02.com/icon_img.png", descriptions: "説明", phone_number: "090-YYYY-XXXX", status: 1})
iex(4)> MateriaWeb.UserView.render("show.json", %{user: user}) |> Map.delete(:id)
%{
  addresses: [],
  back_ground_img_url: "https://test02.com/bg_img.jpg",
  descriptions: "説明",
  email: "test02@example.com",
  external_user_id: nil,
  icon_img_url: "https://test02.com/icon_img.png",
  lock_version: 1,
  name: "テスト02",
  organization: [],
  role: "operator",
  phone_number: "090-YYYY-XXXX",
  status: 1
}
Link to this function delete_account(account)

Deletes a Account.

Examples

iex(1)> {:ok, account} = Materia.Accounts.create_account(%{“external_code” => “delete_account_test001”}) iex(2)> {:ok, deleted_account} = Materia.Accounts.delete_account(account) iex(3)> Materia.Accounts.list_accounts_by_params(%{“and” => [ %{“id” => account.id} ] }) []

Link to this function delete_grant(grant)

Deletes a Grant.

Examples


iex(1)> grant = Materia.Accounts.get_grant!(1)
iex(2)> {:ok, deleted_grant} = Materia.Accounts.delete_grant(grant)
iex(3)> grants = Materia.Accounts.list_grants()
iex(4)> MateriaWeb.GrantView.render("index.json", %{grants: grants}) |> Enum.chunk_every(2) |> List.first()
[
  %{
    id: 2,
    method: "ANY",
    request_path: "/api/ops/grants",
    role: "admin"
  },
  %{
    id: 3,
    method: "GET",
    request_path: "/api/ops/grants",
    role: "operator"
  }
]
Link to this function delete_user(user)

Deletes a User.

Cascade delete on address

Examples


iex(1)> {:ok, user} = Materia.Accounts.create_user(%{name: "test_delete_user001", email: "test_delete_user001@example.com", password: "test_delete_user001", role: "operator", organization_id: 1})
iex(2)> Materia.Accounts.list_users_by_params(%{ "and" => [%{"name" => "test_delete_user001"}]}) |> length()
1
iex(3)> Materia.Accounts.delete_user(user)
iex(4)> Materia.Accounts.list_users_by_params(%{ "and" => [%{"name" => "test_delete_user001"}]}) |> length()
0
Link to this function get_account!(id)

Gets a single account.

Raises Ecto.NoResultsError if the Account does not exist.

Examples

iex(1)> account = Materia.Accounts.get_account!(1) iex(2)> MateriaWeb.AccountView.render(“show.json”, %{account: account}) |> Map.delete(:start_datetime) %{ descriptions: nil, expired_datetime: nil, external_code: “hogehoge_code”, frozen_datetime: nil, id: 1, lock_version: 0, main_user: nil, name: “hogehoge account”, organization: nil, status: 1 }

Gets a single grant.

Raises Ecto.NoResultsError if the Grant does not exist.

Examples


iex(1)> grant = Materia.Accounts.get_grant!(1)
iex(2)> MateriaWeb.GrantView.render("show.json", %{grant: grant})
%{id: 1, method: "ANY", request_path: "/api/ops/users", role: "anybody"}
Link to this function get_grant_by_role(role)

指定されらロールで実行可能な権限の一覧を返す。

結果の権限リストにはrole “anybody”で登録された権限も含む。

Examples


iex(1)> grants = Materia.Accounts.get_grant_by_role("admin")
iex(2)> MateriaWeb.GrantView.render("index.json", %{grants: grants}) |> Enum.chunk_every(2) |> List.first()
[
  %{
    id: 1,
    method: "ANY",
    request_path: "/api/ops/users",
    role: "anybody"
  },
  %{
    id: 2,
    method: "ANY",
    request_path: "/api/ops/grants",
    role: "admin"
  }
]

Gets a single user.

Raises Ecto.NoResultsError if the User does not exist.

Examples

iex(1)> user = Materia.Accounts.get_user!(1)
iex(2)> MateriaWeb.UserView.render("show.json", %{user: user})
%{
  addresses: [
    %{
      address1: "福岡市中央区",
      address2: "大名 x-x-xx",
      id: 2,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "billing",
      user: [],
      zip_code: "810-ZZZZ"
    },
    %{
      address1: "福岡市中央区",
      address2: "港 x-x-xx",
      id: 1,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "living",
      user: [],
      zip_code: "810-ZZZZ"
    }
  ],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "hogehoge@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  id: 1,
  lock_version: 2,
  name: "hogehoge",
  organization: %{
    addresses: [],
    back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
    hp_url: "https://hogehoge.inc",
    id: 1,
    lock_version: 1,
    name: "hogehoge.inc",
    one_line_message: "let's do this.",
    phone_number: nil,
    profile_img_url: "https://hogehoge.com/prof_img.jpg",
    status: 1,
    users: []
  },
  phone_number: nil,
  role: "admin",
  status: 1
}
Link to this function get_user_by_email(email)

Examples

iex(1)> user = Materia.Accounts.get_user_by_email("hogehoge@example.com")
iex(2)> MateriaWeb.UserView.render("show.json", %{user: user})
%{
addresses: [],
back_ground_img_url: nil,
descriptions: nil,
email: "hogehoge@example.com",
external_user_id: nil,
icon_img_url: nil,
id: 1,
lock_version: 2,
name: "hogehoge",
organization: %{
  addresses: [],
  back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
  hp_url: "https://hogehoge.inc",
  id: 1,
  lock_version: 1,
  name: "hogehoge.inc",
  one_line_message: "let's do this.",
  phone_number: nil,
  profile_img_url: "https://hogehoge.com/prof_img.jpg",
  status: 1,
  users: []
},
phone_number: nil,
role: "admin",
status: 1
}
iex(3)> user = Materia.Accounts.get_user_by_email("not_exist@example.com")
nil
Link to this function list_accounts()

Returns the list of accounts.

Examples

iex(1)> accounts = Materia.Accounts.list_accounts() iex(2)> length(accounts) 1

Link to this function list_accounts_by_params(params)

iex(1)> accounts = Materia.Accounts.list_accounts_by_params(%{“and” => [ %{“status” => 1}, %{“organization_id” => 1} ]}) iex(2)> length(accounts) 1

Returns the list of grants.

Examples


iex(1)> grants = Materia.Accounts.list_grants()
iex(2)> MateriaWeb.GrantView.render("index.json", %{grants: grants}) |> Enum.chunk_every(2) |> List.first()
[
  %{
    id: 1,
    method: "ANY",
    request_path: "/api/ops/users",
    role: "anybody"
  },
  %{
    id: 2,
    method: "ANY",
    request_path: "/api/ops/grants",
    role: "admin"
  }
]

Returns the list of users.

Examples


iex(1)> users = Materia.Accounts.list_users()
iex(2)> MateriaWeb.UserView.render("index.json", %{users: users})
[
  %{
    addresses: [],
    back_ground_img_url: nil,
    descriptions: nil,
    email: "fugafuga@example.com",
    external_user_id: nil,
    icon_img_url: nil,
    id: 2,
    lock_version: 1,
    name: "fugafuga",
    organization: nil,
    phone_number: nil,
    role: "operator",
    status: 1
  },
  %{
    addresses: [
      %{
        address1: "福岡市中央区",
        address2: "港 x-x-xx",
        id: 1,
        latitude: nil,
        location: "福岡県",
        lock_version: 0,
        longitude: nil,
        organization: [],
        subject: "living",
        user: [],
        zip_code: "810-ZZZZ"
      },
      %{
        address1: "福岡市中央区",
        address2: "大名 x-x-xx",
        id: 2,
        latitude: nil,
        location: "福岡県",
        lock_version: 0,
        longitude: nil,
        organization: [],
        subject: "billing",
        user: [],
        zip_code: "810-ZZZZ"
      }
    ],
    back_ground_img_url: nil,
    descriptions: nil,
    email: "hogehoge@example.com",
    external_user_id: nil,
    icon_img_url: nil,
    id: 1,
    lock_version: 2,
    name: "hogehoge",
    organization: %{
      addresses: [],
      back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
      hp_url: "https://hogehoge.inc",
      id: 1,
      lock_version: 1,
      name: "hogehoge.inc",
      one_line_message: "let's do this.",
      phone_number: nil,
      profile_img_url: "https://hogehoge.com/prof_img.jpg",
      status: 1,
      users: []
    },
    phone_number: nil,
    role: "admin",
    status: 1
  }
]
Link to this function list_users_by_params(params)

User汎用検索


iex(1)> users = Materia.Accounts.list_users_by_params(%{"and" => [%{"role" => "admin"}], "or" => [%{"status" => 1}, %{"status" => 2}] })
iex(2)> MateriaWeb.UserView.render("index.json", %{users: users})
[
  %{
    addresses: [],
    back_ground_img_url: nil,
    descriptions: nil,
    email: "hogehoge@example.com",
    external_user_id: nil,
    icon_img_url: nil,
    id: 1,
    lock_version: 2,
    name: "hogehoge",
    organization: %{
      addresses: [],
      back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
      hp_url: "https://hogehoge.inc",
      id: 1,
      lock_version: 1,
      name: "hogehoge.inc",
      one_line_message: "let's do this.",
      phone_number: nil,
      profile_img_url: "https://hogehoge.com/prof_img.jpg",
      status: 1,
      users: []
    },
    phone_number: nil,
    role: "admin",
    status: 1
  }
]
iex(3)> users = Materia.Accounts.list_users_by_params(%{"and" => [%{"status" => 1}]})
iex(4)> MateriaWeb.UserView.render("index.json", %{users: users})
[
  %{
    addresses: [],
    back_ground_img_url: nil,
    descriptions: nil,
    email: "fugafuga@example.com",
    external_user_id: nil,
    icon_img_url: nil,
    id: 2,
    lock_version: 1,
    name: "fugafuga",
    organization: nil,
    phone_number: nil,
    role: "operator",
    status: 1
  },
  %{
    addresses: [],
    back_ground_img_url: nil,
    descriptions: nil,
    email: "hogehoge@example.com",
    external_user_id: nil,
    icon_img_url: nil,
    id: 1,
    lock_version: 2,
    name: "hogehoge",
    organization: %{
      addresses: [],
      back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
      hp_url: "https://hogehoge.inc",
      id: 1,
      lock_version: 1,
      name: "hogehoge.inc",
      one_line_message: "let's do this.",
      phone_number: nil,
      profile_img_url: "https://hogehoge.com/prof_img.jpg",
      status: 1,
      users: []
    },
    phone_number: nil,
    role: "admin",
    status: 1
  }
]
Link to this function registration_user(result, user, attr)

ユーザー本登録

仮登録ユーザー情報を更新しログイン可能な本登録ユーザー情報にする。

Examples

iex(1)> {:ok, tmp_user} = Materia.Accounts.regster_tmp_user(%{}, "test002@example.com", "operator")
iex(2)> {:ok, user} = Materia.Accounts.registration_user(%{}, tmp_user.user, %{name: "test002 user", password: "password"})
iex(3)> MateriaWeb.UserView.render("show.json", %{user: user}) |> Map.delete(:id)
%{
  addresses: [],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "test002@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  lock_version: 2,
  name: "test002 user",
  organization: [],
  phone_number: nil,
  role: "operator",
  status: 1
}
Link to this function registration_user_and_sign_in(result, user, attr)

ユーザー本登録&サインイン

仮登録ユーザー情報を更新しログイン可能な本登録ユーザー情報に更新し、同時にログイン処理を行う。

Examples

iex(1)> {:ok, tmp_user} = Materia.Accounts.regster_tmp_user(%{}, "test003@example.com", "operator")
iex(2)> {:ok, user_token} = Materia.Accounts.registration_user_and_sign_in(%{}, tmp_user.user, %{name: "test003 user", password: "password"})
iex(3)> MateriaWeb.UserView.render("show.json", %{user: user_token.user}) |> Map.delete(:id)
%{
  addresses: [],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "test003@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  lock_version: 2,
  name: "test003 user",
  organization: [],
  phone_number: nil,
  role: "operator",
  status: 1
}
iex(4)> user_token.access_token != nil
true
iex(5)> user_token.refresh_token != nil
true
Link to this function regster_tmp_user(result, email, role)

ユーザー仮登録

ログイン不可能なユーザー情報を仮登録する。

Examples

iex(1)> {:ok, tmp_user} = Materia.Accounts.regster_tmp_user(%{}, "test001@example.com", "operator")
iex(2)> MateriaWeb.UserView.render("show.json", %{user: tmp_user.user}) |> Map.delete(:id)
%{
  addresses: [],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "test001@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  lock_version: 1,
  name: nil,
  organization: [],
  phone_number: nil,
  role: "operator",
  status: 0
}
iex(3)> tmp_user.user_registration_token != nil
true
Link to this function request_password_reset(result, email)

パスワードリセット要求

パスワードリセット用のトークン発酵を行う。

Examples

iex(1)> {:ok, token} = Materia.Accounts.request_password_reset(%{}, "hogehoge@example.com")
iex(2)> token.password_reset_token != nil
true
Link to this function reset_my_password(result, user, password)

パスワードリセット

パスワードリセットを行う。

Examples

iex(1)> user = Materia.Accounts.get_user!(1)
iex(2)> {:ok, updated_user} = Materia.Accounts.reset_my_password(%{}, user, "password2")
iex(3)> MateriaWeb.UserView.render("show.json", %{user: updated_user}) |> Map.delete(:id)
%{
  addresses: [
    %{
      address1: "福岡市中央区",
      address2: "大名 x-x-xx",
      id: 2,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "billing",
      user: [],
      zip_code: "810-ZZZZ"
    },
    %{
      address1: "福岡市中央区",
      address2: "港 x-x-xx",
      id: 1,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "living",
      user: [],
      zip_code: "810-ZZZZ"
    }
  ],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "hogehoge@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  lock_version: 3,
  name: "hogehoge",
  organization: %{
    addresses: [],
    back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
    hp_url: "https://hogehoge.inc",
    id: 1,
    lock_version: 1,
    name: "hogehoge.inc",
    one_line_message: "let's do this.",
    phone_number: nil,
    profile_img_url: "https://hogehoge.com/prof_img.jpg",
    status: 1,
    users: []
  },
  phone_number: nil,
  role: "admin",
  status: 1
}
Link to this function update_account(account, attrs)

Updates a account.

Examples

iex(1)> {:ok, account} = Materia.Accounts.create_account(%{“external_code” => “update_account_test001”}) iex(2)> {:ok, updated_account} = Materia.Accounts.update_account(account, %{“status” => 8}) iex(3)> updated_account.status 8 iex(4)> updated_account.frozen_datetime != nil true

Link to this function update_grant(grant, attrs)

Updates a grant.

Examples


iex(1)> grant = Materia.Accounts.get_grant!(1)
iex(2)> {:ok, updated_grant} = Materia.Accounts.update_grant(grant, %{request_path: "/hogehoge"})
iex(3)> MateriaWeb.GrantView.render("show.json", %{grant: updated_grant})
%{id: 1, method: "ANY", request_path: "/hogehoge", role: "anybody"}
Link to this function update_user(user, attrs)

Updates a user.

Examples


iex(1)> user = Materia.Accounts.get_user!(1)
iex(2)> {:ok, updated_user} = Materia.Accounts.update_user(user, %{name: "更新済みユーザー"})
iex(3)> MateriaWeb.UserView.render("show.json", %{user: updated_user})
%{
  addresses: [
    %{
      address1: "福岡市中央区",
      address2: "大名 x-x-xx",
      id: 2,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "billing",
      user: [],
      zip_code: "810-ZZZZ"
    },
    %{
      address1: "福岡市中央区",
      address2: "港 x-x-xx",
      id: 1,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "living",
      user: [],
      zip_code: "810-ZZZZ"
    }
  ],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "hogehoge@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  id: 1,
  lock_version: 3,
  name: "更新済みユーザー",
  organization: %{
    addresses: [],
    back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
    hp_url: "https://hogehoge.inc",
    id: 1,
    lock_version: 1,
    name: "hogehoge.inc",
    one_line_message: "let's do this.",
    phone_number: nil,
    profile_img_url: "https://hogehoge.com/prof_img.jpg",
    status: 1,
    users: []
  },
  phone_number: nil,
  role: "admin",
  status: 1
}
Link to this function update_user(result, user, attr)

Ecto.Mulit用ユーザー更新


iex(1)> user = Materia.Accounts.get_user!(1)
iex(13)> {:ok, updated_user} = Materia.Accounts.update_user(%{}, user, %{name: "updated user"})
iex(15)> MateriaWeb.UserView.render("show.json", %{user: updated_user})
%{
  addresses: [
    %{
      address1: "福岡市中央区",
      address2: "大名 x-x-xx",
      id: 2,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "billing",
      user: [],
      zip_code: "810-ZZZZ"
    },
    %{
      address1: "福岡市中央区",
      address2: "港 x-x-xx",
      id: 1,
      latitude: nil,
      location: "福岡県",
      lock_version: 0,
      longitude: nil,
      organization: [],
      subject: "living",
      user: [],
      zip_code: "810-ZZZZ"
    }
  ],
  back_ground_img_url: nil,
  descriptions: nil,
  email: "hogehoge@example.com",
  external_user_id: nil,
  icon_img_url: nil,
  id: 1,
  lock_version: 3,
  name: "updated user",
  organization: %{
    addresses: [],
    back_ground_img_url: "https://hogehoge.com/ib_img.jpg",
    hp_url: "https://hogehoge.inc",
    id: 1,
    lock_version: 1,
    name: "hogehoge.inc",
    one_line_message: "let's do this.",
    phone_number: nil,
    profile_img_url: "https://hogehoge.com/prof_img.jpg",
    status: 1,
    users: []
  },
  phone_number: nil,
  role: "admin",
  status: 1
}