From 063baca5e4f3a100c0d45dffb14e4968599ef43b Mon Sep 17 00:00:00 2001 From: Ivan Tashkinov Date: Thu, 14 Feb 2019 00:29:29 +0300 Subject: [#468] User UI for OAuth permissions restriction. Standardized storage format for `scopes` fields, updated usages. --- ...20190208131753_add_scope_to_o_auth_entities.exs | 11 -------- ...0190208131753_add_scopes_to_o_auth_entities.exs | 11 ++++++++ ...123318_data_migration_populate_o_auth_scope.exs | 29 ---------------------- ...23318_data_migration_populate_o_auth_scopes.exs | 28 +++++++++++++++++++++ ...3185503_change_apps_scopes_to_varchar_array.exs | 17 +++++++++++++ 5 files changed, 56 insertions(+), 40 deletions(-) delete mode 100644 priv/repo/migrations/20190208131753_add_scope_to_o_auth_entities.exs create mode 100644 priv/repo/migrations/20190208131753_add_scopes_to_o_auth_entities.exs delete mode 100644 priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scope.exs create mode 100644 priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scopes.exs create mode 100644 priv/repo/migrations/20190213185503_change_apps_scopes_to_varchar_array.exs (limited to 'priv') diff --git a/priv/repo/migrations/20190208131753_add_scope_to_o_auth_entities.exs b/priv/repo/migrations/20190208131753_add_scope_to_o_auth_entities.exs deleted file mode 100644 index 809e9ab22..000000000 --- a/priv/repo/migrations/20190208131753_add_scope_to_o_auth_entities.exs +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Pleroma.Repo.Migrations.AddScopeToOAuthEntities do - use Ecto.Migration - - def change do - for t <- [:oauth_authorizations, :oauth_tokens] do - alter table(t) do - add :scope, :string - end - end - end -end diff --git a/priv/repo/migrations/20190208131753_add_scopes_to_o_auth_entities.exs b/priv/repo/migrations/20190208131753_add_scopes_to_o_auth_entities.exs new file mode 100644 index 000000000..4efbebc4d --- /dev/null +++ b/priv/repo/migrations/20190208131753_add_scopes_to_o_auth_entities.exs @@ -0,0 +1,11 @@ +defmodule Pleroma.Repo.Migrations.AddScopeSToOAuthEntities do + use Ecto.Migration + + def change do + for t <- [:oauth_authorizations, :oauth_tokens] do + alter table(t) do + add :scopes, {:array, :string}, default: [], null: false + end + end + end +end diff --git a/priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scope.exs b/priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scope.exs deleted file mode 100644 index 722cd6cf9..000000000 --- a/priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scope.exs +++ /dev/null @@ -1,29 +0,0 @@ -defmodule Pleroma.Repo.Migrations.DataMigrationPopulateOAuthScope do - use Ecto.Migration - - require Ecto.Query - - alias Ecto.Query - alias Pleroma.Repo - alias Pleroma.Web.OAuth - alias Pleroma.Web.OAuth.{App, Authorization, Token} - - def up do - for app <- Repo.all(Query.from(app in App)) do - scopes = OAuth.parse_scopes(app.scopes) - scope = Enum.join(scopes, " ") - - Repo.update_all( - Query.from(auth in Authorization, where: auth.app_id == ^app.id), - set: [scope: scope] - ) - - Repo.update_all( - Query.from(token in Token, where: token.app_id == ^app.id), - set: [scope: scope] - ) - end - end - - def down, do: :noop -end diff --git a/priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scopes.exs b/priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scopes.exs new file mode 100644 index 000000000..30b10f56f --- /dev/null +++ b/priv/repo/migrations/20190209123318_data_migration_populate_o_auth_scopes.exs @@ -0,0 +1,28 @@ +defmodule Pleroma.Repo.Migrations.DataMigrationPopulateOAuthScopes do + use Ecto.Migration + + require Ecto.Query + + alias Ecto.Query + alias Pleroma.Repo + alias Pleroma.Web.OAuth + alias Pleroma.Web.OAuth.{App, Authorization, Token} + + def up do + for app <- Repo.all(Query.from(app in App)) do + scopes = OAuth.parse_scopes(app.scopes) + + Repo.update_all( + Query.from(auth in Authorization, where: auth.app_id == ^app.id), + set: [scopes: scopes] + ) + + Repo.update_all( + Query.from(token in Token, where: token.app_id == ^app.id), + set: [scopes: scopes] + ) + end + end + + def down, do: :noop +end diff --git a/priv/repo/migrations/20190213185503_change_apps_scopes_to_varchar_array.exs b/priv/repo/migrations/20190213185503_change_apps_scopes_to_varchar_array.exs new file mode 100644 index 000000000..72decd401 --- /dev/null +++ b/priv/repo/migrations/20190213185503_change_apps_scopes_to_varchar_array.exs @@ -0,0 +1,17 @@ +defmodule Pleroma.Repo.Migrations.ChangeAppsScopesToVarcharArray do + use Ecto.Migration + + @alter_apps_scopes "ALTER TABLE apps ALTER COLUMN scopes" + + def up do + execute "#{@alter_apps_scopes} TYPE varchar(255)[] USING string_to_array(scopes, ',')::varchar(255)[];" + execute "#{@alter_apps_scopes} SET DEFAULT ARRAY[]::character varying[];" + execute "#{@alter_apps_scopes} SET NOT NULL;" + end + + def down do + execute "#{@alter_apps_scopes} DROP NOT NULL;" + execute "#{@alter_apps_scopes} DROP DEFAULT;" + execute "#{@alter_apps_scopes} TYPE varchar(255) USING array_to_string(scopes, ',')::varchar(255);" + end +end -- cgit v1.2.3