Sha256: 2ae1815cf614605350f69467f9fa53cf663f67ce8f5b16ccfde8e9ca30e7cf13

Contents?: true

Size: 1.93 KB

Versions: 3

Compression:

Stored size: 1.93 KB

Contents

require 'active_record'
module Cbac
  module CbacPristine
    class PristineRole < ActiveRecord::Base
      self.table_name = "cbac_staged_roles"
      attr_readonly :role_type, :role_id, :name

      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 || PristineRole.new do |role|
          role.role_id = 1
          role.role_type = PristineRole.ROLE_TYPES[:admin]
          role.name = "administrator"
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
cbac-0.6.10 lib/cbac/cbac_pristine/pristine_role.rb
cbac-0.6.9 lib/cbac/cbac_pristine/pristine_role.rb
cbac-0.6.8 lib/cbac/cbac_pristine/pristine_role.rb