|
|
@ -6,24 +6,25 @@ class CostQuery::Filter::PermissionFilter < CostQuery::Filter::Base |
|
|
|
initialize_query_with { |query| query.filter self.to_s.demodulize.to_sym } |
|
|
|
initialize_query_with { |query| query.filter self.to_s.demodulize.to_sym } |
|
|
|
|
|
|
|
|
|
|
|
def permission_statement(permission) |
|
|
|
def permission_statement(permission) |
|
|
|
User.current.allowed_for(permission).gsub(/(user|project)s?\.id/, 'entries.\1_id') |
|
|
|
User.current.allowed_for(permission).gsub(/(user|project)s?\.id/, '\1_id') |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def permission_for(type) |
|
|
|
def permission_for(type) |
|
|
|
"(entries.type != '#{type.capitalize}Entry' " \ |
|
|
|
"(#{permission_statement :"view_own_#{type}_entries"} " \ |
|
|
|
"OR #{permission_statement :"view_own_#{type}_entries"} " \ |
|
|
|
|
|
|
|
"OR #{permission_statement :"view_#{type}_entries"})" |
|
|
|
"OR #{permission_statement :"view_#{type}_entries"})" |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def display_costs |
|
|
|
|
|
|
|
"(#{permission_statement :view_hourly_rates} " \ |
|
|
|
|
|
|
|
"AND #{permission_statement :view_cost_rates}) OR (#{permission_statement :view_own_hourly_rate})" |
|
|
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def sql_statement |
|
|
|
def sql_statement |
|
|
|
super.tap do |query| |
|
|
|
super.tap do |query| |
|
|
|
if User.current.admin? |
|
|
|
query.default_select :display_costs => 'sum(display_costs)' |
|
|
|
query.default_select :display_costs => '1' |
|
|
|
query.from.each_subselect do |sub| |
|
|
|
else |
|
|
|
sub.where permission_for(sub == query.from.first ? 'time' : 'cost') |
|
|
|
query.where permission_for('time') |
|
|
|
sub.default_select :display_costs => switch(display_costs => '1', :else => 0) |
|
|
|
query.where permission_for('cost') |
|
|
|
|
|
|
|
query.default_select :display_costs => "(#{permission_statement :view_hourly_rates} " \ |
|
|
|
|
|
|
|
"AND #{permission_statement :view_cost_rates}) OR (#{permission_statement :view_own_hourly_rate})" |
|
|
|
|
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|
end |
|
|
|