<commit_before>from django.conf import settings
from command_runner import runCommand
from django.db import transaction

from .utils import createPasswdHash


ISSUE_SAMBA_COMMANDS = getattr(settings, 'ISSUE_SAMBA_COMMANDS', False)
DELETE_HOME_ON_DELETION = getattr(settings, 'DELETE_HOME_ON_DELETION', False)
HOMES_PATH = getattr(settings, 'HOMES_PATH', '/home')
PGINA_HACKS = getattr(settings, 'PGINA_HACKS', False)


def userPostSaved(sender, instance, created, **kwargs):
    # fist we solve change password situation
    if not created and hasattr(instance, 'rawpwd') and ISSUE_SAMBA_COMMANDS:
        runCommand('(echo %s; echo %s) | smbpasswd -s %s' %\
            (instance.rawpwd, instance.rawpwd, instance.username))
        _syncSysUser(instance, **kwargs)
    # this is to happen whene user is manip. via admin interface
    else:
        instance._deferredCMDs = (
            lambda: _userDeferredSync(instance, created, **kwargs),
        )


def userPostDeleted(sender, instance, **kwargs):
    _delSambaUserAndHome(sender, instance, **kwargs)
    sysUser = _getSysUser(instance.username)
    sysUser.delete()


def runDefferedCmds(obj):
    if hasattr(obj, '_deferredCMDs'):
        for cmd in obj._deferredCMDs:
            cmd()


def _userDeferredSync(instance, created, **kwargs):
    su = _syncSysUser(instance, **kwargs)
    _syncSysUserGroups(su, instance)
    if created:
        _createHome(instance)
        if ISSUE_SAMBA_COMMANDS:
            mkSmbUsr = '(echo %s; echo %s) | smbpasswd -s -a %s' %\
                (instance.rawpwd, instance.rawpwd, instance.username)
            runCommand(mkSmbUsr)


def _syncSysUser(user, **kwargs):
    """
    Make appropriate SysUser instance according user
    """
    sysUser = _getSysUser(user.username)
    if PGINA_HACKS:
        sysUser.user = user.username
        sysUser.hash_method = 'MD5'

    if hasattr(user, 'rawpwd'):
        sysUser.unixpwd = createPasswdHash(user.rawpwd)
        if PGINA_HACKS:
            from hashlib import md5
            sysUser.password = md5(user.rawpwd).hexdigest()

    sysUser.user_name = user.username
    sysUser.realname = '%s %s' % (user.first_name, user.last_name)
    sysUser.status = 'A'

    sysUser.save()
    transaction.commit()
    return sysUser


def _syncSysUserGroups(sysUser, user):
    GID, otherGrs = _extractPrimaryGroup(user)
    if GID:
        sysUser.gid = _getOrCreateSysGroup(GID)

    _syncGroups(otherGrs, sysUser)
    sysUser.save()


def _extractPrimaryGroup(user):
    GID, others = None, []
    for g in user.groups.all():
        if not GID:
            GID = g
        else:
            others.append(g)
    return GID, others


def _syncGroups(otherGrs, sysUser):
    from .models import SysMembership

    # delete all memberships
    userMships = SysMembership.objects.filter(user=sysUser.user_id)
    for g in userMships:
        g.delete()

    # create them all
    for g in otherGrs:
        sysgr = _getOrCreateSysGroup(g)
        ms = SysMembership(user=sysUser.user_id, group=sysgr.group_id)
        ms.save()


def _getOrCreateSysGroup(group):
    from .models import SysGroup
    try:
        g = SysGroup.objects.get(group_name=group.name)
    except SysGroup.DoesNotExist:
        g = SysGroup(group_name=group.name)
        g.save()
    return g


def _getSysUser(uname):
    from .models import SysUser
    try:
        sysUser = SysUser.objects.get(user=uname)
    except SysUser.DoesNotExist:
        sysUser = SysUser(user=uname)
    return sysUser


def _createHome(user):
    """
    Create home dir and samba user on create<commit_msg>user-group relationship object creation fix<commit_after>from django.conf import settings
from command_runner import runCommand
from django.db import transaction

from .utils import createPasswdHash


ISSUE_SAMBA_COMMANDS = getattr(settings, 'ISSUE_SAMBA_COMMANDS', False)
DELETE_HOME_ON_DELETION = getattr(settings, 'DELETE_HOME_ON_DELETION', False)
HOMES_PATH = getattr(settings, 'HOMES_PATH', '/home')
PGINA_HACKS = getattr(settings, 'PGINA_HACKS', False)


def userPostSaved(sender, instance, created, **kwargs):
    # fist we solve change password situation
    if not created and hasattr(instance, 'rawpwd') and ISSUE_SAMBA_COMMANDS:
        runCommand('(echo %s; echo %s) | smbpasswd -s %s' %\
            (instance.rawpwd, instance.rawpwd, instance.username))
        _syncSysUser(instance, **kwargs)
    # this is to happen whene user is manip. via admin interface
    else:
        instance._deferredCMDs = (
            lambda: _userDeferredSync(instance, created, **kwargs),
        )


def userPostDeleted(sender, instance, **kwargs):
    _delSambaUserAndHome(sender, instance, **kwargs)
    sysUser = _getSysUser(instance.username)
    sysUser.delete()


def runDefferedCmds(obj):
    if hasattr(obj, '_deferredCMDs'):
        for cmd in obj._deferredCMDs:
            cmd()


def _userDeferredSync(instance, created, **kwargs):
    su = _syncSysUser(instance, **kwargs)
    _syncSysUserGroups(su, instance)
    if created:
        _createHome(instance)
        if ISSUE_SAMBA_COMMANDS:
            mkSmbUsr = '(echo %s; echo %s) | smbpasswd -s -a %s' %\
                (instance.rawpwd, instance.rawpwd, instance.username)
            runCommand(mkSmbUsr)


def _syncSysUser(user, **kwargs):
    """
    Make appropriate SysUser instance according user
    """
    sysUser = _getSysUser(user.username)
    if PGINA_HACKS:
        sysUser.user = user.username
        sysUser.hash_method = 'MD5'

    if hasattr(user, 'rawpwd'):
        sysUser.unixpwd = createPasswdHash(user.rawpwd)
        if PGINA_HACKS:
            from hashlib import md5
            sysUser.password = md5(user.rawpwd).hexdigest()

    sysUser.user_name = user.username
    sysUser.realname = '%s %s' % (user.first_name, user.last_name)
    sysUser.status = 'A'

    sysUser.save()
    transaction.commit()
    return sysUser


def _syncSysUserGroups(sysUser, user):
    GID, otherGrs = _extractPrimaryGroup(user)
    if GID:
        sysUser.gid = _getOrCreateSysGroup(GID)

    _syncGroups(otherGrs, sysUser)
    sysUser.save()


def _extractPrimaryGroup(user):
    GID, others = None, []
    for g in user.groups.all():
        if not GID:
            GID = g
        else:
            others.append(g)
    return GID, others


def _syncGroups(otherGrs, sysUser):
    from .models import SysMembership

    # delete all memberships
    userMships = SysMembership.objects.filter(user=sysUser.user_id)
    for g in userMships:
        g.delete()

    # create them all
    for g in otherGrs:
        sysgr = _getOrCreateSysGroup(g)
        ms = SysMembership(user=sysUser, group=sysgr)
        ms.save()


def _getOrCreateSysGroup(group):
    from .models import SysGroup
    try:
        g = SysGroup.objects.get(group_name=group.name)
    except SysGroup.DoesNotExist:
        g = SysGroup(group_name=group.name)
        g.save()
    return g


def _getSysUser(uname):
    from .models import SysUser
    try:
        sysUser = SysUser.objects.get(user=uname)
    except SysUser.DoesNotExist:
        sysUser = SysUser(user=uname)
    return sysUser


def _createHome(user):
    """
    Create home dir and samba user on create
{'fraction_non_alphanumeric': 0.09815865983663298, 'fraction_numerical': 0.0008306797729475288, 'mean_word_length': 3.013333333333333, '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': '2056655', 'n_tokens_mistral': 2429, 'n_tokens_neox': 2356, 'n_words': 549}