Class: Authorizer::Admin

Inherits:
Base show all
Defined in:
lib/authorizer/admin.rb

Overview

helper class that does administrative functions for you

Constant Summary collapse

@@user_class_name =

not everybody uses “User” as user class name. We do :o -=- :))) ffFF ww ppO :)))

"User"

Class Method Summary collapse

Methods inherited from Base

authorize, authorize!, authorize_user, count, create_ownership, find, find_user_for_object, is_authorized?, remove_authorization, user_is_authorized?

Class Method Details

.create_brand_new_object_roles(options = {}) ⇒ Object

create_brand_new_object_roles

For if you’ve been working without Authorizer and want to start using it. Obviously, if you don’t have any ObjectRoles then you’ll find yourself blocked out of your own app. This method will assign all objects listed in an array to a certain user. For instance:

user = User.find(1) objects = [ “Post”, “Category” ] Authorizer::Admin.create_brand_new_object_roles( :user => user, :objects => objects )

If objects is not specified, Admin will look for all descendants of ActiveRecord::Base and exclude the ObjectRole and User classes.

Authorizer::Admin.create_brand_new_object_roles( :user => user )



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
# File 'lib/authorizer/admin.rb', line 28

def self.create_brand_new_object_roles(options = {})
  OptionsChecker.check(options, [ :user ])

  objects = gather_direct_descendants_of_activerecord_base || options[:objects]

  ret = false

  raise "objects must be an Array" unless objects.is_a?(Array)

  # Nothing to do ..
  return ret if objects.blank?

  begin
    user_id = options[:user].id
  rescue
  end

  unless user_id.nil?
    for object in objects
      evaled_klazz = nil

      begin
        evaled_klazz = eval(object)
      rescue
      end

      unless evaled_klazz.nil?
        # One is enough to return exit status OK.
        ret = true
        # Let's find all. This is the same as Post.all
        if evaled_klazz.is_a?(Class)
          collection = evaled_klazz.all

          # Go
          unless collection.blank?
            for coll_ in collection
              ObjectRole.create!( :klazz_name => object, :object_reference => coll_.id, :user_id => user_id, :role => "owner" )
            end
          end
        end
      end
    end
  end

  ret
end

.remove_all_unused_authorization_objects(options = {}) ⇒ Object

remove_all_unused_authorization_objects

Remove all stale (non-object) authorization objects.



81
82
83
84
85
86
87
88
# File 'lib/authorizer/admin.rb', line 81

def self.remove_all_unused_authorization_objects options = {}
  # no options
  # ___
  # Let's iterate all ObjectRoles
  for object_role in ObjectRole.all
    object_role.destroy if object_role.object.nil?
  end
end