project_team.rb 2.29 KB
Newer Older
1
class ProjectTeam
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
2 3 4 5
  attr_accessor :project

  def initialize(project)
    @project = project
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  end

  # Shortcut to add users
  #
  # Use:
  #   @team << [@user, :master]
  #   @team << [@users, :master]
  #
  def << args
    users = args.first

    if users.respond_to?(:each)
      add_users(users, args.second)
    else
      add_user(users, args.second)
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
22 23
  end

24 25 26 27
  def get_tm user_id
    project.users_projects.find_by_user_id(user_id)
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
28 29 30 31 32 33 34 35
  def add_user(user, access)
    add_users_ids([user.id], access)
  end

  def add_users(users, access)
    add_users_ids(users.map(&:id), access)
  end

36
  def add_users_ids(user_ids, access)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    UsersProject.add_users_into_projects(
      [project.id],
      user_ids,
      access
    )
  end

  # Remove all users from project team
  def truncate
    UsersProject.truncate_team(project)
  end

  def members
    project.users_projects
  end

  def guests
    members.guests.map(&:user)
  end

  def reporters
    members.reporters.map(&:user)
  end

  def developers
    members.developers.map(&:user)
  end

  def masters
    members.masters.map(&:user)
  end
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

  def repository_readers
    repository_members[UsersProject::REPORTER]
  end

  def repository_writers
    repository_members[UsersProject::DEVELOPER]
  end

  def repository_masters
    repository_members[UsersProject::MASTER]
  end

  def repository_members
    keys = Hash.new {|h,k| h[k] = [] }
    UsersProject.select("keys.identifier, project_access").
        joins(user: :keys).where(project_id: project.id).
        each {|row| keys[row.project_access] << [row.identifier] }

    keys[UsersProject::REPORTER] += project.deploy_keys.pluck(:identifier)
    keys
  end

  def import(source_project)
    target_project = project

    source_team = source_project.users_projects.all
    target_team = target_project.users_projects.all
    target_user_ids = target_team.map(&:user_id)

    source_team.reject! do |tm|
      # Skip if user already present in team
      target_user_ids.include?(tm.user_id)
    end

    source_team.map! do |tm|
      new_tm = tm.dup
      new_tm.id = nil
      new_tm.project_id = target_project.id
      new_tm.skip_git = true
      new_tm
    end

    UsersProject.transaction do
      source_team.each do |tm|
        tm.save
      end
    end

    true
  rescue
    false
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
121
end