Skip to content

Commit

Permalink
feat: permission handler
Browse files Browse the repository at this point in the history
  • Loading branch information
dorianmercatante committed Aug 30, 2023
1 parent 90de52f commit e22f339
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ erl_crash.dump
*.iml
# Ignore package tarball (built via "mix hex.build").
*.tar
.idea/
37 changes: 34 additions & 3 deletions lib/arke_postgres.ex
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

defmodule ArkePostgres do
alias Arke.Boundary.GroupManager
alias ArkePostgres.{Table, ArkeUnit}

def init() do
Expand All @@ -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
Expand All @@ -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)

Check warning on line 56 in lib/arke_postgres.ex

View workflow job for this annotation

GitHub Actions / publish

variable "mod_group_list" is unused (there is a variable with the same name in the context, use the pin operator (^) to match on it or prefix this variable with underscore if it is not meant to be used)
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

Check warning on line 75 in lib/arke_postgres.ex

View workflow job for this annotation

GitHub Actions / publish

clauses with the same name and arity (number of arguments) should be grouped together, "defp check_group_module/4" was previously defined (lib/arke_postgres.ex:63)

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.")
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions lib/arke_postgres/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ defmodule ArkePostgres.Query do
"string",
"unit",
"link",
"dynamic",
"date",
"datetime",
"time"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -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"

Expand Down

0 comments on commit e22f339

Please sign in to comment.