<commit_before>import example_logic_tier as base
import urllib, utils, os, jwt, cryptography
import rdf_json
from rdf_json import URI
import operation_primitives
from base_constants import AC, CE, AC_R, AC_C, AC_A, AC_ALL, ANY_USER, ADMIN_USER, RDF
from base_constants import URL_POLICY as url_policy
import logging
import requests

NAMESPACE_MAPPINGS = {AC : 'ac'}
NAMESPACE_MAPPINGS.update(base.NAMESPACE_MAPPINGS)
CHECK_ACCESS_RIGHTS = os.environ.get('CHECK_ACCESS_RIGHTS') != 'False'

class Domain_Logic(base.Domain_Logic):
    def namespace_mappings(self):
        return NAMESPACE_MAPPINGS
        
    def default_resource_group(self):
        if self.namespace == 'ac':
            return self.document_url()
        return super(Domain_Logic, self).default_resource_group()
    
    def permissions_for_resource(self, resource_uri):
        resource_uri = str(resource_uri)
        resource_uri = self.absolute_url(resource_uri)
        
        # if it's the null uri then return AC_ALL        
        if resource_uri == self.request_url() or self.user == ADMIN_USER: 
            return 200, AC_ALL

        headers = {
            'Accept': 'application/rdf+json+ce',
            'Cookie': 'SSSESSIONID=%s' % cryptography.encode_jwt({'user': self.user})
        }
        r = utils.intra_system_get(resource_uri, headers)
        #r = utils.intra_system_get(resource_uri)
        if r.status_code == 200:
            document = rdf_json.RDF_JSON_Document(r)
            owner = document.get_value(CE+'owner')
            if str(owner) == self.user:
                return 200, AC_ALL
        
        # resource_uri isn't null and user isn't owner so now do actual permission check
        permissions_url = url_policy.construct_url(self.request_hostname, self.tenant, 'ac-permissions') + ('?%s&%s' % (urllib.quote(resource_uri), urllib.quote(self.user)))
        r = utils.intra_system_get(permissions_url)
        if r.status_code == 200:
            return 200, int(r.text)
        else:
            return r.status_code, 'url: %s text: %s' % (permissions_url, r.text)
    
    def permissions(self, document, insert_document=None):
        user_group = insert_document if insert_document else document
        if type(user_group) is dict:
            user_group = rdf_json.RDF_JSON_Document(user_group, '')
        if str(user_group.get_value(RDF+'type')) == AC+'UserGroup':
            ac_mays = user_group.get_value(AC+'may')
            for may in ac_mays:
                ac_may_props = user_group.get_properties(may)
                for to in ac_may_props[AC+'to']:
                    # make sure the user has ADMIN permissions on all of them
                    status, permissions = self.permissions_for_resource(to)
                    if status == 200:
                        if not permissions & AC_A:
                            return 403, 0
                    else:
                        return status, 0<commit_msg>use self.intra_system, not utils.intra_system<commit_after>import example_logic_tier as base
import urllib, utils, os, jwt, cryptography
import rdf_json
from rdf_json import URI
import operation_primitives
from base_constants import AC, CE, AC_R, AC_C, AC_A, AC_ALL, ANY_USER, ADMIN_USER, RDF
from base_constants import URL_POLICY as url_policy
import logging
import requests

NAMESPACE_MAPPINGS = {AC : 'ac'}
NAMESPACE_MAPPINGS.update(base.NAMESPACE_MAPPINGS)
CHECK_ACCESS_RIGHTS = os.environ.get('CHECK_ACCESS_RIGHTS') != 'False'

class Domain_Logic(base.Domain_Logic):
    def namespace_mappings(self):
        return NAMESPACE_MAPPINGS
        
    def default_resource_group(self):
        if self.namespace == 'ac':
            return self.document_url()
        return super(Domain_Logic, self).default_resource_group()
    
    def permissions_for_resource(self, resource_uri):
        resource_uri = str(resource_uri)
        resource_uri = self.absolute_url(resource_uri)
        
        # if it's the null uri then return AC_ALL        
        if resource_uri == self.request_url() or self.user == ADMIN_USER: 
            return 200, AC_ALL

        r = self.intra_system_get(resource_uri)
        if r.status_code == 200:
            document = rdf_json.RDF_JSON_Document(r)
            owner = document.get_value(CE+'owner')
            if str(owner) == self.user:
                return 200, AC_ALL
        
        # resource_uri isn't null and user isn't owner so now do actual permission check
        permissions_url = url_policy.construct_url(self.request_hostname, self.tenant, 'ac-permissions') + ('?%s&%s' % (urllib.quote(resource_uri), urllib.quote(self.user)))
        r = utils.intra_system_get(permissions_url)
        if r.status_code == 200:
            return 200, int(r.text)
        else:
            return r.status_code, 'url: %s text: %s' % (permissions_url, r.text)
    
    def permissions(self, document, insert_document=None):
        user_group = insert_document if insert_document else document
        if type(user_group) is dict:
            user_group = rdf_json.RDF_JSON_Document(user_group, '')
        if str(user_group.get_value(RDF+'type')) == AC+'UserGroup':
            ac_mays = user_group.get_value(AC+'may')
            for may in ac_mays:
                ac_may_props = user_group.get_properties(may)
                for to in ac_may_props[AC+'to']:
                    # make sure the user has ADMIN permissions on all of them
                    status, permissions = self.permissions_for_resource(to)
                    if status == 200:
                        if not permissions & AC_A:
                            return 403, 0
                    else:
                        return status, 0
{'fraction_non_alphanumeric': 0.0882816605359958, 'fraction_numerical': 0.008057453144158346, 'mean_word_length': 2.466909532483303, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1417726', 'n_tokens_mistral': 1780, 'n_tokens_neox': 1728, 'n_words': 408}