2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-14 13:30:23 +08:00
|
|
|
require "base64"
|
2017-05-11 06:16:57 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
class Admin::ThemesController < Admin::AdminController
|
2023-03-23 20:01:04 +08:00
|
|
|
MAX_REMOTE_LENGTH = 10_000
|
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
skip_before_action :check_xhr, only: %i[show preview export]
|
2022-09-30 02:00:20 +08:00
|
|
|
before_action :ensure_admin
|
2017-04-15 01:35:12 +08:00
|
|
|
|
|
|
|
def preview
|
2019-04-11 13:56:43 +08:00
|
|
|
theme = Theme.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless theme
|
|
|
|
|
|
|
|
redirect_to path("/?preview_theme_id=#{theme.id}")
|
2017-04-15 01:35:12 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
def upload_asset
|
2020-07-27 08:23:54 +08:00
|
|
|
ban_in_allowlist_mode!
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2017-05-10 05:20:28 +08:00
|
|
|
path = params[:file].path
|
2017-12-18 07:40:10 +08:00
|
|
|
|
|
|
|
hijack do
|
|
|
|
File.open(path) do |file|
|
|
|
|
filename = params[:file]&.original_filename || File.basename(path)
|
2019-02-12 22:17:34 +08:00
|
|
|
upload = UploadCreator.new(file, filename, for_theme: true).create_for(theme_user.id)
|
2017-12-18 07:40:10 +08:00
|
|
|
if upload.errors.count > 0
|
|
|
|
render_json_error upload
|
|
|
|
else
|
|
|
|
render json: { upload_id: upload.id }, status: :created
|
|
|
|
end
|
2017-05-10 05:20:28 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-03-09 13:14:21 +08:00
|
|
|
def generate_key_pair
|
|
|
|
require "sshkey"
|
|
|
|
k = SSHKey.generate
|
2022-09-01 18:15:23 +08:00
|
|
|
Discourse.redis.setex("ssh_key_#{k.ssh_public_key}", 1.hour, k.private_key)
|
|
|
|
render json: { public_key: k.ssh_public_key }
|
2018-03-09 13:14:21 +08:00
|
|
|
end
|
|
|
|
|
2019-12-12 02:50:23 +08:00
|
|
|
THEME_CONTENT_TYPES ||= %w[
|
|
|
|
application/gzip
|
|
|
|
application/x-gzip
|
|
|
|
application/x-zip-compressed
|
|
|
|
application/zip
|
2023-01-09 20:20:10 +08:00
|
|
|
]
|
2019-12-12 02:50:23 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def import
|
|
|
|
@theme = nil
|
2019-01-23 22:40:21 +08:00
|
|
|
if params[:theme] && params[:theme].content_type == "application/json"
|
2020-07-27 08:23:54 +08:00
|
|
|
ban_in_allowlist_mode!
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
# .dcstyle.json import. Deprecated, but still available to allow conversion
|
2017-12-14 18:40:14 +08:00
|
|
|
json = JSON.parse(params[:theme].read)
|
2017-04-12 22:52:52 +08:00
|
|
|
theme = json["theme"]
|
|
|
|
|
2021-12-02 00:58:13 +08:00
|
|
|
@theme = Theme.new(name: theme["name"], user_id: theme_user.id, auto_update: false)
|
2017-04-12 22:52:52 +08:00
|
|
|
theme["theme_fields"]&.each do |field|
|
2017-11-14 13:30:23 +08:00
|
|
|
if field["raw_upload"]
|
|
|
|
begin
|
|
|
|
tmp = Tempfile.new
|
|
|
|
tmp.binmode
|
|
|
|
file = Base64.decode64(field["raw_upload"])
|
|
|
|
tmp.write(file)
|
|
|
|
tmp.rewind
|
2019-02-12 22:17:34 +08:00
|
|
|
upload = UploadCreator.new(tmp, field["filename"]).create_for(theme_user.id)
|
2017-11-14 13:30:23 +08:00
|
|
|
field["upload_id"] = upload.id
|
|
|
|
ensure
|
|
|
|
tmp.unlink
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@theme.set_field(
|
|
|
|
target: field["target"],
|
|
|
|
name: field["name"],
|
|
|
|
value: field["value"],
|
|
|
|
type_id: field["type_id"],
|
|
|
|
upload_id: field["upload_id"],
|
|
|
|
)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
if @theme.save
|
|
|
|
log_theme_change(nil, @theme)
|
|
|
|
render json: @theme, status: :created
|
|
|
|
else
|
|
|
|
render json: @theme.errors, status: :unprocessable_entity
|
|
|
|
end
|
2020-06-03 11:19:42 +08:00
|
|
|
elsif remote = params[:remote]
|
2023-03-23 20:01:04 +08:00
|
|
|
if remote.length > MAX_REMOTE_LENGTH
|
|
|
|
error =
|
|
|
|
I18n.t("themes.import_error.not_allowed_theme", { repo: remote[0..MAX_REMOTE_LENGTH] })
|
|
|
|
return render_json_error(error, status: 422)
|
|
|
|
end
|
|
|
|
|
2021-04-20 19:28:59 +08:00
|
|
|
begin
|
|
|
|
guardian.ensure_allowed_theme_repo_import!(remote.strip)
|
|
|
|
rescue Discourse::InvalidAccess
|
|
|
|
render_json_error I18n.t("themes.import_error.not_allowed_theme", { repo: remote.strip }),
|
|
|
|
status: :forbidden
|
|
|
|
return
|
|
|
|
end
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2022-11-04 03:02:26 +08:00
|
|
|
hijack do
|
|
|
|
begin
|
|
|
|
branch = params[:branch] ? params[:branch] : nil
|
|
|
|
private_key =
|
|
|
|
params[:public_key] ? Discourse.redis.get("ssh_key_#{params[:public_key]}") : nil
|
|
|
|
if params[:public_key].present? && private_key.blank?
|
|
|
|
return render_json_error I18n.t("themes.import_error.ssh_key_gone")
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2022-09-01 18:15:23 +08:00
|
|
|
|
2022-11-02 00:33:17 +08:00
|
|
|
@theme =
|
|
|
|
RemoteTheme.import_theme(remote, theme_user, private_key: private_key, branch: branch)
|
|
|
|
render json: @theme, status: :created
|
2022-11-04 03:02:26 +08:00
|
|
|
rescue RemoteTheme::ImportError => e
|
|
|
|
if params[:force]
|
2023-01-21 02:52:49 +08:00
|
|
|
theme_name = params[:remote].gsub(/.git\z/, "").split("/").last
|
2022-11-04 03:02:26 +08:00
|
|
|
|
|
|
|
remote_theme = RemoteTheme.new
|
|
|
|
remote_theme.private_key = private_key
|
|
|
|
remote_theme.branch = params[:branch] ? params[:branch] : nil
|
|
|
|
remote_theme.remote_url = params[:remote]
|
|
|
|
remote_theme.save!
|
|
|
|
|
|
|
|
@theme = Theme.new(user_id: theme_user&.id || -1, name: theme_name)
|
|
|
|
@theme.remote_theme = remote_theme
|
|
|
|
@theme.save!
|
|
|
|
|
|
|
|
render json: @theme, status: :created
|
|
|
|
else
|
|
|
|
render_json_error e.message
|
|
|
|
end
|
2022-08-10 18:30:18 +08:00
|
|
|
end
|
2018-03-12 15:36:06 +08:00
|
|
|
end
|
2019-12-12 02:50:23 +08:00
|
|
|
elsif params[:bundle] ||
|
|
|
|
(params[:theme] && THEME_CONTENT_TYPES.include?(params[:theme].content_type))
|
2020-07-27 08:23:54 +08:00
|
|
|
ban_in_allowlist_mode!
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
# params[:bundle] used by theme CLI. params[:theme] used by admin UI
|
|
|
|
bundle = params[:bundle] || params[:theme]
|
2019-01-30 22:17:04 +08:00
|
|
|
theme_id = params[:theme_id]
|
2020-03-27 06:11:56 +08:00
|
|
|
update_components = params[:components]
|
2024-01-11 14:04:02 +08:00
|
|
|
run_migrations = !params[:skip_migrations]
|
|
|
|
|
2018-03-12 15:36:06 +08:00
|
|
|
begin
|
2021-12-02 00:58:13 +08:00
|
|
|
@theme =
|
|
|
|
RemoteTheme.update_zipped_theme(
|
|
|
|
bundle.path,
|
|
|
|
bundle.original_filename,
|
|
|
|
user: theme_user,
|
2024-01-11 14:04:02 +08:00
|
|
|
theme_id:,
|
|
|
|
update_components:,
|
|
|
|
run_migrations:,
|
2021-12-02 00:58:13 +08:00
|
|
|
)
|
2024-01-11 14:04:02 +08:00
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
log_theme_change(nil, @theme)
|
2018-03-12 15:36:06 +08:00
|
|
|
render json: @theme, status: :created
|
2019-01-23 22:40:21 +08:00
|
|
|
rescue RemoteTheme::ImportError => e
|
|
|
|
render_json_error e.message
|
2018-03-09 13:14:21 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
else
|
2019-01-28 19:51:14 +08:00
|
|
|
render_json_error I18n.t("themes.import_error.unknown_file_type"),
|
|
|
|
status: :unprocessable_entity
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
rescue Theme::SettingsMigrationError => err
|
|
|
|
render_json_error err.message
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def index
|
2021-04-27 19:30:29 +08:00
|
|
|
@themes = Theme.include_relations.order(:name)
|
2018-12-21 01:13:05 +08:00
|
|
|
@color_schemes = ColorScheme.all.includes(:theme, color_scheme_colors: :color_scheme).to_a
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
payload = {
|
2024-03-26 14:02:05 +08:00
|
|
|
themes: serialize_data(@themes, ThemeSerializer),
|
2017-04-12 22:52:52 +08:00
|
|
|
extras: {
|
2024-03-26 14:02:05 +08:00
|
|
|
color_schemes: serialize_data(@color_schemes, ColorSchemeSerializer),
|
2017-04-12 22:52:52 +08:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
respond_to { |format| format.json { render json: payload } }
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2020-07-27 08:23:54 +08:00
|
|
|
ban_in_allowlist_mode!
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
@theme =
|
|
|
|
Theme.new(
|
|
|
|
name: theme_params[:name],
|
2019-02-12 22:17:34 +08:00
|
|
|
user_id: theme_user.id,
|
2017-04-12 22:52:52 +08:00
|
|
|
user_selectable: theme_params[:user_selectable] || false,
|
2018-08-24 09:30:00 +08:00
|
|
|
color_scheme_id: theme_params[:color_scheme_id],
|
|
|
|
component: [true, "true"].include?(theme_params[:component]),
|
|
|
|
)
|
2017-04-12 22:52:52 +08:00
|
|
|
set_fields
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @theme.save
|
|
|
|
update_default_theme
|
|
|
|
log_theme_change(nil, @theme)
|
|
|
|
format.json { render json: @theme, status: :created }
|
|
|
|
else
|
|
|
|
format.json { render json: @theme.errors, status: :unprocessable_entity }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2021-04-27 19:30:29 +08:00
|
|
|
@theme = Theme.include_relations.find_by(id: params[:id])
|
2019-04-11 13:56:43 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:id) unless @theme
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
original_json = ThemeSerializer.new(@theme, root: false).to_json
|
2019-07-03 16:18:11 +08:00
|
|
|
disables_component = [false, "false"].include?(theme_params[:enabled])
|
|
|
|
enables_component = [true, "true"].include?(theme_params[:enabled])
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2020-11-16 20:44:09 +08:00
|
|
|
%i[name color_scheme_id user_selectable enabled auto_update].each do |field|
|
2019-05-07 09:27:05 +08:00
|
|
|
@theme.public_send("#{field}=", theme_params[field]) if theme_params.key?(field)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2023-06-23 02:57:39 +08:00
|
|
|
@theme.child_theme_ids = theme_params[:child_theme_ids] if theme_params.key?(:child_theme_ids)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2023-06-23 02:57:39 +08:00
|
|
|
@theme.parent_theme_ids = theme_params[:parent_theme_ids] if theme_params.key?(
|
|
|
|
:parent_theme_ids,
|
|
|
|
)
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
set_fields
|
2018-03-05 08:04:23 +08:00
|
|
|
update_settings
|
2019-01-17 19:46:11 +08:00
|
|
|
update_translations
|
2018-08-24 09:30:00 +08:00
|
|
|
handle_switch
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
@theme.remote_theme.update_remote_version if params[:theme][:remote_check]
|
|
|
|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
if params[:theme][:remote_update]
|
|
|
|
@theme.remote_theme.update_from_remote(raise_if_theme_save_fails: false)
|
|
|
|
else
|
|
|
|
@theme.save
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
respond_to do |format|
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
if @theme.errors.blank?
|
2017-04-12 22:52:52 +08:00
|
|
|
update_default_theme
|
|
|
|
|
2021-04-27 19:30:29 +08:00
|
|
|
@theme = Theme.include_relations.find(@theme.id)
|
2019-07-03 16:18:11 +08:00
|
|
|
|
|
|
|
if (!disables_component && !enables_component) || theme_params.keys.size > 1
|
|
|
|
log_theme_change(original_json, @theme)
|
|
|
|
end
|
|
|
|
log_theme_component_disabled if disables_component
|
|
|
|
log_theme_component_enabled if enables_component
|
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
format.json { render json: @theme, status: :ok }
|
2017-04-12 22:52:52 +08:00
|
|
|
else
|
2018-08-08 12:46:34 +08:00
|
|
|
format.json do
|
|
|
|
error = @theme.errors.full_messages.join(", ").presence
|
|
|
|
error = I18n.t("themes.bad_color_scheme") if @theme.errors[:color_scheme].present?
|
|
|
|
error ||= I18n.t("themes.other_error")
|
2017-04-18 04:55:53 +08:00
|
|
|
|
|
|
|
render json: { errors: [error] }, status: :unprocessable_entity
|
2018-08-08 12:46:34 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
end
|
2019-02-05 21:49:16 +08:00
|
|
|
rescue RemoteTheme::ImportError => e
|
|
|
|
render_json_error e.message
|
FEATURE: Theme settings migrations (#24071)
This commit introduces a new feature that allows theme developers to manage the transformation of theme settings over time. Similar to Rails migrations, the theme settings migration system enables developers to write and execute migrations for theme settings, ensuring a smooth transition when changes are required in the format or structure of setting values.
Example use cases for the theme settings migration system:
1. Renaming a theme setting.
2. Changing the data type of a theme setting (e.g., transforming a string setting containing comma-separated values into a proper list setting).
3. Altering the format of data stored in a theme setting.
All of these use cases and more are now possible while preserving theme setting values for sites that have already modified their theme settings.
Usage:
1. Create a top-level directory called `migrations` in your theme/component, and then within the `migrations` directory create another directory called `settings`.
2. Inside the `migrations/settings` directory, create a JavaScript file using the format `XXXX-some-name.js`, where `XXXX` is a unique 4-digit number, and `some-name` is a descriptor of your choice that describes the migration.
3. Within the JavaScript file, define and export (as the default) a function called `migrate`. This function will receive a `Map` object and must also return a `Map` object (it's acceptable to return the same `Map` object that the function received).
4. The `Map` object received by the `migrate` function will include settings that have been overridden or changed by site administrators. Settings that have never been changed from the default will not be included.
5. The keys and values contained in the `Map` object that the `migrate` function returns will replace all the currently changed settings of the theme.
6. Migrations are executed in numerical order based on the XXXX segment in the migration filenames. For instance, `0001-some-migration.js` will be executed before `0002-another-migration.js`.
Here's a complete example migration script that renames a setting from `setting_with_old_name` to `setting_with_new_name`:
```js
// File name: 0001-rename-setting.js
export default function migrate(settings) {
if (settings.has("setting_with_old_name")) {
settings.set("setting_with_new_name", settings.get("setting_with_old_name"));
}
return settings;
}
```
Internal topic: t/109980
2023-11-02 13:10:15 +08:00
|
|
|
rescue Theme::SettingsMigrationError => e
|
|
|
|
render_json_error e.message
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2019-04-11 13:56:43 +08:00
|
|
|
@theme = Theme.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless @theme
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
StaffActionLogger.new(current_user).log_theme_destroy(@theme)
|
|
|
|
@theme.destroy
|
|
|
|
|
|
|
|
respond_to { |format| format.json { head :no_content } }
|
|
|
|
end
|
|
|
|
|
2023-10-09 05:35:53 +08:00
|
|
|
def bulk_destroy
|
|
|
|
themes = Theme.where(id: params[:theme_ids])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless themes.present?
|
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
themes.each { |theme| StaffActionLogger.new(current_user).log_theme_destroy(theme) }
|
|
|
|
themes.destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to { |format| format.json { head :no_content } }
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def show
|
2021-04-27 19:30:29 +08:00
|
|
|
@theme = Theme.include_relations.find_by(id: params[:id])
|
2019-04-11 13:56:43 +08:00
|
|
|
raise Discourse::InvalidParameters.new(:id) unless @theme
|
|
|
|
|
2024-03-26 14:02:05 +08:00
|
|
|
render_serialized(@theme, ThemeSerializer)
|
2019-01-23 22:40:21 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
def export
|
2019-04-11 13:56:43 +08:00
|
|
|
@theme = Theme.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless @theme
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2019-10-03 21:19:35 +08:00
|
|
|
exporter = ThemeStore::ZipExporter.new(@theme)
|
2019-01-23 22:40:21 +08:00
|
|
|
file_path = exporter.package_filename
|
2019-07-18 20:34:48 +08:00
|
|
|
|
2019-01-23 22:40:21 +08:00
|
|
|
headers["Content-Length"] = File.size(file_path).to_s
|
|
|
|
send_data File.read(file_path),
|
|
|
|
filename: File.basename(file_path),
|
2019-11-20 23:19:16 +08:00
|
|
|
content_type: "application/zip"
|
2019-01-23 22:40:21 +08:00
|
|
|
ensure
|
|
|
|
exporter.cleanup!
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
|
|
|
|
2024-03-19 00:00:28 +08:00
|
|
|
def get_translations
|
|
|
|
params.require(:locale)
|
|
|
|
unless I18n.available_locales.include?(params[:locale].to_sym)
|
|
|
|
raise Discourse::InvalidParameters.new(:locale)
|
|
|
|
end
|
|
|
|
|
|
|
|
I18n.locale = params[:locale]
|
|
|
|
|
|
|
|
@theme = Theme.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless @theme
|
|
|
|
|
|
|
|
translations =
|
|
|
|
@theme.translations.map do |translation|
|
|
|
|
{ key: translation.key, value: translation.value, default: translation.default }
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: { translations: translations }, status: :ok
|
|
|
|
end
|
|
|
|
|
2019-06-22 01:49:14 +08:00
|
|
|
def update_single_setting
|
|
|
|
params.require("name")
|
|
|
|
@theme = Theme.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless @theme
|
|
|
|
|
|
|
|
setting_name = params[:name].to_sym
|
|
|
|
new_value = params[:value] || nil
|
|
|
|
|
2020-05-02 00:51:11 +08:00
|
|
|
previous_value = @theme.cached_settings[setting_name]
|
2019-06-22 01:49:14 +08:00
|
|
|
@theme.update_setting(setting_name, new_value)
|
|
|
|
@theme.save
|
|
|
|
|
|
|
|
log_theme_setting_change(setting_name, previous_value, new_value)
|
|
|
|
|
2020-05-02 00:51:11 +08:00
|
|
|
updated_setting = @theme.cached_settings.select { |key, val| key == setting_name }
|
2019-06-22 01:49:14 +08:00
|
|
|
render json: updated_setting, status: :ok
|
|
|
|
end
|
|
|
|
|
2024-02-16 14:31:49 +08:00
|
|
|
def schema
|
|
|
|
raise Discourse::InvalidAccess if !SiteSetting.experimental_objects_type_for_theme_settings
|
|
|
|
end
|
|
|
|
|
2024-03-26 14:02:05 +08:00
|
|
|
def objects_setting_metadata
|
|
|
|
raise Discourse::InvalidAccess if !SiteSetting.experimental_objects_type_for_theme_settings
|
|
|
|
|
|
|
|
theme = Theme.find_by(id: params[:id])
|
|
|
|
raise Discourse::InvalidParameters.new(:id) unless theme
|
|
|
|
|
|
|
|
theme_setting = theme.settings[params[:setting_name].to_sym]
|
|
|
|
raise Discourse::InvalidParameters.new(:setting_name) unless theme_setting
|
|
|
|
|
|
|
|
render_serialized(theme_setting, ThemeObjectsSettingMetadataSerializer, root: false)
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
private
|
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
def ban_in_allowlist_mode!
|
2021-10-29 23:46:52 +08:00
|
|
|
raise Discourse::InvalidAccess if !Theme.allowed_remote_theme_ids.nil?
|
2020-06-03 11:19:42 +08:00
|
|
|
end
|
|
|
|
|
2020-11-13 23:57:49 +08:00
|
|
|
def ban_for_remote_theme!
|
2020-12-10 03:41:42 +08:00
|
|
|
raise Discourse::InvalidAccess if @theme.remote_theme&.is_git?
|
2020-11-13 23:57:49 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def update_default_theme
|
|
|
|
if theme_params.key?(:default)
|
2017-09-28 02:05:31 +08:00
|
|
|
is_default = theme_params[:default].to_s == "true"
|
2018-07-12 12:18:21 +08:00
|
|
|
if @theme.id == SiteSetting.default_theme_id && !is_default
|
2017-04-12 22:52:52 +08:00
|
|
|
Theme.clear_default!
|
2017-09-28 02:05:31 +08:00
|
|
|
elsif is_default
|
2017-04-12 22:52:52 +08:00
|
|
|
@theme.set_default!
|
|
|
|
end
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
|
|
|
def theme_params
|
|
|
|
@theme_params ||=
|
|
|
|
begin
|
|
|
|
# deep munge is a train wreck, work around it for now
|
|
|
|
params[:theme][:child_theme_ids] ||= [] if params[:theme].key?(:child_theme_ids)
|
2019-11-28 13:19:01 +08:00
|
|
|
params[:theme][:parent_theme_ids] ||= [] if params[:theme].key?(:parent_theme_ids)
|
2018-06-07 13:28:18 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
params.require(:theme).permit(
|
|
|
|
:name,
|
|
|
|
:color_scheme_id,
|
|
|
|
:default,
|
2017-05-10 05:20:28 +08:00
|
|
|
:user_selectable,
|
2018-08-24 09:30:00 +08:00
|
|
|
:component,
|
2019-07-03 16:18:11 +08:00
|
|
|
:enabled,
|
2020-11-16 20:44:09 +08:00
|
|
|
:auto_update,
|
2024-03-19 00:00:28 +08:00
|
|
|
:locale,
|
2017-05-10 05:20:28 +08:00
|
|
|
settings: {
|
|
|
|
},
|
2019-01-17 19:46:11 +08:00
|
|
|
translations: {
|
|
|
|
},
|
2017-05-10 05:20:28 +08:00
|
|
|
theme_fields: %i[name target value upload_id type_id],
|
2019-11-28 13:19:01 +08:00
|
|
|
child_theme_ids: [],
|
|
|
|
parent_theme_ids: [],
|
2017-05-10 05:20:28 +08:00
|
|
|
)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def set_fields
|
|
|
|
return unless fields = theme_params[:theme_fields]
|
2018-06-07 13:28:18 +08:00
|
|
|
|
2020-07-27 08:23:54 +08:00
|
|
|
ban_in_allowlist_mode!
|
2020-11-13 23:57:49 +08:00
|
|
|
ban_for_remote_theme!
|
2020-06-03 11:19:42 +08:00
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
fields.each do |field|
|
2017-05-10 05:20:28 +08:00
|
|
|
@theme.set_field(
|
|
|
|
target: field[:target],
|
|
|
|
name: field[:name],
|
|
|
|
value: field[:value],
|
2017-11-14 13:30:23 +08:00
|
|
|
type_id: field[:type_id],
|
|
|
|
upload_id: field[:upload_id],
|
2018-06-07 13:28:18 +08:00
|
|
|
)
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
|
2018-03-05 08:04:23 +08:00
|
|
|
def update_settings
|
|
|
|
return unless target_settings = theme_params[:settings]
|
|
|
|
|
|
|
|
target_settings.each_pair do |setting_name, new_value|
|
|
|
|
@theme.update_setting(setting_name.to_sym, new_value)
|
|
|
|
end
|
2018-06-07 13:28:18 +08:00
|
|
|
end
|
2018-03-05 08:04:23 +08:00
|
|
|
|
2019-01-17 19:46:11 +08:00
|
|
|
def update_translations
|
|
|
|
return unless target_translations = theme_params[:translations]
|
|
|
|
|
2024-03-19 00:00:28 +08:00
|
|
|
locale = theme_params[:locale].presence
|
|
|
|
if locale
|
|
|
|
unless I18n.available_locales.include?(locale.to_sym)
|
|
|
|
raise Discourse::InvalidParameters.new(:locale)
|
|
|
|
end
|
|
|
|
I18n.locale = locale
|
|
|
|
end
|
|
|
|
|
2019-01-17 19:46:11 +08:00
|
|
|
target_translations.each_pair do |translation_key, new_value|
|
|
|
|
@theme.update_translation(translation_key, new_value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-12 22:52:52 +08:00
|
|
|
def log_theme_change(old_record, new_record)
|
|
|
|
StaffActionLogger.new(current_user).log_theme_change(old_record, new_record)
|
|
|
|
end
|
|
|
|
|
2019-06-22 01:49:14 +08:00
|
|
|
def log_theme_setting_change(setting_name, previous_value, new_value)
|
|
|
|
StaffActionLogger.new(current_user).log_theme_setting_change(
|
|
|
|
setting_name,
|
|
|
|
previous_value,
|
|
|
|
new_value,
|
|
|
|
@theme,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2019-07-03 16:18:11 +08:00
|
|
|
def log_theme_component_disabled
|
|
|
|
StaffActionLogger.new(current_user).log_theme_component_disabled(@theme)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_theme_component_enabled
|
|
|
|
StaffActionLogger.new(current_user).log_theme_component_enabled(@theme)
|
|
|
|
end
|
|
|
|
|
2018-08-24 09:30:00 +08:00
|
|
|
def handle_switch
|
|
|
|
param = theme_params[:component]
|
|
|
|
if param.to_s == "false" && @theme.component?
|
2021-01-12 06:53:04 +08:00
|
|
|
if @theme.id == SiteSetting.default_theme_id
|
|
|
|
raise Discourse::InvalidParameters.new(:component)
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2018-08-24 09:30:00 +08:00
|
|
|
@theme.switch_to_theme!
|
|
|
|
elsif param.to_s == "true" && !@theme.component?
|
2021-01-12 06:53:04 +08:00
|
|
|
if @theme.id == SiteSetting.default_theme_id
|
|
|
|
raise Discourse::InvalidParameters.new(:component)
|
2023-01-09 20:20:10 +08:00
|
|
|
end
|
2018-08-24 09:30:00 +08:00
|
|
|
@theme.switch_to_component!
|
|
|
|
end
|
|
|
|
end
|
2019-02-12 22:17:34 +08:00
|
|
|
|
|
|
|
# Overridden by theme-creator plugin
|
|
|
|
def theme_user
|
|
|
|
current_user
|
|
|
|
end
|
2017-04-12 22:52:52 +08:00
|
|
|
end
|