def create_by_user(params, user)
namespace_id = params.delete(:namespace_id)
- namespace_id ||= user.namespace.try(:id)
project = Project.new params
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
# 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
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
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
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
= 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
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
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
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) }