Commit 6db2843b authored by Valery Sizov's avatar Valery Sizov

fix of project creation

parent f5af4efa
......@@ -55,7 +55,7 @@ module Ci
def create
project_data = OpenStruct.new(JSON.parse(params["project"]))
unless can?(current_user, :manage_project, ::Project.find(project_data.id))
unless can?(current_user, :admin_project, ::Project.find(project_data.id))
return redirect_to ci_root_path, alert: 'You have to have at least master role to enable CI for this project'
end
......
module Ci
class UserSessionsController < Ci::ApplicationController
before_filter :authenticate_user!, except: [:new, :callback, :auth]
def show
@user = current_user
end
def new
end
def auth
redirect_to client.auth_code.authorize_url({
redirect_uri: callback_ci_user_sessions_url,
state: params[:state]
})
end
def callback
token = client.auth_code.get_token(params[:code], redirect_uri: callback_ci_user_sessions_url).token
@user_session = Ci::UserSession.new
user = @user_session.authenticate(access_token: token)
if user && sign_in(user)
return_to = get_ouath_state_return_to(params[:state])
redirect_to(return_to || ci_root_path)
else
@error = 'Invalid credentials'
render :new
end
end
def destroy
sign_out
redirect_to new_ci_user_sessions_path
end
protected
def client
@client ||= ::OAuth2::Client.new(
GitlabCi.config.gitlab_server.app_id,
GitlabCi.config.gitlab_server.app_secret,
{
site: GitlabCi.config.gitlab_server.url,
authorize_url: 'oauth/authorize',
token_url: 'oauth/token'
}
)
end
end
end
......@@ -99,7 +99,7 @@ module Ci
private
def url
GitlabCi.config.gitlab_server.url
Gitlab.config.gitlab.url
end
def default_opts
......
# User object is stored in session
module Ci
class User
DEVELOPER_ACCESS = 30
attr_reader :attributes
def initialize(hash)
@attributes = hash
end
def gitlab_projects(search = nil, page = 1, per_page = 100)
Rails.cache.fetch(cache_key(page, per_page, search)) do
Ci::Project.from_gitlab(self, :authorized, { page: page, per_page: per_page, search: search, ci_enabled_first: true })
end
end
def method_missing(meth, *args, &block)
if attributes.has_key?(meth.to_s)
attributes[meth.to_s]
else
super
end
end
def avatar_url
attributes['avatar_url']
end
def cache_key(*args)
"#{self.id}:#{args.join(":")}:#{sync_at.to_s}"
end
def sync_at
@sync_at ||= Time.now
end
def reset_cache
@sync_at = Time.now
end
def authorized_runners
Ci::Runner.specific.includes(:runner_projects).
where(Ci::RunnerProject.table_name => { project_id: authorized_projects } )
end
def authorized_projects
Ci::Project.where(gitlab_id: current_user.authorized_projects.map(&:id))
end
def authenticate_options
if attributes['access_token']
{ access_token: attributes['access_token'] }
else
{ private_token: attributes['private_token'] }
end
end
private
def project_info(project_gitlab_id)
Rails.cache.fetch(cache_key("project_info", project_gitlab_id, sync_at)) do
Ci::Network.new.project(authenticate_options, project_gitlab_id)
end
end
end
end
module Ci
class UserSession
include ActiveModel::Conversion
include Ci::StaticModel
extend ActiveModel::Naming
def authenticate(auth_opts)
network = Ci::Network.new
user = network.authenticate(auth_opts)
if user
user["access_token"] = auth_opts[:access_token]
return Ci::User.new(user)
else
nil
end
user
rescue
nil
end
end
end
......@@ -13,7 +13,7 @@ module Ci
project_url: project_route.gsub(":project_id", @project.id.to_s),
}
unless Ci::Network.new.enable_ci(@project.gitlab_id, data, current_user.authenticate_options)
unless Ci::Network.new.enable_ci(@project.gitlab_id, data, {private_token: current_user.private_token})
raise ActiveRecord::Rollback
end
end
......
......@@ -8,7 +8,7 @@ module Ci
builds =
if current_runner.shared?
# don't run projects which have not enables shared runners
builds.includes(:project).where(projects: { shared_runners_enabled: true })
builds.includes(:project).where(ci_projects: { shared_runners_enabled: true })
else
# do run projects which are only assigned to this runner
builds.where(project_id: current_runner.projects)
......
......@@ -8,7 +8,8 @@
%th Builds
%th
= render @projects
- @projects.each do |project|
= render "ci/admin/projects/project", project: @projects
= paginate @projects
......@@ -11,5 +11,5 @@
Added
- else
= form_tag ci_projects_path do
= hidden_field_tag :project, project.to_json
= hidden_field_tag :project, project.to_json(methods: [:name_with_namespace, :path_with_namespace, :ssh_url_to_repo])
= submit_tag 'Add project to CI', class: 'btn btn-default btn-sm'
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