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

This commit is contained in:
Dmitriy Zaporozhets 2012-11-25 11:57:01 +02:00
parent 2f22874ba6
commit c31d48dd25
7 changed files with 62 additions and 13 deletions

View File

@ -95,7 +95,6 @@ class Project < ActiveRecord::Base
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 @@ class Project < ActiveRecord::Base
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

View File

@ -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 @@ class User < ActiveRecord::Base
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

View File

@ -26,6 +26,18 @@ module Account
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 @@ module Account
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

View File

@ -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

View File

@ -22,4 +22,14 @@ describe Namespace do
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

View File

@ -40,6 +40,7 @@ describe Project do
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

View File

@ -40,6 +40,7 @@ describe User do
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) }