gitlabhq/doc/code/classes/Gitlab/Auth.html
2012-12-30 14:43:00 +02:00

315 lines
No EOL
17 KiB
HTML

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Gitlab::Auth</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<div class="banner">
<h1>
<span class="type">Class</span>
Gitlab::Auth
<span class="parent">&lt;
Object
</span>
</h1>
<ul class="files">
<li><a href="../../files/lib/gitlab/auth_rb.html">lib/gitlab/auth.rb</a></li>
</ul>
</div>
<div id="bodyContent">
<div id="content">
<!-- Method ref -->
<div class="sectiontitle">Methods</div>
<dl class="methods">
<dt>C</dt>
<dd>
<ul>
<li>
<a href="#method-i-create_from_omniauth">create_from_omniauth</a>
</li>
</ul>
</dd>
<dt>F</dt>
<dd>
<ul>
<li>
<a href="#method-i-find_for_ldap_auth">find_for_ldap_auth</a>,
</li>
<li>
<a href="#method-i-find_or_new_for_omniauth">find_or_new_for_omniauth</a>
</li>
</ul>
</dd>
<dt>L</dt>
<dd>
<ul>
<li>
<a href="#method-i-log">log</a>
</li>
</ul>
</dd>
</dl>
<!-- Methods -->
<div class="sectiontitle">Instance Public methods</div>
<div class="method">
<div class="title method-title" id="method-i-create_from_omniauth">
<b>create_from_omniauth</b>(auth, ldap = false)
<a href="../../classes/Gitlab/Auth.html#method-i-create_from_omniauth" name="method-i-create_from_omniauth" class="permalink">Link</a>
</div>
<div class="description">
</div>
<div class="sourcecode">
<p class="source-link">
Source:
<a href="javascript:toggleSource('method-i-create_from_omniauth_source')" id="l_method-i-create_from_omniauth_source">show</a>
</p>
<div id="method-i-create_from_omniauth_source" class="dyn-source">
<pre><span class="ruby-comment"># File lib/gitlab/auth.rb, line 20</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">create_from_omniauth</span>(<span class="ruby-identifier">auth</span>, <span class="ruby-identifier">ldap</span> = <span class="ruby-keyword">false</span>)
<span class="ruby-identifier">provider</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">provider</span>
<span class="ruby-identifier">uid</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">uid</span> <span class="ruby-operator">||</span> <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">uid</span>
<span class="ruby-identifier">name</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">force_encoding</span>(<span class="ruby-string">&quot;utf-8&quot;</span>)
<span class="ruby-identifier">email</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">email</span>.<span class="ruby-identifier">downcase</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">email</span>.<span class="ruby-identifier">nil?</span>
<span class="ruby-identifier">ldap_prefix</span> = <span class="ruby-identifier">ldap</span> <span class="ruby-operator">?</span> <span class="ruby-string">'(LDAP) '</span> <span class="ruby-operator">:</span> <span class="ruby-string">''</span>
<span class="ruby-identifier">raise</span> <span class="ruby-constant">OmniAuth</span><span class="ruby-operator">::</span><span class="ruby-constant">Error</span>, <span class="ruby-node">&quot;#{ldap_prefix}#{provider} does not provide an email&quot;</span> <span class="ruby-string">&quot; address&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">email</span>.<span class="ruby-identifier">blank?</span>
<span class="ruby-identifier">log</span>.<span class="ruby-identifier">info</span> <span class="ruby-node">&quot;#{ldap_prefix}Creating user from #{provider} login&quot;</span> <span class="ruby-node">&quot; {uid =&gt; #{uid}, name =&gt; #{name}, email =&gt; #{email}}&quot;</span>
<span class="ruby-identifier">password</span> = <span class="ruby-constant">Devise</span>.<span class="ruby-identifier">friendly_token</span>[<span class="ruby-number">0</span>, <span class="ruby-number">8</span>].<span class="ruby-identifier">downcase</span>
<span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">new</span>({
<span class="ruby-identifier">extern_uid</span><span class="ruby-operator">:</span> <span class="ruby-identifier">uid</span>,
<span class="ruby-identifier">provider</span><span class="ruby-operator">:</span> <span class="ruby-identifier">provider</span>,
<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-identifier">name</span>,
<span class="ruby-identifier">username</span><span class="ruby-operator">:</span> <span class="ruby-identifier">email</span>.<span class="ruby-identifier">match</span>(<span class="ruby-regexp">%r^[^@]*/</span>)[<span class="ruby-number">0</span>],
<span class="ruby-identifier">email</span><span class="ruby-operator">:</span> <span class="ruby-identifier">email</span>,
<span class="ruby-identifier">password</span><span class="ruby-operator">:</span> <span class="ruby-identifier">password</span>,
<span class="ruby-identifier">password_confirmation</span><span class="ruby-operator">:</span> <span class="ruby-identifier">password</span>,
<span class="ruby-identifier">projects_limit</span><span class="ruby-operator">:</span> <span class="ruby-constant">Gitlab</span>.<span class="ruby-identifier">config</span>.<span class="ruby-identifier">gitlab</span>.<span class="ruby-identifier">default_projects_limit</span>,
}, <span class="ruby-identifier">as</span><span class="ruby-operator">:</span> <span class="ruby-value">:admin</span>)
<span class="ruby-keyword">if</span> <span class="ruby-constant">Gitlab</span>.<span class="ruby-identifier">config</span>.<span class="ruby-identifier">omniauth</span>[<span class="ruby-string">'block_auto_created_users'</span>] <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-operator">!</span><span class="ruby-identifier">ldap</span>
<span class="ruby-ivar">@user</span>.<span class="ruby-identifier">blocked</span> = <span class="ruby-keyword">true</span>
<span class="ruby-keyword">end</span>
<span class="ruby-ivar">@user</span>.<span class="ruby-identifier">save!</span>
<span class="ruby-ivar">@user</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div class="method">
<div class="title method-title" id="method-i-find_for_ldap_auth">
<b>find_for_ldap_auth</b>(auth, signed_in_resource = nil)
<a href="../../classes/Gitlab/Auth.html#method-i-find_for_ldap_auth" name="method-i-find_for_ldap_auth" class="permalink">Link</a>
</div>
<div class="description">
</div>
<div class="sourcecode">
<p class="source-link">
Source:
<a href="javascript:toggleSource('method-i-find_for_ldap_auth_source')" id="l_method-i-find_for_ldap_auth_source">show</a>
</p>
<div id="method-i-find_for_ldap_auth_source" class="dyn-source">
<pre><span class="ruby-comment"># File lib/gitlab/auth.rb, line 3</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">find_for_ldap_auth</span>(<span class="ruby-identifier">auth</span>, <span class="ruby-identifier">signed_in_resource</span> = <span class="ruby-keyword">nil</span>)
<span class="ruby-identifier">uid</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">uid</span>
<span class="ruby-identifier">provider</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">provider</span>
<span class="ruby-identifier">email</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">email</span>.<span class="ruby-identifier">downcase</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">email</span>.<span class="ruby-identifier">nil?</span>
<span class="ruby-identifier">raise</span> <span class="ruby-constant">OmniAuth</span><span class="ruby-operator">::</span><span class="ruby-constant">Error</span>, <span class="ruby-string">&quot;LDAP accounts must provide an uid and email address&quot;</span> <span class="ruby-keyword">if</span> <span class="ruby-identifier">uid</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-keyword">or</span> <span class="ruby-identifier">email</span>.<span class="ruby-identifier">nil?</span>
<span class="ruby-keyword">if</span> <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by_extern_uid_and_provider</span>(<span class="ruby-identifier">uid</span>, <span class="ruby-identifier">provider</span>)
<span class="ruby-ivar">@user</span>
<span class="ruby-keyword">elsif</span> <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by_email</span>(<span class="ruby-identifier">email</span>)
<span class="ruby-identifier">log</span>.<span class="ruby-identifier">info</span> <span class="ruby-node">&quot;Updating legacy LDAP user #{email} with extern_uid =&gt; #{uid}&quot;</span>
<span class="ruby-ivar">@user</span>.<span class="ruby-identifier">update_attributes</span>(<span class="ruby-value">:extern_uid</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">uid</span>, <span class="ruby-value">:provider</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">provider</span>)
<span class="ruby-ivar">@user</span>
<span class="ruby-keyword">else</span>
<span class="ruby-identifier">create_from_omniauth</span>(<span class="ruby-identifier">auth</span>, <span class="ruby-keyword">true</span>)
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div class="method">
<div class="title method-title" id="method-i-find_or_new_for_omniauth">
<b>find_or_new_for_omniauth</b>(auth)
<a href="../../classes/Gitlab/Auth.html#method-i-find_or_new_for_omniauth" name="method-i-find_or_new_for_omniauth" class="permalink">Link</a>
</div>
<div class="description">
</div>
<div class="sourcecode">
<p class="source-link">
Source:
<a href="javascript:toggleSource('method-i-find_or_new_for_omniauth_source')" id="l_method-i-find_or_new_for_omniauth_source">show</a>
</p>
<div id="method-i-find_or_new_for_omniauth_source" class="dyn-source">
<pre><span class="ruby-comment"># File lib/gitlab/auth.rb, line 50</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">find_or_new_for_omniauth</span>(<span class="ruby-identifier">auth</span>)
<span class="ruby-identifier">provider</span>, <span class="ruby-identifier">uid</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">provider</span>, <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">uid</span>
<span class="ruby-identifier">email</span> = <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">email</span>.<span class="ruby-identifier">downcase</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">auth</span>.<span class="ruby-identifier">info</span>.<span class="ruby-identifier">email</span>.<span class="ruby-identifier">nil?</span>
<span class="ruby-keyword">if</span> <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by_provider_and_extern_uid</span>(<span class="ruby-identifier">provider</span>, <span class="ruby-identifier">uid</span>)
<span class="ruby-ivar">@user</span>
<span class="ruby-keyword">elsif</span> <span class="ruby-ivar">@user</span> = <span class="ruby-constant">User</span>.<span class="ruby-identifier">find_by_email</span>(<span class="ruby-identifier">email</span>)
<span class="ruby-ivar">@user</span>.<span class="ruby-identifier">update_attributes</span>(<span class="ruby-value">:extern_uid</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">uid</span>, <span class="ruby-value">:provider</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">provider</span>)
<span class="ruby-ivar">@user</span>
<span class="ruby-keyword">else</span>
<span class="ruby-keyword">if</span> <span class="ruby-constant">Gitlab</span>.<span class="ruby-identifier">config</span>.<span class="ruby-identifier">omniauth</span>[<span class="ruby-string">'allow_single_sign_on'</span>]
<span class="ruby-ivar">@user</span> = <span class="ruby-identifier">create_from_omniauth</span>(<span class="ruby-identifier">auth</span>)
<span class="ruby-ivar">@user</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
<div class="method">
<div class="title method-title" id="method-i-log">
<b>log</b>()
<a href="../../classes/Gitlab/Auth.html#method-i-log" name="method-i-log" class="permalink">Link</a>
</div>
<div class="description">
</div>
<div class="sourcecode">
<p class="source-link">
Source:
<a href="javascript:toggleSource('method-i-log_source')" id="l_method-i-log_source">show</a>
</p>
<div id="method-i-log_source" class="dyn-source">
<pre><span class="ruby-comment"># File lib/gitlab/auth.rb, line 67</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">log</span>
<span class="ruby-constant">Gitlab</span><span class="ruby-operator">::</span><span class="ruby-constant">AppLogger</span>
<span class="ruby-keyword">end</span></pre>
</div>
</div>
</div>
</div>
</div>
</body>
</html>