def is_back(user, processor): """ After a user moved temporarily out, this function sets group memberships and creates a log message :param user: The User who is back. :param processor: The admin recognizing the users return. :return: The user who returned. """ membership = Membership.q.join( (PropertyGroup, Membership.group_id == PropertyGroup.id)).filter( PropertyGroup.name == config["move_out_tmp"]["group"], Membership.user_id == user.id, Membership.active).one() membership.disable() subnets = user.room.dormitory.subnets ip_address = host.get_free_ip(subnets) subnet = host.select_subnet_for_ip(ip_address, subnets) for user_host in user.user_hosts: create_ip(address=ip_address, subnet=subnet, net_device=user_host.user_net_device) create_user_log_entry(message=config["move_out_tmp"]["log_message_back"], timestamp=datetime.now(), author=processor, user=user) return user
def move(user, dormitory, level, room_number, processor): """ Moves the user into another room. :param user: The user to be moved. :param dormitory: The new dormitory. :param level: The level of the new room. :param room_number: The number of the new room. :param processor: The user who is currently logged in. :return: The user object of the moved user. """ old_room = user.room new_room = Room.q.filter_by( number=room_number, level=level, dormitory_id=dormitory.id ).one() assert old_room is not new_room,\ "A User is only allowed to move in a different room!" user.room = new_room create_user_log_entry( author=processor, message=config["move"]["log_message"].format( from_room=old_room, to_room=new_room), timestamp=datetime.now(), user=user ) # assign a new IP to each net_device for user_host in user.user_hosts: net_dev = user_host.user_net_device if old_room.dormitory_id != new_room.dormitory_id: assert len(net_dev.ips) == 1, "A user should only have one ip!" ip_addr = net_dev.ips[0] old_ip = ip_addr.address new_ip = host.get_free_ip(dormitory.subnets) new_subnet = host.select_subnet_for_ip(new_ip, dormitory.subnets) ip_addr.change_ip(new_ip, new_subnet) create_user_log_entry(author=processor, message=config["move"]["ip_change_log_message"].format( old_ip=old_ip, new_ip=new_ip), timestamp=datetime.now(), user=user) #TODO set new PatchPort for each NetDevice in each Host that moves to the new room #moves the host in the new room and assign the belonging net_device to the new patch_port for user_host in user.user_hosts: user_host.room = new_room return user
def move(user, dormitory, level, room_number, processor): """ Moves the user into another room. :param user: The user to be moved. :param dormitory: The new dormitory. :param level: The level of the new room. :param room_number: The number of the new room. :param processor: The user who is currently logged in. :return: The user object of the moved user. """ old_room = user.room new_room = Room.q.filter_by(number=room_number, level=level, dormitory_id=dormitory.id).one() assert old_room is not new_room,\ "A User is only allowed to move in a different room!" user.room = new_room create_user_log_entry(author=processor, message=config["move"]["log_message"].format( from_room=old_room, to_room=new_room), timestamp=datetime.now(), user=user) # assign a new IP to each net_device for user_host in user.user_hosts: net_dev = user_host.user_net_device if old_room.dormitory_id != new_room.dormitory_id: assert len(net_dev.ips) == 1, "A user should only have one ip!" ip_addr = net_dev.ips[0] old_ip = ip_addr.address new_ip = host.get_free_ip(dormitory.subnets) new_subnet = host.select_subnet_for_ip(new_ip, dormitory.subnets) ip_addr.change_ip(new_ip, new_subnet) create_user_log_entry( author=processor, message=config["move"]["ip_change_log_message"].format( old_ip=old_ip, new_ip=new_ip), timestamp=datetime.now(), user=user) #TODO set new PatchPort for each NetDevice in each Host that moves to the new room #moves the host in the new room and assign the belonging net_device to the new patch_port for user_host in user.user_hosts: user_host.room = new_room return user
def test_0030_get_free_ip_next_to_full(self): subnets = dormitory.Subnet.q.order_by(dormitory.Subnet.gateway).all() host = Host.q.filter(Host.id == UserHostData.dummy_host1.id).one() nd = UserNetDevice(mac="00:00:00:00:00:00", host = host) for num in range(0, 490): if num >= 488: self.assertRaises(SubnetFullException, get_free_ip, subnets) continue ip = get_free_ip(subnets) net = select_subnet_for_ip(ip, subnets) if num < 244: self.assertEqual(ip, self.ip_s1(num)) else: self.assertEqual(ip, self.ip_s2(num % 244)) ip_addr = Ip(address=ip, subnet=net, net_device=nd) session.session.add(ip_addr) session.session.commit() Ip.q.filter(Ip.net_device == nd).delete() session.session.delete(nd) session.session.commit()
def test_0030_get_free_ip_next_to_full(self): subnets = dormitory.Subnet.q.order_by(dormitory.Subnet.gateway).all() host = Host.q.filter(Host.id == UserHostData.dummy_host1.id).one() nd = UserNetDevice(mac="00:00:00:00:00:00", host=host) for num in range(0, 490): if num >= 488: self.assertRaises(SubnetFullException, get_free_ip, subnets) continue ip = get_free_ip(subnets) net = select_subnet_for_ip(ip, subnets) if num < 244: self.assertEqual(ip, self.ip_s1(num)) else: self.assertEqual(ip, self.ip_s2(num % 244)) ip_addr = Ip(address=ip, subnet=net, net_device=nd) session.session.add(ip_addr) session.session.commit() Ip.q.filter(Ip.net_device == nd).delete() session.session.delete(nd) session.session.commit()
def is_back(user, processor): """ After a user moved temporarily out, this function sets group memberships and creates a log message :param user: The User who is back. :param processor: The admin recognizing the users return. :return: The user who returned. """ membership = Membership.q.join( (PropertyGroup, Membership.group_id == PropertyGroup.id) ).filter( PropertyGroup.name == config["move_out_tmp"]["group"], Membership.user_id == user.id, Membership.active ).one() membership.disable() subnets = user.room.dormitory.subnets ip_address = host.get_free_ip(subnets) subnet = host.select_subnet_for_ip(ip_address, subnets) for user_host in user.user_hosts: create_ip( address=ip_address, subnet=subnet, net_device=user_host.user_net_device ) create_user_log_entry( message=config["move_out_tmp"]["log_message_back"], timestamp=datetime.now(), author=processor, user=user ) return user
def test_0020_select_subnet_for_ip(self): subnets = dormitory.Subnet.q.order_by(dormitory.Subnet.gateway).all() for subnet in subnets: for ip in ipaddr.IPNetwork(subnet.address).iterhosts(): selected = select_subnet_for_ip(ip.compressed, subnets) self.assertEqual(subnet, selected)
def move_in(name, login, email, dormitory, level, room_number, mac, processor, host_name=None): """ This function creates a new user, assign him to a room and creates some initial groups and transactions. :param name: The full name of the user. (Max Mustermann) :param login: The unix login for the user. :param email: E-Mail address of the user. :param dormitory: The dormitory the user moves in. :param level: The level the user moves in. :param room_number: The room number the user moves in. :param mac: The mac address of the users pc. :param host_name: An optional Hostname for the users pc. :return: The new user object. """ room = Room.q.filter_by(number=room_number, level=level, dormitory=dormitory).one() # create a new user new_user = User( login=login, name=name, email=email, room=room, registration_date=datetime.now() ) plain_password = user.generate_password(12) #TODO: print plain password on paper instead print u"new password: "******"move_in"] for membership in conf["group_memberships"]: group = Group.q.filter(Group.name == membership["name"]).one() start_date = datetime.now() if membership.get("offset"): start_date += timedelta(membership["offset"]) new_membership = create_membership( start_date=start_date, end_date=None, group=group, user=new_user ) if membership.get("duration"): assert membership["duration"] > 0 new_membership.end_date = datetime.now() + timedelta(membership["duration"]) setup_user_finance_account(new_user, processor) move_in_user_log_entry = create_user_log_entry( author=processor, message=conf["log_message"], timestamp=datetime.now(), user=new_user ) return new_user
def move_in(name, login, email, dormitory, level, room_number, mac, processor, host_name=None): """ This function creates a new user, assign him to a room and creates some initial groups and transactions. :param name: The full name of the user. (Max Mustermann) :param login: The unix login for the user. :param email: E-Mail address of the user. :param dormitory: The dormitory the user moves in. :param level: The level the user moves in. :param room_number: The room number the user moves in. :param mac: The mac address of the users pc. :param host_name: An optional Hostname for the users pc. :return: The new user object. """ room = Room.q.filter_by(number=room_number, level=level, dormitory=dormitory).one() # create a new user new_user = User(login=login, name=name, email=email, room=room, registration_date=datetime.now()) plain_password = user.generate_password(12) #TODO: print plain password on paper instead print u"new password: "******"move_in"] for membership in conf["group_memberships"]: group = Group.q.filter(Group.name == membership["name"]).one() start_date = datetime.now() if membership.get("offset"): start_date += timedelta(membership["offset"]) new_membership = create_membership(start_date=start_date, end_date=None, group=group, user=new_user) if membership.get("duration"): assert membership["duration"] > 0 new_membership.end_date = datetime.now() + timedelta( membership["duration"]) setup_user_finance_account(new_user, processor) move_in_user_log_entry = create_user_log_entry(author=processor, message=conf["log_message"], timestamp=datetime.now(), user=new_user) return new_user