<commit_before>require 'active_model'
require 'aspect4r'
require 'cg_service_client'

module CgRoleClient
  # A role represents a set of permissions for an actor or group
  # on a particular target.
  class Role
    include ActiveModel::Validations
    include Aspect4r # this has to be here for the class level "around" to work
    include CgServiceClient::Serializable
    extend CgServiceClient::Serviceable

    uses_service("Role", "1", "CgRoleClient::RestEndpoint")

    serializable_attr_accessor :id, :group_id, :target_id, :role_type_id, :created_at, :updated_at

    validates_presence_of :group_id, :target_id, :role_type_id

    class << self
      include Aspect4r

      around :grant, :aggregate_role do |*args, &block |
        begin
          ensure_endpoint
          block.call(*args)
        rescue Exception => e
          puts e
          raise
        end
      end

        # Grant a new role for an actor or group on a target. Target
        # must be an entity with an ID.
      def grant(role_type, actor_or_group, target)
        group = group_for(actor_or_group)
        role = Role.new({:role_type_id => role_type.id,
                         :group_id => group.id,
                         :target_id => target.id})
        @endpoint.create_role(role)
      end

        # Get the aggregate role for an actor or group on a target.
        # See CgRoleClient::AggregateRole
      def aggregate_role(actor_or_group, target)
        group = group_for(actor_or_group)
        begin
          roles = @endpoint.find_group_roles_on_target(group.id, target.class, target.id)
        rescue => e
          if e.kind_of?(CgServiceClient::Exceptions::ClientError) && e.http_code == 404<commit_msg>aggregate role can take either a target object or a hash. CP-1485<commit_after>require 'active_model'
require 'aspect4r'
require 'cg_service_client'

module CgRoleClient
  # A role represents a set of permissions for an actor or group
  # on a particular target.
  class Role
    include ActiveModel::Validations
    include Aspect4r # this has to be here for the class level "around" to work
    include CgServiceClient::Serializable
    extend CgServiceClient::Serviceable

    uses_service("Role", "1", "CgRoleClient::RestEndpoint")

    serializable_attr_accessor :id, :group_id, :target_id, :role_type_id, :created_at, :updated_at

    validates_presence_of :group_id, :target_id, :role_type_id

    class << self
      include Aspect4r

      around :grant, :aggregate_role do |*args, &block |
        begin
          ensure_endpoint
          block.call(*args)
        rescue Exception => e
          puts e
          raise
        end
      end

        # Grant a new role for an actor or group on a target. Target
        # must be an entity with an ID.
      def grant(role_type, actor_or_group, target)
        group = group_for(actor_or_group)
        role = Role.new({:role_type_id => role_type.id,
                         :group_id => group.id,
                         :target_id => target.id})
        @endpoint.create_role(role)
      end

        # Get the aggregate role for an actor or group on a target.
        # See CgRoleClient::AggregateRole
      def aggregate_role(actor_or_group, target)
        group = group_for(actor_or_group)
        if target.class == Hash
          target = target
        else
          target = {:class => target.class, :id => target.id}
        end
        begin
          roles = @endpoint.find_group_roles_on_target(group.id, target[:class], target[:id])
        rescue => e
          if e.kind_of?(CgServiceClient::Exceptions::ClientError) && e.http_code == 404
{'fraction_non_alphanumeric': 0.08083585372559803, 'fraction_numerical': 0.004949133901567226, 'mean_word_length': 2.5320388349514564, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '210366', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1071, 'n_words': 314}