Commit c6a0f109 authored by James Lopez's avatar James Lopez

refactored code as projects only have one owner. Kept some refactoring in...

refactored code as projects only have one owner. Kept some refactoring in place (has_owners concern)
parent 1b14bc59
...@@ -6,17 +6,17 @@ class Ability ...@@ -6,17 +6,17 @@ class Ability
return [] if user.blocked? return [] if user.blocked?
case subject.class.name case subject.class.name
when "Project" then project_abilities(user, subject) when "Project" then project_abilities(user, subject)
when "Issue" then issue_abilities(user, subject) when "Issue" then issue_abilities(user, subject)
when "Note" then note_abilities(user, subject) when "Note" then note_abilities(user, subject)
when "ProjectSnippet" then project_snippet_abilities(user, subject) when "ProjectSnippet" then project_snippet_abilities(user, subject)
when "PersonalSnippet" then personal_snippet_abilities(user, subject) when "PersonalSnippet" then personal_snippet_abilities(user, subject)
when "MergeRequest" then merge_request_abilities(user, subject) when "MergeRequest" then merge_request_abilities(user, subject)
when "Group" then group_abilities(user, subject) when "Group" then group_abilities(user, subject)
when "Namespace" then namespace_abilities(user, subject) when "Namespace" then namespace_abilities(user, subject)
when "GroupMember" then group_member_abilities(user, subject) when "GroupMember" then group_member_abilities(user, subject)
when "ProjectMember" then project_member_abilities(user, subject) when "ProjectMember" then project_member_abilities(user, subject)
else [] else []
end.concat(global_abilities(user)) end.concat(global_abilities(user))
end end
...@@ -232,17 +232,17 @@ class Ability ...@@ -232,17 +232,17 @@ class Ability
# Only group masters and group owners can create new projects in group # Only group masters and group owners can create new projects in group
if group.has_master?(user) || group.has_owner?(user) || user.admin? if group.has_master?(user) || group.has_owner?(user) || user.admin?
rules.push(*[ rules.push(*[
:create_projects, :create_projects,
]) ])
end end
# Only group owner and administrators can admin group # Only group owner and administrators can admin group
if group.has_owner?(user) || user.admin? if group.has_owner?(user) || user.admin?
rules.push(*[ rules.push(*[
:admin_group, :admin_group,
:admin_namespace, :admin_namespace,
:admin_group_member :admin_group_member
]) ])
end end
rules.flatten rules.flatten
...@@ -254,9 +254,9 @@ class Ability ...@@ -254,9 +254,9 @@ class Ability
# Only namespace owner and administrators can admin it # Only namespace owner and administrators can admin it
if namespace.owner == user || user.admin? if namespace.owner == user || user.admin?
rules.push(*[ rules.push(*[
:create_projects, :create_projects,
:admin_namespace :admin_namespace
]) ])
end end
rules.flatten rules.flatten
...@@ -323,12 +323,12 @@ class Ability ...@@ -323,12 +323,12 @@ class Ability
project = subject.project project = subject.project
can_manage = project_abilities(user, project).include?(:admin_project_member) can_manage = project_abilities(user, project).include?(:admin_project_member)
if can_manage && (user != target_user) if can_manage && user != target_user && target_user != project.owner
rules << :update_project_member rules << :update_project_member
rules << :destroy_project_member rules << :destroy_project_member
end end
if !project.last_owner?(user) && (can_manage || (user == target_user)) if user == target_user && target_user != project.owner
rules << :destroy_project_member rules << :destroy_project_member
end end
rules rules
...@@ -336,10 +336,10 @@ class Ability ...@@ -336,10 +336,10 @@ class Ability
def abilities def abilities
@abilities ||= begin @abilities ||= begin
abilities = Six.new abilities = Six.new
abilities << self abilities << self
abilities abilities
end end
end end
private private
......
...@@ -6,10 +6,10 @@ module HasOwners ...@@ -6,10 +6,10 @@ module HasOwners
extend ActiveSupport::Concern extend ActiveSupport::Concern
def owners def owners
@owners ||= my_members.owners.includes(:user).map(&:user) @owners ||= members.owners.includes(:user).map(&:user)
end end
def my_members def members
raise NotImplementedError, "Expected my_members to be defined in #{self.class.name}" raise NotImplementedError, "Expected my_members to be defined in #{self.class.name}"
end end
......
...@@ -22,7 +22,7 @@ class Group < Namespace ...@@ -22,7 +22,7 @@ class Group < Namespace
include HasOwners include HasOwners
has_many :group_members, dependent: :destroy, as: :source, class_name: 'GroupMember' has_many :group_members, dependent: :destroy, as: :source, class_name: 'GroupMember'
alias_method :my_members, :group_members alias_method :members, :group_members
has_many :users, through: :group_members has_many :users, through: :group_members
validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? } validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
...@@ -91,10 +91,6 @@ class Group < Namespace ...@@ -91,10 +91,6 @@ class Group < Namespace
add_user(user, Gitlab::Access::MASTER, current_user) add_user(user, Gitlab::Access::MASTER, current_user)
end end
def members
group_members
end
def avatar_type def avatar_type
unless self.avatar.image? unless self.avatar.image?
self.errors.add :avatar, "only images allowed" self.errors.add :avatar, "only images allowed"
......
...@@ -30,13 +30,13 @@ class Member < ActiveRecord::Base ...@@ -30,13 +30,13 @@ class Member < ActiveRecord::Base
validates :user, presence: true, unless: :invite? validates :user, presence: true, unless: :invite?
validates :source, presence: true validates :source, presence: true
validates :user_id, uniqueness: { scope: [:source_type, :source_id], validates :user_id, uniqueness: { scope: [:source_type, :source_id],
message: "already exists in source", message: "already exists in source",
allow_nil: true } allow_nil: true }
validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true
validates :invite_email, presence: { if: :invite? }, validates :invite_email, presence: { if: :invite? },
email: { strict_mode: true, allow_nil: true }, email: { strict_mode: true, allow_nil: true },
uniqueness: { scope: [:source_type, :source_id], allow_nil: true } uniqueness: { scope: [:source_type, :source_id], allow_nil: true }
scope :invite, -> { where(user_id: nil) } scope :invite, -> { where(user_id: nil) }
scope :non_invite, -> { where("user_id IS NOT NULL") } scope :non_invite, -> { where("user_id IS NOT NULL") }
...@@ -94,8 +94,7 @@ class Member < ActiveRecord::Base ...@@ -94,8 +94,7 @@ class Member < ActiveRecord::Base
def can_update_member?(current_user, member) def can_update_member?(current_user, member)
!current_user || current_user.can?(:update_group_member, member) || !current_user || current_user.can?(:update_group_member, member) ||
(member.respond_to?(:project) && current_user.can?(:update_project_member, member)
current_user.can?(:update_project_member, member))
end end
end end
...@@ -105,7 +104,7 @@ class Member < ActiveRecord::Base ...@@ -105,7 +104,7 @@ class Member < ActiveRecord::Base
def accept_invite!(new_user) def accept_invite!(new_user)
return false unless invite? return false unless invite?
self.invite_token = nil self.invite_token = nil
self.invite_accepted_at = Time.now.utc self.invite_accepted_at = Time.now.utc
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment