swaminadhan
Joined: 14 Sep 2009 Posts: 84
|
Posted: Thu Nov 19, 2009 11:52 am Post subject: Acts as Authenticated Plugin. |
|
|
hello friends !
Most of the sites will have the authentication functionality.For that we can write our own code based on the conditions. And for this we do have, different plugins for the authentication property.One of the plugin for authentication property is Acts_as_Authenticated.
So let us discuss about the Acts_as_Authentication plugin for the Authentication and its advantages :
1. First of all we have to install the plugin in our application.
To install the plugin in your application do
ruby script/install plugin http://svn.techno-weenie.net/projects/plugins/acts_as_authenticated
The plugin will be installed in your app/vendor/plugins.
Once the plugin gets installed.we have to generate the generator.
2. Rails will have the basic authentication generator.. To run this generator do
ruby script/generate authenticated user account.
Here the user is the model and account is the controller.
So once we run the above command automatically the model and controller will be created with the predefined methods.
3. Once do rake db:migrate for migrations.
4. Now run the server --- ruby script/server.
Now you are up with the Authentication Functionality.
Generally, we have to code a lot to get this functionality without installing the plugin.Lets check the advantages of this plugin
1. In this plugin we have a method 'logged_in?' . It will return true when the user gets authenticated or return false when user authentication fails.
2. In this plugin we have a method 'current_user' . In which it will get the details of the authenticated user.so that we can get the authenticated user details by using this method name directly. When we didnt use this plugin normally we have to declare a variable and we have to get the details into that variable and after that we have to use this variable inorder to get the user details.
for example:
When we wants to get the user details of the authenticated user we have to
@var=var.find(session[user_id])
so instead of using this we can use directly the "current_user" method.
3. Similarly filter method to enforce a login requirement.To require logins for all actions, use this in your controllers use:
before_filter :login_required
4. To require logins for specific actions, use this in your controllers:
before_filter :login_required, :only => [ :edit, :update ]
5. Similarly we have 'redirect_back_or_default(default)' method .
def redirect_back_or_default(default)
session[:return_to] ? redirect_to(session[:return_to]) : redirect_to(default)
session[:return_to] = nil
end
The advantage of this method is when we are not logged in,when we clicked on a particular url it will ask for the authentication,when we provides the login details it will directly take you to the url that you clicked before logged in. To write this functionality manually we have to code a lot.
Thank you,
swaminadhan |
|