fesplugas/typus — pull request #329 by iainad (2011-12-26 10:28:11+00:00)
Title: Allowing custom authenticate_user! for devise authentication

In my application I wanted to use Devise for my normal users and have a subset of these to have admin roles and be able to access Typus. I expect this is a fairly common thing if people use devise and typus together.

It seemed to me that the simplest way would be to add an option to the devise support in Typus to call a custom method rather than 'authenticate_#{model_name}' as it does now.

These changes are to add a customisable prefix to the devise authentication method - 'devise_authentication_method_prefix' that ha a default as it is today.

So for example I can now add in my typus.rb initialiser:

```ruby
# config/initializers/typus.rb
config.authentication = :devise
config.user_class_name = "User"
config.devise_authentication_method_prefix = "authenticate_admin"
```
and then add a new module to my application with this custom authenticate method:

```ruby
# lib/typus_custom_authentication.rb
module TypusCustomAuthentication

def authenticate_admin_user!(opts={})
authenticate_user!(opts)
unless current_user.is_site_admin?
redirect_to unauthorised_path
end
end

ActionController::Base.send(:include, TypusCustomAuthentication)
end
```

My user model then has this custom method:

```ruby
# app/models/user.rb
def is_site_admin?
# some test returning true or false, e.g. using cancan
end
```

I then have a named route called unauthorised:

```ruby
# config/routes.rb
match '/unauthorised', :to => "static#unauthorised", :as => "unauthorised"
```

To make the custom admin available to the admin controllers in Typus this I add in my typus initializer:

```ruby
# config/initializers/typus.rb
include TypusCustomAuthentication
```

This seems to work just fine so it would be great to have the option as one in Typus itself rather than my fork. The changes are really small so should not be too painful.