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
return [] if user.blocked?
case subject.class.name
when "Project" then project_abilities(user, subject)
when "Issue" then issue_abilities(user, subject)
when "Note" then note_abilities(user, subject)
when "ProjectSnippet" then project_snippet_abilities(user, subject)
when "PersonalSnippet" then personal_snippet_abilities(user, subject)
when "MergeRequest" then merge_request_abilities(user, subject)
when "Group" then group_abilities(user, subject)
when "Namespace" then namespace_abilities(user, subject)
when "GroupMember" then group_member_abilities(user, subject)
when "ProjectMember" then project_member_abilities(user, subject)
else []
when "Project" then project_abilities(user, subject)
when "Issue" then issue_abilities(user, subject)
when "Note" then note_abilities(user, subject)
when "ProjectSnippet" then project_snippet_abilities(user, subject)
when "PersonalSnippet" then personal_snippet_abilities(user, subject)
when "MergeRequest" then merge_request_abilities(user, subject)
when "Group" then group_abilities(user, subject)
when "Namespace" then namespace_abilities(user, subject)
when "GroupMember" then group_member_abilities(user, subject)
when "ProjectMember" then project_member_abilities(user, subject)
else []
end.concat(global_abilities(user))
end
......@@ -232,17 +232,17 @@ class Ability
# Only group masters and group owners can create new projects in group
if group.has_master?(user) || group.has_owner?(user) || user.admin?
rules.push(*[
:create_projects,
])
:create_projects,
])
end
# Only group owner and administrators can admin group
if group.has_owner?(user) || user.admin?
rules.push(*[
:admin_group,
:admin_namespace,
:admin_group_member
])
:admin_group,
:admin_namespace,
:admin_group_member
])
end
rules.flatten
......@@ -254,9 +254,9 @@ class Ability
# Only namespace owner and administrators can admin it
if namespace.owner == user || user.admin?
rules.push(*[
:create_projects,
:admin_namespace
])
:create_projects,
:admin_namespace
])
end
rules.flatten
......@@ -323,12 +323,12 @@ class Ability
project = subject.project
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 << :destroy_project_member
end
if !project.last_owner?(user) && (can_manage || (user == target_user))
if user == target_user && target_user != project.owner
rules << :destroy_project_member
end
rules
......@@ -336,10 +336,10 @@ class Ability
def abilities
@abilities ||= begin
abilities = Six.new
abilities << self
abilities
end
abilities = Six.new
abilities << self
abilities
end
end
private
......
......@@ -6,10 +6,10 @@ module HasOwners
extend ActiveSupport::Concern
def owners
@owners ||= my_members.owners.includes(:user).map(&:user)
@owners ||= members.owners.includes(:user).map(&:user)
end
def my_members
def members
raise NotImplementedError, "Expected my_members to be defined in #{self.class.name}"
end
......
......@@ -22,7 +22,7 @@ class Group < Namespace
include HasOwners
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
validate :avatar_type, if: ->(user) { user.avatar.present? && user.avatar_changed? }
......@@ -91,10 +91,6 @@ class Group < Namespace
add_user(user, Gitlab::Access::MASTER, current_user)
end
def members
group_members
end
def avatar_type
unless self.avatar.image?
self.errors.add :avatar, "only images allowed"
......
......@@ -30,13 +30,13 @@ class Member < ActiveRecord::Base
validates :user, presence: true, unless: :invite?
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",
allow_nil: true }
validates :access_level, inclusion: { in: Gitlab::Access.all_values }, presence: true
validates :invite_email, presence: { if: :invite? },
email: { strict_mode: true, allow_nil: true },
uniqueness: { scope: [:source_type, :source_id], allow_nil: true }
validates :invite_email, presence: { if: :invite? },
email: { strict_mode: true, allow_nil: true },
uniqueness: { scope: [:source_type, :source_id], allow_nil: true }
scope :invite, -> { where(user_id: nil) }
scope :non_invite, -> { where("user_id IS NOT NULL") }
......@@ -94,8 +94,7 @@ class Member < ActiveRecord::Base
def can_update_member?(current_user, 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
......@@ -105,7 +104,7 @@ class Member < ActiveRecord::Base
def accept_invite!(new_user)
return false unless invite?
self.invite_token = nil
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