Exemplo n.º 1
0
    def post(self, request):
        with self._handle_exception(request):
            groupname = request.data.get('groupname', None)
            gid = request.data.get('gid', None)
            if (gid is not None):
                gid = int(gid)
            admin = request.data.get('admin', True)
            if (groupname is None or
                re.match(settings.USERNAME_REGEX, groupname) is None):
                e_msg = ('Groupname is invalid. It must confirm to the regex: %s' %
                         (settings.USERNAME_REGEX))
                handle_exception(Exception(e_msg), request)
            if (len(groupname) > 30):
                e_msg = ('Groupname cannot be more than 30 characters long')
                handle_exception(Exception(e_msg), request)

            for g in combined_groups():
                if (g.groupname == groupname):
                    e_msg = ('Group(%s) already exists. Choose a different one' %
                             g.groupname)
                    handle_exception(Exception(e_msg), request)
                if (g.gid == gid):
                    e_msg = ('GID(%s) already exists. Choose a different one' %
                             gid)
                    handle_exception(Exception(e_msg), request)

            groupadd(groupname, gid)
            grp_entries = grp.getgrnam(groupname)
            gid = grp_entries[2]
            group = Group(gid=gid, groupname=groupname, admin=admin)
            group.save()

            return Response(GroupSerializer(group).data)
Exemplo n.º 2
0
    def delete(self, request, groupname):
        with self._handle_exception(request):
            if groupname in self.exclude_list:
                e_msg = ("Delete of restricted group ({}) is not supported."
                         ).format(groupname)
                handle_exception(Exception(e_msg), request, status_code=400)

            if Group.objects.filter(groupname=groupname).exists():
                g = Group.objects.get(groupname=groupname)
                g.delete()
            else:
                found = False
                for g in combined_groups():
                    if g.groupname == groupname:
                        found = True
                        break
                if found is False:
                    e_msg = "Group ({}) does not exist.".format(groupname)
                    handle_exception(Exception(e_msg), request)

            try:
                groupdel(groupname)
            except Exception as e:
                handle_exception(e, request)

            return Response()
Exemplo n.º 3
0
    def post(self, request):
        with self._handle_exception(request):
            groupname = request.data.get('groupname', None)
            gid = request.data.get('gid', None)
            if (gid is not None):
                gid = int(gid)
            admin = request.data.get('admin', True)
            if (groupname is None
                    or re.match(settings.USERNAME_REGEX, groupname) is None):
                e_msg = (
                    'Groupname is invalid. It must confirm to the regex: %s' %
                    (settings.USERNAME_REGEX))
                handle_exception(Exception(e_msg), request)
            if (len(groupname) > 30):
                e_msg = ('Groupname cannot be more than 30 characters long')
                handle_exception(Exception(e_msg), request)

            for g in combined_groups():
                if (g.groupname == groupname):
                    e_msg = (
                        'Group(%s) already exists. Choose a different one' %
                        g.groupname)
                    handle_exception(Exception(e_msg), request)
                if (g.gid == gid):
                    e_msg = ('GID(%s) already exists. Choose a different one' %
                             gid)
                    handle_exception(Exception(e_msg), request)

            groupadd(groupname, gid)
            grp_entries = grp.getgrnam(groupname)
            gid = grp_entries[2]
            group = Group(gid=gid, groupname=groupname, admin=admin)
            group.save()

            return Response(GroupSerializer(group).data)
Exemplo n.º 4
0
    def delete(self, request, groupname):
        with self._handle_exception(request):
            if (groupname in self.exclude_list):
                e_msg = ('Delete of restricted group(%s) is not supported.' %
                         groupname)
                handle_exception(Exception(e_msg), request)

            if (Group.objects.filter(groupname=groupname).exists()):
                g = Group.objects.get(groupname=groupname)
                g.delete()
            else:
                found = False
                for g in combined_groups():
                    if (g.groupname == groupname):
                        found = True
                        break
                if (found is False):
                    e_msg = ('Group(%s) does not exist' % groupname)
                    handle_exception(Exception(e_msg), request)

            try:
                groupdel(groupname)
            except Exception, e:
                handle_exception(e, request)

            return Response()
Exemplo n.º 5
0
    def delete(self, request, groupname):
        with self._handle_exception(request):
            if (groupname in self.exclude_list):
                e_msg = ('Delete of restricted group(%s) is not supported.' %
                         groupname)
                handle_exception(Exception(e_msg), request)

            if (Group.objects.filter(groupname=groupname).exists()):
                g = Group.objects.get(groupname=groupname)
                g.delete()
            else:
                found = False
                for g in combined_groups():
                    if (g.groupname == groupname):
                        found = True
                        break
                if (found is False):
                    e_msg = ('Group(%s) does not exist' % groupname)
                    handle_exception(Exception(e_msg), request)

            try:
                groupdel(groupname)
            except Exception, e:
                handle_exception(e, request)

            return Response()
Exemplo n.º 6
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.º 7
0
 def get_queryset(self, *args, **kwargs):
     if ('groupname' in kwargs):
         self.paginate_by = 0
         try:
             return Group.objects.get(username=kwargs['groupname'])
         except:
             return []
     return combined_groups()
Exemplo n.º 8
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.º 9
0
    def delete(self, request, username):
        with self._handle_exception(request):
            if request.user.username == username:
                e_msg = "Cannot delete the currently logged in user."
                handle_exception(Exception(e_msg), request)

            if username in self.exclude_list:
                e_msg = ("Delete of restricted user ({}) is not supported."
                         ).format(username)
                handle_exception(Exception(e_msg), request)

            gid = None
            if User.objects.filter(username=username).exists():
                u = User.objects.get(username=username)
                if u.user is not None:
                    u.user.delete()
                gid = u.gid
                u.delete()
            else:
                sysusers = combined_users()
                found = False
                for u in sysusers:
                    if u.username == username:
                        found = True
                        break
                if found is False:
                    e_msg = "User ({}) does not exist.".format(username)
                    handle_exception(Exception(e_msg), request)

            for g in combined_groups():
                if (g.gid == gid and g.admin
                        and not User.objects.filter(gid=gid).exists()):
                    g.delete()

            # When user deleted destroy all Pincard entries
            flush_pincard(username_to_uid(username))

            try:
                userdel(username)
            except Exception as e:
                logger.exception(e)
                e_msg = (
                    "A low level error occurred while deleting the user ({})."
                ).format(username)
                handle_exception(Exception(e_msg), request)

            return Response()
Exemplo n.º 10
0
    def delete(self, request, username):
        with self._handle_exception(request):
            if request.user.username == username:
                e_msg = ('Cannot delete the currently logged in user')
                handle_exception(Exception(e_msg), request)

            if (username in self.exclude_list):
                e_msg = ('Delete of restricted user(%s) is not supported.' %
                         username)
                handle_exception(Exception(e_msg), request)

            gid = None
            if (User.objects.filter(username=username).exists()):
                u = User.objects.get(username=username)
                if (u.user is not None):
                    u.user.delete()
                gid = u.gid
                u.delete()
            else:
                sysusers = combined_users()
                found = False
                for u in sysusers:
                    if (u.username == username):
                        found = True
                        break
                if (found is False):
                    e_msg = ('User(%s) does not exist' % username)
                    handle_exception(Exception(e_msg), request)

            for g in combined_groups():
                if (g.gid == gid and g.admin and
                        not User.objects.filter(gid=gid).exists()):
                    g.delete()

            # When user deleted destroy all Pincard entries
            flush_pincard(username_to_uid(username))

            try:
                userdel(username)
            except Exception as e:
                logger.exception(e)
                e_msg = ('A low level error occured while deleting '
                         'the user: %s' % username)
                handle_exception(Exception(e_msg), request)

            return Response()
Exemplo n.º 11
0
    def delete(self, request, username):
        with self._handle_exception(request):
            if request.user.username == username:
                e_msg = ('Cannot delete the currently logged in user')
                handle_exception(Exception(e_msg), request)

            if (username in self.exclude_list):
                e_msg = ('Delete of restricted user(%s) is not supported.' %
                         username)
                handle_exception(Exception(e_msg), request)

            gid = None
            if (User.objects.filter(username=username).exists()):
                u = User.objects.get(username=username)
                if (u.user is not None):
                    u.user.delete()
                gid = u.gid
                u.delete()
            else:
                sysusers = combined_users()
                found = False
                for u in sysusers:
                    if (u.username == username):
                        found = True
                        break
                if (found is False):
                    e_msg = ('User(%s) does not exist' % username)
                    handle_exception(Exception(e_msg), request)

            for g in combined_groups():
                if (g.gid == gid and g.admin
                        and not User.objects.filter(gid=gid).exists()):
                    g.delete()

            try:
                userdel(username)
            except Exception, e:
                logger.exception(e)
                e_msg = (
                    'A low level error occured while deleting the user: %s' %
                    username)
                handle_exception(Exception(e_msg), request)

            return Response()
Exemplo n.º 12
0
    def post(self, request):
        with self._handle_exception(request):
            groupname = request.data.get("groupname", None)
            gid = request.data.get("gid", None)
            if gid is not None:
                gid = int(gid)
            admin = request.data.get("admin", True)
            if (groupname is None
                    or re.match(settings.USERNAME_REGEX, groupname) is None):
                e_msg = (
                    "Groupname is invalid. It must conform to the regex: ({})."
                ).format(settings.USERNAME_REGEX)
                handle_exception(Exception(e_msg), request, status_code=400)
            if len(groupname) > 30:
                e_msg = "Groupname cannot be more than 30 characters long."
                handle_exception(Exception(e_msg), request, status_code=400)

            for g in combined_groups():
                if g.groupname == groupname:
                    e_msg = (
                        "Group ({}) already exists. Choose a different one."
                    ).format(g.groupname)
                    handle_exception(Exception(e_msg),
                                     request,
                                     status_code=400)
                if g.gid == gid:
                    e_msg = ("GID ({}) already exists. Choose a different one."
                             ).format(gid)
                    handle_exception(Exception(e_msg),
                                     request,
                                     status_code=400)

            groupadd(groupname, gid)
            grp_entries = grp.getgrnam(groupname)
            gid = grp_entries[2]
            group = Group(gid=gid, groupname=groupname, admin=admin)
            group.save()

            return Response(GroupSerializer(group).data)
Exemplo n.º 13
0
    def delete(self, request, username):
        if request.user.username == username:
            e_msg = "Cannot delete the currently logged in user"
            handle_exception(Exception(e_msg), request)

        if username in self.exclude_list:
            e_msg = "Delete of restricted user(%s) is not supported." % username
            handle_exception(Exception(e_msg), request)

        gid = None
        if User.objects.filter(username=username).exists():
            u = User.objects.get(username=username)
            if u.user is not None:
                u.user.delete()
            gid = u.gid
            u.delete()
        else:
            sysusers = combined_users()
            found = False
            for u in sysusers:
                if u.username == username:
                    found = True
                    break
            if found is False:
                e_msg = "User(%s) does not exist" % username
                handle_exception(Exception(e_msg), request)

        for g in combined_groups():
            if g.gid == gid and g.admin and not User.objects.filter(gid=gid).exists():
                g.delete()

        try:
            userdel(username)
        except Exception, e:
            logger.exception(e)
            e_msg = "A low level error occured while deleting the user: %s" % username
            handle_exception(Exception(e_msg), request)
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 ({}) 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.º 15
0
 def get_queryset(self, *args, **kwargs):
     with self._handle_exception(self.request):
         return combined_groups()
Exemplo n.º 16
0
 def get_queryset(self, *args, **kwargs):
     return combined_groups()
Exemplo n.º 17
0
 def get_queryset(self, *args, **kwargs):
     return combined_groups()
Exemplo n.º 18
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.º 19
0
 def get_queryset(self, *args, **kwargs):
     with self._handle_exception(self.request):
         return combined_groups()