BigW Consortium Gitlab

group.rb 4.99 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
  include SelectForProjectAuthorization
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 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 committed
29

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 51 52
    end

    def sort(method)
      order_by(method)
    end
53 54

    def reference_prefix
55 56 57 58 59
      User.reference_prefix
    end

    def reference_pattern
      User.reference_pattern
60
    end
61 62 63 64

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

    def select_for_project_authorization
      if current_scope.joins_values.include?(:shared_projects)
68 69 70
        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")
71 72 73 74
      else
        super
      end
    end
75 76 77 78
  end

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

81
  def web_url
82
    Gitlab::Routing.url_helpers.group_canonical_url(self)
83 84
  end

85 86 87
  def human_name
    name
  end
88

Felipe Artur committed
89 90 91 92
  def visibility_level_field
    visibility_level
  end

93
  def visibility_level_allowed_by_projects
Douwe Maan committed
94
    allowed_by_projects = self.projects.where('visibility_level > ?', self.visibility_level).none?
95 96 97 98 99 100 101 102 103

    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

104
  def avatar_url(size = nil)
105
    if self[:avatar].present?
106 107 108 109
      [gitlab_config.url, avatar.url].join
    end
  end

110 111 112 113 114 115 116
  def lfs_enabled?
    return false unless Gitlab.config.lfs.enabled
    return Gitlab.config.lfs.enabled if self[:lfs_enabled].nil?

    self[:lfs_enabled]
  end

117 118 119 120 121 122 123 124
  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
    )
125 126
  end

127
  def add_user(user, access_level, current_user: nil, expires_at: nil)
128 129 130 131 132 133 134
    GroupMember.add_user(
      self,
      user,
      access_level,
      current_user: current_user,
      expires_at: expires_at
    )
135 136
  end

137
  def add_guest(user, current_user = nil)
138
    add_user(user, :guest, current_user: current_user)
139 140 141
  end

  def add_reporter(user, current_user = nil)
142
    add_user(user, :reporter, current_user: current_user)
143 144 145
  end

  def add_developer(user, current_user = nil)
146
    add_user(user, :developer, current_user: current_user)
147 148 149
  end

  def add_master(user, current_user = nil)
150
    add_user(user, :master, current_user: current_user)
151 152
  end

Douwe Maan committed
153
  def add_owner(user, current_user = nil)
154
    add_user(user, :owner, current_user: current_user)
Douwe Maan committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168
  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
169 170 171 172 173
  def avatar_type
    unless self.avatar.image?
      self.errors.add :avatar, "only images allowed"
    end
  end
174

175
  def post_create_hook
176 177
    Gitlab::AppLogger.info("Group \"#{name}\" was created")

178 179 180 181
    system_hook_service.execute_hooks_for(self, :create)
  end

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

184 185 186 187 188 189
    system_hook_service.execute_hooks_for(self, :destroy)
  end

  def system_hook_service
    SystemHooksService.new
  end
190 191 192 193

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