Class: Member

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Gitlab::Access, Sortable
Defined in:
app/models/member.rb

Overview

Schema Information

Table name: members

id                 :integer          not null, primary key
access_level       :integer          not null
source_id          :integer          not null
source_type        :string           not null
user_id            :integer
notification_level :integer          not null
type               :string
created_at         :datetime
updated_at         :datetime
created_by_id      :integer
invite_email       :string
invite_token       :string
invite_accepted_at :datetime

Direct Known Subclasses

GroupMember, ProjectMember

Constant Summary

Constants included from Gitlab::Access

Gitlab::Access::DEVELOPER, Gitlab::Access::GUEST, Gitlab::Access::MASTER, Gitlab::Access::OWNER, Gitlab::Access::PROTECTION_DEV_CAN_PUSH, Gitlab::Access::PROTECTION_FULL, Gitlab::Access::PROTECTION_NONE, Gitlab::Access::REPORTER

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Access

all_values, #human_access, options, options_with_owner, #owner?, protection_options, protection_values, sym_options, values

Instance Attribute Details

#raw_invite_tokenObject

Returns the value of attribute raw_invite_token


24
25
26
# File 'app/models/member.rb', line 24

def raw_invite_token
  @raw_invite_token
end

Class Method Details

.add_user(members, user_id, access_level, current_user = nil) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/member.rb', line 84

def add_user(members, user_id, access_level, current_user = nil)
  user = user_for_id(user_id)

  # `user` can be either a User object or an email to be invited
  if user.is_a?(User)
    member = members.find_or_initialize_by(user_id: user.id)
  else
    member = members.build
    member.invite_email = user
  end

  if can_update_member?(current_user, member) || project_creator?(member, access_level)
    member.created_by ||= current_user
    member.access_level = access_level

    member.save
  end
end

.find_by_invite_token(invite_token) ⇒ Object


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

def find_by_invite_token(invite_token)
  invite_token = Devise.token_generator.digest(self, :invite_token, invite_token)
  find_by(invite_token: invite_token)
end

.user_for_id(user_id) ⇒ Object

This method is used to find users that have been entered into the “Add members” field. These can be the User objects directly, their IDs, their emails, or new emails to be invited.


75
76
77
78
79
80
81
82
# File 'app/models/member.rb', line 75

def user_for_id(user_id)
  return user_id if user_id.is_a?(User)

  user = User.find_by(id: user_id)
  user ||= User.find_by(email: user_id)
  user ||= user_id
  user
end

Instance Method Details

#accept_invite!(new_user) ⇒ Object


122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/models/member.rb', line 122

def accept_invite!(new_user)
  return false unless invite?

  self.invite_token = nil
  self.invite_accepted_at = Time.now.utc

  self.user = new_user

  saved = self.save

  after_accept_invite if saved

  saved
end

#create_notification_settingObject


165
166
167
# File 'app/models/member.rb', line 165

def create_notification_setting
  user.notification_settings.find_or_create_for(source)
end

#decline_invite!Object


137
138
139
140
141
142
143
144
145
# File 'app/models/member.rb', line 137

def decline_invite!
  return false unless invite?

  destroyed = self.destroy

  after_decline_invite if destroyed

  destroyed
end

#generate_invite_tokenObject


147
148
149
150
151
# File 'app/models/member.rb', line 147

def generate_invite_token
  raw, enc = Devise.token_generator.generate(self.class, :invite_token)
  @raw_invite_token = raw
  self.invite_token = enc
end

#generate_invite_token!Object


153
154
155
# File 'app/models/member.rb', line 153

def generate_invite_token!
  generate_invite_token && save(validate: false)
end

#invite?Boolean

Returns:

  • (Boolean)

118
119
120
# File 'app/models/member.rb', line 118

def invite?
  self.invite_token.present?
end

#notification_settingObject


169
170
171
# File 'app/models/member.rb', line 169

def notification_setting
  @notification_setting ||= user.notification_settings_for(source)
end

#resend_inviteObject


157
158
159
160
161
162
163
# File 'app/models/member.rb', line 157

def resend_invite
  return unless invite?

  generate_invite_token! unless @raw_invite_token

  send_invite
end