2014-10-09 11:22:20 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: members
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# access_level :integer not null
|
|
|
|
# source_id :integer not null
|
|
|
|
# source_type :string(255) not null
|
2015-05-03 12:05:38 -04:00
|
|
|
# user_id :integer
|
2014-10-09 11:22:20 -04:00
|
|
|
# notification_level :integer not null
|
|
|
|
# type :string(255)
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2015-04-10 08:46:09 -04:00
|
|
|
# created_by_id :integer
|
2015-05-03 12:05:38 -04:00
|
|
|
# invite_email :string(255)
|
|
|
|
# invite_token :string(255)
|
2015-04-10 08:46:09 -04:00
|
|
|
# invite_accepted_at :datetime
|
2014-10-09 11:22:20 -04:00
|
|
|
#
|
|
|
|
|
2014-09-14 10:54:10 -04:00
|
|
|
class Member < ActiveRecord::Base
|
2015-02-05 17:20:55 -05:00
|
|
|
include Sortable
|
2014-09-14 10:54:10 -04:00
|
|
|
include Notifiable
|
|
|
|
include Gitlab::Access
|
|
|
|
|
2015-04-14 12:04:29 -04:00
|
|
|
attr_accessor :raw_invite_token
|
|
|
|
|
2015-04-10 08:46:09 -04:00
|
|
|
belongs_to :created_by, class_name: "User"
|
2014-09-14 10:54:10 -04:00
|
|
|
belongs_to :user
|
|
|
|
belongs_to :source, polymorphic: true
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
validates :user, presence: true, unless: :invite?
|
2014-09-14 10:54:10 -04:00
|
|
|
validates :source, presence: true
|
2015-11-11 10:42:27 -05:00
|
|
|
validates :user_id, uniqueness: { scope: [:source_type, :source_id],
|
2015-04-10 09:09:37 -04:00
|
|
|
message: "already exists in source",
|
|
|
|
allow_nil: true }
|
2014-09-14 12:32:51 -04:00
|
|
|
validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true
|
2015-11-17 09:49:37 -05:00
|
|
|
validates :invite_email,
|
|
|
|
presence: {
|
|
|
|
if: :invite?
|
|
|
|
},
|
2016-02-09 11:59:47 -05:00
|
|
|
email: {
|
2015-11-17 09:49:37 -05:00
|
|
|
allow_nil: true
|
|
|
|
},
|
|
|
|
uniqueness: {
|
|
|
|
scope: [:source_type, :source_id],
|
|
|
|
allow_nil: true
|
|
|
|
}
|
2014-09-14 10:54:10 -04:00
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
scope :invite, -> { where(user_id: nil) }
|
|
|
|
scope :non_invite, -> { where("user_id IS NOT NULL") }
|
2014-09-14 12:32:51 -04:00
|
|
|
scope :guests, -> { where(access_level: GUEST) }
|
|
|
|
scope :reporters, -> { where(access_level: REPORTER) }
|
|
|
|
scope :developers, -> { where(access_level: DEVELOPER) }
|
|
|
|
scope :masters, -> { where(access_level: MASTER) }
|
|
|
|
scope :owners, -> { where(access_level: OWNER) }
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
before_validation :generate_invite_token, on: :create, if: -> (member) { member.invite_email.present? }
|
|
|
|
after_create :send_invite, if: :invite?
|
|
|
|
after_create :post_create_hook, unless: :invite?
|
|
|
|
after_update :post_update_hook, unless: :invite?
|
|
|
|
after_destroy :post_destroy_hook, unless: :invite?
|
|
|
|
|
2014-09-14 12:32:51 -04:00
|
|
|
delegate :name, :username, :email, to: :user, prefix: true
|
2015-04-10 09:09:37 -04:00
|
|
|
|
2015-04-14 06:33:27 -04:00
|
|
|
class << self
|
|
|
|
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
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
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
|
|
|
|
|
|
|
|
def add_user(members, user_id, access_level, current_user = nil)
|
|
|
|
user = user_for_id(user_id)
|
2015-10-30 15:55:19 -04:00
|
|
|
|
2015-04-14 06:33:27 -04:00
|
|
|
# `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
|
2015-10-30 15:55:19 -04:00
|
|
|
|
2016-01-20 13:55:47 -05:00
|
|
|
if can_update_member?(current_user, member) || project_creator?(member, access_level)
|
2015-10-29 12:10:27 -04:00
|
|
|
member.created_by ||= current_user
|
|
|
|
member.access_level = access_level
|
2015-04-14 06:33:27 -04:00
|
|
|
|
2015-10-29 12:10:27 -04:00
|
|
|
member.save
|
|
|
|
end
|
2015-04-14 06:33:27 -04:00
|
|
|
end
|
2015-10-30 15:55:19 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2015-11-03 06:11:56 -05:00
|
|
|
def can_update_member?(current_user, member)
|
2015-11-17 09:49:37 -05:00
|
|
|
# There is no current user for bulk actions, in which case anything is allowed
|
|
|
|
!current_user ||
|
|
|
|
current_user.can?(:update_group_member, member) ||
|
2015-11-11 10:42:27 -05:00
|
|
|
current_user.can?(:update_project_member, member)
|
2015-10-30 15:55:19 -04:00
|
|
|
end
|
2016-01-20 13:55:47 -05:00
|
|
|
|
|
|
|
def project_creator?(member, access_level)
|
|
|
|
member.new_record? && member.owner? &&
|
|
|
|
access_level.to_i == ProjectMember::MASTER
|
|
|
|
end
|
2015-04-10 09:22:31 -04:00
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def invite?
|
|
|
|
self.invite_token.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def accept_invite!(new_user)
|
2015-04-10 09:22:31 -04:00
|
|
|
return false unless invite?
|
2015-11-11 10:42:27 -05:00
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
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
|
|
|
|
|
2015-04-10 10:37:02 -04:00
|
|
|
def decline_invite!
|
|
|
|
return false unless invite?
|
|
|
|
|
|
|
|
destroyed = self.destroy
|
|
|
|
|
|
|
|
after_decline_invite if destroyed
|
|
|
|
|
|
|
|
destroyed
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def generate_invite_token
|
|
|
|
raw, enc = Devise.token_generator.generate(self.class, :invite_token)
|
|
|
|
@raw_invite_token = raw
|
|
|
|
self.invite_token = enc
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_invite_token!
|
|
|
|
generate_invite_token && save(validate: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
def resend_invite
|
|
|
|
return unless invite?
|
|
|
|
|
|
|
|
generate_invite_token! unless @raw_invite_token
|
|
|
|
|
|
|
|
send_invite
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def send_invite
|
|
|
|
# override in subclass
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_create_hook
|
|
|
|
system_hook_service.execute_hooks_for(self, :create)
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_update_hook
|
|
|
|
# override in subclass
|
|
|
|
end
|
|
|
|
|
|
|
|
def post_destroy_hook
|
|
|
|
system_hook_service.execute_hooks_for(self, :destroy)
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_accept_invite
|
|
|
|
post_create_hook
|
|
|
|
end
|
|
|
|
|
2015-04-10 10:37:02 -04:00
|
|
|
def after_decline_invite
|
|
|
|
# override in subclass
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:09:37 -04:00
|
|
|
def system_hook_service
|
|
|
|
SystemHooksService.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification_service
|
|
|
|
NotificationService.new
|
|
|
|
end
|
2014-09-14 10:54:10 -04:00
|
|
|
end
|