Hello I’m actually using Ryan’ Bates cancan gem authorization to make my application’s users to only manage the data that they create.I’m using Sorcery gem to handle authentification.
models/ability.rb
class Ability
include CanCan::Ability
def initialize(user)
user ||= User.new # guest user (not logged in)
if user
can :manage, Profile, :user_id => user.id
can :manage, Album, :user_id => user.id
end
end
end
controllers/albums_controllers.rb
# -*- encoding : utf-8 -*-
class AlbumsController < ApplicationController
# Authentification before accessing Albums
before_filter :require_login, :except => [:not_authenticated]
load_and_authorize_resource
def index
@albums = Album.all
respond_to do |format|
format.html # index.html.erb
format.json { render json: @albums }
end
end
def show
@album = Client.find(params[:id])
authorize! :show, @album
respond_to do |format|
format.html # show.html.erb
format.json { render json: @album }
end
end
def new
@album = Album.new
respond_to do |format|
format.html # new.html.erb
format.json { render json: @album }
end
end
def edit
@album = Album.find(params[:id])
authorize! :edit, @album
end
def create
@album = Album.new(params[:album])
respond_to do |format|
if @album.save
format.html { redirect_to @album, notice: 'album was successfully created.' }
format.json { render json: @album, status: :created, location: @album }
else
format.html { render action: "new" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
def update
@album = Album.find(params[:id])
authorize! :update, @album
respond_to do |format|
if @album.update_attributes(params[:album])
format.html { redirect_to @album, notice: 'Album was successfully updated.' }
format.json { head :ok }
else
format.html { render action: "edit" }
format.json { render json: @album.errors, status: :unprocessable_entity }
end
end
end
def destroy
@album = Album.find(params[:id])
@album.destroy
respond_to do |format|
format.html { redirect_to albums_url }
format.json { head :ok }
end
end
end
But after this a user can still operate on another user’s data.What am I missing to do.
load_and_authorize_resourceautomatically provides you with @albums (so there is no need to set it again inindex. So in the following:@albumsis being loaded again with all the albums, that’s why it is showing them all. You can replace that with this:But even this is not required as
load_and_authorize_resourcepopulates@albumswith the correct albums for the current user. So the following would suffice:will give you the same result. This is the power of cancan.Also, instead of
load_and_authorize_resource, you can useauthorize_resourceorload_resourseseparately. More details here