group.rb 7.8 KB
Newer Older
S
Steven Thonus 已提交
1 2
require 'carrierwave/orm/activerecord'

3
class Group < Namespace
4
  include Gitlab::ConfigHelper
R
Rémy Coutable 已提交
5
  include AccessRequestable
6
  include Avatarable
7
  include Referable
8
  include SelectForProjectAuthorization
F
Felipe Artur 已提交
9

10
  has_many :group_members, -> { where(requested_at: nil) }, dependent: :destroy, as: :source # rubocop:disable Cop/ActiveRecordDependent
11
  alias_method :members, :group_members
12
  has_many :users, through: :group_members
13
  has_many :owners,
14
    -> { where(members: { access_level: Gitlab::Access::OWNER }) },
15 16 17
    through: :group_members,
    source: :user

18
  has_many :requesters, -> { where.not(requested_at: nil) }, dependent: :destroy, as: :source, class_name: 'GroupMember' # rubocop:disable Cop/ActiveRecordDependent
19

F
Felipe Artur 已提交
20
  has_many :milestones
21
  has_many :project_group_links, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
22
  has_many :shared_projects, through: :project_group_links, source: :project
23
  has_many :notification_settings, dependent: :destroy, as: :source # rubocop:disable Cop/ActiveRecordDependent
D
Douglas Barbosa Alexandre 已提交
24
  has_many :labels, class_name: 'GroupLabel'
S
Shinya Maeda 已提交
25
  has_many :variables, class_name: 'Ci::GroupVariable'
A
Andrey Kumanyaev 已提交
26

27
  validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
28 29
  validate :visibility_level_allowed_by_projects

30
  validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
S
Steven Thonus 已提交
31

32 33
  validates :two_factor_grace_period, presence: true, numericality: { greater_than_or_equal_to: 0 }

D
Douwe Maan 已提交
34
  mount_uploader :avatar, AvatarUploader
35
  has_many :uploads, as: :model, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
36

37 38
  after_create :post_create_hook
  after_destroy :post_destroy_hook
39
  after_save :update_two_factor_requirement
40

41
  class << self
42 43 44 45
    def supports_nested_groups?
      Gitlab::Database.postgresql?
    end

46 47 48 49 50 51 52
    # 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.
53
    def search(query)
54
      table   = Namespace.arel_table
55 56 57
      pattern = "%#{query}%"

      where(table[:name].matches(pattern).or(table[:path].matches(pattern)))
58 59 60
    end

    def sort(method)
M
Markus Koller 已提交
61 62 63 64 65 66 67
      if method == 'storage_size_desc'
        # storage_size is a virtual column so we need to
        # pass a string to avoid AR adding the table name
        reorder('storage_size DESC, namespaces.id DESC')
      else
        order_by(method)
      end
68
    end
69 70

    def reference_prefix
71 72 73 74 75
      User.reference_prefix
    end

    def reference_pattern
      User.reference_pattern
76
    end
77 78 79 80

    def visible_to_user(user)
      where(id: user.authorized_groups.select(:id).reorder(nil))
    end
81 82 83

    def select_for_project_authorization
      if current_scope.joins_values.include?(:shared_projects)
84 85
        joins('INNER JOIN namespaces project_namespace ON project_namespace.id = projects.namespace_id')
          .where('project_namespace.share_with_group_lock = ?',  false)
86
          .select("projects.id AS project_id, LEAST(project_group_links.group_access, members.access_level) AS access_level")
87 88 89 90
      else
        super
      end
    end
91 92
  end

93
  def to_reference(_from_project = nil, full: nil)
94
    "#{self.class.reference_prefix}#{full_path}"
95 96
  end

97
  def web_url
98
    Gitlab::Routing.url_helpers.group_canonical_url(self)
99 100
  end

101
  def human_name
102
    full_name
103
  end
104

105
  def visibility_level_allowed_by_projects
D
Douwe Maan 已提交
106
    allowed_by_projects = self.projects.where('visibility_level > ?', self.visibility_level).none?
107 108 109 110 111 112 113 114 115

    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

116 117 118 119
  def avatar_url(**args)
    # We use avatar_path instead of overriding avatar_url because of carrierwave.
    # See https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/11001/diffs#note_28659864
    avatar_path(args)
120 121
  end

122 123 124 125 126 127 128
  def lfs_enabled?
    return false unless Gitlab.config.lfs.enabled
    return Gitlab.config.lfs.enabled if self[:lfs_enabled].nil?

    self[:lfs_enabled]
  end

129
  def add_users(users, access_level, current_user: nil, expires_at: nil)
130
    GroupMember.add_users(
131 132 133 134 135 136
      self,
      users,
      access_level,
      current_user: current_user,
      expires_at: expires_at
    )
137 138
  end

139
  def add_user(user, access_level, current_user: nil, expires_at: nil)
140 141 142 143 144 145 146
    GroupMember.add_user(
      self,
      user,
      access_level,
      current_user: current_user,
      expires_at: expires_at
    )
147 148
  end

149
  def add_guest(user, current_user = nil)
150
    add_user(user, :guest, current_user: current_user)
151 152 153
  end

  def add_reporter(user, current_user = nil)
154
    add_user(user, :reporter, current_user: current_user)
155 156 157
  end

  def add_developer(user, current_user = nil)
158
    add_user(user, :developer, current_user: current_user)
159 160 161
  end

  def add_master(user, current_user = nil)
162
    add_user(user, :master, current_user: current_user)
163 164
  end

D
Douwe Maan 已提交
165
  def add_owner(user, current_user = nil)
166
    add_user(user, :owner, current_user: current_user)
D
Douwe Maan 已提交
167 168 169
  end

  def has_owner?(user)
170 171
    return false unless user

172
    members_with_parents.owners.where(user_id: user).any?
D
Douwe Maan 已提交
173 174 175
  end

  def has_master?(user)
176 177
    return false unless user

178
    members_with_parents.masters.where(user_id: user).any?
D
Douwe Maan 已提交
179 180
  end

181 182
  # Check if user is a last owner of the group.
  # Parent owners are ignored for nested groups.
D
Douwe Maan 已提交
183
  def last_owner?(user)
184
    owners.include?(user) && owners.size == 1
D
Douwe Maan 已提交
185 186
  end

S
Steven Thonus 已提交
187 188 189 190 191
  def avatar_type
    unless self.avatar.image?
      self.errors.add :avatar, "only images allowed"
    end
  end
192

193
  def post_create_hook
194 195
    Gitlab::AppLogger.info("Group \"#{name}\" was created")

196 197 198 199
    system_hook_service.execute_hooks_for(self, :create)
  end

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

202 203 204 205 206 207
    system_hook_service.execute_hooks_for(self, :destroy)
  end

  def system_hook_service
    SystemHooksService.new
  end
208 209

  def refresh_members_authorized_projects
210 211
    UserProjectAccessChangedService.new(user_ids_for_project_authorizations)
      .execute
212 213 214
  end

  def user_ids_for_project_authorizations
215
    members_with_parents.pluck(:user_id)
216 217 218
  end

  def members_with_parents
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
    # Avoids an unnecessary SELECT when the group has no parents
    source_ids =
      if parent_id
        self_and_ancestors.reorder(nil).select(:id)
      else
        id
      end

    GroupMember
      .active_without_invites
      .where(source_id: source_ids)
  end

  def members_with_descendants
    GroupMember
      .active_without_invites
      .where(source_id: self_and_descendants.reorder(nil).select(:id))
236 237 238
  end

  def users_with_parents
239 240 241
    User
      .where(id: members_with_parents.select(:user_id))
      .reorder(nil)
242
  end
Z
Z.J. van de Weg 已提交
243

244
  def users_with_descendants
245 246 247
    User
      .where(id: members_with_descendants.select(:user_id))
      .reorder(nil)
248 249
  end

250 251 252
  def max_member_access_for_user(user)
    return GroupMember::OWNER if user.admin?

253 254 255 256
    members_with_parents
      .where(user_id: user)
      .reorder(access_level: :desc)
      .first&.
257 258 259
      access_level || GroupMember::NO_ACCESS
  end

Z
Z.J. van de Weg 已提交
260 261 262 263 264 265 266 267 268
  def mattermost_team_params
    max_length = 59

    {
      name: path[0..max_length],
      display_name: name[0..max_length],
      type: public? ? 'O' : 'I' # Open vs Invite-only
    }
  end
269

S
Shinya Maeda 已提交
270
  def secret_variables_for(ref, project)
271 272 273 274 275
    list_of_ids = [self] + ancestors
    variables = Ci::GroupVariable.where(group: list_of_ids)
    variables = variables.unprotected unless project.protected_for?(ref)
    variables = variables.group_by(&:group_id)
    list_of_ids.reverse.map { |group| variables[group.id] }.compact.flatten
S
Shinya Maeda 已提交
276 277
  end

278 279 280 281 282 283 284
  protected

  def update_two_factor_requirement
    return unless require_two_factor_authentication_changed? || two_factor_grace_period_changed?

    users.find_each(&:update_two_factor_requirement)
  end
285
end