Class: Group

Inherits:
Namespace show all
Includes:
Gitlab::ConfigHelper, Gitlab::VisibilityLevel, Referable
Defined in:
app/models/group.rb

Constant Summary

Constants included from Gitlab::VisibilityLevel

Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PRIVATE, Gitlab::VisibilityLevel::PUBLIC

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Referable

#reference_link_text

Methods included from Gitlab::VisibilityLevel

allowed_for?, allowed_level?, #internal?, level_name, non_restricted_level?, options, #private?, #public?, valid_level?, values

Methods included from Gitlab::CurrentSettings

#current_application_settings, #fake_application_settings

Methods included from Gitlab::ConfigHelper

#gitlab_config, #gitlab_config_features

Methods inherited from Namespace

by_path, clean_path, #ensure_dir_exist, find_by_path_or_name, #find_fork_of, #kind, #move_dir, #rm_dir, #send_update_instructions, #to_param

Methods included from Gitlab::ShellAdapter

#gitlab_shell

Class Method Details

.reference_patternObject


65
66
67
# File 'app/models/group.rb', line 65

def reference_pattern
  User.reference_pattern
end

.reference_prefixObject


61
62
63
# File 'app/models/group.rb', line 61

def reference_prefix
  User.reference_prefix
end

.search(query) ⇒ Object

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.


50
51
52
53
54
55
# File 'app/models/group.rb', line 50

def search(query)
  table   = Namespace.arel_table
  pattern = "%#{query}%"

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

.sort(method) ⇒ Object


57
58
59
# File 'app/models/group.rb', line 57

def sort(method)
  order_by(method)
end

.visible_to_user(user) ⇒ Object


69
70
71
# File 'app/models/group.rb', line 69

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

Instance Method Details

#add_developer(user, current_user = nil) ⇒ Object


125
126
127
# File 'app/models/group.rb', line 125

def add_developer(user, current_user = nil)
  add_user(user, Gitlab::Access::DEVELOPER, current_user)
end

#add_guest(user, current_user = nil) ⇒ Object


117
118
119
# File 'app/models/group.rb', line 117

def add_guest(user, current_user = nil)
  add_user(user, Gitlab::Access::GUEST, current_user)
end

#add_master(user, current_user = nil) ⇒ Object


129
130
131
# File 'app/models/group.rb', line 129

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

#add_owner(user, current_user = nil) ⇒ Object


133
134
135
# File 'app/models/group.rb', line 133

def add_owner(user, current_user = nil)
  add_user(user, Gitlab::Access::OWNER, current_user)
end

#add_reporter(user, current_user = nil) ⇒ Object


121
122
123
# File 'app/models/group.rb', line 121

def add_reporter(user, current_user = nil)
  add_user(user, Gitlab::Access::REPORTER, current_user)
end

#add_user(user, access_level, current_user = nil) ⇒ Object


113
114
115
# File 'app/models/group.rb', line 113

def add_user(user, access_level, current_user = nil)
  add_users([user], access_level, current_user)
end

#add_users(user_ids, access_level, current_user = nil) ⇒ Object


107
108
109
110
111
# File 'app/models/group.rb', line 107

def add_users(user_ids, access_level, current_user = nil)
  user_ids.each do |user_id|
    Member.add_user(self.group_members, user_id, access_level, current_user)
  end
end

#avatar_typeObject


149
150
151
152
153
# File 'app/models/group.rb', line 149

def avatar_type
  unless self.avatar.image?
    self.errors.add :avatar, "only images allowed"
  end
end

#avatar_url(size = nil) ⇒ Object


97
98
99
100
101
# File 'app/models/group.rb', line 97

def avatar_url(size = nil)
  if avatar.present?
    [gitlab_config.url, avatar.url].join
  end
end

#has_master?(user) ⇒ Boolean

Returns:

  • (Boolean)

141
142
143
# File 'app/models/group.rb', line 141

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

#has_owner?(user) ⇒ Boolean

Returns:

  • (Boolean)

137
138
139
# File 'app/models/group.rb', line 137

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

#human_nameObject


78
79
80
# File 'app/models/group.rb', line 78

def human_name
  name
end

#last_owner?(user) ⇒ Boolean

Returns:

  • (Boolean)

145
146
147
# File 'app/models/group.rb', line 145

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

#ownersObject


103
104
105
# File 'app/models/group.rb', line 103

def owners
  @owners ||= group_members.owners.includes(:user).map(&:user)
end

#post_create_hookObject


155
156
157
158
159
# File 'app/models/group.rb', line 155

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

  system_hook_service.execute_hooks_for(self, :create)
end

#post_destroy_hookObject


161
162
163
164
165
# File 'app/models/group.rb', line 161

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

  system_hook_service.execute_hooks_for(self, :destroy)
end

#system_hook_serviceObject


167
168
169
# File 'app/models/group.rb', line 167

def system_hook_service
  SystemHooksService.new
end

#to_reference(_from_project = nil) ⇒ Object


74
75
76
# File 'app/models/group.rb', line 74

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

#visibility_level_allowed_by_projectsObject


86
87
88
89
90
91
92
93
94
95
# File 'app/models/group.rb', line 86

def visibility_level_allowed_by_projects
  allowed_by_projects = self.projects.where('visibility_level > ?', self.visibility_level).none?

  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

#visibility_level_fieldObject


82
83
84
# File 'app/models/group.rb', line 82

def visibility_level_field
  visibility_level
end