LoginEngine questions

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 :wink: 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.

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?

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. [email protected] wrote:

def authenticate(login, pass)
return nil unless pass == “blue”

def fullname
http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org

  • J *
    ~

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

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. [email protected] wrote:

def authenticate(login, pass)
return nil unless pass == “blue”

def fullname
http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org

  • J *
    ~

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

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

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

Here is how you can do it:
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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs