dimanche 24 mai 2015

Rails 4: different base controllers for general users and admin users

I have a fairly standard application that has a layout for users after they have authenticated and a layout for admin users. I keep the users and admins in separate tables as an admin is a much different "user" of the application than a normal user.

To that end, I will have different authentication structures for an admin vs a user.

I've broken my application up into 2 main sections (general and admin) and use 2 separate controllers to inherit from.

class BaseGeneralController < ApplicationController
  before_action :require_user

  def require_user
    # user authentication...
  end
end

class BaseAdminController < ApplicationController
  before_action :require_admin

  def require_admin
    # admin authentication...
  end
end

# Each account has many users, and a user may perform CRUD on the
# users for his account.
class UsersController < BaseGeneralController
  # ...
end

# Administrating accounts belongs to an admin, so inherits from the
# admin controller instead.
class AccountsController < BaseAdminController
  # ...
end

However, I feel like I'm missing something and breaking convention with this approach. Since an admin and a user are 2 drastically different users of the application, I felt it necessary to break them apart. But now I'm breaking my controllers apart as well, and that seems unconventional.

Is there a convention or standard for addressing issues like these? Is there a design pattern that this process is known by?

Not sure if this falls under an "opinion" question, but I couldn't find any good literature from Google about situations like this so wanted to ask a targeted community. Chunking up parts of an application seems very standard, but I don't want to use an approach that will hurt my development efforts later on.

Aucun commentaire:

Enregistrer un commentaire