Exemplo n.º 1
0
    def post(self, request):
        with self._handle_exception(request):

            invar = self._validate_input(request)
            # Check that a django user with the same name does not exist
            e_msg = ('User(%s) already exists. Please choose a different'
                     ' username' % invar['username'])
            if (DjangoUser.objects.filter(
                    username=invar['username']).exists() or
                    User.objects.filter(username=invar['username']).exists()):
                handle_exception(Exception(e_msg), request)
            users = combined_users()
            groups = combined_groups()
            invar['gid'] = None
            admin_group = None
            if (invar['group'] is not None):
                for g in groups:
                    if (g.groupname == invar['group']):
                        invar['gid'] = g.gid
                        admin_group = g
                        invar['group'] = g
                        break

            for u in users:
                if (u.username == invar['username']):
                    handle_exception(Exception(e_msg), request)
                elif (u.uid == invar['uid']):
                    e_msg = ('uid: %d already exists. Please choose a '
                             'different one.' % invar['uid'])
                    handle_exception(Exception(e_msg), request)

            if (invar['admin']):
                # Create Django user
                auser = DjangoUser.objects.create_user(invar['username'],
                                                       None, invar['password'])
                auser.is_active = True
                auser.save()
                invar['user'] = auser

            useradd(invar['username'], invar['shell'], uid=invar['uid'],
                    gid=invar['gid'])
            pw_entries = pwd.getpwnam(invar['username'])
            invar['uid'] = pw_entries[2]
            invar['gid'] = pw_entries[3]
            usermod(invar['username'], invar['password'])
            smbpasswd(invar['username'], invar['password'])
            if (invar['public_key'] is not None):
                add_ssh_key(invar['username'], invar['public_key'])
            del(invar['password'])
            if (admin_group is None):
                admin_group = Group(gid=invar['gid'],
                                    groupname=invar['username'],
                                    admin=True)
                admin_group.save()
                invar['group'] = admin_group
            suser = User(**invar)
            suser.full_clean()
            suser.save()
            return Response(SUserSerializer(suser).data)
Exemplo n.º 2
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.º 3
0
    def post(self, request):
        try:
            invar = self._validate_input(request)
            # Check that a django user with the same name does not exist
            e_msg = "user: %s already exists. Please choose a different" " username" % invar["username"]
            if DjangoUser.objects.filter(username=invar["username"]).exists():
                handle_exception(Exception(e_msg), request)
            users = combined_users()
            for u in users:
                if u.username == invar["username"]:
                    handle_exception(Exception(e_msg), request)
                if u.uid == invar["uid"]:
                    e_msg = "uid: %d already exists." % invar["uid"]
                    handle_exception(Exception(e_msg), request)

            groups = combined_groups()
            invar["gid"] = None
            admin_group = None
            if invar["group"] is not None:
                for g in groups:
                    if g.groupname == invar["group"]:
                        invar["gid"] = g.gid
                        admin_group = g
                        break

            if invar["admin"]:
                # Create Django user
                auser = DjangoUser.objects.create_user(invar["username"], None, invar["password"])
                auser.is_active = True
                auser.save()
                invar["user"] = auser

            useradd(invar["username"], invar["shell"], uid=invar["uid"], gid=invar["gid"])
            pw_entries = pwd.getpwnam(invar["username"])
            invar["uid"] = pw_entries[2]
            invar["gid"] = pw_entries[3]
            usermod(invar["username"], invar["password"])
            smbpasswd(invar["username"], invar["password"])
            if invar["public_key"] is not None:
                add_ssh_key(invar["username"], invar["public_key"])
            del (invar["password"])
            invar["group"] = None
            if admin_group is None:
                admin_group = Group(gid=invar["gid"], groupname=invar["username"], admin=True)
                admin_group.save()
                invar["group"] = admin_group
            invar["admin"] = True
            suser = User(**invar)
            suser.save()
            return Response(SUserSerializer(suser).data)
        except RockStorAPIException:
            raise
        except Exception, e:
            handle_exception(e, request)
Exemplo n.º 4
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.º 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 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.º 7
0
 def _set_admin_users(admin_users, smb_share):
     for au in admin_users:
         try:
             auo = User.objects.get(username=au)
         except User.DoesNotExist:
             #check if the user is a system user, then create a temp user object.
             try:
                 system_user = pwd.getpwnam(au)
                 auo = User(username=au, uid=system_user.pw_uid,
                           gid=system_user.pw_gid, admin=False)
                 auo.save()
             except KeyError:
                 #raise the outer exception as it's more meaningful to the user.
                 raise Exception('Requested admin user(%s) does not exist.' % au)
         finally:
             auo.smb_shares.add(smb_share)
Exemplo n.º 8
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.º 9
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.º 10
0
 def _set_admin_users(admin_users, smb_share):
     for au in admin_users:
         try:
             auo = User.objects.get(username=au)
         except User.DoesNotExist:
             #check if the user is a system user, then create a temp user object.
             try:
                 system_user = pwd.getpwnam(au)
                 auo = User(username=au,
                            uid=system_user.pw_uid,
                            gid=system_user.pw_gid,
                            admin=False)
                 auo.save()
             except KeyError:
                 #raise the outer exception as it's more meaningful to the user.
                 raise Exception(
                     'Requested admin user(%s) does not exist.' % au)
         finally:
             auo.smb_shares.add(smb_share)
Exemplo n.º 11
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.º 12
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.º 13
0
    def post(self, request):
        with self._handle_exception(request):

            invar = self._validate_input(request)
            # Check that a django user with the same name does not exist
            e_msg = (
                "User ({}) already exists. Please choose a different username."
            ).format(invar["username"])
            if (DjangoUser.objects.filter(username=invar["username"]).exists()
                    or
                    User.objects.filter(username=invar["username"]).exists()):

                handle_exception(Exception(e_msg), request, status_code=400)
            users = combined_users()
            groups = combined_groups()
            # As we have not yet established a pre-existing group, set to None.
            admin_group = None
            if invar["group"] is not None:
                # We have a group setting so search for existing group name
                # match. Matching by group name has precedence over gid.
                for g in groups:
                    if g.groupname == invar["group"]:
                        # We have an existing group name match in invar
                        # so overwrite requested gid to match existing gid.
                        invar["gid"] = g.gid
                        # Set the admin_group to our existing group object.
                        admin_group = g
                        admin_group.save()
                        invar["group"] = g  # exchange name for db group item.
                        break

            for u in users:
                if u.username == invar["username"]:
                    handle_exception(Exception(e_msg),
                                     request,
                                     status_code=400)
                elif u.uid == invar["uid"]:
                    e_msg = (
                        "UID ({}) already exists. Please choose a different one."
                    ).format(invar["uid"])
                    handle_exception(Exception(e_msg), request)

            if invar["admin"]:
                # Create Django user
                auser = DjangoUser.objects.create_user(invar["username"], None,
                                                       invar["password"])
                auser.is_active = True
                auser.save()
                invar["user"] = auser

            useradd(invar["username"],
                    invar["shell"],
                    uid=invar["uid"],
                    gid=invar["gid"])
            pw_entries = pwd.getpwnam(invar["username"])
            invar["uid"] = pw_entries[2]
            invar["gid"] = pw_entries[3]
            usermod(invar["username"], invar["password"])
            smbpasswd(invar["username"], invar["password"])
            if invar["public_key"] is not None:
                add_ssh_key(invar["username"], invar["public_key"])
            del invar["password"]
            if admin_group is None:
                # We have no identified pre-existing group by name but there
                # could still be an existing group match by gid, if so we
                # use that group object as our new User.group foreign key link.
                if Group.objects.filter(gid=invar["gid"]).exists():
                    admin_group = Group.objects.get(gid=invar["gid"])
                else:
                    # As we are creating a new group we set admin=True to
                    # flag this group as administered by Rockstor.
                    if invar["group"] is None:
                        admin_group = Group(gid=invar["gid"],
                                            groupname=invar["username"],
                                            admin=True)
                    else:
                        admin_group = Group(gid=invar["gid"],
                                            groupname=invar["group"],
                                            admin=True)
                    admin_group.save()  # save our new group object.
                # set our invar dict group entry to our admin_group object.
                invar["group"] = admin_group
            # now we create our user object based on the contents of invar[]
            suser = User(**invar)
            # validate and save our suser object.
            suser.full_clean()
            suser.save()
            return Response(SUserSerializer(suser).data)
Exemplo n.º 14
0
    def post(self, request):
        with self._handle_exception(request):

            invar = self._validate_input(request)
            # Check that a django user with the same name does not exist
            e_msg = ('User(%s) already exists. Please choose a different'
                     ' username' % invar['username'])
            if (DjangoUser.objects.filter(username=invar['username']).exists()
                    or
                    User.objects.filter(username=invar['username']).exists()):
                handle_exception(Exception(e_msg), request)
            users = combined_users()
            groups = combined_groups()
            invar['gid'] = None
            admin_group = None
            if (invar['group'] is not None):
                for g in groups:
                    if (g.groupname == invar['group']):
                        invar['gid'] = g.gid
                        admin_group = g
                        invar['group'] = g
                        break

            for u in users:
                if (u.username == invar['username']):
                    handle_exception(Exception(e_msg), request)
                elif (u.uid == invar['uid']):
                    e_msg = ('uid: %d already exists. Please choose a '
                             'different one.' % invar['uid'])
                    handle_exception(Exception(e_msg), request)

            if (invar['admin']):
                # Create Django user
                auser = DjangoUser.objects.create_user(invar['username'], None,
                                                       invar['password'])
                auser.is_active = True
                auser.save()
                invar['user'] = auser

            useradd(invar['username'],
                    invar['shell'],
                    uid=invar['uid'],
                    gid=invar['gid'])
            pw_entries = pwd.getpwnam(invar['username'])
            invar['uid'] = pw_entries[2]
            invar['gid'] = pw_entries[3]
            usermod(invar['username'], invar['password'])
            smbpasswd(invar['username'], invar['password'])
            if (invar['public_key'] is not None):
                add_ssh_key(invar['username'], invar['public_key'])
            del (invar['password'])
            if (admin_group is None):
                admin_group = Group(gid=invar['gid'],
                                    groupname=invar['username'],
                                    admin=True)
                admin_group.save()
                invar['group'] = admin_group
            suser = User(**invar)
            suser.full_clean()
            suser.save()
            return Response(SUserSerializer(suser).data)