<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>42 &#187; users</title>
	<atom:link href="http://www.42.mach7x.com/tag/users/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.42.mach7x.com</link>
	<description>Thoughts and tips about programming with Ruby on Rails</description>
	<lastBuildDate>Wed, 01 Feb 2012 14:38:36 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>using authentication with acts_as_authenticated</title>
		<link>http://www.42.mach7x.com/2007/12/18/using-authentication-with-acts_as_authenticated/</link>
		<comments>http://www.42.mach7x.com/2007/12/18/using-authentication-with-acts_as_authenticated/#comments</comments>
		<pubDate>Tue, 18 Dec 2007 14:59:56 +0000</pubDate>
		<dc:creator>kosmas</dc:creator>
				<category><![CDATA[ruby on rails]]></category>
		<category><![CDATA[acts_as_authenticated]]></category>
		<category><![CDATA[authentication]]></category>
		<category><![CDATA[menus]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[users]]></category>

		<guid isPermaLink="false">http://www.42.mach7x.com/2007/12/18/using-authentication-with-acts_as_authenticated/</guid>
		<description><![CDATA[Problem You want to use the easiest authentication method, in order to add users/permissions to your application. Solution Install the act_as_authenticated plugin: script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated Generate controllers, models and migration: script/generate authenticated user account Add necessary user foreign keys in appropriate tables (ie customer in xxx_create_users.rb), plus other fields you may want to use in [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Problem</strong><br />
You want to use the easiest authentication method, in order to add users/permissions to your application.</p>
<p><strong>Solution</strong></p>
<ol>
<li>Install the act_as_authenticated plugin:
<pre class="code">script/plugin install http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated</pre>
</li>
<li>Generate controllers, models and migration:
<pre class="code">script/generate authenticated user account</pre>
</li>
<li>Add necessary user foreign keys in appropriate tables (ie customer in xxx_create_users.rb), plus other fields you may want to use in your user table (ie role):
<pre class="code">create table "users", :force =&gt; true do |t|
  t.column :login, :string
  ...
  t.column :role, :string, :default =&gt; 'C'
  ...
  end
  add_column :customers, :user_id, :integer
  ...
  def self.down
    ...
    remove_column :customers, :user_id
  end</pre>
</li>
<li>Run the migration:
<pre class="code">rake db:migrate VERSION=xxx</pre>
</li>
<li>Comment out from app/controllers/account_controller.rb the following:
<pre class="code">include AuthenticatedSystem</pre>
</li>
<li>Add it in the app/controllers/application.rb just under the class declaration.</li>
<li>Add in application.rb (same as above) the following just after the session section:
<pre class="code">
before_filter :login_required, :except =&gt; [:login, :signup, :logout]</pre>
</li>
<li>Comment out the default redirection after login and put your own (ie customers): in the account_controller.rb, just before<br />
the flash[:notice] = &#8220;Logged in successfully&#8221;.<br />
Also add the else for the the invalid login:</p>
<pre class="code">
  redirect_back_or_default(:controller =&gt; 'customers', :action =&gt; 'list')
  flash[:notice] = "Logged in successfully"
else
  flash[:notice] = "Invalid Login/Password!"</pre>
</li>
<li> Change the action in the signup and logout functions in the account_controller.rb file from index to login</li>
<li>Add (optionally) more fields (role) in the signup page (app/views/account/signup.rhtml):
<pre class="code">

<label for="role">User role</label>

&lt;%= f.select :role, ['A','R','C'] %&gt;</pre>
</li>
<li>Add restrictions for displaying records depending on user logged in, by using the conditions_for_collection of the activescaffold plugin in app/controllers/customers_controller.rb:
<pre class="code">
def conditions_for_collection
  ['customers.user_id = (?)', current_user.id]
end</pre>
</li>
<li>Add a menu page layout in app/views/layouts/_menu.rhtml:
<pre class="code">
<p id="sub-nav">
  &lt;% if logged_in? %&gt;
    &lt;% if current_user.role == 'C' %&gt;
      &lt;%= link_to "add user", :controller =&gt; "account", :action =&gt; "signup" %&gt;   |
      &lt;%= link_to "assign customer", :controller =&gt; "users", :action =&gt; "list" %&gt;   |
      &lt;%= link_to "change password", :controller =&gt; "account", :action =&gt; "change_password" %&gt;   |
      &lt;%= link_to "customers", :controller =&gt; "customers", :action =&gt; "list" %&gt;   |
    &lt;% end %&gt;
    &lt;%= link_to "logout", :controller =&gt; "account", :action =&gt; "logout" %&gt;   |
    &lt;%= "Logged in as " + current_user.login %&gt;
  &lt;% end %&gt;</pre>
</li>
<li>Add the call to the menu partial in the app/views/layouts/application.rhtml, just before the div with id=main :
<pre class="code">
<%= render :partial => "layouts/menu"%></pre>
</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://www.42.mach7x.com/2007/12/18/using-authentication-with-acts_as_authenticated/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

