<commit_before>from django.contrib import auth
from django.conf import settings
from django.core import validators
from django.db import DEFAULT_DB_ALIAS, router
from django.core.mail import send_mail
from django.db import models
from django.utils.crypto import get_random_string
from django.utils.encoding import force_text, smart_text, python_2_unicode_compatible
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from ldapdb.models.fields import CharField, ListField

from .util import process_shells, CustomRDNModel
from .fields import DefaultListField, BooleanField, SimpleRelationField, DateTimeField, IntField

LDAP_DN_SUFFIX = getattr(settings, 'LDAP_DN_SUFFIX', '')

AVAILABLE_SHELLS = process_shells(getattr(settings, 'LDAP_SHELLS', [
    '/bin/bash',
    '/bin/sh',
    '/bin/csh',
    '/bin/false',
    '/usr/sbin/nologin',
]))


class SambaDomainName(CustomRDNModel):
    object_classes = ['sambaDomain', 'sambaUnixIdPool', 'top']
    gid = IntField(db_column='gidNumber')
    domain_name = CharField(db_column='sambaDomainName', primary_key=True)
    sambaSID = CharField(db_column='sambaSID')
    uid = IntField(db_column='uidNumber')

    def format_user_sid(self, uid):
        return "%s-%s" % (self.sambaSID, (uid * 2))

    def format_group_sid(self, gid):
        return "%s-%s" % (self.sambaSID, (gid * 2) + 1)

    def get_max(self, model, attr, attr_self=None):
        if attr_self is None:
            attr_self = attr
        current_max = getattr(self, attr_self)
        while True:
            try:
                model.objects.get(**{attr: current_max})
            except model.DoesNotExist:
                break
            current_max += 1
        setattr(self, attr_self, current_max + 1)
        return current_max

    def get_max_gid(self):
        return self.get_max(Group, 'gid')

    def get_max_uid(self):
        return self.get_max(User, 'id', 'uid')


@python_2_unicode_compatible
class Group(CustomRDNModel):
    dn_prefix = 'ou=groups'
    object_classes = [
        'posixGroup',
        'sambaGroupMapping',
        'djangoGroup',
        'groupOfNames',
        'top',
    ]

    def build_rdn(self):
        return 'cn=%s' % self.name

    gid = IntField(db_column='gidNumber', primary_key=True)
    name = CharField(_('name'), db_column='cn', max_length=200, unique=True)
    members = DefaultListField(db_column='member', editable=False)
    usernames = ListField(db_column='memberUid', editable=False)

    description = CharField(_('description'), db_column='description', max_length=200, default='')

    permissions = ListField(db_column='djangoPermission', blank=True, verbose_name=_('permissions'))

    _samba_sid = CharField(db_column='sambaSID', editable=False)
    _samba_group_type = CharField(db_column='sambaGroupType', default='5', editable=False)

    def check_hidden_fields(self):
        workgroup = SambaDomainName.objects.first()
        if self.gid is None:
            self.gid = workgroup.get_max_gid()
            workgroup.save()
        self._samba_sid = workgroup.format_group_sid(self.gid)
        self.usernames = list(set([x.split(',', 1)[0].split('=', 1)[0] for x in self.members]))
        if not self.description:
            self.description = '.'

    def save(self, using=None, *args, **kwargs):
        self.check_hidden_fields()
        super(Group, self).save(using=using)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('group')
        verbose_name_plural = _('groups')

username_validator = validators.RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username. '
                                                                 'This value may contain only letters, numbers '
                                                                 'and @/./+/-/_ characters.'), 'invalid')

<commit_msg>-Fix: make sure the memberUid property is set properly.<commit_after>from django.contrib import auth
from django.conf import settings
from django.core import validators
from django.db import DEFAULT_DB_ALIAS, router
from django.core.mail import send_mail
from django.db import models
from django.utils.crypto import get_random_string
from django.utils.encoding import force_text, smart_text, python_2_unicode_compatible
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from ldapdb.models.fields import CharField, ListField

from .util import process_shells, CustomRDNModel
from .fields import DefaultListField, BooleanField, SimpleRelationField, DateTimeField, IntField

LDAP_DN_SUFFIX = getattr(settings, 'LDAP_DN_SUFFIX', '')

AVAILABLE_SHELLS = process_shells(getattr(settings, 'LDAP_SHELLS', [
    '/bin/bash',
    '/bin/sh',
    '/bin/csh',
    '/bin/false',
    '/usr/sbin/nologin',
]))


class SambaDomainName(CustomRDNModel):
    object_classes = ['sambaDomain', 'sambaUnixIdPool', 'top']
    gid = IntField(db_column='gidNumber')
    domain_name = CharField(db_column='sambaDomainName', primary_key=True)
    sambaSID = CharField(db_column='sambaSID')
    uid = IntField(db_column='uidNumber')

    def format_user_sid(self, uid):
        return "%s-%s" % (self.sambaSID, (uid * 2))

    def format_group_sid(self, gid):
        return "%s-%s" % (self.sambaSID, (gid * 2) + 1)

    def get_max(self, model, attr, attr_self=None):
        if attr_self is None:
            attr_self = attr
        current_max = getattr(self, attr_self)
        while True:
            try:
                model.objects.get(**{attr: current_max})
            except model.DoesNotExist:
                break
            current_max += 1
        setattr(self, attr_self, current_max + 1)
        return current_max

    def get_max_gid(self):
        return self.get_max(Group, 'gid')

    def get_max_uid(self):
        return self.get_max(User, 'id', 'uid')


@python_2_unicode_compatible
class Group(CustomRDNModel):
    dn_prefix = 'ou=groups'
    object_classes = [
        'posixGroup',
        'sambaGroupMapping',
        'djangoGroup',
        'groupOfNames',
        'top',
    ]

    def build_rdn(self):
        return 'cn=%s' % self.name

    gid = IntField(db_column='gidNumber', primary_key=True)
    name = CharField(_('name'), db_column='cn', max_length=200, unique=True)
    members = DefaultListField(db_column='member', editable=False)
    usernames = ListField(db_column='memberUid', editable=False)

    description = CharField(_('description'), db_column='description', max_length=200, default='')

    permissions = ListField(db_column='djangoPermission', blank=True, verbose_name=_('permissions'))

    _samba_sid = CharField(db_column='sambaSID', editable=False)
    _samba_group_type = CharField(db_column='sambaGroupType', default='5', editable=False)

    def check_hidden_fields(self):
        workgroup = SambaDomainName.objects.first()
        if self.gid is None:
            self.gid = workgroup.get_max_gid()
            workgroup.save()
        self._samba_sid = workgroup.format_group_sid(self.gid)
        self.usernames = list(set([x.split(',', 1)[0].split('=', 1)[-1] for x in self.members]))
        if not self.description:
            self.description = '.'

    def save(self, using=None, *args, **kwargs):
        self.check_hidden_fields()
        super(Group, self).save(using=using)

    def __str__(self):
        return self.name

    class Meta:
        verbose_name = _('group')
        verbose_name_plural = _('groups')

username_validator = validators.RegexValidator(r'^[\w.@+-]+$', _('Enter a valid username. '
                                                                 'This value may contain only letters, numbers '
                                                                 'and @/./+/-/_ characters.'), 'invalid')


{'fraction_non_alphanumeric': 0.1181991122384274, 'fraction_numerical': 0.004565630944831959, 'mean_word_length': 3.0607621009268797, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4429349', 'n_tokens_mistral': 2512, 'n_tokens_neox': 2403, 'n_words': 518}