예제 #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)
예제 #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
    )
예제 #3
0
def combined_groups():
    groups = []
    sys_groups = get_groups()
    gname_list = sys_groups.keys()
    for g in gname_list:
        try:
            go = Group.objects.get(groupname=g)
            go.gid = sys_groups[g]
            go.save()
            groups.append(go)
        except Group.DoesNotExist:
            groups.append(Group(groupname=g, gid=sys_groups[g]))
    for g in Group.objects.all():
        if (g.groupname not in gname_list):
            groups.append(g)
    return groups
예제 #4
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()))
예제 #5
0
def combined_groups():
    groups = []
    sys_groups = get_groups()
    gname_list = sys_groups.keys()
    for g in gname_list:
        try:
            go = Group.objects.get(groupname=g)
            go.gid = sys_groups[g]
            go.save()
            groups.append(go)
        except Group.DoesNotExist:
            groups.append(Group(groupname=g, gid=sys_groups[g]))
    for g in Group.objects.all():
        if (g.groupname not in gname_list):
            groups.append(g)
    return sorted(groups, cmp=lambda x, y: cmp(x.groupname.lower(),  # noqa F821
                                               y.groupname.lower()))
예제 #6
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)
예제 #7
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)
예제 #8
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)