|
| 1 | +# credo:disable-for-this-file Credo.Check.Design.AliasUsage |
| 2 | +if Code.ensure_loaded?(Igniter) do |
| 3 | + defmodule Mix.Tasks.AshAuthentication.Upgrade do |
| 4 | + @moduledoc false |
| 5 | + |
| 6 | + use Igniter.Mix.Task |
| 7 | + |
| 8 | + @impl Igniter.Mix.Task |
| 9 | + def info(_argv, _composing_task) do |
| 10 | + %Igniter.Mix.Task.Info{ |
| 11 | + # Groups allow for overlapping arguments for tasks by the same author |
| 12 | + # See the generators guide for more. |
| 13 | + group: :ash_authentication, |
| 14 | + # *other* dependencies to add |
| 15 | + # i.e `{:foo, "~> 2.0"}` |
| 16 | + adds_deps: [], |
| 17 | + # *other* dependencies to add and call their associated installers, if they exist |
| 18 | + # i.e `{:foo, "~> 2.0"}` |
| 19 | + installs: [], |
| 20 | + # An example invocation |
| 21 | + # example: __MODULE__.Docs.example(), |
| 22 | + example: "example", |
| 23 | + # a list of positional arguments, i.e `[:file]` |
| 24 | + positional: [:from, :to], |
| 25 | + # Other tasks your task composes using `Igniter.compose_task`, passing in the CLI argv |
| 26 | + # This ensures your option schema includes options from nested tasks |
| 27 | + composes: [], |
| 28 | + # `OptionParser` schema |
| 29 | + schema: [], |
| 30 | + # Default values for the options in the `schema` |
| 31 | + defaults: [], |
| 32 | + # CLI aliases |
| 33 | + aliases: [], |
| 34 | + # A list of options in the schema that are required |
| 35 | + required: [] |
| 36 | + } |
| 37 | + end |
| 38 | + |
| 39 | + @impl Igniter.Mix.Task |
| 40 | + def igniter(igniter) do |
| 41 | + positional = igniter.args.positional |
| 42 | + options = igniter.args.options |
| 43 | + |
| 44 | + upgrades = |
| 45 | + %{ |
| 46 | + "4.4.9" => [&fix_token_is_revoked_action/2] |
| 47 | + } |
| 48 | + |
| 49 | + # For each version that requires a change, add it to this map |
| 50 | + # Each key is a version that points at a list of functions that take an |
| 51 | + # igniter and options (i.e. flags or other custom options). |
| 52 | + # See the upgrades guide for more. |
| 53 | + Igniter.Upgrades.run(igniter, positional.from, positional.to, upgrades, |
| 54 | + custom_opts: options |
| 55 | + ) |
| 56 | + end |
| 57 | + |
| 58 | + def fix_token_is_revoked_action(igniter, _opts) do |
| 59 | + case find_all_token_resources(igniter) do |
| 60 | + {igniter, []} -> |
| 61 | + igniter |
| 62 | + |
| 63 | + {igniter, resources} -> |
| 64 | + Enum.reduce(resources, igniter, fn resource, igniter -> |
| 65 | + maybe_fix_is_revoked_action(igniter, resource) |
| 66 | + end) |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + defp find_all_token_resources(igniter) do |
| 71 | + Igniter.Project.Module.find_all_matching_modules(igniter, fn _module, zipper -> |
| 72 | + with {:ok, zipper} <- Igniter.Code.Module.move_to_use(zipper, Ash.Resource), |
| 73 | + {:ok, zipper} <- Igniter.Code.Function.move_to_nth_argument(zipper, 1), |
| 74 | + {:ok, zipper} <- Igniter.Code.Keyword.get_key(zipper, :extensions) do |
| 75 | + if Igniter.Code.List.list?(zipper) do |
| 76 | + match?( |
| 77 | + {:ok, _}, |
| 78 | + Igniter.Code.List.move_to_list_item( |
| 79 | + zipper, |
| 80 | + &Igniter.Code.Common.nodes_equal?(&1, AshAuthentication.TokenResource) |
| 81 | + ) |
| 82 | + ) |
| 83 | + else |
| 84 | + Igniter.Code.Common.nodes_equal?(zipper, AshAuthentication.TokenResource) |
| 85 | + end |
| 86 | + end |
| 87 | + end) |
| 88 | + end |
| 89 | + |
| 90 | + defp maybe_fix_is_revoked_action(igniter, resource) do |
| 91 | + Igniter.Project.Module.find_and_update_module!(igniter, resource, fn zipper -> |
| 92 | + with {:ok, action_zipper} <- move_to_action(zipper, :action, :revoked?), |
| 93 | + {:ok, zipper} <- Igniter.Code.Common.move_to_do_block(action_zipper), |
| 94 | + {:ok, zipper} <- remove_argument_option(zipper, :token, :allow_nil?), |
| 95 | + {:ok, zipper} <- remove_argument_option(zipper, :jti, :allow_nil?) do |
| 96 | + add_action_return_type(zipper, :boolean) |
| 97 | + else |
| 98 | + :error -> {:ok, zipper} |
| 99 | + end |
| 100 | + end) |
| 101 | + end |
| 102 | + |
| 103 | + defp move_to_action(zipper, type, name) do |
| 104 | + Igniter.Code.Function.move_to_function_call( |
| 105 | + zipper, |
| 106 | + type, |
| 107 | + 2, |
| 108 | + &Igniter.Code.Function.argument_equals?(&1, 0, name) |
| 109 | + ) |
| 110 | + end |
| 111 | + |
| 112 | + defp add_action_return_type(zipper, type) do |
| 113 | + zipper = Sourceror.Zipper.top(zipper) |
| 114 | + |
| 115 | + with {:ok, zipper} <- move_to_action(zipper, :action, :revoked?), |
| 116 | + {:ok, zipper} <- Igniter.Code.Function.move_to_nth_argument(zipper, 1) do |
| 117 | + {:ok, |
| 118 | + Sourceror.Zipper.insert_left( |
| 119 | + zipper, |
| 120 | + quote do |
| 121 | + unquote(type) |
| 122 | + end |
| 123 | + )} |
| 124 | + end |
| 125 | + end |
| 126 | + |
| 127 | + defp remove_argument_option(zipper, argument_name, option) do |
| 128 | + with {:ok, zipper} <- |
| 129 | + Igniter.Code.Function.move_to_function_call( |
| 130 | + zipper, |
| 131 | + :argument, |
| 132 | + 3, |
| 133 | + &Igniter.Code.Function.argument_equals?(&1, 0, argument_name) |
| 134 | + ), |
| 135 | + {:ok, zipper} <- Igniter.Code.Function.move_to_nth_argument(zipper, 2) do |
| 136 | + if Igniter.Code.List.find_list_item_index( |
| 137 | + zipper, |
| 138 | + &Igniter.Code.Tuple.elem_equals?(&1, 0, :do) |
| 139 | + ) do |
| 140 | + Igniter.Code.Common.within(zipper, fn zipper -> |
| 141 | + {:ok, |
| 142 | + Igniter.Code.Common.remove( |
| 143 | + zipper, |
| 144 | + &Igniter.Code.Function.function_call?(&1, option, 1) |
| 145 | + )} |
| 146 | + end) |
| 147 | + else |
| 148 | + Igniter.Code.List.remove_from_list( |
| 149 | + zipper, |
| 150 | + &Igniter.Code.Tuple.elem_equals?(&1, 0, option) |
| 151 | + ) |
| 152 | + end |
| 153 | + end |
| 154 | + end |
| 155 | + end |
| 156 | +else |
| 157 | + defmodule Mix.Tasks.AshAuthentication.Upgrade do |
| 158 | + @moduledoc false |
| 159 | + |
| 160 | + use Mix.Task |
| 161 | + |
| 162 | + def run(_argv) do |
| 163 | + Mix.shell().error(""" |
| 164 | + The task 'ash_authentication.upgrade' requires igniter. Please install igniter and try again. |
| 165 | +
|
| 166 | + For more information, see: https://hexdocs.pm/igniter/readme.html#installation |
| 167 | + """) |
| 168 | + |
| 169 | + exit({:shutdown, 1}) |
| 170 | + end |
| 171 | + end |
| 172 | +end |
0 commit comments