2013-06-19 08:40:33 -04:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: users_groups
|
|
|
|
#
|
2013-08-21 05:34:02 -04:00
|
|
|
# id :integer not null, primary key
|
|
|
|
# group_access :integer not null
|
|
|
|
# group_id :integer not null
|
|
|
|
# user_id :integer not null
|
2014-04-09 08:05:03 -04:00
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
2013-08-21 05:34:02 -04:00
|
|
|
# notification_level :integer default(3), not null
|
2013-06-19 08:40:33 -04:00
|
|
|
#
|
|
|
|
|
2013-06-17 06:07:16 -04:00
|
|
|
class UsersGroup < ActiveRecord::Base
|
2013-06-21 16:17:58 -04:00
|
|
|
include Notifiable
|
2013-08-20 08:59:45 -04:00
|
|
|
include Gitlab::Access
|
2013-06-17 06:07:16 -04:00
|
|
|
|
2013-06-17 06:26:58 -04:00
|
|
|
def self.group_access_roles
|
2013-08-20 08:59:45 -04:00
|
|
|
Gitlab::Access.options_with_owner
|
2013-06-17 06:26:58 -04:00
|
|
|
end
|
|
|
|
|
2013-06-17 06:07:16 -04:00
|
|
|
belongs_to :user
|
2013-06-17 10:03:56 -04:00
|
|
|
belongs_to :group
|
2013-06-17 06:07:16 -04:00
|
|
|
|
|
|
|
scope :guests, -> { where(group_access: GUEST) }
|
|
|
|
scope :reporters, -> { where(group_access: REPORTER) }
|
|
|
|
scope :developers, -> { where(group_access: DEVELOPER) }
|
|
|
|
scope :masters, -> { where(group_access: MASTER) }
|
|
|
|
scope :owners, -> { where(group_access: OWNER) }
|
|
|
|
|
|
|
|
scope :with_group, ->(group) { where(group_id: group.id) }
|
|
|
|
scope :with_user, ->(user) { where(user_id: user.id) }
|
2013-06-17 06:26:58 -04:00
|
|
|
|
2014-06-17 14:36:40 -04:00
|
|
|
after_create :notify_create
|
|
|
|
after_update :notify_update
|
|
|
|
|
2013-06-17 06:26:58 -04:00
|
|
|
validates :group_access, inclusion: { in: UsersGroup.group_access_roles.values }, presence: true
|
|
|
|
validates :user_id, presence: true
|
|
|
|
validates :group_id, presence: true
|
2013-06-17 09:51:43 -04:00
|
|
|
validates :user_id, uniqueness: { scope: [:group_id], message: "already exists in group" }
|
2013-06-17 06:26:58 -04:00
|
|
|
|
2013-06-17 06:41:35 -04:00
|
|
|
delegate :name, :username, :email, to: :user, prefix: true
|
|
|
|
|
2013-08-20 08:59:45 -04:00
|
|
|
def access_field
|
|
|
|
group_access
|
2013-06-17 06:26:58 -04:00
|
|
|
end
|
2014-06-17 14:36:40 -04:00
|
|
|
|
|
|
|
def notify_create
|
|
|
|
notification_service.new_group_member(self)
|
|
|
|
end
|
|
|
|
|
|
|
|
def notify_update
|
|
|
|
if group_access_changed?
|
|
|
|
notification_service.update_group_member(self)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def notification_service
|
|
|
|
NotificationService.new
|
|
|
|
end
|
2013-06-17 06:07:16 -04:00
|
|
|
end
|