Forum: Rails Engines LoginEngine questions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Josh R. (Guest)
on 2006-03-08 05:41
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.
Jon Bloch (Guest)
on 2006-03-09 00:04
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 M. (Guest)
on 2006-03-20 20:41
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
>
James A. (Guest)
on 2006-03-21 03:03
(Received via mailing list)
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> wrote:
>   def authenticate(login, pass)
>     return nil unless pass == "blue"
>
> >  def fullname
> http://lists.rails-engines.org/listinfo.cgi/engine...
>


--
* J *
  ~
Nathan M. (Guest)
on 2006-03-21 17:28
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> wrote:
>>   def authenticate(login, pass)
>>     return nil unless pass == "blue"
>>
>> >  def fullname
>> http://lists.rails-engines.org/listinfo.cgi/engine...
>>
>
>
> --
> * J *
>   ~
Nathan M. (Guest)
on 2006-03-21 18:12
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
Joe K (Guest)
on 2006-04-10 06:03
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
Josh R. (Guest)
on 2006-04-10 06:14
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/overloadin...


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
Nathan M. (Guest)
on 2006-07-27 17:33
*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 topic is locked and can not be replied to.