Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix custom fields date pair and datetime pair not saving #1319

Merged
merged 4 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions app/controllers/admin/fields_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@
# GET /fields/1/edit AJAX
#----------------------------------------------------------------------------
def edit
@field = Field.find(params[:id])
@field = Field.find(params["id"])
respond_with(@field)
end

# POST /fields
# POST /fields.xml AJAX
#----------------------------------------------------------------------------
def create
as = field_params[:as]
as = field_params["as"]
@field =
if as.match?(/pair/)
CustomFieldPair.create_pair(params).first
CustomFieldPair.create_pair("pair" => pair_params, "field" => field_params).first
elsif as.present?
klass = find_class(Field.lookup_class(as))
klass.create(field_params)
else
Field.new(field_params).tap(&:valid?)

Check failure

Code scanning / CodeQL

Insecure Mass Assignment Critical

This mass assignment operation can assign user-controlled attributes from
this remote flow source
.
end

respond_with(@field)
Expand All @@ -59,11 +59,11 @@
# PUT /fields/1.xml AJAX
#----------------------------------------------------------------------------
def update
if field_params[:as].match?(/pair/)
@field = CustomFieldPair.update_pair(params).first
if field_params["as"].match?(/pair/)
@field = CustomFieldPair.update_pair("pair" => pair_params, "field" => field_params).first
else
@field = Field.find(params[:id])
@field = Field.find(params["id"])
@field.update(field_params)

Check failure

Code scanning / CodeQL

Insecure Mass Assignment Critical

This mass assignment operation can assign user-controlled attributes from
this remote flow source
.
end

respond_with(@field)
Expand All @@ -73,7 +73,7 @@
# DELETE /fields/1.xml HTML and AJAX
#----------------------------------------------------------------------------
def destroy
@field = Field.find(params[:id])
@field = Field.find(params["id"])
@field.destroy

respond_with(@field)
Expand All @@ -82,7 +82,7 @@
# POST /fields/sort
#----------------------------------------------------------------------------
def sort
field_group_id = params[:field_group_id].to_i
field_group_id = params["field_group_id"].to_i
field_ids = params["fields_field_group_#{field_group_id}"] || []

field_ids.each_with_index do |id, index|
Expand All @@ -96,8 +96,7 @@
#----------------------------------------------------------------------------
def subform
field = field_params
as = field[:as]

as = field_params["as"]
@field = if (id = field[:id]).present?
Field.find(id).tap { |f| f.as = as }
else
Expand All @@ -114,7 +113,13 @@
protected

def field_params
params[:field].permit!
# Sets the +permitted+ attribute to +true+. This can be used to pass
# mass assignment. Returns +self+.
params.require(:field).permit!
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
end

def pair_params
params.require(:pair).permit!
github-advanced-security[bot] marked this conversation as resolved.
Fixed
Show resolved Hide resolved
end

def setup_current_tab
Expand Down
11 changes: 5 additions & 6 deletions app/models/fields/custom_field_pair.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ class CustomFieldPair < CustomField
#------------------------------------------------------------------------------
def self.create_pair(params)
fields = params['field']
as = params['field']['as']
pair = params.delete('pair')
pair = params['pair']
base_params = fields.delete_if { |k, _v| !%w[field_group_id label as].include?(k) }
klass = ("custom_field_" + as.gsub('pair', '_pair')).classify.constantize
klass = ("custom_field_" + fields['as'].gsub('pair', '_pair')).classify.constantize
field1 = klass.create(base_params.merge(pair['0']))
field2 = klass.create(base_params.merge(pair['1']).merge('pair_id' => field1.id, 'required' => field1.required, 'disabled' => field1.disabled))
[field1, field2]
Expand All @@ -25,9 +24,9 @@ def self.create_pair(params)
#------------------------------------------------------------------------------
def self.update_pair(params)
fields = params['field']
pair = params.delete('pair')
pair = params['pair']
base_params = fields.delete_if { |k, _v| !%w[field_group_id label as].include?(k) }
field1 = CustomFieldPair.find(params['id'])
field1 = CustomFieldPair.find(pair['0']['id'])
field1.update(base_params.merge(pair['0']))
field2 = field1.paired_with
field2.update(base_params.merge(pair['1']).merge('required' => field1.required, 'disabled' => field1.disabled))
Expand All @@ -37,7 +36,7 @@ def self.update_pair(params)
# Returns the field that this field is paired with
#------------------------------------------------------------------------------
def paired_with
pair || CustomFieldPair.where(pair_id: id).first
pair
end

ActiveSupport.run_load_hooks(:fat_free_crm_custom_field_pair, self)
Expand Down
2 changes: 1 addition & 1 deletion app/views/admin/custom_fields/_date_pair_field.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- field1 = f.object || CustomFieldPair.new
- field2 = f.object.respond_to?(:paired_with) ? field1.paired_with : CustomFieldPair.new
- field2 = (field1.pair || CustomFieldPair.new)

%table{class: :pairs}
= fields_for("pair[0]", field1) do |first|
Expand Down
4 changes: 2 additions & 2 deletions lib/fat_free_crm/custom_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@
#
# Register CustomFields when Field class is loaded
ActiveSupport.on_load(:fat_free_crm_field) do # self == Field
register(as: 'date_pair', klass: 'CustomFieldDatePair', type: 'date')
register(as: 'datetime_pair', klass: 'CustomFieldDatetimePair', type: 'timestamp')
CustomFieldDatePair.register(as: 'date_pair', klass: 'CustomFieldDatePair', type: 'date')
CustomFieldDatetimePair.register(as: 'datetime_pair', klass: 'CustomFieldDatetimePair', type: 'timestamp')
end
Loading