Rails Forum
Rails Work - the best place to post and find great Ruby on Rails jobs.
Username
Password

You are not logged in.

New Posts in this thread
  • Index
  •  » Tutorials
  •  » User Management Using Salted Hash Login Generator

#1 2006-11-12 03:42:36

patrick@iws
Ticketholder
From: Nashville, TN USA
Registered: 2006-11-11
Posts: 3
Website

User Management Using Salted Hash Login Generator

This is a simple tutorial for RoR beginners. 

This tutorial is meant for those using the Salted Hash Login Generator.

Problem:  You need a login system that will show the user their information only.  For example:  A user has many blog entries and they should only be able to edit their blog entry.

Solution:  Very simple!  You just asign a foreign key to the model associated with the user (for this we'll use user_id).

After you install and configure the Slated Hash Login Generator add the following:

1.  Relate the Post model with the User model using has_many and belongs_to.

/app/models/user.rb


Code :   - fold - unfold
  1. class User < ActiveRecord::Base
  2.   has_many :posts,
  3.     :class_name => "User",
  4.     :foreign_key => "user_id"
/app/models/posts.rb


Code :   - fold - unfold
  1. class Posts < ActiveRecord::Base
  2.   belongs_to :user,
  3.        :class_name => "User",
  4.              :foreign_key => "user_id"
2. Edit def create and def list in your Posts controller so you are adding the user's id to each post and calling the correct query for list/show.

/app/controllers/posts_controller.rb


Code :   - fold - unfold
  1. class PostsController < ApplicationController
  2.   def create
  3.     @post = Post.new(params[:post])
  4.     @post.user_id = @session['user'].id
  5.     ...
  6.   end
  7.  
  8.   def list
  9.     @posts = Post.find(:all,
  10.            :conditions => ['user_id = ?', @session['user'].id])
  11.     ...
  12.   end
  13. end
There you have it!  It's that simple.  If you have questions with this tutorial you can shoot me an email and I will be happy to assist you where possible.

Last edited by patrick@iws (2006-11-12 03:48:20)

Offline

 

#2 2006-11-14 17:08:56

Lake
First Class
From: Charlotte
Registered: 2006-09-16
Posts: 245
Website

Re: User Management Using Salted Hash Login Generator

Hey, thankss.

Offline

 

#3 2006-11-15 18:54:52

mr_jbuck
Ticketholder
Registered: 2006-11-15
Posts: 4

Re: User Management Using Salted Hash Login Generator

Great Post!  Do you think this would also work with the Model Security Gem written by Bruce Perens?

Offline

 

#4 2006-11-16 03:02:44

mr_jbuck
Ticketholder
Registered: 2006-11-15
Posts: 4

Re: User Management Using Salted Hash Login Generator

Thanks again I was able to get it working!  Do you think you will make any other ones like this?

Offline

 

#5 2006-11-22 17:56:10

patrick@iws
Ticketholder
From: Nashville, TN USA
Registered: 2006-11-11
Posts: 3
Website

Re: User Management Using Salted Hash Login Generator

I will be posting a complete tutorial on authentication shortly after thanksgiving.  It will be a rather long one, so be prepared to spend at least an hour digging through the code and making fit for your use.

And yes, this same method will work with any authentication gem.  You just have to make sure you are referring to the same session set in the authentication controller.

Offline

 

#6 2006-11-29 22:02:45

mr_jbuck
Ticketholder
Registered: 2006-11-15
Posts: 4

Re: User Management Using Salted Hash Login Generator

Thanks again! 

I can't wait to read your other postings.  I hope the posting you are referring to will include how to add more types of before_filters.

Offline

 

#7 2006-12-05 00:48:36

Lake
First Class
From: Charlotte
Registered: 2006-09-16
Posts: 245
Website

Re: User Management Using Salted Hash Login Generator

dude, this is fantastic.
I love you.
edit:
and i can't wait for the next on.

Last edited by ldenman (2006-12-05 00:48:57)

Offline

 

#8 2007-03-08 16:17:16

andy_dick
Ticketholder
Registered: 2007-03-08
Posts: 4

Re: User Management Using Salted Hash Login Generator

I got a problem with my first try of this.
It works well but...

Ruby version    1.8.5 (i386-mswin32)
Rails version    1.2.2
Database adapter    oci

I'm using Oracle and salted_login_generator 1.1.1

As you can see below users have one role:

Code :   - fold - unfold
  1. CREATE TABLE  "USERS"
  2.    (  "ID" NUMBER(11,0),
  3.   "ROLE_ID" NUMBER(11,0),
  4.   "SALTED_PASSWORD" VARCHAR2(250),
  5.   "LASTNAME" NVARCHAR2(250),
  6.   "FIRSTNAME" NVARCHAR2(100),
  7.   "LOGIN" VARCHAR2(100),
  8.   "EMAIL" VARCHAR2(60),
  9.   "SALT" VARCHAR2(40),
  10.   "VERIFIED" NUMBER(1,0),
  11.   "SECURITY_TOKEN" VARCHAR2(40),
  12.   "TOKEN_EXPIRY" DATE,
  13.   "CREATED_AT" DATE,
  14.   "UPDATED_AT" DATE,
  15.   "LOGGED_IN_AT" DATE,
  16.   "DELETED" NUMBER(1,0),
  17.   "DELETE_AFTER" DATE,
  18.    PRIMARY KEY ("ID") ENABLE,
  19.    CONSTRAINT "FK_USERS" FOREIGN KEY ("ROLE_ID")
  20.     REFERENCES  "ROLES" ("ID") ON DELETE CASCADE ENABLE
  21.    )

Code :   - fold - unfold
  1. CREATE TABLE  "ROLES"
  2.    (  "ID" NUMBER(11,0),
  3.   "TITLE" NVARCHAR2(100),
  4.    PRIMARY KEY ("ID") ENABLE
  5.    )
Similar in model code:

Code :   - fold - unfold
  1. class User < ActiveRecord::Base
  2.   ...
  3.   has_one :role, :class_name => "User", :foreign_key => "role_id"
  4.   ...
  5. end

Code :   - fold - unfold
  1. class Role < ActiveRecord::Base
  2.   belongs_to :user
  3. end
But when I'm trying to access

Me wrote:

@session['user'].role.title

it throws NoMethodError, meanwhile

Me wrote:

@session['user'].role.id

works well.

Where did I mistake?

Ruby wrote:

NoMethodError in Project_man#list

Showing app/views/layouts/project_man.rhtml where line #12 raised:

undefined method `title' for #<User:0x7ac90d4>

Extracted source (around line #12):

9: </head>
10: <body>
11:
12: <p>Hello <%= @session['user'].role.title.to_s + " " + @session['user'].login.to_s %></p>

Offline

 

#9 2007-10-31 11:08:55

prey
Ticketholder
Registered: 2007-10-31
Posts: 1

Re: User Management Using Salted Hash Login Generator

Great. I had to deal with the same problem and my personal solution looks exactly the same. Thank you for the confirmation of my work.

Offline

 
  • Index
  •  » Tutorials
  •  » User Management Using Salted Hash Login Generator

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson