camaleon_cms 2.9.1 → 2.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +29 -9
- data/Rakefile +3 -16
- data/app/apps/plugins/attack/admin_controller.rb +9 -6
- data/app/apps/plugins/attack/attack_helper.rb +35 -25
- data/app/apps/plugins/authoring_post/authoring_post_helper.rb +23 -16
- data/app/apps/plugins/front_cache/admin_controller.rb +16 -10
- data/app/apps/plugins/front_cache/config/initializer.rb +1 -1
- data/app/apps/plugins/front_cache/front_cache_helper.rb +57 -24
- data/app/apps/plugins/visibility_post/config/config.json +1 -0
- data/app/apps/plugins/visibility_post/config/routes_front.txt +1 -0
- data/app/apps/plugins/visibility_post/front_controller.rb +23 -0
- data/app/apps/plugins/visibility_post/visibility_post_helper.rb +141 -70
- data/app/apps/themes/camaleon_first/main_helper.rb +18 -12
- data/app/apps/themes/default/assets/css/style.css.scss +24 -0
- data/app/apps/themes/default/assets/genericons/genericons.css +3 -8
- data/app/apps/themes/default/default_helper.rb +2 -1
- data/app/apps/themes/default/views/category.html.erb +1 -1
- data/app/apps/themes/default/views/layouts/index.html.erb +3 -1
- data/app/apps/themes/default/views/post_tag.html.erb +1 -1
- data/app/apps/themes/default/views/post_type.html.erb +1 -1
- data/app/apps/themes/default/views/search.html.erb +1 -1
- data/app/apps/themes/new/assets/css/styles.css.scss +24 -0
- data/app/apps/themes/new/custom_helper.rb +5 -9
- data/app/apps/themes/new/views/category.html.erb +1 -1
- data/app/apps/themes/new/views/layouts/_header.html.erb +3 -1
- data/app/apps/themes/new/views/post_tag.html.erb +1 -1
- data/app/apps/themes/new/views/post_type.html.erb +1 -1
- data/app/apps/themes/new/views/search.html.erb +1 -1
- data/app/assets/javascripts/camaleon_cms/admin/_data.js +16 -0
- data/app/assets/javascripts/camaleon_cms/admin/_post.js +5 -2
- data/app/assets/javascripts/camaleon_cms/admin/nav_menu.js +14 -3
- data/app/assets/javascripts/camaleon_cms/admin/uploader/_media_manager.js +5 -0
- data/app/controllers/camaleon_cms/admin/appearances/nav_menus_controller.rb +29 -10
- data/app/controllers/camaleon_cms/admin/appearances/themes_controller.rb +8 -5
- data/app/controllers/camaleon_cms/admin/appearances/widgets/assign_controller.rb +9 -2
- data/app/controllers/camaleon_cms/admin/appearances/widgets/main_controller.rb +4 -3
- data/app/controllers/camaleon_cms/admin/appearances/widgets/sidebar_controller.rb +2 -2
- data/app/controllers/camaleon_cms/admin/categories_controller.rb +27 -8
- data/app/controllers/camaleon_cms/admin/comments_controller.rb +0 -1
- data/app/controllers/camaleon_cms/admin/installers_controller.rb +32 -1
- data/app/controllers/camaleon_cms/admin/media_controller.rb +84 -19
- data/app/controllers/camaleon_cms/admin/plugins_controller.rb +1 -3
- data/app/controllers/camaleon_cms/admin/post_tags_controller.rb +15 -6
- data/app/controllers/camaleon_cms/admin/posts/drafts_controller.rb +40 -11
- data/app/controllers/camaleon_cms/admin/posts_controller.rb +58 -22
- data/app/controllers/camaleon_cms/admin/sessions_controller.rb +238 -45
- data/app/controllers/camaleon_cms/admin/settings/custom_fields_controller.rb +103 -18
- data/app/controllers/camaleon_cms/admin/settings/post_types_controller.rb +17 -5
- data/app/controllers/camaleon_cms/admin/settings/sites_controller.rb +33 -7
- data/app/controllers/camaleon_cms/admin/settings_controller.rb +11 -6
- data/app/controllers/camaleon_cms/admin/user_roles_controller.rb +9 -7
- data/app/controllers/camaleon_cms/admin/users_controller.rb +89 -18
- data/app/controllers/camaleon_cms/admin_controller.rb +144 -26
- data/app/controllers/camaleon_cms/apps/plugins_admin_controller.rb +5 -0
- data/app/controllers/camaleon_cms/camaleon_controller.rb +37 -92
- data/app/controllers/camaleon_cms/frontend_controller.rb +140 -70
- data/app/controllers/concerns/camaleon_cms/admin/custom_fields_concern.rb +44 -0
- data/app/controllers/concerns/camaleon_cms/frontend_concern.rb +46 -17
- data/app/controllers/concerns/camaleon_cms/frontend_visited_state_concern.rb +72 -0
- data/app/controllers/concerns/camaleon_cms/hook_lifecycle_concern.rb +104 -0
- data/app/controllers/concerns/camaleon_cms/request_context_concern.rb +113 -0
- data/app/controllers/concerns/camaleon_cms/runtime_admin_menu_concern.rb +237 -0
- data/app/controllers/concerns/camaleon_cms/runtime_captcha_image_concern.rb +13 -0
- data/app/controllers/concerns/camaleon_cms/runtime_html_content_concern.rb +143 -0
- data/app/controllers/concerns/camaleon_cms/runtime_shortcode_theme_concern.rb +246 -0
- data/app/controllers/concerns/camaleon_cms/runtime_state_concern.rb +13 -0
- data/app/controllers/concerns/camaleon_cms/runtime_uploader_concern.rb +54 -0
- data/app/controllers/concerns/camaleon_cms/session_captcha_runtime_concern.rb +19 -0
- data/app/controllers/concerns/camaleon_cms/session_runtime_concern.rb +40 -0
- data/app/decorators/camaleon_cms/application_decorator.rb +11 -8
- data/app/decorators/camaleon_cms/category_decorator.rb +3 -2
- data/app/decorators/camaleon_cms/post_comment_decorator.rb +13 -5
- data/app/decorators/camaleon_cms/post_decorator.rb +67 -51
- data/app/decorators/camaleon_cms/post_type_decorator.rb +1 -1
- data/app/decorators/camaleon_cms/site_decorator.rb +29 -23
- data/app/decorators/camaleon_cms/term_taxonomy_decorator.rb +21 -10
- data/app/decorators/camaleon_cms/theme_decorator.rb +3 -3
- data/app/decorators/camaleon_cms/user_decorator.rb +26 -5
- data/app/helpers/camaleon_cms/admin/application_helper.rb +21 -24
- data/app/helpers/camaleon_cms/admin/breadcrumb_helper.rb +1 -1
- data/app/helpers/camaleon_cms/admin/custom_fields_helper.rb +24 -23
- data/app/helpers/camaleon_cms/admin/menus_helper.rb +249 -139
- data/app/helpers/camaleon_cms/admin/post_type_helper.rb +51 -31
- data/app/helpers/camaleon_cms/camaleon_helper.rb +31 -39
- data/app/helpers/camaleon_cms/captcha_helper.rb +73 -53
- data/app/helpers/camaleon_cms/comment_helper.rb +87 -41
- data/app/helpers/camaleon_cms/content_helper.rb +13 -6
- data/app/helpers/camaleon_cms/email_helper.rb +7 -3
- data/app/helpers/camaleon_cms/frontend/content_select_helper.rb +34 -20
- data/app/helpers/camaleon_cms/frontend/nav_menu_helper.rb +144 -67
- data/app/helpers/camaleon_cms/frontend/seo_helper.rb +39 -28
- data/app/helpers/camaleon_cms/frontend/site_helper.rb +32 -12
- data/app/helpers/camaleon_cms/hooks_helper.rb +14 -4
- data/app/helpers/camaleon_cms/html_helper.rb +73 -38
- data/app/helpers/camaleon_cms/plugins_helper.rb +23 -24
- data/app/helpers/camaleon_cms/session_helper.rb +195 -39
- data/app/helpers/camaleon_cms/short_code_helper.rb +181 -85
- data/app/helpers/camaleon_cms/site_helper.rb +43 -9
- data/app/helpers/camaleon_cms/theme_helper.rb +35 -19
- data/app/helpers/camaleon_cms/uploader_helper.rb +19 -422
- data/app/helpers/camaleon_cms/user_roles_helper.rb +5 -4
- data/app/mailers/camaleon_cms/html_mailer.rb +4 -4
- data/app/models/camaleon_cms/ability.rb +65 -105
- data/app/models/camaleon_cms/category.rb +7 -5
- data/app/models/camaleon_cms/custom_field.rb +29 -6
- data/app/models/camaleon_cms/custom_field_group.rb +75 -21
- data/app/models/camaleon_cms/custom_fields_relationship.rb +133 -2
- data/app/models/camaleon_cms/media.rb +8 -2
- data/app/models/camaleon_cms/meta.rb +2 -2
- data/app/models/camaleon_cms/nav_menu.rb +6 -2
- data/app/models/camaleon_cms/nav_menu_item.rb +18 -8
- data/app/models/camaleon_cms/plugin.rb +4 -3
- data/app/models/camaleon_cms/post.rb +113 -27
- data/app/models/camaleon_cms/post_comment.rb +10 -6
- data/app/models/camaleon_cms/post_default.rb +42 -12
- data/app/models/camaleon_cms/post_relationship.rb +6 -4
- data/app/models/camaleon_cms/post_tag.rb +4 -3
- data/app/models/camaleon_cms/post_type.rb +22 -12
- data/app/models/camaleon_cms/site.rb +40 -28
- data/app/models/camaleon_cms/term_relationship.rb +10 -7
- data/app/models/camaleon_cms/term_taxonomy.rb +51 -5
- data/app/models/camaleon_cms/theme.rb +3 -3
- data/app/models/camaleon_cms/user.rb +17 -5
- data/app/models/camaleon_cms/user_role.rb +77 -4
- data/app/models/camaleon_cms/widget/assigned.rb +11 -6
- data/app/models/camaleon_cms/widget/main.rb +13 -7
- data/app/models/camaleon_cms/widget/sidebar.rb +3 -6
- data/app/models/camaleon_record.rb +123 -5
- data/app/models/concerns/camaleon_cms/categories_tags_for_posts.rb +20 -38
- data/app/models/concerns/camaleon_cms/common_relationships.rb +18 -5
- data/app/models/concerns/camaleon_cms/custom_fields_read.rb +115 -58
- data/app/models/concerns/camaleon_cms/metas.rb +32 -7
- data/app/models/concerns/camaleon_cms/normalize_attrs.rb +2 -8
- data/app/models/concerns/camaleon_cms/site_default_settings.rb +120 -16
- data/app/models/concerns/camaleon_cms/user_methods.rb +44 -19
- data/app/models/current_request.rb +23 -0
- data/app/uploaders/camaleon_cms_aws_uploader.rb +39 -11
- data/app/uploaders/camaleon_cms_local_uploader.rb +76 -19
- data/app/uploaders/camaleon_cms_uploader.rb +23 -10
- data/app/validators/camaleon_cms/post_uniq_validator.rb +21 -8
- data/app/validators/camaleon_cms/uniq_validator.rb +1 -1
- data/app/validators/camaleon_cms/user_url_validator.rb +215 -36
- data/app/views/camaleon_cms/admin/appearances/nav_menus/_left_menu_items.html.erb +2 -2
- data/app/views/camaleon_cms/admin/appearances/widgets/main/form.html.erb +1 -1
- data/app/views/camaleon_cms/admin/categories/index.html.erb +3 -3
- data/app/views/camaleon_cms/admin/comments/index.html.erb +2 -2
- data/app/views/camaleon_cms/admin/comments/list.html.erb +1 -1
- data/app/views/camaleon_cms/admin/installers/form.html.erb +5 -0
- data/app/views/camaleon_cms/admin/installers/welcome.html.erb +30 -6
- data/app/views/camaleon_cms/admin/plugins/_plugins_list.html.erb +2 -2
- data/app/views/camaleon_cms/admin/post_tags/index.html.erb +3 -3
- data/app/views/camaleon_cms/admin/posts/_sidebar.html.erb +2 -2
- data/app/views/camaleon_cms/admin/posts/form.html.erb +3 -2
- data/app/views/camaleon_cms/admin/posts/index.html.erb +6 -6
- data/app/views/camaleon_cms/admin/search.html.erb +1 -1
- data/app/views/camaleon_cms/admin/sessions/back_to_parent.html.erb +39 -0
- data/app/views/camaleon_cms/admin/sessions/logout_confirm.html.erb +27 -0
- data/app/views/camaleon_cms/admin/settings/_email_settings.html.erb +4 -1
- data/app/views/camaleon_cms/admin/settings/custom_fields/_get_items.html.erb +1 -1
- data/app/views/camaleon_cms/admin/settings/custom_fields/_render.html.erb +42 -6
- data/app/views/camaleon_cms/admin/settings/custom_fields/_render_category_simple.html.erb +1 -1
- data/app/views/camaleon_cms/admin/settings/custom_fields/fields/_select_eval.html.erb +1 -1
- data/app/views/camaleon_cms/admin/settings/custom_fields/form.html.erb +5 -4
- data/app/views/camaleon_cms/admin/settings/custom_fields/index.html.erb +1 -1
- data/app/views/camaleon_cms/admin/settings/post_types/index.html.erb +1 -1
- data/app/views/camaleon_cms/admin/settings/shortcodes.html.erb +4 -4
- data/app/views/camaleon_cms/admin/settings/site.html.erb +1 -1
- data/app/views/camaleon_cms/admin/settings/sites/form.html.erb +1 -1
- data/app/views/camaleon_cms/admin/settings/sites/index.html.erb +1 -1
- data/app/views/camaleon_cms/admin/user_roles/form.html.erb +102 -7
- data/app/views/camaleon_cms/admin/user_roles/index.html.erb +1 -1
- data/app/views/camaleon_cms/admin/users/form.html.erb +8 -4
- data/app/views/camaleon_cms/admin/users/index.html.erb +2 -2
- data/app/views/camaleon_cms/default_theme/partials/_render_custom_field.html.erb +8 -2
- data/app/views/camaleon_cms/default_theme/sitemap.html.erb +1 -1
- data/app/views/camaleon_cms/default_theme/sitemap.xml.builder +1 -1
- data/app/views/layouts/camaleon_cms/admin/_flash_messages.html.erb +2 -2
- data/app/views/layouts/camaleon_cms/admin/_header.html.erb +1 -1
- data/config/initializers/action_view.rb +42 -5
- data/config/initializers/active_record_extension.rb +13 -27
- data/config/initializers/assets.rb +52 -23
- data/config/initializers/custom_initializers.rb +3 -3
- data/config/locales/camaleon_cms/admin/ar.yml +7 -2
- data/config/locales/camaleon_cms/admin/de.yml +7 -2
- data/config/locales/camaleon_cms/admin/en.yml +39 -2
- data/config/locales/camaleon_cms/admin/es.yml +7 -2
- data/config/locales/camaleon_cms/admin/fr.yml +7 -2
- data/config/locales/camaleon_cms/admin/it.yml +7 -2
- data/config/locales/camaleon_cms/admin/nl.yml +8 -2
- data/config/locales/camaleon_cms/admin/pt-BR.yml +7 -2
- data/config/locales/camaleon_cms/admin/pt.yml +7 -2
- data/config/locales/camaleon_cms/admin/ru.yml +7 -2
- data/config/locales/camaleon_cms/admin/uk.yml +7 -2
- data/config/locales/camaleon_cms/admin/zh-CH.yml +7 -2
- data/config/routes/admin.rb +33 -15
- data/config/routes/frontend.rb +21 -15
- data/db/migrate/20150611161134_post_table_into_utf8.rb +14 -14
- data/db/migrate/20150926095310_rename_column_posts.rb +3 -3
- data/db/migrate/20151212095328_add_confirm_token_to_users.rb +3 -3
- data/db/migrate/20160504155652_add_feature_to_posts.rb +1 -1
- data/db/migrate/20160504155653_move_first_name_of_users.rb +2 -2
- data/db/migrate/20160609121449_add_group_to_custom_field_values.rb +1 -1
- data/db/migrate/20161215202255_drop_user_relationship_table.rb +1 -1
- data/db/migrate/20180124132318_create_media.rb +1 -1
- data/db/migrate/20180704211100_adjust_field_length.rb +1 -1
- data/lib/camaleon_cms/assets_precompile.rb +86 -0
- data/lib/camaleon_cms/captcha_image_generation.rb +65 -0
- data/lib/camaleon_cms/content_security.rb +158 -0
- data/lib/camaleon_cms/engine.rb +47 -9
- data/lib/camaleon_cms/media_security_headers.rb +27 -0
- data/lib/camaleon_cms/setup_token.rb +69 -0
- data/lib/camaleon_cms/svg_content_checker.rb +123 -0
- data/lib/camaleon_cms/task_reporter.rb +14 -0
- data/lib/camaleon_cms/unsafe_markup.rb +207 -0
- data/lib/camaleon_cms/uploader_content_security.rb +248 -0
- data/lib/camaleon_cms/uploader_image_processing.rb +166 -0
- data/lib/camaleon_cms/uploader_path_security.rb +146 -0
- data/lib/camaleon_cms/uploader_pipeline.rb +350 -0
- data/lib/camaleon_cms/uploader_support.rb +86 -0
- data/lib/camaleon_cms/version.rb +1 -1
- data/lib/camaleon_cms.rb +11 -0
- data/lib/ext/hash.rb +28 -2
- data/lib/ext/string.rb +13 -8
- data/lib/ext/translator.rb +10 -2
- data/lib/generators/camaleon_cms/gem_plugin_template/app/controllers/plugins/my_plugin/admin_controller.rb +5 -1
- data/lib/generators/camaleon_cms/install_generator.rb +7 -4
- data/lib/generators/camaleon_cms/install_template/plugin_routes.rb +5 -12
- data/lib/generators/camaleon_cms/theme_generator.rb +5 -10
- data/lib/generators/camaleon_cms/theme_template/views/layouts/index.html.erb +3 -1
- data/lib/plugin_routes.rb +412 -368
- data/lib/tasks/camaleon_cms/camaleon_tasks.rake +2 -1
- data/lib/tasks/cross_site_field_groups.rake +74 -0
- data/lib/tasks/custom_fields_roles.rake +58 -0
- data/lib/tasks/orphaned_comments.rake +42 -0
- data/lib/tasks/private_upload_acls.rake +31 -0
- data/lib/tasks/site_custom_field_groups.rake +36 -0
- data/lib/tasks/unsafe_stored_content.rake +42 -0
- data/lib/tasks/unsafe_stored_uploads.rake +70 -0
- data/lib/tasks/user_field_groups.rake +27 -0
- metadata +185 -7
|
@@ -5,22 +5,37 @@ module CamaleonCms
|
|
|
5
5
|
before_action :set_post_data_params, only: %i[create update]
|
|
6
6
|
|
|
7
7
|
def index
|
|
8
|
+
# Security (audit 2026-08-11 M12): this action had no authorization, so any signed-in user
|
|
9
|
+
# could read a post type's JSON. Gate it on the same permission the post listing requires.
|
|
10
|
+
authorize! :posts, @post_type
|
|
8
11
|
render json: @post_type
|
|
9
12
|
end
|
|
10
13
|
|
|
11
14
|
def create
|
|
12
|
-
if
|
|
13
|
-
|
|
15
|
+
if @draft_parent_post.present?
|
|
16
|
+
# A draft buffer is an artifact of editing its parent post — authorize that post,
|
|
17
|
+
# and only ever touch the current user's own buffer
|
|
18
|
+
authorize! :update, @draft_parent_post
|
|
19
|
+
@post_draft = @post_type.posts.drafts.where(post_parent: @draft_parent_post.id,
|
|
20
|
+
user_id: cama_current_user.id).first
|
|
14
21
|
if @post_draft.present?
|
|
15
22
|
@post_draft.set_option('draft_status', @post_draft.status)
|
|
16
23
|
@post_draft.attributes = @post_data
|
|
17
24
|
end
|
|
25
|
+
else
|
|
26
|
+
authorize! :create_post, @post_type
|
|
27
|
+
end
|
|
28
|
+
if @post_draft.blank?
|
|
29
|
+
@post_draft = @post_type.posts.new(@post_data)
|
|
30
|
+
@post_draft.user_id = cama_current_user.id
|
|
18
31
|
end
|
|
19
|
-
@post_draft = @post_type.posts.new(@post_data) unless @post_draft.present?
|
|
20
32
|
r = { post: @post_draft, post_type: @post_type }
|
|
21
33
|
hooks_run('create_post_draft', r)
|
|
22
34
|
if @post_draft.save(validate: false)
|
|
23
|
-
|
|
35
|
+
# Security (audit M8): confine field values to slugs actually registered on the post type,
|
|
36
|
+
# like PostsController#save_post_with_fields -- raw params[:field_options] let a caller
|
|
37
|
+
# write custom_field_values with attacker-chosen slugs/ids/group numbers.
|
|
38
|
+
@post_draft.set_params(params[:meta], cama_permitted_field_options('PostType_Post'), @post_data[:keywords])
|
|
24
39
|
msg = { draft: { id: @post_draft.id },
|
|
25
40
|
_drafts_path: cama_admin_post_type_draft_path(@post_type.id, @post_draft) }
|
|
26
41
|
r = { post: @post_draft, post_type: @post_type }
|
|
@@ -33,18 +48,25 @@ module CamaleonCms
|
|
|
33
48
|
end
|
|
34
49
|
|
|
35
50
|
def update
|
|
36
|
-
@post_draft =
|
|
51
|
+
@post_draft = @post_type.posts.drafts.where(user_id: cama_current_user.id).find(params[:id])
|
|
52
|
+
authorize! :update, @post_draft.parent || @post_draft
|
|
37
53
|
@post_draft.attributes = @post_data
|
|
38
54
|
r = { post: @post_draft, post_type: @post_type }
|
|
39
55
|
hooks_run('update_post_draft', r)
|
|
40
56
|
if @post_draft.save(validate: false)
|
|
41
|
-
|
|
57
|
+
# Security (audit M8): confine field values to the post type's registered slugs (see #create).
|
|
58
|
+
@post_draft.set_params(params[:meta], cama_permitted_field_options('PostType_Post'), params[:options])
|
|
42
59
|
hooks_run('updated_post_draft', { post: @post_draft, post_type: @post_type })
|
|
43
60
|
msg = { draft: { id: @post_draft.id } }
|
|
44
61
|
else
|
|
45
62
|
msg = { error: @post_draft.errors.full_messages }
|
|
46
63
|
end
|
|
47
64
|
render json: msg
|
|
65
|
+
rescue ActiveRecord::RecordNotFound
|
|
66
|
+
# Another user's buffer answers exactly like a nonexistent draft id (same rescue
|
|
67
|
+
# behavior as PostsController#set_post) — no existence oracle for foreign buffers
|
|
68
|
+
flash[:error] = t('camaleon_cms.admin.post.message.error', post_type: @post_type.decorate.the_title)
|
|
69
|
+
redirect_to cama_admin_path
|
|
48
70
|
end
|
|
49
71
|
|
|
50
72
|
def destroy; end
|
|
@@ -52,12 +74,19 @@ module CamaleonCms
|
|
|
52
74
|
private
|
|
53
75
|
|
|
54
76
|
def set_post_data_params
|
|
55
|
-
post_data = params
|
|
56
|
-
|
|
57
|
-
|
|
77
|
+
post_data = params
|
|
78
|
+
.require(:post).permit(
|
|
79
|
+
:title, :slug, :content, :excerpt, :status, :comment_status, :visibility,
|
|
80
|
+
:visibility_value, :post_order, :published_at
|
|
81
|
+
).to_h
|
|
82
|
+
post_data.delete(:created_at) if params[:post][:created_at].blank?
|
|
83
|
+
post_data.delete(:updated_at) if params[:post][:updated_at].blank?
|
|
58
84
|
post_data[:status] = 'draft_child'
|
|
59
|
-
|
|
60
|
-
|
|
85
|
+
if action_name == 'create'
|
|
86
|
+
@draft_parent_post = current_site.posts.find_by(id: params[:post_id]) if params[:post_id].present?
|
|
87
|
+
# post_parent is create-only and always overwritten — never client-writable via post[post_parent]
|
|
88
|
+
post_data[:post_parent] = @draft_parent_post&.id
|
|
89
|
+
end
|
|
61
90
|
post_data[:data_tags] = params[:tags].to_s
|
|
62
91
|
post_data[:data_categories] = params[:categories] || []
|
|
63
92
|
@post_data = post_data
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
module CamaleonCms
|
|
2
2
|
module Admin
|
|
3
3
|
class PostsController < CamaleonCms::AdminController
|
|
4
|
+
include CamaleonCms::Admin::CustomFieldsConcern
|
|
5
|
+
|
|
4
6
|
add_breadcrumb I18n.t('camaleon_cms.admin.sidebar.contents')
|
|
7
|
+
|
|
5
8
|
before_action :set_post_type, except: [:ajax]
|
|
6
9
|
before_action :set_post, only: %w[show edit update destroy]
|
|
7
10
|
skip_before_action :admin_logged_actions, only: %i[trash restore destroy ajax], raise: false
|
|
@@ -9,19 +12,28 @@ module CamaleonCms
|
|
|
9
12
|
|
|
10
13
|
def index
|
|
11
14
|
authorize! :posts, @post_type
|
|
15
|
+
# Array/hash-typed query params (?q[]=x, ?s[]=published, ?taxonomy_id[]=1) have no meaning here
|
|
16
|
+
# and 500ed the action (Array#downcase / Array#to_sym / find(Array).decorate); treat them as absent.
|
|
17
|
+
%i[q s taxonomy taxonomy_id].each { |k| params[k] = nil unless params[k].nil? || params[k].is_a?(String) }
|
|
12
18
|
per_page = current_site.admin_per_page
|
|
13
19
|
posts_all = @post_type.posts.eager_load(:parent, :post_type)
|
|
14
20
|
if params[:taxonomy].present? && params[:taxonomy_id].present?
|
|
21
|
+
# Security (audit 2026-08-11 M11): keep the listing scoped to @post_type. The taxonomy is
|
|
22
|
+
# resolved within the authorized post type -- a foreign category/tag id raises RecordNotFound
|
|
23
|
+
# instead of rendering that taxonomy's title/edit URL in the breadcrumb (a name/existence
|
|
24
|
+
# oracle across the very boundary `authorize! :posts, @post_type` draws). The listing then
|
|
25
|
+
# intersects with the post type's posts, so a filter can only ever narrow the authorized set.
|
|
15
26
|
if params[:taxonomy] == 'category'
|
|
16
|
-
cat_owner =
|
|
17
|
-
|
|
27
|
+
cat_owner = @post_type.full_categories.find(params[:taxonomy_id]).decorate
|
|
28
|
+
# reorder(nil): the relation becomes an IN subquery, where its default-scope ORDER BY is dead sort work
|
|
29
|
+
posts_all = posts_all.where(id: cat_owner.posts.reorder(nil))
|
|
18
30
|
add_breadcrumb t('camaleon_cms.admin.post_type.category'), @post_type.the_admin_url('category')
|
|
19
31
|
add_breadcrumb cat_owner.the_title, cat_owner.the_edit_url
|
|
20
32
|
end
|
|
21
33
|
|
|
22
34
|
if params[:taxonomy] == 'post_tag'
|
|
23
|
-
tag_owner =
|
|
24
|
-
posts_all = tag_owner.posts
|
|
35
|
+
tag_owner = @post_type.post_tags.find(params[:taxonomy_id]).decorate
|
|
36
|
+
posts_all = posts_all.where(id: tag_owner.posts.reorder(nil))
|
|
25
37
|
add_breadcrumb t('camaleon_cms.admin.post_type.tags'), @post_type.the_admin_url('tag')
|
|
26
38
|
add_breadcrumb tag_owner.the_title, tag_owner.the_edit_url
|
|
27
39
|
end
|
|
@@ -36,10 +48,10 @@ module CamaleonCms
|
|
|
36
48
|
)
|
|
37
49
|
end
|
|
38
50
|
|
|
39
|
-
posts_all = posts_all
|
|
51
|
+
posts_all = cama_admin_visible_posts(posts_all, [@post_type])
|
|
40
52
|
|
|
41
53
|
@posts = posts_all
|
|
42
|
-
params[:s] = 'published'
|
|
54
|
+
params[:s] = 'published' if params[:s].blank?
|
|
43
55
|
@lists_tab = params[:s]
|
|
44
56
|
case params[:s]
|
|
45
57
|
when 'published', 'pending', 'trash'
|
|
@@ -50,8 +62,13 @@ module CamaleonCms
|
|
|
50
62
|
@posts = @posts.no_trash
|
|
51
63
|
end
|
|
52
64
|
|
|
53
|
-
@btns = {
|
|
54
|
-
|
|
65
|
+
@btns = {
|
|
66
|
+
published: "#{t('camaleon_cms.admin.post_type.published')} (#{posts_all.published.size})",
|
|
67
|
+
all: "#{t('camaleon_cms.admin.post_type.all')} (#{posts_all.no_trash.size})",
|
|
68
|
+
pending: "#{t('camaleon_cms.admin.post_type.pending')} (#{posts_all.pending.size})",
|
|
69
|
+
draft: "#{t('camaleon_cms.admin.post_type.draft')} (#{posts_all.drafts.size})",
|
|
70
|
+
trash: "#{t('camaleon_cms.admin.post_type.trash')} (#{posts_all.trash.size})"
|
|
71
|
+
}
|
|
55
72
|
per_page = 9_999_999 if @post_type.manage_hierarchy?
|
|
56
73
|
r = { posts: @posts, post_type: @post_type, btns: @btns, all_posts: posts_all, render: 'index',
|
|
57
74
|
per_page: per_page }
|
|
@@ -85,10 +102,7 @@ module CamaleonCms
|
|
|
85
102
|
r = { post: @post, post_type: @post_type }
|
|
86
103
|
hooks_run('create_post', r)
|
|
87
104
|
@post = r[:post]
|
|
88
|
-
if @post
|
|
89
|
-
@post.set_metas(params[:meta])
|
|
90
|
-
@post.set_field_values(params[:field_options])
|
|
91
|
-
@post.set_options(params[:options])
|
|
105
|
+
if save_post_with_fields(@post)
|
|
92
106
|
flash[:notice] = t('camaleon_cms.admin.post.message.created', post_type: @post_type.decorate.the_title)
|
|
93
107
|
r = { post: @post, post_type: @post_type }
|
|
94
108
|
hooks_run('created_post', r)
|
|
@@ -114,7 +128,7 @@ module CamaleonCms
|
|
|
114
128
|
if @post.draft_child? && @post.parent.present?
|
|
115
129
|
# This is a draft (as a child of the original post)
|
|
116
130
|
original_parent = @post.parent.parent
|
|
117
|
-
post_data[:post_parent] = original_parent
|
|
131
|
+
post_data[:post_parent] = original_parent&.id
|
|
118
132
|
@post = @post.parent
|
|
119
133
|
delete_drafts = true
|
|
120
134
|
elsif @post.draft?
|
|
@@ -125,12 +139,9 @@ module CamaleonCms
|
|
|
125
139
|
r = { post: @post, post_type: @post_type }
|
|
126
140
|
hooks_run('update_post', r)
|
|
127
141
|
@post = r[:post]
|
|
128
|
-
if @post
|
|
142
|
+
if save_post_with_fields(@post, post_data)
|
|
129
143
|
# delete drafts only on successful update operation
|
|
130
144
|
@post.drafts.destroy_all if delete_drafts
|
|
131
|
-
@post.set_metas(params[:meta])
|
|
132
|
-
@post.set_field_values(params[:field_options])
|
|
133
|
-
@post.set_options(params[:options])
|
|
134
145
|
hooks_run('updated_post', { post: @post, post_type: @post_type })
|
|
135
146
|
flash[:notice] = t('camaleon_cms.admin.post.message.updated', post_type: @post_type.decorate.the_title)
|
|
136
147
|
redirect_to action: :edit, id: @post.id
|
|
@@ -144,7 +155,7 @@ module CamaleonCms
|
|
|
144
155
|
authorize! :destroy, @post
|
|
145
156
|
@post.set_option('status_default', @post.status)
|
|
146
157
|
# @post.children.destroy_all unless @post.draft? TODO: why delete children?
|
|
147
|
-
@post.update_column(
|
|
158
|
+
@post.update_column(:status, 'trash') # rubocop:disable Rails/SkipsModelValidations
|
|
148
159
|
@post.update_extra_data
|
|
149
160
|
hooks_run('trashed_post', { post: @post, post_type: @post_type })
|
|
150
161
|
flash[:notice] = t('camaleon_cms.admin.post.message.trash', post_type: @post_type.decorate.the_title)
|
|
@@ -154,7 +165,9 @@ module CamaleonCms
|
|
|
154
165
|
def restore
|
|
155
166
|
@post = @post_type.posts.find(params[:post_id])
|
|
156
167
|
authorize! :update, @post
|
|
157
|
-
|
|
168
|
+
# rubocop:disable Rails/SkipsModelValidations
|
|
169
|
+
@post.update_column(:status, @post.options[:status_default] || 'pending')
|
|
170
|
+
# rubocop:enable Rails/SkipsModelValidations
|
|
158
171
|
@post.update_extra_data
|
|
159
172
|
hooks_run('restored_post', { post: @post, post_type: @post_type })
|
|
160
173
|
flash[:notice] = t('camaleon_cms.admin.post.message.restore', post_type: @post_type.decorate.the_title)
|
|
@@ -190,10 +203,29 @@ module CamaleonCms
|
|
|
190
203
|
|
|
191
204
|
private
|
|
192
205
|
|
|
206
|
+
# Persist the post together with its metas, field values and options atomically (audit M10).
|
|
207
|
+
# Before this, the parent was saved and its metas committed before set_field_values ran, so a
|
|
208
|
+
# field value the scan-and-reject gate refused (CustomFieldsRelationship RecordInvalid) left a
|
|
209
|
+
# half-applied post -- an orphan on create, skipped options, only a redirect explaining why.
|
|
210
|
+
# Wrapping the whole sequence in one transaction rolls the parent save back with the refused
|
|
211
|
+
# value, and the RecordInvalid propagates to AdminController's rescue_from (flash + redirect
|
|
212
|
+
# back) with nothing persisted. Returns true on success, false on a parent validation failure.
|
|
213
|
+
def save_post_with_fields(post, update_attrs = nil)
|
|
214
|
+
ActiveRecord::Base.transaction do
|
|
215
|
+
saved = update_attrs ? post.update(update_attrs) : post.save
|
|
216
|
+
raise ActiveRecord::Rollback unless saved
|
|
217
|
+
|
|
218
|
+
post.set_metas(params[:meta])
|
|
219
|
+
post.set_field_values(cama_permitted_field_options('PostType_Post'))
|
|
220
|
+
post.set_options(params[:options])
|
|
221
|
+
true
|
|
222
|
+
end
|
|
223
|
+
end
|
|
224
|
+
|
|
193
225
|
# define post type parent
|
|
194
226
|
def set_post_type
|
|
195
|
-
@post_type = current_site.post_types.
|
|
196
|
-
|
|
227
|
+
@post_type = current_site.post_types.find_by(id: params[:post_type_id])
|
|
228
|
+
if @post_type.blank?
|
|
197
229
|
flash[:error] = t('camaleon_cms.admin.request_error_message')
|
|
198
230
|
redirect_to cama_admin_path
|
|
199
231
|
return
|
|
@@ -213,7 +245,11 @@ module CamaleonCms
|
|
|
213
245
|
# return common params data for posts
|
|
214
246
|
# is_create: indicate if this info is for create a new post
|
|
215
247
|
def get_post_data(is_create = false)
|
|
216
|
-
post_data = params
|
|
248
|
+
post_data = params
|
|
249
|
+
.require(:post).permit(
|
|
250
|
+
:title, :slug, :content, :excerpt, :status, :comment_status, :post_parent, :visibility,
|
|
251
|
+
:visibility_value, :post_order, :published_at
|
|
252
|
+
).to_h
|
|
217
253
|
post_data[:user_id] = cama_current_user.id if is_create
|
|
218
254
|
post_data[:status] = 'pending' if post_data[:status] == 'published' && cannot?(:publish_post, @post_type)
|
|
219
255
|
post_data[:data_tags] = params[:tags].to_s
|
|
@@ -7,9 +7,31 @@ module CamaleonCms
|
|
|
7
7
|
before_action :verificate_register_permission, only: [:register]
|
|
8
8
|
layout 'camaleon_cms/login'
|
|
9
9
|
|
|
10
|
-
#
|
|
10
|
+
# Security (audit M6): logout acts only over POST, so a logout submitted with a stale CSRF token
|
|
11
|
+
# (a second tab that already reset the session, or a cached theme page) would otherwise render a
|
|
12
|
+
# raw 422 instead of ending the session. For logout alone, fall back to the GET confirmation so
|
|
13
|
+
# the user gets a fresh token and one extra click; every other action keeps the strict failure.
|
|
14
|
+
rescue_from ActionController::InvalidAuthenticityToken do |error|
|
|
15
|
+
raise error unless action_name == 'logout'
|
|
16
|
+
|
|
17
|
+
redirect_to cama_admin_logout_path
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# A precomputed bcrypt digest spent only to equalize login timing when the username does not exist,
|
|
21
|
+
# so a missing username is not distinguishable from a wrong password by response time (audit finding
|
|
22
|
+
# M14: username enumeration by login timing). Computed once at load, at this environment's cost.
|
|
23
|
+
TIMING_EQUALIZER_DIGEST = BCrypt::Password.create('cama-login-timing-equalizer').to_s.freeze
|
|
24
|
+
|
|
25
|
+
# Minimum interval between password-reset emails to the same account, so the forgot-password
|
|
26
|
+
# endpoint cannot be used to flood a known address (audit finding M13). Within the window a
|
|
27
|
+
# reset request is accepted (and answered identically) but sends no new email; the previously
|
|
28
|
+
# issued token stays valid for its 2h lifetime (see #forgot).
|
|
29
|
+
PASSWORD_RESET_EMAIL_COOLDOWN = 5.minutes
|
|
30
|
+
|
|
31
|
+
# you can pass return_to as a param (mysite.com/admin/login?return_to=my-url) and
|
|
32
|
+
# this will be used after user logged in
|
|
11
33
|
def login
|
|
12
|
-
return
|
|
34
|
+
return cama_safe_redirect(params[:return_to], cama_admin_dashboard_path) if signin?
|
|
13
35
|
|
|
14
36
|
cookies[:return_to] = params[:return_to] if params[:return_to].present?
|
|
15
37
|
@user ||= current_site.users.new
|
|
@@ -18,21 +40,28 @@ module CamaleonCms
|
|
|
18
40
|
end
|
|
19
41
|
|
|
20
42
|
def login_post
|
|
43
|
+
return if cama_halt_when_login_locked_out('login')
|
|
44
|
+
|
|
21
45
|
data_user = user_permit_data
|
|
22
|
-
|
|
46
|
+
# Custom class finder (not the dynamic finder): usernames are stored downcased,
|
|
47
|
+
# so the lookup must lower-case both sides regardless of DB collation.
|
|
48
|
+
@user = current_site.users.find_by_username(data_user[:username]) # rubocop:disable Rails/DynamicFindBy
|
|
23
49
|
captcha_validate = captcha_verify_if_under_attack('login')
|
|
24
50
|
r = { user: @user, params: params, password: data_user[:password], captcha_validate: captcha_validate,
|
|
25
51
|
stop_process: false }
|
|
26
52
|
hooks_run('user_before_login', r)
|
|
27
53
|
return if r[:stop_process] # permit to redirect for data completion
|
|
28
54
|
|
|
29
|
-
if captcha_validate && @user
|
|
55
|
+
if captcha_validate && cama_password_matches?(@user, data_user[:password])
|
|
30
56
|
# Email validation if is necessary
|
|
31
57
|
if @user.is_valid_email? || !current_site.need_validate_email?
|
|
32
58
|
cama_captcha_reset_attack('login')
|
|
33
59
|
r = { user: @user, redirect_to: params[:format] == 'json' ? false : nil }
|
|
34
60
|
hooks_run('after_login', r)
|
|
35
|
-
|
|
61
|
+
# An after_login hook may set r[:allow_external_redirect] to vouch for an off-site r[:redirect_to]
|
|
62
|
+
# (SSO/payment); without it the destination is same-host/allowlist-only. See login_user.
|
|
63
|
+
login_user(@user, params[:remember_me].present?, r[:redirect_to],
|
|
64
|
+
allow_external: r[:allow_external_redirect].present?)
|
|
36
65
|
render(json: flash.discard.to_hash) if params[:format] == 'json'
|
|
37
66
|
return
|
|
38
67
|
else
|
|
@@ -54,75 +83,161 @@ module CamaleonCms
|
|
|
54
83
|
end
|
|
55
84
|
|
|
56
85
|
def logout
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
86
|
+
# A request that is no longer authenticated has nothing to confirm and nothing a forged GET
|
|
87
|
+
# could end -- but it may still carry session leftovers (a stale impersonation stash, H6),
|
|
88
|
+
# so it goes through cama_logout_user's cleanup on any verb, exactly as before.
|
|
89
|
+
return cama_logout_user unless cama_sign_in?
|
|
90
|
+
|
|
91
|
+
# While impersonating, the ordinary Logout link must not silently hand the admin account back
|
|
92
|
+
# to whoever holds the session — returning to the parent now requires the admin's password
|
|
93
|
+
# (see #back_to_parent). `?full=1` forces a real logout of the impersonated session instead.
|
|
94
|
+
if session[:parent_auth_token].present? && params[:full].blank?
|
|
95
|
+
redirect_to cama_admin_back_to_parent_path
|
|
96
|
+
elsif request.post?
|
|
60
97
|
cama_logout_user
|
|
98
|
+
else
|
|
99
|
+
# Security (audit M6): logging out changes state, so only the POST above performs it —
|
|
100
|
+
# keyed on request.post?, not !request.get?, because Rails exempts HEAD from CSRF exactly
|
|
101
|
+
# like GET. The GET renders a confirmation instead of 404ing: frontend themes across the
|
|
102
|
+
# ecosystem link this path, and their visitors get one extra click, not a broken link.
|
|
103
|
+
prepare_logout_confirmation
|
|
104
|
+
# Force the HTML template/layout: the confirmation is HTML-only, so a non-HTML GET (an ajax
|
|
105
|
+
# `/admin/logout.json`) renders the same confirmation instead of raising MissingTemplate (500),
|
|
106
|
+
# still leaving the session untouched.
|
|
107
|
+
render :logout_confirm, formats: [:html], content_type: 'text/html'
|
|
61
108
|
end
|
|
62
109
|
end
|
|
63
110
|
|
|
111
|
+
# Re-authenticate the impersonating admin before restoring their session (H6 residual). Reachable
|
|
112
|
+
# only while an impersonation is active; a GET renders the confirmation form, a POST checks the
|
|
113
|
+
# admin's password and only then returns to the parent session.
|
|
114
|
+
def back_to_parent
|
|
115
|
+
return redirect_to(cama_admin_login_path) unless session[:parent_auth_token].present? && cama_sign_in?
|
|
116
|
+
|
|
117
|
+
@parent_user = cama_impersonation_parent_user
|
|
118
|
+
# A stash that no longer resolves to a user — or resolves to an account with no password
|
|
119
|
+
# digest, which #authenticate would raise on and which cannot prove the holder is the admin —
|
|
120
|
+
# cannot be returned to; fail closed and end the session.
|
|
121
|
+
return cama_logout_user if @parent_user.blank? || @parent_user.password_digest.blank?
|
|
122
|
+
|
|
123
|
+
@impersonated_user = cama_current_user
|
|
124
|
+
if request.post?
|
|
125
|
+
return if cama_halt_when_login_locked_out('back_to_parent')
|
|
126
|
+
|
|
127
|
+
# Failed guesses feed the login form's attack counter (same 'login' key), so this endpoint is
|
|
128
|
+
# not an unthrottled oracle for the admin password: past the threshold a captcha is also
|
|
129
|
+
# required, and because the parent stash lives in this same session, resetting the counter
|
|
130
|
+
# (new session / full logout) destroys the stash being attacked.
|
|
131
|
+
captcha_validate = captcha_verify_if_under_attack('login')
|
|
132
|
+
if captcha_validate && @parent_user.authenticate(params[:password].to_s)
|
|
133
|
+
cama_captcha_reset_attack('login')
|
|
134
|
+
return session_back_to_parent(cama_admin_dashboard_path)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
cama_captcha_increment_attack('login')
|
|
138
|
+
flash.now[:error] = if captcha_validate
|
|
139
|
+
t('camaleon_cms.admin.login.message.reauth_failed',
|
|
140
|
+
default: 'Incorrect password. Please try again.')
|
|
141
|
+
else
|
|
142
|
+
t('camaleon_cms.admin.login.message.invalid_caption')
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
render 'back_to_parent'
|
|
146
|
+
end
|
|
147
|
+
|
|
64
148
|
def forgot
|
|
65
149
|
@user = current_site.users.new
|
|
66
150
|
# get form reset password
|
|
67
|
-
if params[:h]
|
|
151
|
+
if params[:h].present?
|
|
152
|
+
# Look up by the DB column (a `where` clause is never shadowed by the Rails 7.1+
|
|
153
|
+
# has_secure_password method), scoped to the current site so a token cannot be redeemed
|
|
154
|
+
# against a site that does not own the account.
|
|
68
155
|
@user = current_site.users.where(password_reset_token: params[:h]).first
|
|
69
156
|
if @user.nil?
|
|
70
157
|
flash[:error] = t('camaleon_cms.admin.login.message.forgot_url_incorrect')
|
|
71
|
-
redirect_to cama_admin_forgot_path
|
|
72
|
-
|
|
73
|
-
|
|
158
|
+
return redirect_to cama_admin_forgot_path
|
|
159
|
+
end
|
|
160
|
+
# nil timestamp (or one past the window) is expired, not a NoMethodError.
|
|
161
|
+
if @user.password_reset_sent_at.blank? || @user.password_reset_sent_at < 2.hours.ago
|
|
74
162
|
flash[:error] = t('camaleon_cms.admin.login.message.forgot_expired')
|
|
75
|
-
redirect_to cama_admin_login_path
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
163
|
+
return redirect_to cama_admin_login_path
|
|
164
|
+
end
|
|
165
|
+
|
|
166
|
+
if params[:user].present?
|
|
167
|
+
# Blank-check the *permitted* password: permit drops a non-scalar value (e.g.
|
|
168
|
+
# `user[password][]=x`), and has_secure_password only validates presence on create —
|
|
169
|
+
# either way `update` would be a silent no-op reported as success, consuming the token.
|
|
170
|
+
# A scalar `user` param (`?user=foo`) has no .permit: treat it as an empty submission.
|
|
171
|
+
reset_params = params[:user].try(:permit, :password, :password_confirmation) || {}
|
|
172
|
+
if reset_params[:password].blank?
|
|
173
|
+
flash[:error] = t('camaleon_cms.admin.login.message.reset_password_error')
|
|
174
|
+
elsif @user.update(reset_params)
|
|
175
|
+
# Single-use: clear the token so the same link cannot be replayed.
|
|
176
|
+
@user.update_columns(password_reset_token: nil, password_reset_sent_at: nil) # rubocop:disable Rails/SkipsModelValidations
|
|
177
|
+
flash[:notice] = t('camaleon_cms.admin.login.message.reset_password_succes')
|
|
178
|
+
return redirect_to cama_admin_login_path
|
|
179
|
+
else
|
|
180
|
+
flash[:error] = t('camaleon_cms.admin.login.message.reset_password_error')
|
|
86
181
|
end
|
|
87
|
-
@form_reset = true
|
|
88
|
-
render 'forgot'
|
|
89
|
-
return
|
|
90
182
|
end
|
|
183
|
+
@form_reset = true
|
|
184
|
+
return render 'forgot'
|
|
91
185
|
end
|
|
92
186
|
|
|
93
187
|
# TODO: Move this out of the controller
|
|
94
188
|
# send email reset password
|
|
95
|
-
return
|
|
189
|
+
return if params[:user].blank?
|
|
96
190
|
|
|
97
191
|
data_user = user_permit_data
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
192
|
+
# Custom class finder: emails are stored downcased, so the lookup must
|
|
193
|
+
# lower-case both sides regardless of DB collation.
|
|
194
|
+
@user = current_site.users.find_by_email(data_user[:email]) # rubocop:disable Rails/DynamicFindBy
|
|
195
|
+
# Security (audit 2026-08-11 M13): do not reveal whether an account exists, and do not let the
|
|
196
|
+
# endpoint mail-bomb a known address. Send at most one reset email per cooldown window, and
|
|
197
|
+
# always answer with the same neutral message whether or not the email matched an account.
|
|
198
|
+
send_password_reset_email(@user) if @user.present? && cama_password_reset_email_allowed?(@user)
|
|
199
|
+
# Only en.yml carries this key while the process locale follows the current admin/site
|
|
200
|
+
# language (audit M15) -- fall back to English rather than emit "translation missing".
|
|
201
|
+
flash[:notice] = t('camaleon_cms.admin.login.message.password_reset_requested',
|
|
202
|
+
default: t('camaleon_cms.admin.login.message.password_reset_requested', locale: :en))
|
|
203
|
+
redirect_to cama_admin_login_path
|
|
108
204
|
end
|
|
109
205
|
|
|
110
206
|
def register
|
|
111
207
|
@user ||= current_site.users.new
|
|
112
|
-
|
|
208
|
+
# A scalar `user` param has no .permit — only a form-shaped submission enters this branch.
|
|
209
|
+
if params[:user].respond_to?(:permit)
|
|
113
210
|
params[:user][:role] = PluginRoutes.system_info['default_user_role']
|
|
114
211
|
params[:user][:is_valid_email] = false if current_site.need_validate_email?
|
|
115
212
|
user_data = user_permit_data
|
|
116
|
-
result = cama_register_user(user_data,
|
|
213
|
+
result = cama_register_user(user_data, nil)
|
|
117
214
|
if result[:result] == false && result[:type] == :captcha_error
|
|
118
|
-
@user.errors.add(:captcha,
|
|
215
|
+
@user.errors.add(:captcha, result[:message])
|
|
119
216
|
render 'register'
|
|
217
|
+
elsif result[:result] == false && result[:type] == :stopped
|
|
218
|
+
# A vetoing user_before_register handler may take over the response itself (render/redirect,
|
|
219
|
+
# the user_before_login convention) or add its own errors to r[:user]; rendering again would
|
|
220
|
+
# raise DoubleRenderError, so re-render only when it did not — and show a generic reason so the
|
|
221
|
+
# visitor is not left with a silently re-rendered form.
|
|
222
|
+
unless performed?
|
|
223
|
+
if @user.errors.empty?
|
|
224
|
+
@user.errors.add(:base, t('camaleon_cms.admin.users.message.registration_stopped',
|
|
225
|
+
default: 'Registration could not be completed.'))
|
|
226
|
+
end
|
|
227
|
+
render 'register'
|
|
228
|
+
end
|
|
120
229
|
elsif result[:result]
|
|
230
|
+
@user = result[:user] if result[:user].present?
|
|
121
231
|
flash[:notice] = result[:message]
|
|
122
232
|
send_user_confirm_email(@user) if current_site.need_validate_email?
|
|
123
233
|
r = { user: @user, redirect_url: result[:redirect_url] }
|
|
124
234
|
hooks_run('user_registered', r)
|
|
125
|
-
|
|
235
|
+
# Host-check the post-registration destination for the same reason login_user host-checks its
|
|
236
|
+
# explicit redirect_url: the user_registered hook is caller-controlled (downstream plugins), so
|
|
237
|
+
# an off-site value falls back to the safe default unless the hook vouches for it via
|
|
238
|
+
# r[:allow_external_redirect] (or its host is allowlisted).
|
|
239
|
+
cama_safe_redirect(r[:redirect_url], cama_admin_login_path,
|
|
240
|
+
allow_external: r[:allow_external_redirect].present?)
|
|
126
241
|
else
|
|
127
242
|
render 'register'
|
|
128
243
|
end
|
|
@@ -143,6 +258,9 @@ module CamaleonCms
|
|
|
143
258
|
flash[:notice] = t('camaleon_cms.admin.login.message.confirm_email_success')
|
|
144
259
|
@user.is_valid_email = true
|
|
145
260
|
@user.save!
|
|
261
|
+
# Security (audit M16): single-use, like the password-reset token -- clear it so the same
|
|
262
|
+
# confirmation link cannot be replayed.
|
|
263
|
+
@user.update_columns(confirm_email_token: nil, confirm_email_sent_at: nil) # rubocop:disable Rails/SkipsModelValidations
|
|
146
264
|
end
|
|
147
265
|
end
|
|
148
266
|
redirect_to cama_admin_login_path
|
|
@@ -150,13 +268,83 @@ module CamaleonCms
|
|
|
150
268
|
|
|
151
269
|
private
|
|
152
270
|
|
|
271
|
+
# Data for the GET logout confirmation page (see #logout). Carries the caller's own logout
|
|
272
|
+
# parameters into the POST button, but only scalar values -- a hash-shaped ?full[x]= or
|
|
273
|
+
# ?return_to[x]= would otherwise crash URL generation with UnfilteredParameters (500). The
|
|
274
|
+
# Cancel destination is the vetted return_to when present (frontend themes link logout with a
|
|
275
|
+
# return-to-site URL) so cancelling goes back to the site, not into the admin panel.
|
|
276
|
+
def prepare_logout_confirmation
|
|
277
|
+
@logout_full = params[:full].presence if params[:full].is_a?(String)
|
|
278
|
+
@logout_return_to = params[:return_to] if params[:return_to].is_a?(String)
|
|
279
|
+
@logout_locale = params[:locale] if params[:locale].is_a?(String)
|
|
280
|
+
@logout_cancel_url = safe_redirect_url(@logout_return_to).presence || cama_admin_dashboard_path
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
# Hard brute-force lockout (audit finding H1): once the per-IP failed-login counter reaches the
|
|
284
|
+
# limit, refuse every attempt from that IP (renders the given template with 429) for the cooldown
|
|
285
|
+
# window, regardless of credentials or captcha. Re-counting the blocked attempt keeps the window
|
|
286
|
+
# fresh so a persistent attacker stays locked. Returns true when it halted the action.
|
|
287
|
+
def cama_halt_when_login_locked_out(template)
|
|
288
|
+
return false unless cama_captcha_attack_ip_count('login') >= cama_login_lockout_limit
|
|
289
|
+
|
|
290
|
+
cama_captcha_increment_attack('login')
|
|
291
|
+
msg = t('camaleon_cms.admin.login.message.too_many_attempts',
|
|
292
|
+
default: 'Too many failed attempts. Please try again later.')
|
|
293
|
+
if params[:format] == 'json'
|
|
294
|
+
render json: { error: msg }, status: :too_many_requests
|
|
295
|
+
else
|
|
296
|
+
flash.now[:error] = msg
|
|
297
|
+
@user ||= current_site.users.new
|
|
298
|
+
render template, status: :too_many_requests
|
|
299
|
+
end
|
|
300
|
+
true
|
|
301
|
+
end
|
|
302
|
+
|
|
303
|
+
# Failures past this per-IP threshold trigger the hard lockout; defaults to 4x the captcha
|
|
304
|
+
# (soft) threshold and is configurable per site.
|
|
305
|
+
def cama_login_lockout_limit
|
|
306
|
+
soft = current_site.get_option('max_try_attack', 5).to_i
|
|
307
|
+
current_site.get_option('login_lockout_attempts', soft * 4).to_i
|
|
308
|
+
end
|
|
309
|
+
|
|
310
|
+
# Verify the password while spending one bcrypt comparison whether or not the username exists, so
|
|
311
|
+
# an unknown username takes about as long as a wrong password (audit M14). The dummy comparison on
|
|
312
|
+
# the missing-user branch does the same work the real path does (BCrypt::Password#is_password?).
|
|
313
|
+
def cama_password_matches?(user, password)
|
|
314
|
+
return user.authenticate(password) if user
|
|
315
|
+
|
|
316
|
+
BCrypt::Password.new(TIMING_EQUALIZER_DIGEST).is_password?(password.to_s)
|
|
317
|
+
false
|
|
318
|
+
end
|
|
319
|
+
|
|
320
|
+
# Whether a fresh reset email may be sent to this account, throttled per account so the endpoint
|
|
321
|
+
# cannot flood a known inbox (M13). Reads the column directly (not shadowed, unlike the token).
|
|
322
|
+
def cama_password_reset_email_allowed?(user)
|
|
323
|
+
sent_at = user.password_reset_sent_at
|
|
324
|
+
sent_at.blank? || sent_at < PASSWORD_RESET_EMAIL_COOLDOWN.ago
|
|
325
|
+
end
|
|
326
|
+
|
|
153
327
|
def before_hook_session
|
|
154
|
-
session[:cama_current_language]
|
|
155
|
-
|
|
156
|
-
|
|
328
|
+
# NOTE: session[:cama_current_language] is for FRONTEND only (user's site language choice)
|
|
329
|
+
# Admin should use admin language setting, not frontend language
|
|
330
|
+
# This prevents language mixing when switching between frontend and admin contexts
|
|
331
|
+
set_login_locale
|
|
157
332
|
hooks_run('session_before_load')
|
|
158
333
|
end
|
|
159
334
|
|
|
335
|
+
# These pages render before authentication, so the site's languages — not a user
|
|
336
|
+
# preference — pick the locale; ?locale= applies only when scalar (?locale[]=
|
|
337
|
+
# would crash to_sym) and offered by the site
|
|
338
|
+
def set_login_locale
|
|
339
|
+
site_languages = current_site.get_languages
|
|
340
|
+
requested = (params[:locale].presence&.to_sym if params[:locale].is_a?(String))
|
|
341
|
+
I18n.locale = if site_languages.include?(requested)
|
|
342
|
+
requested
|
|
343
|
+
else
|
|
344
|
+
site_languages.first || I18n.default_locale
|
|
345
|
+
end
|
|
346
|
+
end
|
|
347
|
+
|
|
160
348
|
def after_hook_session
|
|
161
349
|
hooks_run('session_after_load')
|
|
162
350
|
end
|
|
@@ -171,7 +359,12 @@ module CamaleonCms
|
|
|
171
359
|
end
|
|
172
360
|
|
|
173
361
|
def user_permit_data
|
|
174
|
-
params.require(:user)
|
|
362
|
+
user_params = params.require(:user)
|
|
363
|
+
# A scalar `user` param (`?user=foo`) has no .permit: treat it as an empty submission.
|
|
364
|
+
return {} unless user_params.respond_to?(:permit)
|
|
365
|
+
|
|
366
|
+
user_params.permit(:first_name, :last_name, :email, :username, :password, :password_confirmation,
|
|
367
|
+
:is_valid_email)
|
|
175
368
|
end
|
|
176
369
|
end
|
|
177
370
|
end
|