Skip to content
This repository
Ben Moss
file 398 lines (379 sloc) 16.133 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
module CanCan

  # This module is automatically included into all controllers.
  # It also makes the "can?" and "cannot?" methods available to all views.
  module ControllerAdditions
    module ClassMethods
      # Sets up a before filter which loads and authorizes the current resource. This performs both
      # load_resource and authorize_resource and accepts the same arguments. See those methods for details.
      #
      # class BooksController < ApplicationController
      # load_and_authorize_resource
      # end
      #
      def load_and_authorize_resource(*args)
        cancan_resource_class.add_before_filter(self, :load_and_authorize_resource, *args)
      end

      # Sets up a before filter which loads the model resource into an instance variable.
      # For example, given an ArticlesController it will load the current article into the @article
      # instance variable. It does this by either calling Article.find(params[:id]) or
      # Article.new(params[:article]) depending upon the action. The index action will
      # automatically set @articles to Article.accessible_by(current_ability).
      #
      # If a conditions hash is used in the Ability, the +new+ and +create+ actions will set
      # the initial attributes based on these conditions. This way these actions will satisfy
      # the ability restrictions.
      #
      # Call this method directly on the controller class.
      #
      # class BooksController < ApplicationController
      # load_resource
      # end
      #
      # A resource is not loaded if the instance variable is already set. This makes it easy to override
      # the behavior through a before_filter on certain actions.
      #
      # class BooksController < ApplicationController
      # before_filter :find_book_by_permalink, :only => :show
      # load_resource
      #
      # private
      #
      # def find_book_by_permalink
      # @book = Book.find_by_permalink!(params[:id)
      # end
      # end
      #
      # If a name is provided which does not match the controller it assumes it is a parent resource. Child
      # resources can then be loaded through it.
      #
      # class BooksController < ApplicationController
      # load_resource :author
      # load_resource :book, :through => :author
      # end
      #
      # Here the author resource will be loaded before each action using params[:author_id]. The book resource
      # will then be loaded through the @author instance variable.
      #
      # That first argument is optional and will default to the singular name of the controller.
      # A hash of options (see below) can also be passed to this method to further customize it.
      #
      # See load_and_authorize_resource to automatically authorize the resource too.
      #
      # Options:
      # [:+only+]
      # Only applies before filter to given actions.
      #
      # [:+except+]
      # Does not apply before filter to given actions.
      #
      # [:+through+]
      # Load this resource through another one. This should match the name of the parent instance variable or method.
      #
      # [:+through_association+]
      # The name of the association to fetch the child records through the parent resource. This is normally not needed
      # because it defaults to the pluralized resource name.
      #
      # [:+shallow+]
      # Pass +true+ to allow this resource to be loaded directly when parent is +nil+. Defaults to +false+.
      #
      # [:+singleton+]
      # Pass +true+ if this is a singleton resource through a +has_one+ association.
      #
      # [:+parent+]
      # True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource
      # name is given which does not match the controller.
      #
      # [:+class+]
      # The class to use for the model (string or constant).
      #
      # [:+instance_name+]
      # The name of the instance variable to load the resource into.
      #
      # [:+find_by+]
      # Find using a different attribute other than id. For example.
      #
      # load_resource :find_by => :permalink # will use find_by_permalink!(params[:id])
      #
      # [:+id_param+]
      # Find using a param key other than :id. For example:
      #
      # load_resource :id_param => :url # will use find(params[:url])
      #
      # [:+collection+]
      # Specify which actions are resource collection actions in addition to :+index+. This
      # is usually not necessary because it will try to guess depending on if the id param is present.
      #
      # load_resource :collection => [:sort, :list]
      #
      # [:+new+]
      # Specify which actions are new resource actions in addition to :+new+ and :+create+.
      # Pass an action name into here if you would like to build a new resource instead of
      # fetch one.
      #
      # load_resource :new => :build
      #
      # [:+prepend+]
      # Passing +true+ will use prepend_before_filter instead of a normal before_filter.
      #
      def load_resource(*args)
        cancan_resource_class.add_before_filter(self, :load_resource, *args)
      end

      # Sets up a before filter which authorizes the resource using the instance variable.
      # For example, if you have an ArticlesController it will check the @article instance variable
      # and ensure the user can perform the current action on it. Under the hood it is doing
      # something like the following.
      #
      # authorize!(params[:action].to_sym, @article || Article)
      #
      # Call this method directly on the controller class.
      #
      # class BooksController < ApplicationController
      # authorize_resource
      # end
      #
      # If you pass in the name of a resource which does not match the controller it will assume
      # it is a parent resource.
      #
      # class BooksController < ApplicationController
      # authorize_resource :author
      # authorize_resource :book
      # end
      #
      # Here it will authorize :+show+, @+author+ on every action before authorizing the book.
      #
      # That first argument is optional and will default to the singular name of the controller.
      # A hash of options (see below) can also be passed to this method to further customize it.
      #
      # See load_and_authorize_resource to automatically load the resource too.
      #
      # Options:
      # [:+only+]
      # Only applies before filter to given actions.
      #
      # [:+except+]
      # Does not apply before filter to given actions.
      #
      # [:+singleton+]
      # Pass +true+ if this is a singleton resource through a +has_one+ association.
      #
      # [:+parent+]
      # True or false depending on if the resource is considered a parent resource. This defaults to +true+ if a resource
      # name is given which does not match the controller.
      #
      # [:+class+]
      # The class to use for the model (string or constant). This passed in when the instance variable is not set.
      # Pass +false+ if there is no associated class for this resource and it will use a symbol of the resource name.
      #
      # [:+instance_name+]
      # The name of the instance variable for this resource.
      #
      # [:+through+]
      # Authorize conditions on this parent resource when instance isn't available.
      #
      # [:+prepend+]
      # Passing +true+ will use prepend_before_filter instead of a normal before_filter.
      #
      def authorize_resource(*args)
        cancan_resource_class.add_before_filter(self, :authorize_resource, *args)
      end

      # Skip both the loading and authorization behavior of CanCan for this given controller. This is primarily
      # useful to skip the behavior of a superclass. You can pass :only and :except options to specify which actions
      # to skip the effects on. It will apply to all actions by default.
      #
      # class ProjectsController < SomeOtherController
      # skip_load_and_authorize_resource :only => :index
      # end
      #
      # You can also pass the resource name as the first argument to skip that resource.
      def skip_load_and_authorize_resource(*args)
        skip_load_resource(*args)
        skip_authorize_resource(*args)
      end

      # Skip the loading behavior of CanCan. This is useful when using +load_and_authorize_resource+ but want to
      # only do authorization on certain actions. You can pass :only and :except options to specify which actions to
      # skip the effects on. It will apply to all actions by default.
      #
      # class ProjectsController < ApplicationController
      # load_and_authorize_resource
      # skip_load_resource :only => :index
      # end
      #
      # You can also pass the resource name as the first argument to skip that resource.
      def skip_load_resource(*args)
        options = args.extract_options!
        name = args.first
        cancan_skipper[:load][name] = options
      end

      # Skip the authorization behavior of CanCan. This is useful when using +load_and_authorize_resource+ but want to
      # only do loading on certain actions. You can pass :only and :except options to specify which actions to
      # skip the effects on. It will apply to all actions by default.
      #
      # class ProjectsController < ApplicationController
      # load_and_authorize_resource
      # skip_authorize_resource :only => :index
      # end
      #
      # You can also pass the resource name as the first argument to skip that resource.
      def skip_authorize_resource(*args)
        options = args.extract_options!
        name = args.first
        cancan_skipper[:authorize][name] = options
      end

      # Add this to a controller to ensure it performs authorization through +authorized+! or +authorize_resource+ call.
      # If neither of these authorization methods are called, a CanCan::AuthorizationNotPerformed exception will be raised.
      # This is normally added to the ApplicationController to ensure all controller actions do authorization.
      #
      # class ApplicationController < ActionController::Base
      # check_authorization
      # end
      #
      # See skip_authorization_check to bypass this check on specific controller actions.
      #
      # Options:
      # [:+only+]
      # Only applies to given actions.
      #
      # [:+except+]
      # Does not apply to given actions.
      #
      # [:+if+]
      # Supply the name of a controller method to be called. The authorization check only takes place if this returns true.
      #
      # check_authorization :if => :admin_controller?
      #
      # [:+unless+]
      # Supply the name of a controller method to be called. The authorization check only takes place if this returns false.
      #
      # check_authorization :unless => :devise_controller?
      #
      def check_authorization(options = {})
        self.after_filter(options.slice(:only, :except)) do |controller|
          next if controller.instance_variable_defined?(:@_authorized)
          next if options[:if] && !controller.send(options[:if])
          next if options[:unless] && controller.send(options[:unless])
          raise AuthorizationNotPerformed, "This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check."
        end
      end

      # Call this in the class of a controller to skip the check_authorization behavior on the actions.
      #
      # class HomeController < ApplicationController
      # skip_authorization_check :only => :index
      # end
      #
      # Any arguments are passed to the +before_filter+ it triggers.
      def skip_authorization_check(*args)
        self.before_filter(*args) do |controller|
          controller.instance_variable_set(:@_authorized, true)
        end
      end

      def skip_authorization(*args)
        raise ImplementationRemoved, "The CanCan skip_authorization method has been renamed to skip_authorization_check. Please update your code."
      end

      def cancan_resource_class
        if ancestors.map(&:to_s).include? "InheritedResources::Actions"
          InheritedResource
        else
          ControllerResource
        end
      end

      def cancan_skipper
        @_cancan_skipper ||= {:authorize => {}, :load => {}}
      end
    end

    def self.included(base)
      base.extend ClassMethods
      base.helper_method :can?, :cannot?, :current_ability
    end

    # Raises a CanCan::AccessDenied exception if the current_ability cannot
    # perform the given action. This is usually called in a controller action or
    # before filter to perform the authorization.
    #
    # def show
    # @article = Article.find(params[:id])
    # authorize! :read, @article
    # end
    #
    # A :message option can be passed to specify a different message.
    #
    # authorize! :read, @article, :message => "Not authorized to read #{@article.name}"
    #
    # You can also use I18n to customize the message. Action aliases defined in Ability work here.
    #
    # en:
    # unauthorized:
    # manage:
    # all: "Not authorized to %{action} %{subject}."
    # user: "Not allowed to manage other user accounts."
    # update:
    # project: "Not allowed to update this project."
    #
    # You can rescue from the exception in the controller to customize how unauthorized
    # access is displayed to the user.
    #
    # class ApplicationController < ActionController::Base
    # rescue_from CanCan::AccessDenied do |exception|
    # redirect_to root_url, :alert => exception.message
    # end
    # end
    #
    # See the CanCan::AccessDenied exception for more details on working with the exception.
    #
    # See the load_and_authorize_resource method to automatically add the authorize! behavior
    # to the default RESTful actions.
    def authorize!(*args)
      @_authorized = true
      current_ability.authorize!(*args)
    end

    def unauthorized!(message = nil)
      raise ImplementationRemoved, "The unauthorized! method has been removed from CanCan, use authorize! instead."
    end

    # Creates and returns the current user's ability and caches it. If you
    # want to override how the Ability is defined then this is the place.
    # Just define the method in the controller to change behavior.
    #
    # def current_ability
    # # instead of Ability.new(current_user)
    # @current_ability ||= UserAbility.new(current_account)
    # end
    #
    # Notice it is important to cache the ability object so it is not
    # recreated every time.
    def current_ability
      @current_ability ||= ::Ability.new(current_user)
    end

    # Use in the controller or view to check the user's permission for a given action
    # and object.
    #
    # can? :destroy, @project
    #
    # You can also pass the class instead of an instance (if you don't have one handy).
    #
    # <% if can? :create, Project %>
    # <%= link_to "New Project", new_project_path %>
    # <% end %>
    #
    # If it's a nested resource, you can pass the parent instance in a hash. This way it will
    # check conditions which reach through that association.
    #
    # <% if can? :create, @category => Project %>
    # <%= link_to "New Project", new_project_path %>
    # <% end %>
    #
    # This simply calls "can?" on the current_ability. See Ability#can?.
    def can?(*args)
      current_ability.can?(*args)
    end

    # Convenience method which works the same as "can?" but returns the opposite value.
    #
    # cannot? :destroy, @project
    #
    def cannot?(*args)
      current_ability.cannot?(*args)
    end
  end
end

if defined? ActionController::Base
  ActionController::Base.class_eval do
    include CanCan::ControllerAdditions
  end
end
Something went wrong with that request. Please try again.