Sofa.Security (sofa v0.7.0)

Copy Markdown View Source

A database's security object: who its admins and members are, by user name and by role.

If the only tool you have is CouchDB, then everything looks like {:ok, :relax}

See https://docs.couchdb.org/en/stable/api/database/security.html. An empty object means every server admin is an admin and everyone else, including anonymous users, is a member. CouchDB 3 creates databases admin-only instead, with the _admin role on both grants, unless its default_security setting says otherwise. Users and roles are those of Sofa.User documents in _users.

Read and write it with Sofa.DB.security/1 and Sofa.DB.set_security/2.

Examples

Sofa.Security.new()
|> Sofa.Security.admin("jan")
|> Sofa.Security.member(role: "users")
|> then(&Sofa.DB.set_security(db, &1))

iex> Sofa.Security.new() |> Sofa.Security.member("jan") |> Sofa.Security.member(role: "users") |> Sofa.Security.to_map()
%{"admins" => %{"names" => [], "roles" => []}, "members" => %{"names" => ["jan"], "roles" => ["users"]}}

Summary

Types

User names and roles granted a level of access

t()

Functions

Grants admin access to a user name, or with role: to a role.

Whether the object grants nothing, which leaves the database open to all.

From the JSON object CouchDB returns. Missing parts are empty.

Grants member access to a user name, or with role: to a role.

An empty security object, as a new database has.

To the JSON object CouchDB expects.

Types

grant()

@type grant() :: %{names: [String.t()], roles: [String.t()]}

User names and roles granted a level of access

t()

@type t() :: %Sofa.Security{admins: grant(), members: grant()}

Functions

admin(security, who)

@spec admin(t(), String.t() | [{:role, String.t()}]) :: t()

Grants admin access to a user name, or with role: to a role.

empty?(security)

@spec empty?(t()) :: boolean()

Whether the object grants nothing, which leaves the database open to all.

from_map(map)

@spec from_map(map()) :: t()

From the JSON object CouchDB returns. Missing parts are empty.

member(security, who)

@spec member(t(), String.t() | [{:role, String.t()}]) :: t()

Grants member access to a user name, or with role: to a role.

new()

@spec new() :: t()

An empty security object, as a new database has.

to_map(security)

@spec to_map(t()) :: map()

To the JSON object CouchDB expects.