Sha256: 48c116796c5188fe1e9fc3f0d5669239f2a0671d84a9b60656cdaf28995d49a8

Contents?: true

Size: 1.85 KB

Versions: 5

Compression:

Stored size: 1.85 KB

Contents

require 'active_record'
module Cbac
  module CbacPristine
    class PristineRole < ActiveRecord::Base
      set_table_name "cbac_staged_roles"

      def self.ROLE_TYPES
        {:context => "context", :generic => "generic", :admin => "administrator"}
      end

     
      #convert this cbac role to a yml statement which can be used to create a yml fixtures file
      #executing this statement will result in one cbac_generic_role in the DB
      def to_yml_fixture(fixture_id = nil)
        fixture_id = role_id if fixture_id.nil?

        return '' if role_type == Cbac::CbacPristine::PristineRole.ROLE_TYPES[:context]
        raise ArgumentError, "cannot convert role #{id.to_s} to yml, because it has no name" if name.blank?

        yml = "cbac_generic_role_00" << fixture_id.to_s << ":\n"
        yml << "  id: " << fixture_id.to_s << "\n"
        yml << "  name: " << name << "\n"
        yml << "  created_at: " << Time.now.strftime("%Y-%m-%d %H:%M:%S") << "\n"
        yml << "  updated_at: " << Time.now.strftime("%Y-%m-%d %H:%M:%S") << "\n"
        yml << "\n"
      end

      def known_permission_type
        # NOTE: known permissions use different type definitions than pristine roles.
        # They only use the file type to determine if it is a generic or context role.
        # Context roles include the admin role (same file) while pristine roles use a different type
        role_type == PristineRole.ROLE_TYPES[:generic] ? Cbac::KnownPermission.PERMISSION_TYPES[:generic] : Cbac::KnownPermission.PERMISSION_TYPES[:context]
      end

      def self.admin_role(use_db = true)
        admin_role =  use_db ? PristineRole.first(:conditions => {:role_type => PristineRole.ROLE_TYPES[:admin]}) : nil

        admin_role.nil?  ? PristineRole.new(:role_id => 1, :role_type => PristineRole.ROLE_TYPES[:admin], :name => "administrator") : admin_role
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
cbac-0.6.2 lib/cbac/cbac_pristine/pristine_role.rb
cbac-0.5.4 lib/cbac/cbac_pristine/pristine_role.rb
cbac-0.5.3 lib/cbac/cbac_pristine/pristine_role.rb
cbac-0.5.2 lib/cbac/cbac_pristine/pristine_role.rb
cbac-0.5.1 lib/cbac/cbac_pristine/pristine_role.rb