Skip to content

Integration with Devise2

Michael edited this page Sep 24, 2013 · 2 revisions

ApplicationController

include TheRoleController

class ApplicationController < ActionController::Base
  # include TheRole::Requires # deprecated

  protect_from_forgery

  def access_denied
    render :text => 'access_denied: requires an role' and return
  end

  alias_method :login_required,     :authenticate_user! # you may change `authenticate_user` if have another Model, for example `Login` and then `authenticate_login`
  alias_method :role_access_denied, :access_denied
end

PagesController

class PagesController < ApplicationController
  before_filter :login_required, :except => [:index, :show]
  before_filter :role_required,  :except => [:index, :show]

  before_filter :find_page,      :only   => [:edit, :update, :destroy]
  before_filter :owner_required, :only   => [:edit, :update, :destroy]

  # Public

  def index
    @pages = Page.published.all
  end

  def show
    @page = Page.published.find params[:id]
  end

  # Login && role

  def new
    @page = Page.new
  end

  def create
    @page = Page.new(params[:page])

    if @page.save
      redirect_to @page, :notice => 'Page was successfully created.'
    else
      render :action => 'new'
    end
  end

  def my
    @pages = current_user.pages
  end

  # login && role && ownership

  def edit; end

  def update
    if @page.update_attributes(params[:page])
      redirect_to @page, :notice => 'Page was successfully updated.'
    else
      render :action => 'edit'
    end
  end

  def destroy
    @page.destroy
    redirect_to pages_url
  end

  # Admin or Pages Moderator Role require

  def manage
    @pages = Page.all
  end

  private

  def find_page
    @page = Page.find params[:id]
    @ownership_checking_object = @page
  end
end