Commit 232d61d5 authored by GitLab's avatar GitLab

Refactor project creation. Added logout link to profile page

parent c7c1a97c
...@@ -42,9 +42,6 @@ ...@@ -42,9 +42,6 @@
line-height: 20px; line-height: 20px;
padding: 8px; padding: 8px;
} }
label {
color: #888;
}
.btn { .btn {
padding: 6px 10px; padding: 6px 10px;
margin-left: 10px; margin-left: 10px;
......
class ProjectUpdateContext < BaseContext
def execute(role = :default)
namespace_id = params[:project].delete(:namespace_id)
params[:project].delete(:public) unless can?(current_user, :change_public_mode, project)
allowed_transfer = can?(current_user, :change_namespace, project) || role == :admin
if allowed_transfer && namespace_id.present?
if namespace_id == Namespace.global_id
if project.namespace.present?
# Transfer to global namespace from anyone
project.transfer(nil)
end
elsif namespace_id.to_i != project.namespace_id
# Transfer to someone namespace
namespace = Namespace.find(namespace_id)
project.transfer(namespace)
end
end
project.update_attributes(params[:project], as: role)
end
end
module Projects
class CreateContext < BaseContext
def execute
# get namespace id
namespace_id = params[:project].delete(:namespace_id)
@project = Project.new(params[:project])
# Parametrize path for project
#
# Ex.
# 'GitLab HQ'.parameterize => "gitlab-hq"
#
@project.path = @project.name.dup.parameterize
if namespace_id
# Find matching namespace and check if it allowed
# for current user if namespace_id passed.
if allowed_namespace?(current_user, namespace_id)
@project.namespace_id = namespace_id unless namespace_id == Namespace.global_id
else
deny_namespace
return @project
end
else
# Set current user namespace if namespace_id is nil
@project.namespace_id = current_user.id
end
Project.transaction do
@project.creator = current_user
@project.save!
# Add user as project master
@project.users_projects.create!(project_access: UsersProject::MASTER, user: current_user)
# when project saved no team member exist so
# project repository should be updated after first user add
@project.update_repository
end
@project
rescue => ex
@project.errors.add(:base, "Can't save project. Please try again later")
@project
end
protected
def deny_namespace
@project.errors.add(:namespace, "is not valid")
end
def allowed_namespace?(user, namespace_id)
if namespace_id == Namespace.global_id
return user.admin
else
namespace = Namespace.find_by_id(namespace_id)
current_user.can?(:manage_namespace, namespace)
end
end
end
end
module Projects
class UpdateContext < BaseContext
def execute(role = :default)
namespace_id = params[:project].delete(:namespace_id)
params[:project].delete(:public) unless can?(current_user, :change_public_mode, project)
allowed_transfer = can?(current_user, :change_namespace, project) || role == :admin
if allowed_transfer && namespace_id.present?
if namespace_id == Namespace.global_id
if project.namespace.present?
# Transfer to global namespace from anyone
project.transfer(nil)
end
elsif namespace_id.to_i != project.namespace_id
# Transfer to someone namespace
namespace = Namespace.find(namespace_id)
project.transfer(namespace)
end
end
project.update_attributes(params[:project], as: role)
end
end
end
...@@ -29,7 +29,7 @@ class Admin::ProjectsController < AdminController ...@@ -29,7 +29,7 @@ class Admin::ProjectsController < AdminController
end end
def update def update
status = ProjectUpdateContext.new(project, current_user, params).execute(:admin) status = Projects::UpdateContext.new(project, current_user, params).execute(:admin)
if status if status
redirect_to [:admin, @project], notice: 'Project was successfully updated.' redirect_to [:admin, @project], notice: 'Project was successfully updated.'
......
...@@ -19,7 +19,7 @@ class ProjectsController < ProjectResourceController ...@@ -19,7 +19,7 @@ class ProjectsController < ProjectResourceController
end end
def create def create
@project = Project.create_by_user(params[:project], current_user) @project = Projects::CreateContext.new(nil, current_user, params).execute
respond_to do |format| respond_to do |format|
flash[:notice] = 'Project was successfully created.' if @project.saved? flash[:notice] = 'Project was successfully created.' if @project.saved?
...@@ -35,7 +35,7 @@ class ProjectsController < ProjectResourceController ...@@ -35,7 +35,7 @@ class ProjectsController < ProjectResourceController
end end
def update def update
status = ProjectUpdateContext.new(project, current_user, params).execute status = Projects::UpdateContext.new(project, current_user, params).execute
respond_to do |format| respond_to do |format|
if status if status
......
module NamespacesHelper module NamespacesHelper
def namespaces_options(selected = :current_user, scope = :default) def namespaces_options(selected = :current_user, scope = :default)
groups = current_user.owned_groups.select {|n| n.type == 'Group'} if current_user.admin
groups = Group.all
users = if scope == :all users = Namespace.root
Namespace.root
else else
current_user.namespaces.reject {|n| n.type == 'Group'} groups = current_user.owned_groups.select {|n| n.type == 'Group'}
users = current_user.namespaces.reject {|n| n.type == 'Group'}
end end
global_opts = ["Global", [['/', Namespace.global_id]] ] global_opts = ["Global", [['/', Namespace.global_id]] ]
group_opts = ["Groups", groups.map {|g| [g.human_name, g.id]} ] group_opts = ["Groups", groups.map {|g| [g.human_name, g.id]} ]
users_opts = [ "Users", users.map {|u| [u.human_name, u.id]} ] users_opts = [ "Users", users.map {|u| [u.human_name, u.id]} ]
......
...@@ -7,7 +7,7 @@ class Ability ...@@ -7,7 +7,7 @@ class Ability
when "Note" then note_abilities(object, subject) when "Note" then note_abilities(object, subject)
when "Snippet" then snippet_abilities(object, subject) when "Snippet" then snippet_abilities(object, subject)
when "MergeRequest" then merge_request_abilities(object, subject) when "MergeRequest" then merge_request_abilities(object, subject)
when "Group" then group_abilities(object, subject) when "Group", "Namespace" then group_abilities(object, subject)
else [] else []
end end
end end
...@@ -102,7 +102,8 @@ class Ability ...@@ -102,7 +102,8 @@ class Ability
# Only group owner and administrators can manage group # Only group owner and administrators can manage group
if group.owner == user || user.admin? if group.owner == user || user.admin?
rules << [ rules << [
:manage_group :manage_group,
:manage_namespace
] ]
end end
......
...@@ -116,55 +116,6 @@ class Project < ActiveRecord::Base ...@@ -116,55 +116,6 @@ class Project < ActiveRecord::Base
end end
end end
def create_by_user(params, user)
namespace_id = params.delete(:namespace_id)
project = Project.new params
Project.transaction do
# Parametrize path for project
#
# Ex.
# 'GitLab HQ'.parameterize => "gitlab-hq"
#
project.path = project.name.dup.parameterize
project.creator = user
# Apply namespace if user has access to it
# else fallback to user namespace
if namespace_id != Namespace.global_id
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
end
project.save!
# Add user as project master
project.users_projects.create!(project_access: UsersProject::MASTER, user: user)
# when project saved no team member exist so
# project repository should be updated after first user add
project.update_repository
end
project
rescue Gitlab::Gitolite::AccessDenied => ex
project.error_code = :gitolite
project
rescue => ex
project.error_code = :db
project.errors.add(:base, "Can't save project. Please try again later")
project
end
def access_options def access_options
UsersProject.access_roles UsersProject.access_roles
end end
......
...@@ -152,11 +152,8 @@ class User < ActiveRecord::Base ...@@ -152,11 +152,8 @@ class User < ActiveRecord::Base
namespaces << self.namespace if self.namespace namespaces << self.namespace if self.namespace
# Add groups you can manage # Add groups you can manage
namespaces += if admin namespaces += groups.all
Group.all
else
groups.all
end
namespaces namespaces
end end
...@@ -234,6 +231,10 @@ class User < ActiveRecord::Base ...@@ -234,6 +231,10 @@ class User < ActiveRecord::Base
end end
end end
def can_select_namespace?
several_namespaces? || admin
end
def can? action, subject def can? action, subject
abilities.allowed?(self, action, subject) abilities.allowed?(self, action, subject)
end end
......
...@@ -9,6 +9,6 @@ ...@@ -9,6 +9,6 @@
%hr %hr
.gitlab-promo .gitlab-promo
= link_to "Homepage", "http://gitlabhq.com" = link_to "Homepage", "http://gitlab.org"
= link_to "Blog", "http://blog.gitlabhq.com" = link_to "Blog", "http://blog.gitlab.org"
= link_to "@gitlabhq", "https://twitter.com/gitlabhq" = link_to "@gitlabhq", "https://twitter.com/gitlabhq"
...@@ -6,6 +6,11 @@ ...@@ -6,6 +6,11 @@
%small %small
= @user.email = @user.email
.right
= link_to destroy_user_session_path, class: "logout", method: :delete do
%small
%i.icon-signout
Logout
%hr %hr
= form_for @user, url: profile_path, method: :put, html: { class: "edit_user form-horizontal" } do |f| = form_for @user, url: profile_path, method: :put, html: { class: "edit_user form-horizontal" } do |f|
......
...@@ -9,20 +9,12 @@ ...@@ -9,20 +9,12 @@
Project name is Project name is
.input .input
= f.text_field :name, placeholder: "Example Project", class: "xxlarge" = f.text_field :name, placeholder: "Example Project", class: "xxlarge"
%fieldset
%legend Advanced settings:
.control-group
= f.label :path do
Repository
.controls
= text_field_tag :ppath, @repository.path_to_repo, class: "xxlarge", readonly: true
- unless @repository.heads.empty? - unless @repository.heads.empty?
.clearfix .clearfix
= f.label :default_branch, "Default Branch" = f.label :default_branch, "Default Branch"
.input= f.select(:default_branch, @repository.heads.map(&:name), {}, style: "width:210px;") .input= f.select(:default_branch, @repository.heads.map(&:name), {}, style: "width:210px;")
%fieldset.features %fieldset.features
%legend Features: %legend Features:
...@@ -87,4 +79,4 @@ ...@@ -87,4 +79,4 @@
- unless @project.new_record? - unless @project.new_record?
- if can?(current_user, :remove_project, @project) - if can?(current_user, :remove_project, @project)
.right .right
= link_to 'Remove', @project, confirm: 'Removed project can not be restored! Are you sure?', method: :delete, class: "btn danger" = link_to 'Remove Project', @project, confirm: 'Removed project can not be restored! Are you sure?', method: :delete, class: "btn danger"
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
= f.text_field :name, placeholder: "Example Project", class: "xxlarge" = f.text_field :name, placeholder: "Example Project", class: "xxlarge"
= f.submit 'Create project', class: "btn success project-submit" = f.submit 'Create project', class: "btn success project-submit"
- if current_user.several_namespaces? - if current_user.can_select_namespace?
.clearfix .clearfix
= f.label :namespace_id do = f.label :namespace_id do
%span.cgray Namespace %span Namespace
.input .input
= f.select :namespace_id, namespaces_options(params[:namespace_id] || :current_user), {}, {class: 'chosen'} = f.select :namespace_id, namespaces_options(params[:namespace_id] || :current_user), {}, {class: 'chosen'}
%hr %hr
......
...@@ -2,11 +2,9 @@ ...@@ -2,11 +2,9 @@
:plain :plain
location.href = "#{project_path(@project)}"; location.href = "#{project_path(@project)}";
- else - else
- if @project.git_error?
location.href = "#{errors_githost_path}";
-else
:plain :plain
$('.project_new_holder').show(); $('.project_new_holder').show();
$("#new_project").replaceWith("#{escape_javascript(render('new_form'))}"); $("#new_project").replaceWith("#{escape_javascript(render('new_form'))}");
$('.save-project-loader').hide(); $('.save-project-loader').hide();
new Projects(); new Projects();
$('select.chosen').chosen()
Feature: Projects Feature: Project Feature
Background: Background:
Given I sign in as a user Given I sign in as a user
And I own project "Shop" And I own project "Shop"
......
class Projects < Spinach::FeatureSteps class ProjectFeature < Spinach::FeatureSteps
include SharedAuthentication include SharedAuthentication
include SharedProject include SharedProject
include SharedPaths include SharedPaths
......
...@@ -47,7 +47,6 @@ module SharedProject ...@@ -47,7 +47,6 @@ module SharedProject
Then 'I should see project settings' do Then 'I should see project settings' do
current_path.should == edit_project_path(@project) current_path.should == edit_project_path(@project)
page.should have_content("Project name is") page.should have_content("Project name is")
page.should have_content("Advanced settings:")
page.should have_content("Features:") page.should have_content("Features:")
end end
......
...@@ -43,7 +43,7 @@ module Gitlab ...@@ -43,7 +43,7 @@ module Gitlab
:wall_enabled, :wall_enabled,
:merge_requests_enabled, :merge_requests_enabled,
:wiki_enabled] :wiki_enabled]
@project = Project.create_by_user(attrs, current_user) @project = Projects::CreateContext.new(nil, attrs, current_user).execute
if @project.saved? if @project.saved?
present @project, with: Entities::Project present @project, with: Entities::Project
else else
......
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