# ----------------------------------------------------------------------
# Copyright (c) 2013-2016 Raytheon BBN Technologies
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and/or hardware specification (the "Work") to
# deal in the Work without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Work, and to permit persons to whom the Work
# is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Work.
#
# THE WORK IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE WORK OR THE USE OR OTHER DEALINGS
# IN THE WORK.
# ----------------------------------------------------------------------

import logging
import tools.pluginmanager as pm
from plugins.chrm.CHDatabaseEngine import *
from plugins.chapiv1rpc.chapi.DelegateBase import DelegateBase
from plugins.chapiv1rpc.chapi.HandlerBase import HandlerBase
from plugins.chrm.ABACGuard import *
from plugins.chapiv1rpc.chapi.Exceptions import *
from sqlalchemy import *
from datetime import *
from dateutil.relativedelta import relativedelta
from plugins.chapiv1rpc.chapi.MethodContext import *
from tools.guard_utils import *
from tools.dbutils import *
from tools.cert_utils import *
from tools.chapi_log import *
from tools.geni_constants import *
from tools.policy_file_checker import PolicyFileChecker

cs_logger = logging.getLogger('csv1')

# Support for GENI Portal/Clearinghouse "Credential Store" a repository
# of assertions from which privileges can be derived

class CSv1Handler(HandlerBase):

    def __init__(self):
        super(CSv1Handler, self).__init__(cs_logger)

    def get_attributes(self, principal, context_type, context, \
                           credentials, options):
        if context == 'None': context = None # For testing with the client
        with MethodContext(self, CS_LOG_PREFIX, 'get_attributes',
                           {'principal' : principal,
                            'context_type' : context_type,
                            'context' : context},
                            credentials, options, read_only=True) as mc:
            if not mc._error:
                mc._result = \
                    self._delegate.get_attributes(mc._client_cert,
                                                  principal, context_type,
                                                  context, credentials,
                                                  options, mc._session)
        return mc._result

    def get_permissions(self, principal, credentials, options):
        with MethodContext(self, CS_LOG_PREFIX, 'get_permissions',
                           {'principal' : principal},
                            credentials, options, read_only=True) as mc:
            if not mc._error:
                mc._result = \
                    self._delegate.get_permissions(mc._client_cert,
                                                   principal,
                                                   credentials, options,
                                                   mc._session)
        return mc._result

class CSv1Delegate(DelegateBase):

    def __init__(self):
        super(CSv1Delegate, self).__init__(cs_logger)
        self.db = pm.getService('chdbengine')