@prefix this: <https://w3id.org/np/RA9TXiF8lJc3NTzYAdqhxoeLjFspJO7O28Q7i4s595o3w> .
@prefix sub: <https://w3id.org/np/RA9TXiF8lJc3NTzYAdqhxoeLjFspJO7O28Q7i4s595o3w/> .
@prefix np: <http://www.nanopub.org/nschema#> .
@prefix dct: <http://purl.org/dc/terms/> .
@prefix nt: <https://w3id.org/np/o/ntemplate/> .
@prefix npx: <http://purl.org/nanopub/x/> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix orcid: <https://orcid.org/> .
@prefix prov: <http://www.w3.org/ns/prov#> .
@prefix foaf: <http://xmlns.com/foaf/0.1/> .

<https://w3id.org/np/RA9TXiF8lJc3NTzYAdqhxoeLjFspJO7O28Q7i4s595o3w> a np:Nanopublication;
  np:hasAssertion sub:assertion;
  np:hasProvenance sub:provenance;
  np:hasPublicationInfo sub:pubinfo;
  dct:created "2026-06-12T18:27:09Z"^^xsd:dateTime;
  dct:creator orcid:0000-0002-1267-0234;
  dct:license <https://creativecommons.org/licenses/by/4.0/>;
  npx:embeds sub:list-space-roles;
  npx:supersedes <https://w3id.org/np/RA1yYY4EzUfGHo3uwnkH1wRP7ipDeR384KAFKeIq7LAdU>;
  rdfs:label "List space roles";
  nt:wasCreatedFromProvenanceTemplate <https://w3id.org/np/RA7lSq6MuK_TIC6JMSHvLtee3lpLoZDOqLJCLXevnrPoU>;
  nt:wasCreatedFromPubinfoTemplate <https://w3id.org/np/RA0J4vUn_dekg-U1kK3AOEt02p9mT2WO03uGxLDec1jLw>,
    <https://w3id.org/np/RAoTD7udB2KtUuOuAe74tJi1t3VzK0DyWS7rYVAq1GRvw>, <https://w3id.org/np/RAukAcWHRDlkqxk7H2XNSegc1WnHI569INvNr-xdptDGI>;
  nt:wasCreatedFromTemplate <https://w3id.org/np/RAEFAt-QcFK0ZhqfvlsmS10BnzGJA0xwOICZXkO-ai87k> .

sub:list-space-roles a <https://w3id.org/kpxl/grlc/grlc-query>;
  dct:description "Lists the roles assigned in a given space as a table: each role (linked, labelled by its schema:name, falling back to its label/title), its tier (Admin/Maintainer/Member/Observer), a count of how many distinct users of the space hold that role, the contributing preset (via_preset, for roles carried by an active preset assignment), who added it, and the date. The built-in Admin role is always included as the first row. Also returns the role-assignment template (for the assign-user action) and the source nanopublication.";
  dct:license <http://www.apache.org/licenses/LICENSE-2.0>;
  rdfs:label "List space roles";
  <https://w3id.org/kpxl/grlc/endpoint> <https://w3id.org/np/l/nanopub-query-1.1/repo/spaces>;
  <https://w3id.org/kpxl/grlc/sparql> """prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>
prefix dct: <http://purl.org/dc/terms/>
prefix np: <http://www.nanopub.org/nschema#>
prefix npa: <http://purl.org/nanopub/admin/>
prefix npx: <http://purl.org/nanopub/x/>
prefix schema: <http://schema.org/>
prefix gen: <https://w3id.org/kpxl/gen/terms/>

select ?role
       (sample(?rlbl) as ?role_label)
       (sample(?rtier) as ?tier)
       (count(distinct ?member) as ?users)
       (sample(?presetX) as ?via_preset)
       (sample(?presetXlbl) as ?via_preset_label)
       (sample(?userX) as ?added_by)
       (max(?dateX) as ?date_added)
       (sample(?rtmpl) as ?roleAssignmentTemplate)
       (sample(?npX) as ?np)
where {
  graph npa:graph { npa:thisRepo npa:hasCurrentSpaceState ?g . }
  values ?_space_multi_iri {}
  {
    bind(<https://w3id.org/np/RA_eEJjQbxzSqYSwPzfjzOZi5sMPpUmHskFNsgJYSws8I/adminRole> as ?role)
    bind(0 as ?sortkey)
    bind(\"admin\" as ?rlbl)
    bind(\"Admin\" as ?rtier)
    bind(<https://w3id.org/np/RAsOQ7k3GNnuUqZuLm57PWwWopQJR_4onnCpNR457CZg8> as ?rtmpl)
    bind(gen:hasAdmin as ?prop)
  } union {
    graph ?g {
      ?ra a gen:RoleAssignment ; npa:forSpace ?_space_multi_iri ; gen:hasRole ?role ; npa:viaNanopub ?npX .
    }
    graph npa:graph { ?npX dct:created ?dateX . }
    optional { graph npa:graph { ?npX npx:signedBy ?userX . } }
    bind(1 as ?sortkey)
    graph npa:spacesGraph {
      ?roleDecl a npa:RoleDeclaration ; npa:role ?role ; npa:viaNanopub ?role_np .
    }
    bind(exists { graph npa:spacesGraph { ?roleDecl npa:hasRoleType gen:MaintainerRole } } as ?isMaintainer)
    bind(exists { graph npa:spacesGraph { ?roleDecl npa:hasRoleType gen:MemberRole } } as ?isMember)
    bind(if(?isMaintainer,\"Maintainer\",if(?isMember,\"Member\",\"Observer\")) as ?rtier)
    graph npa:graph { ?role_np np:hasAssertion ?role_a . }
    optional { graph ?role_a { ?role rdfs:label ?rl1 } }
    optional { graph ?role_a { ?role dct:title ?rt1 } }
    optional { graph ?role_a { ?role schema:name ?rn1 } }
    optional { graph ?role_a { ?role gen:hasRoleAssignmentTemplate ?rtmpl } }
    bind(coalesce(?rn1, ?rl1, ?rt1) as ?rlbl)
    optional {
      { graph npa:spacesGraph { ?roleDecl gen:hasRegularProperty ?prop } }
      union
      { graph npa:spacesGraph { ?roleDecl gen:hasInverseProperty ?prop } }
    }
  } union {
    {
      select ?role ?presetX ?presetXlbl ?userX ?npX ?dateX ?pubkeyA ?_space_multi_iri {
        service <https://w3id.org/np/l/nanopub-query-1.1/repo/full> {
          graph npa:graph {
            ?npX npx:hasNanopubType gen:PresetAssignment ;
                npa:hasValidSignatureForPublicKeyHash ?pubkeyA ;
                dct:created ?dateX ;
                npx:embeds ?assignment ;
                np:hasAssertion ?aX .
            filter not exists { ?invX npx:invalidates ?npX ; npa:hasValidSignatureForPublicKeyHash ?pubkeyA . }
            optional { ?npX npx:signedBy ?userX }
          }
          graph ?aX {
            ?assignment gen:isAssignmentFor ?_space_multi_iri ;
                        gen:isAssignmentOfPreset ?presetX .
            optional {
              values ?mX { gen:ActivatedPresetAssignment gen:DeactivatedPresetAssignment }
              ?assignment a ?mX .
            }
          }
          filter(!bound(?mX) || !contains(str(?mX), \"Deactivated\"))
          filter not exists {
            graph npa:graph {
              ?np2 npx:hasNanopubType gen:PresetAssignment ;
                  npa:hasValidSignatureForPublicKeyHash ?pubkey2 ;
                  dct:created ?date2 ;
                  npx:embeds ?assignment2 ;
                  np:hasAssertion ?a2 .
              filter not exists { ?npx2 npx:invalidates ?np2 ; npa:hasValidSignatureForPublicKeyHash ?pubkey2 . }
            }
            graph ?a2 {
              ?assignment2 a gen:DeactivatedPresetAssignment ;
                          gen:isAssignmentFor ?_space_multi_iri ;
                          gen:isAssignmentOfPreset ?presetX .
            }
            filter(?date2 > ?dateX)
          }
          graph npa:graph { ?presetNp npx:embeds ?presetX ; np:hasAssertion ?paX . }
          graph ?paX {
            ?presetX a gen:Preset .
            optional { ?presetX rdfs:label ?presetXlbl . }
            ?presetX gen:hasRole ?role .
          }
        }
      }
    }
    graph ?g {
      ?riA a gen:RoleInstantiation ; npa:forSpace ?_space_multi_iri ; npa:forAgent ?authAgent ;
          (npa:inverseProperty|npa:regularProperty) ?rolePropA .
      ?authAcct a npa:AccountState ; npa:agent ?authAgent ; npa:pubkey ?pubkeyA .
    }
    optional {
      graph npa:spacesGraph {
        ?rdA a npa:RoleDeclaration ; npa:forSpace ?_space_multi_iri ;
            npa:hasRoleType gen:MaintainerRole ;
            (npa:inverseProperty|npa:regularProperty) ?rolePropA .
        bind(true as ?isMaintA)
      }
    }
    filter(?rolePropA = gen:hasAdmin || bound(?isMaintA))
    bind(1 as ?sortkey)
    graph npa:spacesGraph {
      ?roleDecl a npa:RoleDeclaration ; npa:role ?role ; npa:viaNanopub ?role_np .
    }
    bind(exists { graph npa:spacesGraph { ?roleDecl npa:hasRoleType gen:MaintainerRole } } as ?isMaintainer)
    bind(exists { graph npa:spacesGraph { ?roleDecl npa:hasRoleType gen:MemberRole } } as ?isMember)
    bind(if(?isMaintainer,\"Maintainer\",if(?isMember,\"Member\",\"Observer\")) as ?rtier)
    graph npa:graph { ?role_np np:hasAssertion ?role_a . }
    optional { graph ?role_a { ?role rdfs:label ?rl1 } }
    optional { graph ?role_a { ?role dct:title ?rt1 } }
    optional { graph ?role_a { ?role schema:name ?rn1 } }
    optional { graph ?role_a { ?role gen:hasRoleAssignmentTemplate ?rtmpl } }
    bind(coalesce(?rn1, ?rl1, ?rt1) as ?rlbl)
    optional {
      { graph npa:spacesGraph { ?roleDecl gen:hasRegularProperty ?prop } }
      union
      { graph npa:spacesGraph { ?roleDecl gen:hasInverseProperty ?prop } }
    }
  }
  optional {
    graph ?g {
      ?ri a gen:RoleInstantiation ; npa:forSpace ?_space_multi_iri ; npa:forAgent ?member .
    }
    optional { graph ?g { ?ri (npa:regularProperty|npa:inverseProperty) ?riPropS } }
    optional { graph npa:spacesGraph { ?ri (npa:regularProperty|npa:inverseProperty) ?riPropX } }
    bind(coalesce(?riPropS, ?riPropX) as ?riProp)
    filter(bound(?prop) && ?riProp = ?prop)
  }
}
group by ?role ?sortkey
order by ?sortkey desc(max(?dateX))""" .

sub:assertion prov:wasAttributedTo orcid:0000-0002-1267-0234 .

orcid:0000-0002-1267-0234 foaf:name "Tobias Kuhn" .

sub:sig npx:hasAlgorithm "RSA";
  npx:hasPublicKey "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwUtewGCpT5vIfXYE1bmf/Uqu1ojqnWdYxv+ySO80ul8Gu7m8KoyPAwuvaPj0lvPtHrg000qMmkxzKhYknEjq8v7EerxZNYp5B3/3+5ZpuWOYAs78UnQVjbHSmDdmryr4D4VvvNIiUmd0yxci47dTFUj4DvfHnGd6hVe5+goqdcwIDAQAB";
  npx:hasSignature "rr+KB3JnysHnMsB8yr8o2SAPmCaRQByiYDS1zEq6GA6dCwD8RjBfByuubTikZfFKQWXfgSuLOghaFlfE3X8klO1HAfx//OLV89sxrDvkkusCZeOWhWVB7yWQpt01vX69e4n+t+NlUD7gUvSDMcJ99V2bodp7Vj2vi8bBp7Tfr6c=";
  npx:hasSignatureTarget <https://w3id.org/np/RA9TXiF8lJc3NTzYAdqhxoeLjFspJO7O28Q7i4s595o3w>;
  npx:signedBy orcid:0000-0002-1267-0234 .

