Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
26 changes: 9 additions & 17 deletions app/volt/tasks/live_query/live_query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,7 @@ def notify_removed(ids, skip_channel)
end

def notify_added(index, data, skip_channel)
# Make model for testing permissions against
model = nil

notify! do |channel|
# Only load the model for filtering if we are sending to a channel
# (skip if we are the only one listening)
model ||= model_for_filter(data)

filtered_data = nil
Volt.as_user(channel.user_id) do
filtered_data = model.filtered_attributes.sync
end

channel.send_message('added', nil, @collection, @query, index, filtered_data)
end
notify_content_update(index, data, 'added')
end

def notify_moved(id, new_position, skip_channel)
Expand All @@ -55,6 +41,11 @@ def notify_moved(id, new_position, skip_channel)
end

def notify_changed(id, data, skip_channel)
notify_content_update(id, data, skip_channel, 'update')
end

def notify_content_update(index, data, skip_channel = nil, type)
# Make model for testing permissions against
model = nil

notify!(skip_channel) do |channel|
Expand All @@ -63,11 +54,12 @@ def notify_changed(id, data, skip_channel)
model ||= model_for_filter(data)

filtered_data = nil

Volt.as_user(channel.user_id) do
filtered_data = model.filtered_attributes.sync
end
# puts "Changed: #{id}, #{data} to #{channel.inspect}"
channel.send_message('changed', nil, @collection, @query, id, filtered_data)

channel.send_message(type, nil, @collection, @query, index, filtered_data)
end
end

Expand Down
20 changes: 12 additions & 8 deletions lib/volt/extra_core/inflector/inflections.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,16 @@ def acronym(word)
# either be a string or a regular expression. The replacement should
# always be a string that may include references to the matched data from
# the rule.
def plural(rule, replacement)
@uncountables.delete(rule) if rule.is_a?(String)
@uncountables.delete(replacement)
@plurals.insert(0, [rule, replacement])
def plural(*args)
register_pluralization_rule('plurals', *args)
end

# Specifies a new singularization rule and its replacement. The rule can
# either be a string or a regular expression. The replacement should
# always be a string that may include references to the matched data from
# the rule.
def singular(rule, replacement)
@uncountables.delete(rule) if rule.is_a?(String)
@uncountables.delete(replacement)
@singulars.insert(0, [rule, replacement])
def singular(*args)
register_pluralization_rule('singulars', *args)
end

# Specifies a new irregular that applies to both pluralization and
Expand Down Expand Up @@ -192,6 +188,14 @@ def clear(scope = :all)
instance_variable_set "@#{scope}", []
end
end

private

def register_pluralization_rule(type, rule, replacement)
@uncountables.delete(rule) if rule.is_a?(String)
@uncountables.delete(replacement)
instance_variable_get("@#{type}").insert(0, [rule, replacement])
end
end

# Yields a singleton instance of Inflector::Inflections so you can specify
Expand Down
55 changes: 24 additions & 31 deletions lib/volt/models/permissions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,26 @@ def self.included(base)
end

def allow(*fields)
if @__allow_fields
if @__allow_fields != true
if fields.size == 0
# No field's were passed, this means we deny all
@__allow_fields = true
else
# Fields were specified, add them to the list
@__allow_fields += fields.map(&:to_sym)
end
end
else
fail 'allow should be called inside of a permissions block'
end
rule :allow, *fields
end

def deny(*fields)
if @__deny_fields
if @__deny_fields != true
rule :deny, *fields
end

def rule(type, *fields)
if @__permission_fields[type]
if @__permission_fields[type] != true
if fields.size == 0
# No field's were passed, this means we deny all
@__deny_fields = true
# No fields were passed, this means we [allow|deny] all
@__permission_fields[type] = true
else
# Fields were specified, add them to the list
@__deny_fields += fields.map(&:to_sym)
@__permission_fields[type] += fields.map(&:to_sym)
end
end
else
fail 'deny should be called inside of a permissions block'
fail "#{type.to_s} should be called inside of a permissions block"
end
end

Expand All @@ -119,7 +111,7 @@ def action_allowed?(action_name)
# TODO: this does some unnecessary work
compute_allow_and_deny(action_name).then do

deny = @__deny_fields == true || (@__deny_fields && @__deny_fields.size > 0)
deny = @__permission_fields[:deny] == true || (@__permission_fields[:deny] && @__permission_fields[:deny].size > 0)

clear_allow_and_deny

Expand All @@ -131,7 +123,7 @@ def action_allowed?(action_name)
def allow_and_deny_fields(action_name)
compute_allow_and_deny(action_name).then do

result = [@__allow_fields, @__deny_fields]
result = [@__permission_fields[:allow], @__permission_fields[:deny]]

clear_allow_and_deny

Expand Down Expand Up @@ -192,25 +184,25 @@ def run_permissions(action_name = nil)

errors = {}

if @__allow_fields == true
if @__permission_fields[:allow] == true
# Allow all fields
elsif @__allow_fields && @__allow_fields.size > 0
elsif @__permission_fields[:allow] && @__permission_fields[:allow].size > 0
# Deny all not specified in the allow list
changed_attributes.keys.each do |field_name|
unless @__allow_fields.include?(field_name)
unless @__permission_fields[:allow].include?(field_name)
add_error_if_changed(errors, field_name)
end
end
end

if @__deny_fields == true
if @__permission_fields[:deny] == true
# Don't allow any field changes
changed_attributes.keys.each do |field_name|
add_error_if_changed(errors, field_name)
end
elsif @__deny_fields
elsif @__permission_fields[:deny]
# Allow all except the denied
@__deny_fields.each do |field_name|
@__permission_fields[:deny].each do |field_name|
add_error_if_changed(errors, field_name) if changed?(field_name)
end
end
Expand All @@ -222,15 +214,16 @@ def run_permissions(action_name = nil)
end

def clear_allow_and_deny
@__deny_fields = nil
@__allow_fields = nil
@__permission_fields[:deny] = nil
@__permission_fields[:allow] = nil
end

# Run through the permission blocks for the action name, acumulate
# all allow/deny fields.
def compute_allow_and_deny(action_name)
@__deny_fields = []
@__allow_fields = []
@__permission_fields ||= {}
@__permission_fields[:deny] = []
@__permission_fields[:allow] = []

# Skip permissions can be run on the server to ignore the permissions
return if Volt.in_mode?(:skip_permissions)
Expand Down