Exemplo n.º 1
0
def combined_users():
    users = []
    sys_users = get_users()
    uname_list = sys_users.keys()
    for u in uname_list:
        try:
            uo = User.objects.get(username=u)
            uo.uid = sys_users[u][0]
            uo.gid = sys_users[u][1]
            uo.shell = sys_users[u][2]
            gname = get_groups(uo.gid).keys()[0]
            create = True
            if uo.group is not None:
                if uo.group.gid == uo.gid or uo.group.groupname == gname:
                    uo.group.groupname = gname
                    uo.group.gid = uo.gid
                    uo.group.save()
                    create = False
            if create:
                try:
                    go = Group.objects.get(groupname=gname)
                    go.gid = uo.gid
                    go.save()
                    uo.group = go
                except Group.DoesNotExist:
                    try:
                        go = Group.objects.get(gid=uo.gid)
                        go.groupname = gname
                        go.save()
                        uo.group = go
                    except Group.DoesNotExist:
                        go = Group(groupname=gname, gid=uo.gid)
                        go.save()
                        uo.group = go
            uo.save()
            uo.pincard_allowed, uo.has_pincard = pincard_states(uo)
            users.append(uo)

        except User.DoesNotExist:
            temp_uo = User(
                username=u,
                uid=sys_users[u][0],
                gid=sys_users[u][1],
                shell=sys_users[u][2],
                admin=False,
            )
            temp_uo.managed_user = False
            temp_uo.pincard_allowed, temp_uo.has_pincard = pincard_states(
                temp_uo)  # noqa E501
            users.append(temp_uo)

    for u in User.objects.all():
        if u.username not in uname_list:
            users.append(u)
    return sorted(
        users,
        cmp=lambda x, y: cmp(x.username.lower(), y.username.lower()
                             )  # noqa F821
    )
Exemplo n.º 2
0
def combined_users():
    users = list(User.objects.all())
    sys_users = get_users(min_uid=0)
    for u in sys_users.keys():
        if (User.objects.filter(username=u).exists()):
            continue
        users.append(User(username=u, uid=sys_users[u][0],
                          gid=sys_users[u][1], admin=False))
    return users
Exemplo n.º 3
0
def combined_users():
    users = []
    sys_users = get_users()
    uname_list = sys_users.keys()
    for u in uname_list:
        try:
            uo = User.objects.get(username=u)
            uo.uid = sys_users[u][0]
            uo.gid = sys_users[u][1]
            uo.shell = sys_users[u][2]
            gname = get_groups(uo.gid).keys()[0]
            create = True
            if (uo.group is not None):
                if (uo.group.gid == uo.gid or uo.group.groupname == gname):
                    uo.group.groupname = gname
                    uo.group.gid = uo.gid
                    uo.group.save()
                    create = False
            if (create):
                try:
                    go = Group.objects.get(groupname=gname)
                    go.gid = uo.gid
                    go.save()
                    uo.group = go
                except Group.DoesNotExist:
                    try:
                        go = Group.objects.get(gid=uo.gid)
                        go.groupname = gname
                        go.save()
                        uo.group = go
                    except Group.DoesNotExist:
                        go = Group(groupname=gname, gid=uo.gid)
                        go.save()
                        uo.group = go
            uo.save()
            uo.pincard_allowed, uo.has_pincard = pincard_states(uo)
            users.append(uo)

        except User.DoesNotExist:
            temp_uo = User(username=u, uid=sys_users[u][0],
                           gid=sys_users[u][1], shell=sys_users[u][2],
                           admin=False)
            temp_uo.managed_user = False
            temp_uo.pincard_allowed, temp_uo.has_pincard = pincard_states(temp_uo)  # noqa E501
            users.append(temp_uo)

    for u in User.objects.all():
        if (u.username not in uname_list):
            users.append(u)
    return sorted(users, cmp=lambda x, y: cmp(x.username.lower(),  # noqa F821
                                              y.username.lower()))
Exemplo n.º 4
0
    def post(self, request):
        try:
            username = request.DATA['username']
            password = request.DATA['password']
            is_active = request.DATA['is_active']
            public_key = request.DATA['public_key']

            # Check that a django user with the same name does not exist
            if (DjangoUser.objects.filter(username=username).exists()
                    or User.objects.filter(username=username).exists()):
                e_msg = ('user: %s already exists. Please choose a different'
                         ' username' % username)
                handle_exception(Exception(e_msg), request)

            # Check that a unix user with the same name does not exist
            unix_users = get_users(min_uid=0, uname=username)
            if (username in unix_users):
                e_msg = ('user: %s exists as a system user. Please choose a '
                         'different username' % username)
                handle_exception(Exception(e_msg), request)

            # Create Django user
            auser = DjangoUser.objects.create_user(username, None, password)
            auser.is_active = is_active
            auser.save()

            # Create unix user
            max_uid = settings.START_UID
            shell = settings.DEFAULT_SHELL
            if (is_active):
                shell = settings.ADMIN_SHELL
            try:
                # Find max uid
                max_uid = User.objects.all().order_by('-uid')[0].uid
            except Exception, e:
                logger.exception(e)
                pass
            uid = max_uid + 1
            useradd(username, uid, shell)
            usermod(username, password)
            smbpasswd(username, password)
            if (public_key is not None):
                add_ssh_key(username, public_key)
            suser = User(username=username,
                         uid=uid,
                         gid=uid,
                         user=auser,
                         public_key=public_key)
            suser.save()

            return Response(UserSerializer(auser).data)
Exemplo n.º 5
0
    def post(self, request):
        try:
            username = request.DATA['username']
            password = request.DATA['password']
            is_active = request.DATA['is_active']
            public_key = request.DATA['public_key']

            # Check that a django user with the same name does not exist
            if (DjangoUser.objects.filter(username=username).exists() or
                User.objects.filter(username=username).exists()):
                e_msg = ('user: %s already exists. Please choose a different'
                         ' username' % username)
                handle_exception(Exception(e_msg), request)

            # Check that a unix user with the same name does not exist
            unix_users = get_users(min_uid=0, uname=username)
            if (username in unix_users):
                e_msg = ('user: %s exists as a system user. Please choose a '
                         'different username' % username)
                handle_exception(Exception(e_msg), request)

            # Create Django user
            auser = DjangoUser.objects.create_user(username, None, password)
            auser.is_active = is_active
            auser.save()

            # Create unix user
            max_uid = settings.START_UID
            shell = settings.DEFAULT_SHELL
            if (is_active):
                shell = settings.ADMIN_SHELL
            try:
                # Find max uid
                max_uid = User.objects.all().order_by('-uid')[0].uid
            except Exception, e:
                logger.exception(e)
                pass
            uid = max_uid + 1
            useradd(username, uid, shell)
            usermod(username, password)
            smbpasswd(username, password)
            if (public_key is not None):
                add_ssh_key(username, public_key)
            suser = User(username=username, uid=uid, gid=uid, user=auser,
                         public_key=public_key)
            suser.save()

            return Response(UserSerializer(auser).data)
Exemplo n.º 6
0
def combined_users():
    users = []
    sys_users = get_users(min_uid=0)
    uname_list = sys_users.keys()
    for u in uname_list:
        try:
            uo = User.objects.get(username=u)
            uo.uid = sys_users[u][0]
            uo.gid = sys_users[u][1]
            gname = get_groups(uo.gid).keys()[0]
            create = True
            if (uo.group is not None):
                if (uo.group.gid == uo.gid or uo.group.groupname == gname):
                    uo.group.groupname = gname
                    uo.group.gid = uo.gid
                    uo.group.save()
                    create = False
            if (create):
                try:
                    go = Group.objects.get(groupname=gname)
                    go.gid = uo.gid
                    go.save()
                    uo.group = go
                except Group.DoesNotExist:
                    try:
                        go = Group.objects.get(gid=uo.gid)
                        go.groupname = gname
                        go.save()
                        uo.group = go
                    except Group.DoesNotExist:
                        go = Group(groupname=gname, gid=uo.gid)
                        go.save()
                        uo.group = go
            uo.save()
            users.append(uo)
        except User.DoesNotExist:
            temp_uo = User(username=u,
                           uid=sys_users[u][0],
                           gid=sys_users[u][1],
                           admin=False)
            temp_uo.managed_user = False
            users.append(temp_uo)
    for u in User.objects.all():
        if (u.username not in uname_list):
            users.append(u)
    return sorted(users,
                  cmp=lambda x, y: cmp(x.username.lower(), y.username.lower()))
Exemplo n.º 7
0
def combined_users():
    users = []
    sys_users = get_users(min_uid=0)
    uname_list = sys_users.keys()
    for u in uname_list:
        try:
            uo = User.objects.get(username=u)
            uo.uid = sys_users[u][0]
            uo.gid = sys_users[u][1]
            gname = get_groups(uo.gid).keys()[0]
            create = True
            if (uo.group is not None):
                if (uo.group.gid == uo.gid or uo.group.groupname == gname):
                    uo.group.groupname = gname
                    uo.group.gid = uo.gid
                    uo.group.save()
                    create = False
            if (create):
                try:
                    go = Group.objects.get(groupname=gname)
                    go.gid = uo.gid
                    go.save()
                    uo.group = go
                except Group.DoesNotExist:
                    try:
                        go = Group.objects.get(gid=uo.gid)
                        go.groupname = gname
                        go.save()
                        uo.group = go
                    except Group.DoesNotExist:
                        go = Group(groupname=gname, gid=uo.gid)
                        go.save()
                        uo.group = go
            uo.save()
            users.append(uo)
        except User.DoesNotExist:
            temp_uo = User(username=u, uid=sys_users[u][0],
                           gid=sys_users[u][1], admin=False)
            temp_uo.managed_user = False
            users.append(temp_uo)
    for u in User.objects.all():
        if (u.username not in uname_list):
            users.append(u)
    return users
Exemplo n.º 8
0
    def post(self, request):
        try:
            username = request.DATA['username']
            password = request.DATA['password']
            is_active = request.DATA['is_active']

            # Check that a django user with the same name does not exist
            if (DjangoUser.objects.filter(username=username).exists()
                    or User.objects.filter(username=username).exists()):
                e_msg = ('user: %s already exists. Please choose a different'
                         'username' % username)
                raise Exception(JSONRenderer().render({'username': e_msg}))

            # Check that a unix user with the same name does not exist
            unix_users = get_users(min_uid=0, uname=username)
            if (username in unix_users):
                e_msg = ('user: %s exists as a system user. Please choose a '
                         'different username' % username)
                raise Exception(JSONRenderer().render({'username': e_msg}))

            # Create Django user
            auser = DjangoUser.objects.create_user(username, None, password)
            auser.is_active = is_active
            auser.save()

            # Create unix user
            max_uid = settings.START_UID
            shell = settings.USER_SHELL
            try:
                # Find max uid
                max_uid = User.objects.all().order_by('-uid')[0].uid
            except Exception, e:
                logger.exception(e)
                pass
            uid = max_uid + 1
            useradd(username, uid, shell)
            usermod(username, password)
            suser = User(username=username, uid=uid, gid=uid, user=auser)
            suser.save()

            return Response(UserSerializer(auser).data)
Exemplo n.º 9
0
    def post(self, request):
        try:
            username = request.DATA['username']
            password = request.DATA['password']
            is_active = request.DATA['is_active']

            # Check that a django user with the same name does not exist
            if (DjangoUser.objects.filter(username=username).exists() or
                User.objects.filter(username=username).exists()):
                e_msg = ('user: %s already exists. Please choose a different'
                         'username' % username)
                raise Exception(JSONRenderer().render({'username': e_msg}))

            # Check that a unix user with the same name does not exist
            unix_users = get_users(min_uid=0, uname=username)
            if (username in unix_users):
                e_msg = ('user: %s exists as a system user. Please choose a '
                         'different username' % username)
                raise Exception(JSONRenderer().render({'username': e_msg}))

            # Create Django user
            auser = DjangoUser.objects.create_user(username, None, password)
            auser.is_active = is_active
            auser.save()

            # Create unix user
            max_uid = settings.START_UID
            shell = settings.USER_SHELL
            try:
                # Find max uid
                max_uid = User.objects.all().order_by('-uid')[0].uid
            except Exception, e:
                logger.exception(e)
                pass
            uid = max_uid + 1
            useradd(username, uid, shell)
            usermod(username, password)
            suser = User(username=username, uid=uid, gid=uid, user=auser)
            suser.save()

            return Response(UserSerializer(auser).data)
Exemplo n.º 10
0
def change_password(username, password):
    try:
        duser = DjangoUser.objects.get(username=username)
        duser.set_password(password)
        duser.save()
    except:
        sys.exit('username: %s does not exist in the admin database' %
                 username)
    try:
        user = User.objects.get(username=username)
    except:
        sys.exit('username: %s does not exist in the database' % username)

    unix_users = get_users(min_uid=0, uname=username)
    if (username not in unix_users):
        sys.exit('username: %s does not exist in /etc/passwd' % username)

    try:
        usermod(username, password)
    except:
        sys.exit('Low level error occured while changing password of user: %s'
        % username)
Exemplo n.º 11
0
def change_password(username, password):
    try:
        duser = DjangoUser.objects.get(username=username)
        duser.set_password(password)
        duser.save()
    except:
        sys.exit('username: %s does not exist in the admin database' %
                 username)
    try:
        user = User.objects.get(username=username)
    except:
        sys.exit('username: %s does not exist in the database' % username)

    unix_users = get_users(min_uid=0, uname=username)
    if (username not in unix_users):
        sys.exit('username: %s does not exist in /etc/passwd' % username)

    try:
        usermod(username, password)
    except:
        sys.exit(
            'Low level error occured while changing password of user: %s' %
            username)