Read about our upcoming Code of Conduct on this issue

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

3
class Group < Namespace
4
  include Gitlab::ConfigHelper
Felipe Artur's avatar
Felipe Artur committed
5
  include Gitlab::VisibilityLevel
Rémy Coutable's avatar
Rémy Coutable committed
6
  include AccessRequestable
7
  include Referable
8
  include SelectForProjectAuthorization
Felipe Artur's avatar
Felipe Artur committed
9

10
  has_many :group_members, -> { where(requested_at: nil) }, dependent: :destroy, as: :source
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
19
  has_many :requesters, -> { where.not(requested_at: nil) }, dependent: :destroy, as: :source, class_name: 'GroupMember'

20
21
  has_many :project_group_links, dependent: :destroy
  has_many :shared_projects, through: :project_group_links, source: :project
22
  has_many :notification_settings, dependent: :destroy, as: :source
23
  has_many :labels, class_name: 'GroupLabel'
Andrey Kumanyaev's avatar
Andrey Kumanyaev committed
24

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

28
  validates :avatar, file_size: { maximum: 200.kilobytes.to_i }
Steven Thonus's avatar
Steven Thonus committed
29

Douwe Maan's avatar
Douwe Maan committed
30
  mount_uploader :avatar, AvatarUploader
31

32
33
34
  after_create :post_create_hook
  after_destroy :post_destroy_hook

35
  class << self
36
37
38
39
40
41
42
    # 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.
43
    def search(query)
44
      table   = Namespace.arel_table
45
46
47
      pattern = "%#{query}%"

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

    def sort(method)
Markus Koller's avatar
Markus Koller committed
51
52
53
54
55
56
57
      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
58
    end
59
60

    def reference_prefix
61
62
63
64
65
      User.reference_prefix
    end

    def reference_pattern
      User.reference_pattern
66
    end
67
68
69
70

    def visible_to_user(user)
      where(id: user.authorized_groups.select(:id).reorder(nil))
    end
71
72
73

    def select_for_project_authorization
      if current_scope.joins_values.include?(:shared_projects)
74
75
76
        joins('INNER JOIN namespaces project_namespace ON project_namespace.id = projects.namespace_id')
          .where('project_namespace.share_with_group_lock = ?',  false)
          .select("members.user_id, projects.id AS project_id, LEAST(project_group_links.group_access, members.access_level) AS access_level")
77
78
79
80
      else
        super
      end
    end
81
82
83
84
  end

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

87
  def web_url
88
    Gitlab::Routing.url_helpers.group_canonical_url(self)
89
90
  end

91
  def human_name
92
    full_name
93
  end
94

Felipe Artur's avatar
Felipe Artur committed
95
96
97
98
  def visibility_level_field
    visibility_level
  end

99
  def visibility_level_allowed_by_projects
Douwe Maan's avatar
Douwe Maan committed
100
    allowed_by_projects = self.projects.where('visibility_level > ?', self.visibility_level).none?
101
102
103
104
105
106
107
108
109

    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

110
  def avatar_url(size = nil)
111
    if self[:avatar].present?
112
113
114
115
      [gitlab_config.url, avatar.url].join
    end
  end

116
117
118
119
120
121
122
  def lfs_enabled?
    return false unless Gitlab.config.lfs.enabled
    return Gitlab.config.lfs.enabled if self[:lfs_enabled].nil?

    self[:lfs_enabled]
  end

123
124
125
126
127
128
129
130
  def add_users(users, access_level, current_user: nil, expires_at: nil)
    GroupMember.add_users_to_group(
      self,
      users,
      access_level,
      current_user: current_user,
      expires_at: expires_at
    )
131
132
  end

133
  def add_user(user, access_level, current_user: nil, expires_at: nil)
134
135
136
137
138
139
140
    GroupMember.add_user(
      self,
      user,
      access_level,
      current_user: current_user,
      expires_at: expires_at
    )
141
142
  end

143
  def add_guest(user, current_user = nil)
144
    add_user(user, :guest, current_user: current_user)
145
146
147
  end

  def add_reporter(user, current_user = nil)
148
    add_user(user, :reporter, current_user: current_user)
149
150
151
  end

  def add_developer(user, current_user = nil)
152
    add_user(user, :developer, current_user: current_user)
153
154
155
  end

  def add_master(user, current_user = nil)
156
    add_user(user, :master, current_user: current_user)
157
158
  end

Douwe Maan's avatar
Douwe Maan committed
159
  def add_owner(user, current_user = nil)
160
    add_user(user, :owner, current_user: current_user)
Douwe Maan's avatar
Douwe Maan committed
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  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's avatar
Steven Thonus committed
175
176
177
178
179
  def avatar_type
    unless self.avatar.image?
      self.errors.add :avatar, "only images allowed"
    end
  end
180

181
  def post_create_hook
182
183
    Gitlab::AppLogger.info("Group \"#{name}\" was created")

184
185
186
187
    system_hook_service.execute_hooks_for(self, :create)
  end

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

190
191
192
193
194
195
    system_hook_service.execute_hooks_for(self, :destroy)
  end

  def system_hook_service
    SystemHooksService.new
  end
196
197
198
199

  def refresh_members_authorized_projects
    UserProjectAccessChangedService.new(users.pluck(:id)).execute
  end
200
end