Example #1
0
def block(user, reason, processor, date=None):
    """
    This function blocks a user for a certain time.
    A logmessage with a reason is created.
    :param user: The user to be blocked.
    :param date: The date the user is not blocked anymore.
    :param reason: The reason of blocking.
    :param processor: The admin who blocked the user.
    :return: The blocked user.
    """
    if date is not None and not isinstance(date, datetime):
        raise ValueError("Date should be a datetime object")

    if date is not None and date < datetime.now():
        raise ValueError("Date should be in the future")

    block_group = PropertyGroup.q.filter(
        PropertyGroup.name == config["block"]["group"]
    ).one()

    if date is not None:
        create_membership(start_date=datetime.now(), end_date=date,
                          group=block_group, user=user)
        log_message = config["block"]["log_message_with_enddate"].format(
            date=date.strftime("%d.%m.%Y"), reason=reason)
    else:
        create_membership(start_date=datetime.now(), end_date=None,
                          group=block_group, user=user)
        log_message = config["block"]["log_message_without_enddate"].format(
            reason=reason)

    create_user_log_entry(message=log_message, timestamp=datetime.now(),
                          author=processor, user=user)

    return user
Example #2
0
def move_out(user, date, comment, processor):
    """
    This function moves out a user and finishes all move_in memberships.
    move_in memberships are parsed from config.
    A log message is created.
    :param user: The user to move out.
    :param date: The date the user is going to move out.
    :param processor: The admin who is going to move out the user.
    :return: The user to move out.
    """
    if not isinstance(date, datetime):
        raise ValueError("Date should be a datetime object!")

    move_in_groups = config["move_in"]["group_memberships"]
    for membership in user.memberships:
        if membership.active:
            for move_in_group in move_in_groups:
                if move_in_group["name"] == membership.group.name:
                    membership.end_date = date

    log_message = config["move_out"]["log_message"].format(
        date=date.strftime("%d.%m.%Y"))
    if comment:
        log_message += config["move_out"]["log_message_comment"].format(
            comment=comment)

    create_user_log_entry(message=log_message,
                          timestamp=datetime.now(),
                          author=processor,
                          user=user)

    return user
Example #3
0
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
Example #4
0
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
Example #5
0
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
Example #6
0
def move_out_tmp(user, date, comment, processor):
    """
    This function moves a user temporally. A log message is created.
    :param user: The user to move out.
    :param date: The date the user is going to move out.
    :param comment: Comment for temp moveout
    :param processor: The admin who is going to move out the user.
    :return: The user to move out.
    """

    if not isinstance(date, datetime):
        raise ValueError("Date should be a datetime object!")

    away_group = PropertyGroup.q.filter(
        PropertyGroup.name == config["move_out_tmp"]["group"]
    ).one()

    tmp_memberships = Membership.q.join(PropertyGroup).filter(
        PropertyGroup.id == away_group.id).all()

    if len(tmp_memberships) > 0:
        # change the existing memberships for tmp_move_out
        for membership in tmp_memberships:
            membership.end_date = None
            membership.start_date = date
    else:
        # if there is no move out membership for the user jet, create one
        create_membership(group=away_group, user=user, start_date=date,
                          end_date=None)

    #TODO: the ip should be deleted just! if the user moves out now!
    for user_host in user.user_hosts:
        if user_host is not None:
            session.session.delete(user_host.user_net_device.ips[0])

    log_message = config["move_out_tmp"]["log_message"].format(
        date=date.strftime("%d.%m.%Y")
    )
    if comment:
        log_message += config["move_out_tmp"]["log_message_comment"].format(
            comment=comment
        )

    create_user_log_entry(
        message=log_message,
        timestamp=datetime.now(),
        author=processor,
        user=user
    )

    return user
Example #7
0
def move_out_tmp(user, date, comment, processor):
    """
    This function moves a user temporally. A log message is created.
    :param user: The user to move out.
    :param date: The date the user is going to move out.
    :param comment: Comment for temp moveout
    :param processor: The admin who is going to move out the user.
    :return: The user to move out.
    """

    if not isinstance(date, datetime):
        raise ValueError("Date should be a datetime object!")

    away_group = PropertyGroup.q.filter(
        PropertyGroup.name == config["move_out_tmp"]["group"]).one()

    tmp_memberships = Membership.q.join(PropertyGroup).filter(
        PropertyGroup.id == away_group.id).all()

    if len(tmp_memberships) > 0:
        # change the existing memberships for tmp_move_out
        for membership in tmp_memberships:
            membership.end_date = None
            membership.start_date = date
    else:
        # if there is no move out membership for the user jet, create one
        create_membership(group=away_group,
                          user=user,
                          start_date=date,
                          end_date=None)

    #TODO: the ip should be deleted just! if the user moves out now!
    for user_host in user.user_hosts:
        if user_host is not None:
            session.session.delete(user_host.user_net_device.ips[0])

    log_message = config["move_out_tmp"]["log_message"].format(
        date=date.strftime("%d.%m.%Y"))
    if comment:
        log_message += config["move_out_tmp"]["log_message_comment"].format(
            comment=comment)

    create_user_log_entry(message=log_message,
                          timestamp=datetime.now(),
                          author=processor,
                          user=user)

    return user
Example #8
0
def edit_name(user, name, processor):
    """
    Changes the name of the user and creates a log entry.
    :param user: The user object.
    :param name: The new full name.
    :return: The changed user object.
    """
    oldName = user.name
    if len(name):
        user.name = name

        create_user_log_entry(author=processor,
            message=u"Nutzer %s umbenannt in %s" % (oldName, name),
            timestamp=datetime.now(), user=user)

    return user
Example #9
0
def edit_email(user, email, processor):
    """
    Changes the email address of a user and creates a log entry.
    :param user: User object to change
    :param email: New email address
    :param processor:User object of the processor, which issues the change
    :return:Changed user object
    """
    oldEmail = user.email
    if len(email):
        user.email = email

        create_user_log_entry(author=processor,
            message=u"E-Mail-Adresse von %s auf %s geändert." % (oldEmail, email),
            timestamp=datetime.now(), user=user)

    return user
Example #10
0
def edit_name(user, name, processor):
    """
    Changes the name of the user and creates a log entry.
    :param user: The user object.
    :param name: The new full name.
    :return: The changed user object.
    """
    oldName = user.name
    if len(name):
        user.name = name

        create_user_log_entry(author=processor,
                              message=u"Nutzer %s umbenannt in %s" %
                              (oldName, name),
                              timestamp=datetime.now(),
                              user=user)

    return user
Example #11
0
def edit_email(user, email, processor):
    """
    Changes the email address of a user and creates a log entry.
    :param user: User object to change
    :param email: New email address
    :param processor:User object of the processor, which issues the change
    :return:Changed user object
    """
    oldEmail = user.email
    if len(email):
        user.email = email

        create_user_log_entry(
            author=processor,
            message=u"E-Mail-Adresse von %s auf %s geändert." %
            (oldEmail, email),
            timestamp=datetime.now(),
            user=user)

    return user
Example #12
0
def block(user, reason, processor, date=None):
    """
    This function blocks a user for a certain time.
    A logmessage with a reason is created.
    :param user: The user to be blocked.
    :param date: The date the user is not blocked anymore.
    :param reason: The reason of blocking.
    :param processor: The admin who blocked the user.
    :return: The blocked user.
    """
    if date is not None and not isinstance(date, datetime):
        raise ValueError("Date should be a datetime object")

    if date is not None and date < datetime.now():
        raise ValueError("Date should be in the future")

    block_group = PropertyGroup.q.filter(
        PropertyGroup.name == config["block"]["group"]).one()

    if date is not None:
        create_membership(start_date=datetime.now(),
                          end_date=date,
                          group=block_group,
                          user=user)
        log_message = config["block"]["log_message_with_enddate"].format(
            date=date.strftime("%d.%m.%Y"), reason=reason)
    else:
        create_membership(start_date=datetime.now(),
                          end_date=None,
                          group=block_group,
                          user=user)
        log_message = config["block"]["log_message_without_enddate"].format(
            reason=reason)

    create_user_log_entry(message=log_message,
                          timestamp=datetime.now(),
                          author=processor,
                          user=user)

    return user
Example #13
0
def move_out(user, date, comment, processor):
    """
    This function moves out a user and finishes all move_in memberships.
    move_in memberships are parsed from config.
    A log message is created.
    :param user: The user to move out.
    :param date: The date the user is going to move out.
    :param processor: The admin who is going to move out the user.
    :return: The user to move out.
    """
    if not isinstance(date, datetime):
        raise ValueError("Date should be a datetime object!")

    move_in_groups = config["move_in"]["group_memberships"]
    for membership in user.memberships:
        if membership.active:
            for move_in_group in move_in_groups:
                if move_in_group["name"] == membership.group.name:
                    membership.end_date = date


    log_message = config["move_out"]["log_message"].format(
        date=date.strftime("%d.%m.%Y")
    )
    if comment:
        log_message += config["move_out"]["log_message_comment"].format(
            comment=comment
        )

    create_user_log_entry(
        message=log_message,
        timestamp=datetime.now(),
        author=processor,
        user=user
    )

    return user
Example #14
0
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
Example #15
0
    def test_0010_create_user_log_entry(self):
        message = "test_message"
        timestamp = datetime.now()
        author = User.q.first()
        user = User.q.first()

        user_log_entry = create_user_log_entry(message=message,
                                               timestamp=timestamp,
                                               author=author,
                                               user=user)

        self.assertIsNotNone(UserLogEntry.q.get(user_log_entry.id))

        db_user_log_entry = UserLogEntry.q.get(user_log_entry.id)

        self.assertEqual(db_user_log_entry.message, message)
        self.assertEqual(db_user_log_entry.timestamp, timestamp)
        self.assertEqual(db_user_log_entry.author, author)
        self.assertEqual(db_user_log_entry.user, user)

        session.session.delete(db_user_log_entry)
        session.session.commit()
Example #16
0
    def test_0010_create_user_log_entry(self):
        message = "test_message"
        timestamp = datetime.now()
        author = User.q.first()
        user = User.q.first()

        user_log_entry = create_user_log_entry(message=message,
                                               timestamp=timestamp,
                                               author=author,
                                               user=user)

        self.assertIsNotNone(UserLogEntry.q.get(user_log_entry.id))

        db_user_log_entry = UserLogEntry.q.get(user_log_entry.id)

        self.assertEqual(db_user_log_entry.message, message)
        self.assertEqual(db_user_log_entry.timestamp, timestamp)
        self.assertEqual(db_user_log_entry.author, author)
        self.assertEqual(db_user_log_entry.user, user)

        session.session.delete(db_user_log_entry)
        session.session.commit()
Example #17
0
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
Example #18
0
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