Commit 0dd94cd86ec0680432e58f2630a3a35fa84afd73

Authored by Florian Unglaub
1 parent 1b0198f1

DRY'ed up the user model

Showing 1 changed file with 32 additions and 37 deletions   Show diff stats
app/models/user.rb
... ... @@ -86,36 +86,42 @@ class User < ActiveRecord::Base
86 86 where('id NOT IN (SELECT DISTINCT(user_id) FROM users_projects)')
87 87 end
88 88  
89   - def self.find_or_new_for_omniauth(oauth)
90   - provider, uid = oauth['provider'], oauth['uid']
  89 + def self.create_from_omniauth(auth, ldap = false)
  90 + provider, uid = auth.provider, auth.uid
  91 + name = auth.info.name.force_encoding("utf-8")
  92 + email = auth.info.email.downcase unless auth.info.email.nil?
  93 +
  94 + ldap_prefix = ldap ? '(LDAP) ' : ''
  95 + raise OmniAuth::Error, "#{ldap_prefix}#{provider} does not provide an email"\
  96 + " address" if auth.info.email.blank?
  97 +
  98 + logger.info "#{ldap_prefix}Creating user from #{provider} login"\
  99 + " {uid => #{uid}, name => #{name}, email => #{email}}"
  100 + password = Devise.friendly_token[0, 8].downcase
  101 + @user = User.new(
  102 + extern_uid: uid,
  103 + provider: provider,
  104 + name: name,
  105 + email: email,
  106 + password: password,
  107 + password_confirmation: password,
  108 + projects_limit: Gitlab.config.default_projects_limit,
  109 + )
  110 + if Gitlab.config.omniauth.block_auto_created_users && !ldap
  111 + @user.blocked = true
  112 + end
  113 + @user.save!
  114 + @user
  115 + end
  116 +
  117 + def self.find_or_new_for_omniauth(auth)
  118 + provider, uid = auth.provider, auth.uid
91 119  
92 120 if @user = User.find_by_provider_and_extern_uid(provider, uid)
93 121 @user
94 122 else
95 123 if Gitlab.config.omniauth.allow_single_sign_on
96   - # Ensure here that all required attributes were passed along with the
97   - # oauth request:
98   - %w(first_name last_name email).each do |attr|
99   - unless oauth[:info][attr].present?
100   - raise OmniAuth::Error,
101   - "#{provider} does not provide the required field #{attr}"
102   - end
103   - end
104   -
105   - password = Devise.friendly_token[0, 8].downcase
106   - @user = User.new(
107   - extern_uid: uid,
108   - provider: provider,
109   - name: "#{oauth[:info][:first_name]} #{oauth[:info][:last_name]}",
110   - email: oauth[:info][:email],
111   - password: password,
112   - password_confirmation: password,
113   - projects_limit: Gitlab.config.default_projects_limit,
114   - )
115   -
116   - @user.blocked = true if Gitlab.config.omniauth.block_auto_created_users
117   - @user.save!
118   -
  124 + @user = User.create_from_omniauth(auth)
119 125 @user
120 126 end
121 127 end
... ... @@ -124,7 +130,6 @@ class User < ActiveRecord::Base
124 130 def self.find_for_ldap_auth(auth, signed_in_resource=nil)
125 131 uid = auth.info.uid
126 132 provider = auth.provider
127   - name = auth.info.name.force_encoding("utf-8")
128 133 email = auth.info.email.downcase unless auth.info.email.nil?
129 134 raise OmniAuth::Error, "LDAP accounts must provide an uid and email address" if uid.nil? or email.nil?
130 135  
... ... @@ -136,17 +141,7 @@ class User < ActiveRecord::Base
136 141 @user.update_attributes(:extern_uid => uid, :provider => provider)
137 142 @user
138 143 else
139   - logger.info "Creating user from LDAP login {uid => #{uid}, name => #{name}, email => #{email}}"
140   - password = Devise.friendly_token[0, 8].downcase
141   - @user = User.create(
142   - :extern_uid => uid,
143   - :provider => provider,
144   - :name => name,
145   - :email => email,
146   - :password => password,
147   - :password_confirmation => password,
148   - :projects_limit => Gitlab.config.default_projects_limit
149   - )
  144 + create_from_omniauth(auth)
150 145 end
151 146 end
152 147  
... ...