From 81395dd0d4c54a3892e24d9f73bcb19ba16d860b Mon Sep 17 00:00:00 2001 From: tkusukawa Date: Sat, 1 Dec 2018 21:24:01 +0900 Subject: [PATCH] visible private issues --- lib/custom_users_as_assignees/issue_patch.rb | 53 ++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/lib/custom_users_as_assignees/issue_patch.rb b/lib/custom_users_as_assignees/issue_patch.rb index 7fad840..5bdd4f9 100644 --- a/lib/custom_users_as_assignees/issue_patch.rb +++ b/lib/custom_users_as_assignees/issue_patch.rb @@ -4,8 +4,47 @@ module IssuePatch def self.included(base) base.send :include, InstanceMethods base.send :has_many, :customized, :class_name => 'CustomValue', :foreign_key => 'customized_id' + base.extend ClassMethods base.class_eval do alias_method_chain :notified_users, :custom_users + alias_method_chain :visible?, :custom_users + class << self + alias_method_chain :visible_condition, :custom_users + end + end + end + + module ClassMethods + def visible_condition_with_custom_users(user, options={}) + + user_ids = [] + if user.logged? + user_ids = [user.id] + user.groups.map(&:id).compact + end + + prj_clause = nil + if !options.nil? && !options[:project].nil? + prj_clause = " #{Project.table_name}.id = #{options[:project].id}" + if options[:with_subprojects] + prj_clause << " OR (#{Project.table_name}.lft > #{options[:project].lft} AND #{Project.table_name}.rgt < #{options[:project].rgt})" + end + end + + issues_clause = "" + unless user_ids.empty? + issues_clause << "#{Issue.table_name}.id in (" + issues_clause << "SELECT cv.customized_id" + issues_clause << " FROM #{CustomField.table_name} AS cf" + issues_clause << " INNER JOIN #{CustomValue.table_name} AS cv" + issues_clause << " ON cv.custom_field_id = cf.id" + issues_clause << " AND cv.customized_type = 'Issue'" + issues_clause << " AND cv.value in (#{user_ids.join(',')})" + issues_clause << " WHERE cf.field_format = 'user'" + issues_clause << ")" + issues_clause << " AND (#{prj_clause})" if prj_clause + end + + "( #{visible_condition_without_custom_users(user, options)} OR (#{issues_clause})) " end end @@ -56,6 +95,20 @@ def notified_users_with_custom_users notified += notified_custom_users notified.uniq end + + def visible_with_custom_users?(usr=nil) + visible = visible_without_custom_users?(usr) + return true if visible + + u = usr + u ||= User.current + if u.logged? + custom_users().each do |custom_user| + return true if custom_user.id == u.id + end + end + return visible + end end end end