Class: CamaleonCms::Admin::Posts::DraftsController

Inherits:
CamaleonCms::Admin::PostsController show all
Defined in:
app/controllers/camaleon_cms/admin/posts/drafts_controller.rb

Instance Method Summary collapse

Methods inherited from CamaleonCms::Admin::PostsController

#ajax, #edit, #new, #restore, #show, #trash

Methods inherited from CamaleonCms::AdminController

#ajax, #cama_get_i18n_frontend, #dashboard, #search

Instance Method Details

#createObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/camaleon_cms/admin/posts/drafts_controller.rb', line 14

def create
  if @draft_parent_post.present?
    # A draft buffer is an artifact of editing its parent post — authorize that post,
    # and only ever touch the current user's own buffer
    authorize! :update, @draft_parent_post
    @post_draft = @post_type.posts.drafts.where(post_parent: @draft_parent_post.id,
                                                user_id: cama_current_user.id).first
    if @post_draft.present?
      @post_draft.set_option('draft_status', @post_draft.status)
      @post_draft.attributes = @post_data
    end
  else
    authorize! :create_post, @post_type
  end
  if @post_draft.blank?
    @post_draft = @post_type.posts.new(@post_data)
    @post_draft.user_id = cama_current_user.id
  end
  r = { post: @post_draft, post_type: @post_type }
  hooks_run('create_post_draft', r)
  if @post_draft.save(validate: false)
    # Security (audit M8): confine field values to slugs actually registered on the post type,
    # like PostsController#save_post_with_fields -- raw params[:field_options] let a caller
    # write custom_field_values with attacker-chosen slugs/ids/group numbers.
    @post_draft.set_params(params[:meta], cama_permitted_field_options('PostType_Post'), @post_data[:keywords])
    msg = { draft: { id: @post_draft.id },
            _drafts_path: cama_admin_post_type_draft_path(@post_type.id, @post_draft) }
    r = { post: @post_draft, post_type: @post_type }
    hooks_run('created_post_draft', r)
  else
    msg = { error: @post_draft.errors.full_messages }
  end

  render json: msg
end

#destroyObject



72
# File 'app/controllers/camaleon_cms/admin/posts/drafts_controller.rb', line 72

def destroy; end

#indexObject



7
8
9
10
11
12
# File 'app/controllers/camaleon_cms/admin/posts/drafts_controller.rb', line 7

def index
  # Security (audit 2026-08-11 M12): this action had no authorization, so any signed-in user
  # could read a post type's JSON. Gate it on the same permission the post listing requires.
  authorize! :posts, @post_type
  render json: @post_type
end

#updateObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/controllers/camaleon_cms/admin/posts/drafts_controller.rb', line 50

def update
  @post_draft = @post_type.posts.drafts.where(user_id: cama_current_user.id).find(params[:id])
  authorize! :update, @post_draft.parent || @post_draft
  @post_draft.attributes = @post_data
  r = { post: @post_draft, post_type: @post_type }
  hooks_run('update_post_draft', r)
  if @post_draft.save(validate: false)
    # Security (audit M8): confine field values to the post type's registered slugs (see #create).
    @post_draft.set_params(params[:meta], cama_permitted_field_options('PostType_Post'), params[:options])
    hooks_run('updated_post_draft', { post: @post_draft, post_type: @post_type })
    msg = { draft: { id: @post_draft.id } }
  else
    msg = { error: @post_draft.errors.full_messages }
  end
  render json: msg
rescue ActiveRecord::RecordNotFound
  # Another user's buffer answers exactly like a nonexistent draft id (same rescue
  # behavior as PostsController#set_post) — no existence oracle for foreign buffers
  flash[:error] = t('camaleon_cms.admin.post.message.error', post_type: @post_type.decorate.the_title)
  redirect_to cama_admin_path
end