<commit_before># Copyright 2014 IBM Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


# This file is the sql_ldap mapping id driver for Liberty or uppper versions of Keystone

from keystone.common import dependency
from keystone.common import sql
from keystone import identity
from keystone import exception
from keystone.identity.mapping_backends import mapping as identity_mapping
from keystone.identity.backends import sql as model
try: from oslo_log import log
except ImportError: from keystone.openstack.common import log

LOG = log.getLogger(__name__)

class IDMapping(sql.ModelBase, sql.ModelDictMixin):
    __tablename__ = 'id_mapping'
    public_id = sql.Column(sql.String(64), primary_key=True)
    domain_id = sql.Column(sql.String(64), nullable=False)
    local_id = sql.Column(sql.String(64), nullable=False)
    # NOTE(henry-nash); Postgres requires a name to be defined for an Enum
    entity_type = sql.Column(
        sql.Enum(identity_mapping.EntityType.USER,
                 identity_mapping.EntityType.GROUP,
                 name='entity_type'),
        nullable=False)
    # Unique constraint to ensure you can't store more than one mapping to the
    # same underlying values
    __table_args__ = (
        sql.UniqueConstraint('domain_id', 'local_id', 'entity_type'), {})


@dependency.requires('id_generator_api')
class Mapping(identity.MappingDriverV8):

    def get_session(self):
        try:
            session = sql.get_session()
        except Exception:
            with sql.session_for_write() as session:
                None
        return session

    def get_public_id(self, local_entity):
        if (local_entity['entity_type'] == identity_mapping.EntityType.GROUP):
            LOG.debug('Trying to get public_id for group %s in %s' % (local_entity['local_id'],
                                                                      local_entity['domain_id']))
            try:
                session = self.get_session()
                query = session.query(model.Group)
                query = query.filter_by(name=local_entity['local_id'])
                query = query.filter_by(domain_id=local_entity['domain_id'])
                try:
                    group_ref = query.one()
                except sql.NotFound:
                    raise exception.GroupNotFound(group_id=local_entity['local_id'])
                group = group_ref.to_dict()

                public_id = group['id']
                LOG.debug('Public_id for group %s in %s is: %s ' % (local_entity['local_id'],
                                                                    local_entity['domain_id'],
                                                                    public_id))
                return public_id
            except Exception:
                return None
        else:        
            # NOTE(henry-nash): Since the Public ID is regeneratable, rather
            # than search for the entry using the local entity values, we
            # could create the hash and do a PK lookup.  However this would only
            # work if we hashed all the entries, even those that already generate
            # UUIDs, like SQL.  Further, this would only work if the generation
            # algorithm was immutable (e.g. it had always been sha256).
            session = self.get_session()
            query = session.query(IDMapping.public_id)
            query = query.filter_by(domain_id=local_entity['domain_id'])
            query = query.filter_by(local_id=local_entity['local_id'])<commit_msg>handle unmatch groups from ldap integration<commit_after># Copyright 2014 IBM Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.


# This file is the sql_ldap mapping id driver for Liberty or uppper versions of Keystone

from keystone.common import dependency
from keystone.common import sql
from keystone import identity
from keystone import exception
from keystone.identity.mapping_backends import mapping as identity_mapping
from keystone.identity.backends import sql as model
try: from oslo_log import log
except ImportError: from keystone.openstack.common import log

LOG = log.getLogger(__name__)

class IDMapping(sql.ModelBase, sql.ModelDictMixin):
    __tablename__ = 'id_mapping'
    public_id = sql.Column(sql.String(64), primary_key=True)
    domain_id = sql.Column(sql.String(64), nullable=False)
    local_id = sql.Column(sql.String(64), nullable=False)
    # NOTE(henry-nash); Postgres requires a name to be defined for an Enum
    entity_type = sql.Column(
        sql.Enum(identity_mapping.EntityType.USER,
                 identity_mapping.EntityType.GROUP,
                 name='entity_type'),
        nullable=False)
    # Unique constraint to ensure you can't store more than one mapping to the
    # same underlying values
    __table_args__ = (
        sql.UniqueConstraint('domain_id', 'local_id', 'entity_type'), {})


@dependency.requires('id_generator_api')
class Mapping(identity.MappingDriverV8):

    def get_session(self):
        try:
            session = sql.get_session()
        except Exception:
            with sql.session_for_write() as session:
                None
        return session

    def get_public_id(self, local_entity):
        if (local_entity['entity_type'] == identity_mapping.EntityType.GROUP):
            LOG.debug('Trying to get public_id for group %s in %s' % (local_entity['local_id'],
                                                                      local_entity['domain_id']))
            try:
                session = self.get_session()
                query = session.query(model.Group)
                query = query.filter_by(name=local_entity['local_id'])
                query = query.filter_by(domain_id=local_entity['domain_id'])
                try:
                    group_ref = query.one()
                except sql.NotFound:
                    session = self.get_session()
                    query = session.query(IDMapping.public_id)
                    query = query.filter_by(domain_id=local_entity['domain_id'])
                    query = query.filter_by(local_id=local_entity['local_id'])
                    query = query.filter_by(entity_type=local_entity['entity_type'])
                    try:
                        public_ref = query.one()
                        public_id = public_ref.public_id
                        return public_id
                    except sql.NotFound:
                        return None

                group = group_ref.to_dict()
                public_id = group['id']
                LOG.debug('Public_id for group %s in %s is: %s ' % (local_entity['local_id'],
                                                                    local_entity['domain_id'],
                                                                    public_id))
                return public_id
            except Exception:
                return None
        else:        
            # NOTE(henry-nash): Since the Public ID is regeneratable, rather
            # than search for the entry using the local entity values, we
            # could create the hash and do a PK lookup.  However this would only
            # work if we hashed all the entries, even those that already generate
            # UUIDs, like SQL.  Further, this would only work if the generation
            # algorithm was immutable (e.g. it had always been sha256).
            session = self.get_session()
            query = session.query(IDMapping.public_id)
            query = query.filter_by(domain_id=local_entity['domain_id'])
            query = query.filter_by(local_id=local_entity['local_id'])
{'fraction_non_alphanumeric': 0.0777610383590219, 'fraction_numerical': 0.004171978213002666, 'mean_word_length': 2.1751287711552614, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5242275', 'n_tokens_mistral': 2237, 'n_tokens_neox': 2144, 'n_words': 691}