additionals/lib/redmine_tweaks/user_macros.rb
2015-08-01 19:15:27 +02:00

77 lines
2.3 KiB
Ruby
Executable File

# Redmine Tweaks plugin for Redmine
# Copyright (C) 2013-2015 AlphaNodes GmbH
module RedmineTweaks
Redmine::WikiFormatting::Macros.register do
desc <<-EOHELP
Display users. Examples:
{{list_users}}
...List all users for all projects
{{list_users(123)}}
...A box showing all members for the project 123
{{list_users(the-identifier)}}
...A box showing all members for the project with the identifier of 'the-identifier'
{{list_users('My project name')}}
...A box showing all members for the project named 'My project name'
{{list_users('My project name', Manager)}}
...A box showing all managers for the project named 'My project name'
EOHELP
macro :list_users do |obj, args|
args, options = extract_macro_options(args, :role, :title)
project_id = args[0]
user_roles = []
if project_id.present?
project_id.strip!
project = Project.visible.find_by_id(project_id)
project ||= Project.visible.find_by_identifier(project_id)
project ||= Project.visible.find_by_name(project_id)
return '' if project.nil?
raw_users = User.active.where(["#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id=(?))", project.id]).sort
return '' if raw_users.nil?
users = []
raw_users.each {|user|
user_roles[user.id] = user.roles_for_project(project)
if !options[:role].present? or RedmineTweaks.check_role_matches(user_roles[user.id], options[:role])
users << user
end
}
else
project_ids = Project.visible.collect(&:id)
if project_ids.any?
# members of the user's projects
users = User.active.where(["#{User.table_name}.id IN (SELECT DISTINCT user_id FROM members WHERE project_id IN (?))", project_ids]).sort
else
return ''
end
end
render :partial => 'wiki/user_macros', :locals => {:users => users,
:user_roles => user_roles,
:list_title => options[:title]}
end
end
def self.check_role_matches(roles, filters)
filters.gsub('|', ',' ).split(',').each {|filter|
roles.each {|role|
if filter.to_s() == role.to_s()
return true
end
}
}
return false
end
end