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.
B2c94554798f93efc509f076d5ba8c9c?d=identicon&s=25 Josh Rickard (idrifter)
on 2006-03-08 04: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.
7ed00c190370c2f48b0327acfd2b6bf8?d=identicon&s=25 Jon Bloch (Guest)
on 2006-03-08 23: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 Rickard 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?
>
40c46c920df128101e508547d6bee066?d=identicon&s=25 Nathan Mahon (Guest)
on 2006-03-20 19: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
>
05d703f649ef1d07e78d7b479fb4c4ac?d=identicon&s=25 James Adam (Guest)
on 2006-03-21 02: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 Mahon <nate@cs.ucf.edu> wrote:
>   def authenticate(login, pass)
>     return nil unless pass == "blue"
>
> >  def fullname
> http://lists.rails-engines.org/listinfo.cgi/engine...
>


--
* J *
  ~
40c46c920df128101e508547d6bee066?d=identicon&s=25 Nathan Mahon (Guest)
on 2006-03-21 16:28
Ahh... i tried duplicating the:
module ClassMethods
end

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

n8

James Adam 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 Mahon <nate@cs.ucf.edu> wrote:
>>   def authenticate(login, pass)
>>     return nil unless pass == "blue"
>>
>> >  def fullname
>> http://lists.rails-engines.org/listinfo.cgi/engine...
>>
>
>
> --
> * J *
>   ~
40c46c920df128101e508547d6bee066?d=identicon&s=25 Nathan Mahon (Guest)
on 2006-03-21 17: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
B3a82d5016fe4780821e6ad080f9d021?d=identicon&s=25 Joe K (Guest)
on 2006-04-10 04: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 Mahon 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
B2c94554798f93efc509f076d5ba8c9c?d=identicon&s=25 Josh Rickard (idrifter)
on 2006-04-10 04: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 Mahon 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
Cda3372d2ea960d0843a4b37ad2b7bec?d=identicon&s=25 Nathan Mahon (Guest)
on 2006-07-27 15: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 Mahon 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.