Skip to content

Commit

Permalink
Merge pull request preciousplum#1 from tkusukawa/master
Browse files Browse the repository at this point in the history
visible private issues
  • Loading branch information
preciousplum authored Jul 6, 2019
2 parents b47221e + b967576 commit 1651256
Showing 1 changed file with 74 additions and 2 deletions.
76 changes: 74 additions & 2 deletions lib/custom_users_as_assignees/issue_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -17,7 +56,26 @@ def custom_users
end
custom_user_ids = custom_user_values.map(&:value).flatten
custom_user_ids.reject! { |id| id.blank? }
User.find(custom_user_ids)
# User.find(custom_user_ids)
# Principal.find(custom_user_ids)
return users_from_ids(custom_user_ids)
end

def users_from_ids(ids)
users = []
ids.each do |id|
user = User.find_by_id(id)
if user
users << user
next
end
group = Group.find_by_id(id)
if group
users += users_from_ids(group.user_ids)
next
end
end
return users
end

# added or removed users selected in custom fields with type 'user'
Expand All @@ -37,7 +95,7 @@ def custom_users_added_or_removed
custom_user_added_ids.uniq!
custom_user_removed_ids.uniq!
custom_user_changed_ids = (custom_user_added_ids + custom_user_removed_ids).uniq
User.find(custom_user_changed_ids)
return users_from_ids(custom_user_changed_ids)
else
[]
end
Expand All @@ -56,6 +114,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

0 comments on commit 1651256

Please sign in to comment.