OpenProject is the leading open source project management software.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
openproject/lib/costs_user_patch.rb

211 lines
6.7 KiB

require_dependency 'project'
require_dependency 'principal'
require_dependency 'user'
module CostsUserPatch
def self.included(base) # :nodoc:
base.send(:include, InstanceMethods)
base.class_eval do
unloadable
has_many :groups_users, :class_name => 'GroupUser', :dependent => :destroy,
:after_add => Proc.new {|user, group_user| group_user.group.user_added(user)},
:after_remove => Proc.new {|user, group_user| group_user.group.user_removed(user)}
has_many :groups, :through => :groups_users,
:after_add => Proc.new {|user, group| group.user_added(user)},
:after_remove => Proc.new {|user, group| group.user_removed(user)}
has_many :rates, :class_name => 'HourlyRate'
has_many :default_rates, :class_name => 'DefaultHourlyRate'
before_save :save_rates
register_allowance_evaluator Costs::PrincipalAllowanceEvaluator::Costs
end
end
module InstanceMethods
def allowed_for_role(action, project, role, users, options={})
allowed = role.allowed_to?(action)
if action.is_a? Symbol
perm = Redmine::AccessControl.permission(action)
if perm.granular_for
allowed && users.include?(options[:for] || self)
elsif !allowed &&
options[:for] &&
granulars = Redmine::AccessControl.permissions.select{|p| p.granular_for == perm}
granulars.any?{|p| self.allowed_to? p.name, project, options} ?
role :
false
else
allowed
end
else
allowed
end
end
def granular_roles_for_project(project)
roles = {}
# No role on archived projects
return roles unless project && project.active?
if logged?
# Find project membership
# FIXME: Use AR proxy object properly and avoid the use enumberable methods
membership = memberships.detect {|m| m.project_id == project.id}
if membership
roles = granular_roles(membership.member_roles)
else
@role_non_member ||= Role.non_member
roles[@role_non_member] = [self]
end
else
@role_anonymous ||= Role.anonymous
roles[@role_anonymous] = [self]
end
roles
end
def allowed_for(permission, projects = nil)
unless projects.nil? or projects.blank?
projects = [projects] unless projects.is_a? Array
projects, ids = projects.partition{|p| p.is_a?(Project)}
projects += Project.find_all_by_id(ids)
else
vis_projects = Project.find(:all, :conditions => Project.visible_by(self), :include => [:enabled_modules])
projects = vis_projects + (projects.nil? ? [] : projects)
# In case there is no Project, we assume that an admin still has all the permissions
return (self.admin? ? "(1=1)" : "(1=0)") if projects.blank?
end
return "(#{Project.table_name}.id in (#{projects.collect(&:id).join(", ")}))" if self.admin?
user_list = projects.inject({}) do |user_list, project|
roles = granular_roles_for_project(project)
return user_list unless roles
users_for_project = []
roles.each_pair do |role, users|
if (project.is_public? || role.member?)
if !Redmine::AccessControl.permission(permission).granular_for && self.allowed_for_role(permission, project, role, users)
users_for_project = nil
break
elsif self.allowed_for_role(permission, project, role, users, :for => self)
users_for_project += users.collect(&:id)
end
end
end
if users_for_project.nil? || !users_for_project.empty?
users_for_project.sort!.uniq! unless users_for_project.nil?
user_list[users_for_project] ||= []
user_list[users_for_project] << project.id
end
user_list
end
cond = ["0=1"]
user_list.each_pair do |users, projects|
if users
cond << "(#{Project.table_name}.id in (#{projects.join(", ")}) AND #{User.table_name}.id IN (#{users.join(", ")}))"
else
cond << "(#{Project.table_name}.id in (#{projects.join(", ")}))"
end
end
"(#{cond.join " OR "})"
end
def current_rate(project = nil, include_default = true)
rate_at(Date.today, project, include_default)
end
def rate_at(date, project = nil, include_default = true)
unless project.nil?
rate = HourlyRate.find(:first, :conditions => [ "user_id = ? and project_id = ? and valid_from <= ?", id, project, date], :order => "valid_from DESC")
if rate.nil?
project = Project.find(project) unless project.is_a?(Project)
rate = HourlyRate.find(:first, :conditions => [ "user_id = ? and project_id in (?) and valid_from <= ?", id, project.ancestors, date], :include => :project, :order => "projects.lft DESC, valid_from DESC")
end
end
rate ||= default_rate_at(date) if include_default
rate
end
def current_default_rate()
default_rate_at(Date.today)
end
def default_rate_at(date)
DefaultHourlyRate.find(:first, :conditions => [ "user_id = ? and valid_from <= ?", id, date], :order => "valid_from DESC")
end
def add_rates(project, rate_attributes)
# set project to nil to set the default rates
return unless rate_attributes
rate_attributes.each do |index, attributes|
attributes[:rate] = Rate.clean_currency(attributes[:rate])
if project.nil?
default_rates.build(attributes)
else
attributes[:project] = project
rates.build(attributes)
end
end
end
def set_existing_rates (project, rate_attributes)
if project.nil?
default_rates.reject(&:new_record?).each do |rate|
update_rate(rate, rate_attributes, false)
end
else
rates.reject{|r| r.new_record? || r.project_id != project.id}.each do |rate|
update_rate(rate, rate_attributes, true)
end
end
end
def save_rates
(default_rates + rates).each do |rate|
rate.save(false)
end
end
private
def granular_roles(member_roles)
roles = {}
member_roles.each do |r|
roles[r.role] = [self]
end
roles
end
def update_rate(rate, rate_attributes, project_rate = true)
attributes = rate_attributes[rate.id.to_s] if rate_attributes
has_rate = false
if attributes && attributes[:rate].present?
attributes[:rate] = Rate.clean_currency(attributes[:rate])
has_rate = true
end
if has_rate
rate.attributes = attributes
else
project_rate ? rates.delete(rate) : default_rates.delete(rate)
end
end
end
end
User.send(:include, CostsUserPatch)