BigW Consortium Gitlab

group.rb 4.43 KB
Newer Older
Steven Thonus committed
1 2
require 'carrierwave/orm/activerecord'

3
class Group < Namespace
4
  include Gitlab::ConfigHelper
5
  include Gitlab::VisibilityLevel
6
  include AccessRequestable
7
  include Referable
8

9
  has_many :group_members, -> { where(requested_at: nil) }, dependent: :destroy, as: :source, class_name: 'GroupMember'
10
  alias_method :members, :group_members
11
  has_many :users, through: :group_members
12
  has_many :owners,
13
    -> { where(members: { access_level: Gitlab::Access::OWNER }) },
14 15 16
    through: :group_members,
    source: :user

17 18
  has_many :requesters, -> { where.not(requested_at: nil) }, dependent: :destroy, as: :source, class_name: 'GroupMember'

19 20
  has_many :project_group_links, dependent: :destroy
  has_many :shared_projects, through: :project_group_links, source: :project
21
  has_many :notification_settings, dependent: :destroy, as: :source
Andrey Kumanyaev committed
22

23
  validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
24 25
  validate :visibility_level_allowed_by_projects

26
  validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
Steven Thonus committed
27

28
  mount_uploader :avatar, AvatarUploader
29

30 31 32
  after_create :post_create_hook
  after_destroy :post_destroy_hook

33
  class << self
34 35 36 37 38 39 40
    # Searches for groups matching the given query.
    #
    # This method uses ILIKE on PostgreSQL and LIKE on MySQL.
    #
    # query - The search query as a String
    #
    # Returns an ActiveRecord::Relation.
41
    def search(query)
42
      table   = Namespace.arel_table
43 44 45
      pattern = "%#{query}%"

      where(table[:name].matches(pattern).or(table[:path].matches(pattern)))
46 47 48 49 50
    end

    def sort(method)
      order_by(method)
    end
51 52

    def reference_prefix
53 54 55 56 57
      User.reference_prefix
    end

    def reference_pattern
      User.reference_pattern
58
    end
59 60 61 62

    def visible_to_user(user)
      where(id: user.authorized_groups.select(:id).reorder(nil))
    end
63 64 65 66
  end

  def to_reference(_from_project = nil)
    "#{self.class.reference_prefix}#{name}"
67 68
  end

69 70 71 72
  def web_url
    Gitlab::Routing.url_helpers.group_url(self)
  end

73 74 75
  def human_name
    name
  end
76

Felipe Artur committed
77 78 79 80
  def visibility_level_field
    visibility_level
  end

81
  def visibility_level_allowed_by_projects
Douwe Maan committed
82
    allowed_by_projects = self.projects.where('visibility_level > ?', self.visibility_level).none?
83 84 85 86 87 88 89 90 91

    unless allowed_by_projects
      level_name = Gitlab::VisibilityLevel.level_name(visibility_level).downcase
      self.errors.add(:visibility_level, "#{level_name} is not allowed since there are projects with higher visibility.")
    end

    allowed_by_projects
  end

92
  def avatar_url(size = nil)
93
    if self[:avatar].present?
94 95 96 97
      [gitlab_config.url, avatar.url].join
    end
  end

98 99 100 101 102 103 104
  def lfs_enabled?
    return false unless Gitlab.config.lfs.enabled
    return Gitlab.config.lfs.enabled if self[:lfs_enabled].nil?

    self[:lfs_enabled]
  end

105
  def add_users(user_ids, access_level, current_user: nil, expires_at: nil)
106
    user_ids.each do |user_id|
107 108 109 110 111 112 113
      Member.add_user(
        self.group_members,
        user_id,
        access_level,
        current_user: current_user,
        expires_at: expires_at
      )
114 115 116
    end
  end

117 118
  def add_user(user, access_level, current_user: nil, expires_at: nil)
    add_users([user], access_level, current_user: current_user, expires_at: expires_at)
119 120
  end

121
  def add_guest(user, current_user = nil)
122
    add_user(user, Gitlab::Access::GUEST, current_user: current_user)
123 124 125
  end

  def add_reporter(user, current_user = nil)
126
    add_user(user, Gitlab::Access::REPORTER, current_user: current_user)
127 128 129
  end

  def add_developer(user, current_user = nil)
130
    add_user(user, Gitlab::Access::DEVELOPER, current_user: current_user)
131 132 133
  end

  def add_master(user, current_user = nil)
134
    add_user(user, Gitlab::Access::MASTER, current_user: current_user)
135 136
  end

Douwe Maan committed
137
  def add_owner(user, current_user = nil)
138
    add_user(user, Gitlab::Access::OWNER, current_user: current_user)
Douwe Maan committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152
  end

  def has_owner?(user)
    owners.include?(user)
  end

  def has_master?(user)
    members.masters.where(user_id: user).any?
  end

  def last_owner?(user)
    has_owner?(user) && owners.size == 1
  end

Steven Thonus committed
153 154 155 156 157
  def avatar_type
    unless self.avatar.image?
      self.errors.add :avatar, "only images allowed"
    end
  end
158

159
  def post_create_hook
160 161
    Gitlab::AppLogger.info("Group \"#{name}\" was created")

162 163 164 165
    system_hook_service.execute_hooks_for(self, :create)
  end

  def post_destroy_hook
166 167
    Gitlab::AppLogger.info("Group \"#{name}\" was removed")

168 169 170 171 172 173
    system_hook_service.execute_hooks_for(self, :destroy)
  end

  def system_hook_service
    SystemHooksService.new
  end
174
end