Commit c31d48dd authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets
Browse files

Allow project creation in scope of group for non-admin but group owners

parent 3b4cab090906
......@@ -95,7 +95,6 @@ def find_with_namespace(id)
def create_by_user(params, user)
namespace_id = params.delete(:namespace_id)
namespace_id ||= user.namespace.try(:id)
project = Project.new params
......@@ -109,7 +108,18 @@ def create_by_user(params, user)
project.path = project.name.dup.parameterize
project.owner = user
project.namespace_id = namespace_id
# Apply namespace if user has access to it
# else fallback to user namespace
project.namespace_id = user.namespace_id
if namespace_id
group = Group.find_by_id(namespace_id)
if user.can? :manage_group, group
project.namespace_id = namespace_id
end
end
project.save!
# Add user as project master
......
......@@ -48,6 +48,7 @@ class User < ActiveRecord::Base
# Namespace for personal projects
has_one :namespace, class_name: "Namespace", foreign_key: :owner_id, conditions: 'type IS NULL', dependent: :destroy
has_many :groups, class_name: "Group", foreign_key: :owner_id
has_many :keys, dependent: :destroy
has_many :projects, through: :users_projects
......@@ -120,15 +121,4 @@ def generate_password
self.password = self.password_confirmation = Devise.friendly_token.first(8)
end
end
def namespaces
namespaces = []
namespaces << self.namespace if self.namespace
namespaces = namespaces + Group.all if admin
namespaces
end
def several_namespaces?
namespaces.size > 1
end
end
......@@ -26,6 +26,18 @@ def can_create_group?
is_admin?
end
def abilities
@abilities ||= begin
abilities = Six.new
abilities << Ability
abilities
end
end
def can? action, subject
abilities.allowed?(self, action, subject)
end
def last_activity_project
projects.first
end
......@@ -70,4 +82,27 @@ def recent_push project_id = nil
def projects_sorted_by_activity
projects.order("(SELECT max(events.created_at) FROM events WHERE events.project_id = projects.id) DESC")
end
def namespaces
namespaces = []
# Add user account namespace
namespaces << self.namespace if self.namespace
# Add groups you can manage
namespaces += if admin
Group.all
else
groups.all
end
namespaces
end
def several_namespaces?
namespaces.size > 1
end
def namespace_id
namespace.try :id
end
end
......@@ -9,4 +9,6 @@
= image_tag gravatar_icon(user.email, 16), class: "avatar s16"
%strong= user.name
%span.cgray= user.email
- if @group.owner == user
%span.btn.btn-small.disabled.right Owner
......@@ -22,4 +22,14 @@
it { should validate_presence_of :path }
it { should validate_uniqueness_of(:path) }
it { should validate_presence_of :owner }
describe "Mass assignment" do
it { should allow_mass_assignment_of(:name) }
it { should allow_mass_assignment_of(:path) }
end
describe "Respond to" do
it { should respond_to(:human_name) }
it { should respond_to(:to_param) }
end
end
......@@ -40,6 +40,7 @@
end
describe "Mass assignment" do
it { should_not allow_mass_assignment_of(:namespace_id) }
it { should_not allow_mass_assignment_of(:owner_id) }
it { should_not allow_mass_assignment_of(:private_flag) }
end
......
......@@ -40,6 +40,7 @@
it { should have_one(:namespace) }
it { should have_many(:users_projects).dependent(:destroy) }
it { should have_many(:projects) }
it { should have_many(:groups) }
it { should have_many(:my_own_projects).class_name('Project') }
it { should have_many(:keys).dependent(:destroy) }
it { should have_many(:events).class_name('Event').dependent(:destroy) }
......
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