-
Notifications
You must be signed in to change notification settings - Fork 82
Integration with Sorcery
the-teacher edited this page Jul 19, 2012
·
4 revisions
rails 3.2.6
sorcery 0.7.12
the_role 1.6.0
git clone
bundle
rake db:create
rails generate sorcery:install
=> db\migrate\XXXXXX_sorcery_core.rb
class SorceryCore < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :username, :null => false
t.string :email, :default => nil
t.string :crypted_password, :default => nil
t.string :salt, :default => nil
# TheRole field
t.integer :role_id, :default => nil
t.timestamps
end
end
end
rake the_role_engine:install:migrations
=> Copied migration 20120719064432_create_roles.the_role_engine.rb
rake db:migrate
rails g model role --migration=false
rake db:roles:test
u = User.new
u.username = 'Jon Dow'
u.email = '[email protected]'
u.password = 'qwerty'
u.save
user_role = Role.where(:name => :user).first
user_role.has?(:pages, :edit) => true
user_role.has?(:pages, :index) => false
user_role.has?(:users, :avatar_upload) => true
u = User.new
u.role = user_role
u.save
u.has_role?(:pages, :edit) => true
u.has_role?(:pages, :index) => false
u.has_role?(:users, :avatar_upload) => true
class ApplicationController < ActionController::Base
include TheRole::Requires
protect_from_forgery
def access_denied
render :text => 'access_denied: requires an role' and return
end
alias_method :login_required, :require_login
alias_method :role_access_denied, :access_denied
end
class WelcomeController < ApplicationController
before_filter :login_required, :except => [:index]
def index; end
def only_for_users
render :text => 'only_for_users' and return
end
end
class PagesController < ApplicationController
# Sorcery and TheRole before_filters
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]
private
def find_page
@page = Page.find params[:id]
@ownership_checking_object = @page
end
end
define @ownership_checking_object with checked object, before invoke of owner_required