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)
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)
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)