aboutsummaryrefslogblamecommitdiff
path: root/shardweb/lib/controllers/identity_controller.ex
blob: 962a888996162133072234a3f6e677b0a9d9a641 (plain) (tree)
1
2
3
4
5
6
7
8
9


                                        





                            

     
                                  












                                                              

     

                                                  
                                      
                                         
                                     
                                                 







                                       
                                                     


                                                     
                                



                                            
                              





                                     
                                                 

     
defmodule ShardWeb.IdentityController do
  use ShardWeb, :controller

  def list(conn, _params) do
    render conn, "list.html"
  end

  def self(conn, _params) do
    render conn, "self.html"
  end

  def view(conn, %{"pk" => pk}) do
    {:ok, pk} = Base.decode16(pk)
    shard = %SApp.Identity.Manifest{pk: pk} |> SData.term_hash
    pid = Shard.Manager.find_proc shard

    if pid == nil do
      render conn, ShardWeb.ErrorView, "404.html"
    else
      render conn, "view.html",
        view_pk: pk,
        view_nick: SApp.Identity.get_nick(pk),
        shard: shard,
        pid: pid
    end
  end

  def update(conn, params) do
    pid = SApp.Identity.find_proc(conn.assigns.pk)
    info = SApp.Identity.get_info(pid)
    info = %{info | nick: params["nick"]}
    SApp.Identity.set_info(pid, info)
    redirect conn, to: identity_path(conn, :self)
  end

  def switch(conn, params) do
    case Base.decode16(params["pk"]) do
      {:ok, pk} ->
        if Shard.Keys.have_sk? pk do
          conn
          |> put_session(:pk, pk)
          |> redirect(to: identity_path(conn, :self))
        else
          conn
          |> put_flash(:error, "No secret key found")
          |> render("self.html")
        end
      _ ->
        conn
        |> put_flash(:error, "Bad argument")
        |> render("self.html")
    end
  end

  def create(conn, _params) do
    pk = Shard.Keys.new_identity
    conn = put_session(conn, :pk, pk)
    redirect conn, to: identity_path(conn, :self)
  end
end