lib/strongbolt/user_abilities.rb
#
# Checks if the instance given fulfills tenant management rules
#
# returns true even if instance has no relationship to any tenant
#
Checks if the instance given fulfills tenant management rules
returns true even if instance has no relationship to any tenant
Checks if the instance given fulfills tenant management rules returns true even if instance has no relationship to any tenant
def has_access_to_tenants? instance, tenants = nil
        tenants ||= Strongbolt.tenants
        populate_tenants_cache
        found_any_tenant_relationship = false
        has_access_to_any_tenant = tenants.inject(false) do |result, tenant|
          begin
            if instance.class == tenant
              tenant_ids = [instance.id]
            elsif instance.respond_to?(tenant.send(:singular_association_name))
              if instance.send(tenant.send(:singular_association_name)).present?
                tenant_ids = [instance.send(tenant.send(:singular_association_name)).id]
              else
                tenant_ids = []
              end
            elsif instance.respond_to?(tenant.send(:plural_association_name))
              tenant_ids = instance.send("#{tenant.send(:singular_association_name)}_ids")
            else
              next result
            end
          rescue ActiveModel::MissingAttributeError
            tenant_ids = []
          end
          found_any_tenant_relationship = true unless tenant_ids.empty?
          has_access_to_current_tenant = (tenant_ids.size > 0 && (@tenants_cache[tenant.name] & tenant_ids).present?)
          result || has_access_to_current_tenant
        end
        has_access_to_any_tenant || !found_any_tenant_relationship
      end
def has_access_to_tenants? instance , tenants = nil tenants ||= Strongbolt . tenants populate_tenants_cache found_any_tenant_relationship = false has_access_to_any_tenant = tenants . inject ( false ) do | result , tenant | begin if instance . class == tenant tenant_ids = [ instance . id ] elsif instance . respond_to? ( tenant . send ( :singular_association_name ) ) if instance . send ( tenant . send ( :singular_association_name ) ) . present? tenant_ids = [ instance . send ( tenant . send ( :singular_association_name ) ) . id ] else tenant_ids = [ ] end elsif instance . respond_to? ( tenant . send ( :plural_association_name ) ) tenant_ids = instance . send ( "#{tenant.send(:singular_association_name)}_ids" ) else next result end rescue ActiveModel :: MissingAttributeError tenant_ids = [ ] end found_any_tenant_relationship = true unless tenant_ids . empty? has_access_to_current_tenant = ( tenant_ids . size > 0 && ( @tenants_cache [ tenant . name ] & tenant_ids ) . present? ) result || has_access_to_current_tenant end has_access_to_any_tenant || ! found_any_tenant_relationship end
Checks if the instance given fulfills tenant management rules
returns true even if instance has no relationship to any tenant
Checks if the instance given fulfills tenant management rules returns true even if instance has no relationship to any tenant
# If no tenants list given, we take all # Populate the cache if needed # Go over each tenants and check if we access to at least one of the tenant # models linked to it # When we perform a :select on a model, we may omit # the attribute(s) that link(s) to the tenant. # In that case, we have to suppose the user has access
instance
tenants