From e22f33953d4cf46e8d174b27b71116f895425ee6 Mon Sep 17 00:00:00 2001 From: dorianmercatante Date: Thu, 31 Aug 2023 00:12:43 +0200 Subject: [PATCH] feat: permission handler --- .gitignore | 1 + lib/arke_postgres.ex | 37 ++++++++++++++++++++++++++++++++++--- lib/arke_postgres/query.ex | 5 +++++ mix.exs | 2 +- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ca5043b..398b903 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ erl_crash.dump *.iml # Ignore package tarball (built via "mix hex.build"). *.tar +.idea/ diff --git a/lib/arke_postgres.ex b/lib/arke_postgres.ex index da2cdac..b9e2c73 100644 --- a/lib/arke_postgres.ex +++ b/lib/arke_postgres.ex @@ -13,6 +13,7 @@ # limitations under the License. defmodule ArkePostgres do + alias Arke.Boundary.GroupManager alias ArkePostgres.{Table, ArkeUnit} def init() do @@ -22,12 +23,13 @@ defmodule ArkePostgres do try do projects = Arke.QueryManager.query(arke: :arke_project, project: :arke_system) - |> Arke.QueryManager.filter(:id, :eq, :arke_system, true) + # |> Arke.QueryManager.filter(:id, :eq, :arke_system, true) |> Arke.QueryManager.filter(:arke_id, :eq, "arke_project") |> Arke.QueryManager.all() Enum.each(projects, fn %{id: project_id} = _project -> start_managers(project_id) + get_group_modules(project_id) end) :ok @@ -42,6 +44,36 @@ defmodule ArkePostgres do end end + defp get_group_modules(project) do + Enum.reduce(:application.loaded_applications(), [], fn {app, _, _}, group_list -> + {:ok, modules} = :application.get_key(app, :modules) + module_group_list = + Enum.reduce(modules, [], fn mod, mod_group_list -> + is_group = + Code.ensure_loaded?(mod) and :erlang.function_exported(mod, :is_group?, 0) and + mod.group_from_attr != nil and mod.is_group? == true + + mod_group_list = check_group_module(project, mod, mod_group_list, is_group) + end) + + group_list ++ module_group_list + end) + end + + defp check_group_module(project, mod, group_list, true) do + %{id: id} = mod.group_from_attr + # IO.inspect("Modifica gruppo Gruppo #{id} - #{project}") + group = GroupManager.get(id, project) + update_group_manager_and_list(group, group_list, mod) + end + defp update_group_manager_and_list(group, group_list, _) when is_nil(group), do: group_list + defp update_group_manager_and_list(group, group_list, mod) do + GroupManager.update(group, Arke.Core.Unit.update(group, __module__: mod)) + [mod | group_list] + end + + defp check_group_module(_, _, group_list, false), do: group_list + def print_missing_env(keys) when is_list(keys) do for k <- keys do IO.puts("#{IO.ANSI.red()} error:#{IO.ANSI.reset()} env key #{k} not found.") @@ -80,8 +112,7 @@ defmodule ArkePostgres do Arke.Boundary.ArkeManager.create(unit, project_id) end) - - Enum.each(groups, fn unit -> Arke.Boundary.GroupManager.create(unit, project_id) end) + Enum.each(groups, fn unit -> Arke.Boundary.GroupManager.create(Arke.Core.Unit.update(unit, __module__: Arke.System.BaseGroup), project_id) end) end def create(project, %{arke_id: arke_id} = unit) do diff --git a/lib/arke_postgres/query.ex b/lib/arke_postgres/query.ex index c26f5f9..fb07e44 100644 --- a/lib/arke_postgres/query.ex +++ b/lib/arke_postgres/query.ex @@ -74,6 +74,7 @@ defmodule ArkePostgres.Query do "string", "unit", "link", + "dynamic", "date", "datetime", "time" @@ -342,6 +343,9 @@ defmodule ArkePostgres.Query do defp get_arke_column(%{id: id, arke_id: :link} = _parameter), do: dynamic([q], fragment("(? -> ? ->> 'value')::text", field(q, :data), ^Atom.to_string(id))) + defp get_arke_column(%{id: id, arke_id: :dynamic} = _parameter), + do: dynamic([q], fragment("(? -> ? ->> 'value')::text", field(q, :data), ^Atom.to_string(id))) + defp get_value(_parameter, value) when is_nil(value), do: value defp get_value(_parameter, value) when is_list(value), do: value defp get_value(_parameter, value) when is_map(value), do: value @@ -415,6 +419,7 @@ defmodule ArkePostgres.Query do defp get_value(%{id: id, arke_id: :dict} = _parameter, value), do: value defp get_value(%{id: id, arke_id: :list} = _parameter, value), do: value defp get_value(%{id: id, arke_id: :link} = _parameter, value), do: value + defp get_value(%{id: id, arke_id: :dynamic} = _parameter, value), do: value defp get_value(%{id: id}, value), do: raise("Parameter(#{id}) value not valid") defp parse_number_list(parameter, value, func) do diff --git a/mix.exs b/mix.exs index b93ad44..afdc272 100644 --- a/mix.exs +++ b/mix.exs @@ -1,7 +1,7 @@ defmodule ArkePostgres.MixProject do use Mix.Project - @version "0.2.4" + @version "0.2.5" @scm_url "https://github.com/arkemishub/arke-postgres" @site_url "https://arkehub.com"