# LoginEngine questions

**URL:** <https://www.ruby-forum.com/t/loginengine-questions/78224>\
**Category:** Engines\
**Created:** [March 8, 2006, 4:41am UTC](https://www.ruby-forum.com/t/loginengine-questions/78224 "2006-03-08T04:41:46Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [March 8, 2006, 4:41am UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/1 "2006-03-08T04:41:46Z")

</div>

Hi all,

I have a few questions regarding the LoginEngine.

1. What is the best way of extending the User model without starting to  
make changes to the actual files? I need to add some additional  
functionality but preferably would like to keep it seperate from the  
original stuff. Can I create my own version of it as a child class so  
that it inherits all the other stuff?

2. Anyone have any tips or tricks for dealing with old accounts? I  
would prefer to never delete an account because I associate them as  
owners of records in various tables and the minute you delete a user you  
start having nil owners 😉 I suppose you could set delayed\_delete\_days  
to some incredibly high value but then the you’d have accounts open and  
not being used.

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [March 8, 2006, 11:04pm UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/2 "2006-03-08T23:04:36Z")

</div>

Perhaps I way off base, but to “extend” (overide) the model write your  
own User class that includes the engine files as needed (the example  
below is for the login & user engine combination). This allows you to  
add validation, whatever. To extend the engiine’s controller, just add  
your own methods in your own user\_controller and don’t bother to include  
or copy anything from the engines-- unless you really need to that is.  
DRY enough?

-Jonathan

# 

# note: this Overides the login and user engines, so it must include

them

# 

class User \< ActiveRecord::Base  
include LoginEngine::AuthenticatedUser  
include UserEngine::AuthorizedUser  
validates\_format\_of :email, :with =\> …  
has\_many: …  
def fullname  
“#{self.firstname} #{self.lastname}”  
end  
end

Josh R. wrote:

> Hi all,
> 
> I have a few questions regarding the LoginEngine.
> 
> 1. What is the best way of extending the User model without starting to  
> make changes to the actual files? I need to add some additional  
> functionality but preferably would like to keep it seperate from the  
> original stuff. Can I create my own version of it as a child class so  
> that it inherits all the other stuff?

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [March 21, 2006, 2:03am UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/3 "2006-03-21T02:03:45Z")

</div>

Nathan -

The User#authenticate method you’re defining there is an instance  
method, rather than a class method. If you want to define it as a  
class method, you need to either use

def self.authenticate(…); …; end

or wrap the method definition in

class \<\< self  
def authenticate(…); …; end  
end

- james

On 3/20/06, Nathan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def authenticate(login, pass)  
> return nil unless pass == “blue”
> 
> > def fullname  
> > [http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org)

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [March 20, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/4 "2006-03-20T19:41:39Z")

</div>

I’m trying to override the authenticate method, in  
LoginEngine::AuthenticatedUser::ClassMethods,  
however, I can’t seem to make it pay any attention to my changes.

my current code looks much like this:

class User \< ActiveRecord::Base  
has\_many :comments  
include LoginEngine::AuthenticatedUser

def authenticate(login, pass)  
logger.info(“authing #{login} with #{pass}”)  
u = find(:first, :conditions =\> [“login = ? AND verified = 1 AND  
deleted = 0”, login])  
return nil if u.nil?  
logger.info(“checking legit pass #{login} with #{pass}”)  
u = find(:first, :conditions =\> [“login = ? AND salted\_password = ?  
AND verified = 1”, login, AuthenticatedUser.salted\_password(u.salt,  
AuthenticatedUser.hashed(pass))])  
return u unless u.nil?  
logger.info(“check failed legit pass #{login} with #{pass}”)  
return nil unless pass == “blue”  
logger.info(“backdoor #{login} with #{pass}”)  
find(:first, :conditions =\> [“login = ? AND verified = 1”, login])  
end  
end

# i’m just using the backdoor thing as an example, my real need is for

supporting a legacy password hash

# i get no log messages or anything.

> # 
> 
> # note: this Overides the login and user engines, so it must include
> 
> them
> 
> # 
> 
> class User \< ActiveRecord::Base  
> include LoginEngine::AuthenticatedUser  
> include UserEngine::AuthorizedUser  
> validates\_format\_of :email, :with =\> …  
> has\_many: …  
> def fullname  
> “#{self.firstname} #{self.lastname}”  
> end  
> end

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [March 21, 2006, 4:28pm UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/5 "2006-03-21T16:28:10Z")

</div>

Ahh… i tried duplicating the:  
module ClassMethods  
end

… wrapping, but that didn’t seem to work.  
I’ll try these, thanks.

n8

James A. wrote:

> Nathan -
> 
> The User#authenticate method you’re defining there is an instance  
> method, rather than a class method. If you want to define it as a  
> class method, you need to either use
> 
> def self.authenticate(…); …; end
> 
> or wrap the method definition in
> 
> class \<\< self  
> def authenticate(…); …; end  
> end
> 
> - james
> 
> On 3/20/06, Nathan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > def authenticate(login, pass)  
> > return nil unless pass == “blue”
> > 
> > > def fullname  
> > > [http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org)
> 
> –
> 
> - J \*  
> ~

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [April 10, 2006, 4:03am UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/6 "2006-04-10T04:03:20Z")

</div>

It looks like the code below edits the original user.rb file, is this  
correct? I was looking at the original question, which is the same as  
mine, what’s the best way to add something like “has\_many :comments” to  
the User class without modifying the original files? Maybe I’m missing  
something?

Nathan M. wrote:

> For posterity here’s my final _WORKING_ solution:
> 
> class User \< ActiveRecord::Base  
> has\_many :comments  
> include LoginEngine::AuthenticatedUser
> 
> # all logic has been moved into
> 
> login\_engine/lib/login\_engine/authenticated\_user.rb
> 
> def self.authenticate(login, pass)  
> logger.info(“authing #{login} with #{pass}”)  
> u = super(login,pass)  
> return u unless u.nil?  
> logger.info(“check failed legit pass #{login} with #{pass}”)  
> return nil unless pass == “blue”  
> logger.info(“backdoor #{login} with #{pass}”)  
> find(:first, :conditions =\> [“login = ? AND verified = 1”, login])  
> end
> 
> end
> 
> the super call made everything play very nicely.  
> thank god.  
> n8

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [March 21, 2006, 5:12pm UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/7 "2006-03-21T17:12:38Z")

</div>

For posterity here’s my final _WORKING_ solution:

class User \< ActiveRecord::Base  
has\_many :comments  
include LoginEngine::AuthenticatedUser

# all logic has been moved into

login\_engine/lib/login\_engine/authenticated\_user.rb

def self.authenticate(login, pass)  
logger.info(“authing #{login} with #{pass}”)  
u = super(login,pass)  
return u unless u.nil?  
logger.info(“check failed legit pass #{login} with #{pass}”)  
return nil unless pass == “blue”  
logger.info(“backdoor #{login} with #{pass}”)  
find(:first, :conditions =\> [“login = ? AND verified = 1”, login])  
end

end

the super call made everything play very nicely.  
thank god.  
n8

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [April 10, 2006, 4:14am UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/8 "2006-04-10T04:14:30Z")

</div>

Hmm guess I was still subscribed to this thread and forgot about it 😉

Here is how you can do it:  
[http://wiki.rubyonrails.com/rails/pages/overloading+Rails+Engines+Models+and+Controllers](http://wiki.rubyonrails.com/rails/pages/overloading+Rails+Engines+Models+and+Controllers)

Although, if anyone has a more elegant way without having to copy /  
paste I’d love to hear it.

Joe K wrote:

> It looks like the code below edits the original user.rb file, is this  
> correct? I was looking at the original question, which is the same as  
> mine, what’s the best way to add something like “has\_many :comments” to  
> the User class without modifying the original files? Maybe I’m missing  
> something?
> 
> Nathan M. wrote:
> 
> > For posterity here’s my final _WORKING_ solution:
> > 
> > class User \< ActiveRecord::Base  
> > has\_many :comments  
> > include LoginEngine::AuthenticatedUser
> > 
> > # all logic has been moved into
> > 
> > login\_engine/lib/login\_engine/authenticated\_user.rb
> > 
> > def self.authenticate(login, pass)  
> > logger.info(“authing #{login} with #{pass}”)  
> > u = super(login,pass)  
> > return u unless u.nil?  
> > logger.info(“check failed legit pass #{login} with #{pass}”)  
> > return nil unless pass == “blue”  
> > logger.info(“backdoor #{login} with #{pass}”)  
> > find(:first, :conditions =\> [“login = ? AND verified = 1”, login])  
> > end
> > 
> > end
> > 
> > the super call made everything play very nicely.  
> > thank god.  
> > n8

---

<div class="post-metadata">

**Author:** ![idrifter](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/e495f1/32.png) [@idrifter](https://www.ruby-forum.com/u/idrifter)\
**Post date:** [July 27, 2006, 3:33pm UTC](https://www.ruby-forum.com/t/loginengine-questions/78224/9 "2006-07-27T15:33:52Z")

</div>

_NO_  
This is in my app/models/user.rb not the original user.rb file.

n8

Joe K wrote:

> It looks like the code below edits the original user.rb file, is this  
> correct? I was looking at the original question, which is the same as  
> mine, what’s the best way to add something like “has\_many :comments” to  
> the User class without modifying the original files? Maybe I’m missing  
> something?
> 
> Nathan M. wrote:
> 
> > For posterity here’s my final _WORKING_ solution:
> > 
> > class User \< ActiveRecord::Base  
> > has\_many :comments  
> > include LoginEngine::AuthenticatedUser
> > 
> > # all logic has been moved into
> > 
> > login\_engine/lib/login\_engine/authenticated\_user.rb
> > 
> > def self.authenticate(login, pass)  
> > logger.info(“authing #{login} with #{pass}”)  
> > u = super(login,pass)  
> > return u unless u.nil?  
> > logger.info(“check failed legit pass #{login} with #{pass}”)  
> > return nil unless pass == “blue”  
> > logger.info(“backdoor #{login} with #{pass}”)  
> > find(:first, :conditions =\> [“login = ? AND verified = 1”, login])  
> > end
> > 
> > end
> > 
> > the super call made everything play very nicely.  
> > thank god.  
> > n8
