From a3601d6005ff4a5f109444e7ab5333d0c229d3ed Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 9 Apr 2015 12:21:59 -0400 Subject: Revert "Preload lib/" This reverts commit 5511a731bcc05034abacd4ec01ff7d5a2a3b89d3. --- config/initializers/2_app.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/config/initializers/2_app.rb b/config/initializers/2_app.rb index 655590dff0b..688cdf5f4b0 100644 --- a/config/initializers/2_app.rb +++ b/config/initializers/2_app.rb @@ -6,8 +6,3 @@ module Gitlab Settings end end - -# -# Load all libs for threadsafety -# -Dir["#{Rails.root}/lib/**/*.rb"].each { |file| require file } -- cgit v1.2.1 From 2cefdbb535a2883f514e1b3482eefbe072976be3 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 9 Apr 2015 13:36:05 -0400 Subject: Move lib/gitlab/oauth to lib/gitlab/o_auth Lets Rails autoload these files by name --- lib/gitlab/ldap/user.rb | 2 +- lib/gitlab/o_auth/auth_hash.rb | 54 +++++++++++++++ lib/gitlab/o_auth/user.rb | 106 ++++++++++++++++++++++++++++++ lib/gitlab/oauth/auth_hash.rb | 54 --------------- lib/gitlab/oauth/user.rb | 106 ------------------------------ spec/lib/gitlab/o_auth/auth_hash_spec.rb | 55 ++++++++++++++++ spec/lib/gitlab/o_auth/user_spec.rb | 109 +++++++++++++++++++++++++++++++ spec/lib/gitlab/oauth/auth_hash_spec.rb | 55 ---------------- spec/lib/gitlab/oauth/user_spec.rb | 109 ------------------------------- 9 files changed, 325 insertions(+), 325 deletions(-) create mode 100644 lib/gitlab/o_auth/auth_hash.rb create mode 100644 lib/gitlab/o_auth/user.rb delete mode 100644 lib/gitlab/oauth/auth_hash.rb delete mode 100644 lib/gitlab/oauth/user.rb create mode 100644 spec/lib/gitlab/o_auth/auth_hash_spec.rb create mode 100644 spec/lib/gitlab/o_auth/user_spec.rb delete mode 100644 spec/lib/gitlab/oauth/auth_hash_spec.rb delete mode 100644 spec/lib/gitlab/oauth/user_spec.rb diff --git a/lib/gitlab/ldap/user.rb b/lib/gitlab/ldap/user.rb index cfa8692659d..b04f5b4ac37 100644 --- a/lib/gitlab/ldap/user.rb +++ b/lib/gitlab/ldap/user.rb @@ -1,4 +1,4 @@ -require 'gitlab/oauth/user' +require 'gitlab/o_auth/user' # LDAP extension for User model # diff --git a/lib/gitlab/o_auth/auth_hash.rb b/lib/gitlab/o_auth/auth_hash.rb new file mode 100644 index 00000000000..ce52beec78e --- /dev/null +++ b/lib/gitlab/o_auth/auth_hash.rb @@ -0,0 +1,54 @@ +# Class to parse and transform the info provided by omniauth +# +module Gitlab + module OAuth + class AuthHash + attr_reader :auth_hash + def initialize(auth_hash) + @auth_hash = auth_hash + end + + def uid + auth_hash.uid.to_s + end + + def provider + auth_hash.provider + end + + def info + auth_hash.info + end + + def name + (info.try(:name) || full_name).to_s.force_encoding('utf-8') + end + + def full_name + "#{info.first_name} #{info.last_name}" + end + + def username + (info.try(:nickname) || generate_username).to_s.force_encoding('utf-8') + end + + def email + (info.try(:email) || generate_temporarily_email).downcase + end + + def password + @password ||= Devise.friendly_token[0, 8].downcase + end + + # Get the first part of the email address (before @) + # In addtion in removes illegal characters + def generate_username + email.match(/^[^@]*/)[0].parameterize + end + + def generate_temporarily_email + "temp-email-for-oauth-#{username}@gitlab.localhost" + end + end + end +end diff --git a/lib/gitlab/o_auth/user.rb b/lib/gitlab/o_auth/user.rb new file mode 100644 index 00000000000..2f5c217d764 --- /dev/null +++ b/lib/gitlab/o_auth/user.rb @@ -0,0 +1,106 @@ +# OAuth extension for User model +# +# * Find GitLab user based on omniauth uid and provider +# * Create new user from omniauth data +# +module Gitlab + module OAuth + class ForbiddenAction < StandardError; end + + class User + attr_accessor :auth_hash, :gl_user + + def initialize(auth_hash) + self.auth_hash = auth_hash + end + + def persisted? + gl_user.try(:persisted?) + end + + def new? + !persisted? + end + + def valid? + gl_user.try(:valid?) + end + + def save + unauthorized_to_create unless gl_user + + if needs_blocking? + gl_user.save! + gl_user.block + else + gl_user.save! + end + + log.info "(OAuth) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}" + gl_user + rescue ActiveRecord::RecordInvalid => e + log.info "(OAuth) Error saving user: #{gl_user.errors.full_messages}" + return self, e.record.errors + end + + def gl_user + @user ||= find_by_uid_and_provider + + if signup_enabled? + @user ||= build_new_user + end + + @user + end + + protected + + def needs_blocking? + new? && block_after_signup? + end + + def signup_enabled? + Gitlab.config.omniauth.allow_single_sign_on + end + + def block_after_signup? + Gitlab.config.omniauth.block_auto_created_users + end + + def auth_hash=(auth_hash) + @auth_hash = AuthHash.new(auth_hash) + end + + def find_by_uid_and_provider + identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid) + identity && identity.user + end + + def build_new_user + user = ::User.new(user_attributes) + user.skip_confirmation! + user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider) + user + end + + def user_attributes + { + name: auth_hash.name, + username: ::Namespace.clean_path(auth_hash.username), + email: auth_hash.email, + password: auth_hash.password, + password_confirmation: auth_hash.password, + password_automatically_set: true + } + end + + def log + Gitlab::AppLogger + end + + def unauthorized_to_create + raise ForbiddenAction.new("Unauthorized to create user, signup disabled for #{auth_hash.provider}") + end + end + end +end diff --git a/lib/gitlab/oauth/auth_hash.rb b/lib/gitlab/oauth/auth_hash.rb deleted file mode 100644 index ce52beec78e..00000000000 --- a/lib/gitlab/oauth/auth_hash.rb +++ /dev/null @@ -1,54 +0,0 @@ -# Class to parse and transform the info provided by omniauth -# -module Gitlab - module OAuth - class AuthHash - attr_reader :auth_hash - def initialize(auth_hash) - @auth_hash = auth_hash - end - - def uid - auth_hash.uid.to_s - end - - def provider - auth_hash.provider - end - - def info - auth_hash.info - end - - def name - (info.try(:name) || full_name).to_s.force_encoding('utf-8') - end - - def full_name - "#{info.first_name} #{info.last_name}" - end - - def username - (info.try(:nickname) || generate_username).to_s.force_encoding('utf-8') - end - - def email - (info.try(:email) || generate_temporarily_email).downcase - end - - def password - @password ||= Devise.friendly_token[0, 8].downcase - end - - # Get the first part of the email address (before @) - # In addtion in removes illegal characters - def generate_username - email.match(/^[^@]*/)[0].parameterize - end - - def generate_temporarily_email - "temp-email-for-oauth-#{username}@gitlab.localhost" - end - end - end -end diff --git a/lib/gitlab/oauth/user.rb b/lib/gitlab/oauth/user.rb deleted file mode 100644 index 2f5c217d764..00000000000 --- a/lib/gitlab/oauth/user.rb +++ /dev/null @@ -1,106 +0,0 @@ -# OAuth extension for User model -# -# * Find GitLab user based on omniauth uid and provider -# * Create new user from omniauth data -# -module Gitlab - module OAuth - class ForbiddenAction < StandardError; end - - class User - attr_accessor :auth_hash, :gl_user - - def initialize(auth_hash) - self.auth_hash = auth_hash - end - - def persisted? - gl_user.try(:persisted?) - end - - def new? - !persisted? - end - - def valid? - gl_user.try(:valid?) - end - - def save - unauthorized_to_create unless gl_user - - if needs_blocking? - gl_user.save! - gl_user.block - else - gl_user.save! - end - - log.info "(OAuth) saving user #{auth_hash.email} from login with extern_uid => #{auth_hash.uid}" - gl_user - rescue ActiveRecord::RecordInvalid => e - log.info "(OAuth) Error saving user: #{gl_user.errors.full_messages}" - return self, e.record.errors - end - - def gl_user - @user ||= find_by_uid_and_provider - - if signup_enabled? - @user ||= build_new_user - end - - @user - end - - protected - - def needs_blocking? - new? && block_after_signup? - end - - def signup_enabled? - Gitlab.config.omniauth.allow_single_sign_on - end - - def block_after_signup? - Gitlab.config.omniauth.block_auto_created_users - end - - def auth_hash=(auth_hash) - @auth_hash = AuthHash.new(auth_hash) - end - - def find_by_uid_and_provider - identity = Identity.find_by(provider: auth_hash.provider, extern_uid: auth_hash.uid) - identity && identity.user - end - - def build_new_user - user = ::User.new(user_attributes) - user.skip_confirmation! - user.identities.new(extern_uid: auth_hash.uid, provider: auth_hash.provider) - user - end - - def user_attributes - { - name: auth_hash.name, - username: ::Namespace.clean_path(auth_hash.username), - email: auth_hash.email, - password: auth_hash.password, - password_confirmation: auth_hash.password, - password_automatically_set: true - } - end - - def log - Gitlab::AppLogger - end - - def unauthorized_to_create - raise ForbiddenAction.new("Unauthorized to create user, signup disabled for #{auth_hash.provider}") - end - end - end -end diff --git a/spec/lib/gitlab/o_auth/auth_hash_spec.rb b/spec/lib/gitlab/o_auth/auth_hash_spec.rb new file mode 100644 index 00000000000..5eb77b492b2 --- /dev/null +++ b/spec/lib/gitlab/o_auth/auth_hash_spec.rb @@ -0,0 +1,55 @@ +require 'spec_helper' + +describe Gitlab::OAuth::AuthHash do + let(:auth_hash) do + Gitlab::OAuth::AuthHash.new(double({ + provider: 'twitter', + uid: uid, + info: double(info_hash) + })) + end + let(:uid) { 'my-uid' } + let(:email) { 'my-email@example.com' } + let(:nickname) { 'my-nickname' } + let(:info_hash) { + { + email: email, + nickname: nickname, + name: 'John', + first_name: "John", + last_name: "Who" + } + } + + context "defaults" do + it { expect(auth_hash.provider).to eql 'twitter' } + it { expect(auth_hash.uid).to eql uid } + it { expect(auth_hash.email).to eql email } + it { expect(auth_hash.username).to eql nickname } + it { expect(auth_hash.name).to eql "John" } + it { expect(auth_hash.password).to_not be_empty } + end + + context "email not provided" do + before { info_hash.delete(:email) } + it "generates a temp email" do + expect( auth_hash.email).to start_with('temp-email-for-oauth') + end + end + + context "username not provided" do + before { info_hash.delete(:nickname) } + + it "takes the first part of the email as username" do + expect( auth_hash.username ).to eql "my-email" + end + end + + context "name not provided" do + before { info_hash.delete(:name) } + + it "concats first and lastname as the name" do + expect( auth_hash.name ).to eql "John Who" + end + end +end \ No newline at end of file diff --git a/spec/lib/gitlab/o_auth/user_spec.rb b/spec/lib/gitlab/o_auth/user_spec.rb new file mode 100644 index 00000000000..44cdd1e4fab --- /dev/null +++ b/spec/lib/gitlab/o_auth/user_spec.rb @@ -0,0 +1,109 @@ +require 'spec_helper' + +describe Gitlab::OAuth::User do + let(:oauth_user) { Gitlab::OAuth::User.new(auth_hash) } + let(:gl_user) { oauth_user.gl_user } + let(:uid) { 'my-uid' } + let(:provider) { 'my-provider' } + let(:auth_hash) { double(uid: uid, provider: provider, info: double(info_hash)) } + let(:info_hash) do + { + nickname: '-john+gitlab-ETC%.git@gmail.com', + name: 'John', + email: 'john@mail.com' + } + end + + describe :persisted? do + let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') } + + it "finds an existing user based on uid and provider (facebook)" do + auth = double(info: double(name: 'John'), uid: 'my-uid', provider: 'my-provider') + expect( oauth_user.persisted? ).to be_truthy + end + + it "returns false if use is not found in database" do + auth_hash.stub(uid: 'non-existing') + expect( oauth_user.persisted? ).to be_falsey + end + end + + describe :save do + let(:provider) { 'twitter' } + + describe 'signup' do + context "with allow_single_sign_on enabled" do + before { Gitlab.config.omniauth.stub allow_single_sign_on: true } + + it "creates a user from Omniauth" do + oauth_user.save + + expect(gl_user).to be_valid + identity = gl_user.identities.first + expect(identity.extern_uid).to eql uid + expect(identity.provider).to eql 'twitter' + end + end + + context "with allow_single_sign_on disabled (Default)" do + it "throws an error" do + expect{ oauth_user.save }.to raise_error StandardError + end + end + end + + describe 'blocking' do + let(:provider) { 'twitter' } + before { Gitlab.config.omniauth.stub allow_single_sign_on: true } + + context 'signup' do + context 'dont block on create' do + before { Gitlab.config.omniauth.stub block_auto_created_users: false } + + it do + oauth_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'block on create' do + before { Gitlab.config.omniauth.stub block_auto_created_users: true } + + it do + oauth_user.save + expect(gl_user).to be_valid + expect(gl_user).to be_blocked + end + end + end + + context 'sign-in' do + before do + oauth_user.save + oauth_user.gl_user.activate + end + + context 'dont block on create' do + before { Gitlab.config.omniauth.stub block_auto_created_users: false } + + it do + oauth_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + + context 'block on create' do + before { Gitlab.config.omniauth.stub block_auto_created_users: true } + + it do + oauth_user.save + expect(gl_user).to be_valid + expect(gl_user).not_to be_blocked + end + end + end + end + end +end diff --git a/spec/lib/gitlab/oauth/auth_hash_spec.rb b/spec/lib/gitlab/oauth/auth_hash_spec.rb deleted file mode 100644 index 5eb77b492b2..00000000000 --- a/spec/lib/gitlab/oauth/auth_hash_spec.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'spec_helper' - -describe Gitlab::OAuth::AuthHash do - let(:auth_hash) do - Gitlab::OAuth::AuthHash.new(double({ - provider: 'twitter', - uid: uid, - info: double(info_hash) - })) - end - let(:uid) { 'my-uid' } - let(:email) { 'my-email@example.com' } - let(:nickname) { 'my-nickname' } - let(:info_hash) { - { - email: email, - nickname: nickname, - name: 'John', - first_name: "John", - last_name: "Who" - } - } - - context "defaults" do - it { expect(auth_hash.provider).to eql 'twitter' } - it { expect(auth_hash.uid).to eql uid } - it { expect(auth_hash.email).to eql email } - it { expect(auth_hash.username).to eql nickname } - it { expect(auth_hash.name).to eql "John" } - it { expect(auth_hash.password).to_not be_empty } - end - - context "email not provided" do - before { info_hash.delete(:email) } - it "generates a temp email" do - expect( auth_hash.email).to start_with('temp-email-for-oauth') - end - end - - context "username not provided" do - before { info_hash.delete(:nickname) } - - it "takes the first part of the email as username" do - expect( auth_hash.username ).to eql "my-email" - end - end - - context "name not provided" do - before { info_hash.delete(:name) } - - it "concats first and lastname as the name" do - expect( auth_hash.name ).to eql "John Who" - end - end -end \ No newline at end of file diff --git a/spec/lib/gitlab/oauth/user_spec.rb b/spec/lib/gitlab/oauth/user_spec.rb deleted file mode 100644 index 44cdd1e4fab..00000000000 --- a/spec/lib/gitlab/oauth/user_spec.rb +++ /dev/null @@ -1,109 +0,0 @@ -require 'spec_helper' - -describe Gitlab::OAuth::User do - let(:oauth_user) { Gitlab::OAuth::User.new(auth_hash) } - let(:gl_user) { oauth_user.gl_user } - let(:uid) { 'my-uid' } - let(:provider) { 'my-provider' } - let(:auth_hash) { double(uid: uid, provider: provider, info: double(info_hash)) } - let(:info_hash) do - { - nickname: '-john+gitlab-ETC%.git@gmail.com', - name: 'John', - email: 'john@mail.com' - } - end - - describe :persisted? do - let!(:existing_user) { create(:omniauth_user, extern_uid: 'my-uid', provider: 'my-provider') } - - it "finds an existing user based on uid and provider (facebook)" do - auth = double(info: double(name: 'John'), uid: 'my-uid', provider: 'my-provider') - expect( oauth_user.persisted? ).to be_truthy - end - - it "returns false if use is not found in database" do - auth_hash.stub(uid: 'non-existing') - expect( oauth_user.persisted? ).to be_falsey - end - end - - describe :save do - let(:provider) { 'twitter' } - - describe 'signup' do - context "with allow_single_sign_on enabled" do - before { Gitlab.config.omniauth.stub allow_single_sign_on: true } - - it "creates a user from Omniauth" do - oauth_user.save - - expect(gl_user).to be_valid - identity = gl_user.identities.first - expect(identity.extern_uid).to eql uid - expect(identity.provider).to eql 'twitter' - end - end - - context "with allow_single_sign_on disabled (Default)" do - it "throws an error" do - expect{ oauth_user.save }.to raise_error StandardError - end - end - end - - describe 'blocking' do - let(:provider) { 'twitter' } - before { Gitlab.config.omniauth.stub allow_single_sign_on: true } - - context 'signup' do - context 'dont block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: false } - - it do - oauth_user.save - expect(gl_user).to be_valid - expect(gl_user).not_to be_blocked - end - end - - context 'block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: true } - - it do - oauth_user.save - expect(gl_user).to be_valid - expect(gl_user).to be_blocked - end - end - end - - context 'sign-in' do - before do - oauth_user.save - oauth_user.gl_user.activate - end - - context 'dont block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: false } - - it do - oauth_user.save - expect(gl_user).to be_valid - expect(gl_user).not_to be_blocked - end - end - - context 'block on create' do - before { Gitlab.config.omniauth.stub block_auto_created_users: true } - - it do - oauth_user.save - expect(gl_user).to be_valid - expect(gl_user).not_to be_blocked - end - end - end - end - end -end -- cgit v1.2.1 From 91c39fd8c70e45aeb2d39f4f2b08164d01300b89 Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 9 Apr 2015 13:56:38 -0400 Subject: Add lib/gitlab.rb to autoload non-conventional paths --- config/initializers/1_settings.rb | 2 ++ lib/gitlab.rb | 5 +++++ 2 files changed, 7 insertions(+) create mode 100644 lib/gitlab.rb diff --git a/config/initializers/1_settings.rb b/config/initializers/1_settings.rb index de79595d031..e6b00c531ac 100644 --- a/config/initializers/1_settings.rb +++ b/config/initializers/1_settings.rb @@ -1,3 +1,5 @@ +require 'gitlab' # Load lib/gitlab.rb as soon as possible + class Settings < Settingslogic source ENV.fetch('GITLAB_CONFIG') { "#{Rails.root}/config/gitlab.yml" } namespace Rails.env diff --git a/lib/gitlab.rb b/lib/gitlab.rb new file mode 100644 index 00000000000..5fc1862c3e9 --- /dev/null +++ b/lib/gitlab.rb @@ -0,0 +1,5 @@ +require 'gitlab/git' + +module Gitlab + autoload :Satellite, 'gitlab/satellite/satellite' +end -- cgit v1.2.1 From abae9b8d63d9c8ec953ce8df2167b52365e7e16c Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 9 Apr 2015 13:57:10 -0400 Subject: Add autoloads for lib/gitlab/satellite/files/* These files don't match their naming convention, but for organizational purposes it makes sense. --- lib/gitlab/satellite/satellite.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/gitlab/satellite/satellite.rb b/lib/gitlab/satellite/satellite.rb index f24c6199c44..398643d68de 100644 --- a/lib/gitlab/satellite/satellite.rb +++ b/lib/gitlab/satellite/satellite.rb @@ -1,5 +1,10 @@ module Gitlab module Satellite + autoload :DeleteFileAction, 'gitlab/satellite/files/delete_file_action' + autoload :EditFileAction, 'gitlab/satellite/files/edit_file_action' + autoload :FileAction, 'gitlab/satellite/files/file_action' + autoload :NewFileAction, 'gitlab/satellite/files/new_file_action' + class CheckoutFailed < StandardError; end class CommitFailed < StandardError; end class PushFailed < StandardError; end -- cgit v1.2.1 From c9a1634f3183be512e4b94c140a83fa467c238ee Mon Sep 17 00:00:00 2001 From: Robert Speicher Date: Thu, 9 Apr 2015 15:34:55 -0400 Subject: Move lib/gitlab/contributors to its correct path --- lib/gitlab/contributor.rb | 9 +++++++++ lib/gitlab/contributors.rb | 9 --------- 2 files changed, 9 insertions(+), 9 deletions(-) create mode 100644 lib/gitlab/contributor.rb delete mode 100644 lib/gitlab/contributors.rb diff --git a/lib/gitlab/contributor.rb b/lib/gitlab/contributor.rb new file mode 100644 index 00000000000..c41e92b620f --- /dev/null +++ b/lib/gitlab/contributor.rb @@ -0,0 +1,9 @@ +module Gitlab + class Contributor + attr_accessor :email, :name, :commits, :additions, :deletions + + def initialize + @commits, @additions, @deletions = 0, 0, 0 + end + end +end diff --git a/lib/gitlab/contributors.rb b/lib/gitlab/contributors.rb deleted file mode 100644 index c41e92b620f..00000000000 --- a/lib/gitlab/contributors.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Gitlab - class Contributor - attr_accessor :email, :name, :commits, :additions, :deletions - - def initialize - @commits, @additions, @deletions = 0, 0, 0 - end - end -end -- cgit v1.2.1