def unblock(user, processor, when=None): """Unblocks a user. This removes his membership of the violation, blocken and payment_in_default group. Note that for unblocking, no further asynchronous action has to be triggered, as opposed to e.g. membership termination. :param User user: The user to be unblocked. :param User processor: The admin who unblocked the user. :param datetime when: The time of membership termination. Note that in comparison to :py:func:`suspend`, you don't provide an _interval_, but a point in time, defaulting to the current time. Will be converted to ``closedopen(when, None)``. :return: The unblocked user. """ if when is None: when = session.utcnow() for group in get_blocked_groups(): if user.member_of(group): remove_member_of(user=user, group=group, processor=processor, during=closedopen(when, None)) return user
def unblock(user, processor, when=None): """Unblocks a user. This removes his membership of the ``config.violation`` group. Note that for unblocking, no further asynchronous action has to be triggered, as opposed to e.g. membership termination. :param User user: The user to be unblocked. :param User processor: The admin who unblocked the user. :param datetime when: The time of membership termination. Note that in comparison to :py:func:`suspend`, you don't provide an _interval_, but a point in time, defaulting to the current time. Will be converted to ``closedopen(when, None)``. :return: The unblocked user. """ if when is None: when = session.utcnow() remove_member_of(user=user, group=config.violation_group, processor=processor, during=closedopen(when, None)) message = deferred_gettext(u"User has been unblocked.") log_user_event(message=message.to_json(), author=processor, user=user) return user
def sync_exceeded_traffic_limits(): """Adds and removes memberships of the 'traffic_limit_exceeded group.' """ processor = User.q.get(0) # Add memberships users = User.q.join(User._current_traffic_balance) \ .filter(CurrentTrafficBalance.amount < 0) \ .except_(User.q.join(User.current_properties)\ .filter(or_(CurrentProperty.property_name == 'traffic_limit_disabled',\ CurrentProperty.property_name == 'traffic_limit_exceeded')))\ .all() for user in users: make_member_of(user, config.traffic_limit_exceeded_group, processor, closed(session.utcnow(), None)) print("Traffic-Limit exceeded: " + user.name) # End memberships users = User.q.join(User.current_properties) \ .filter(CurrentProperty.property_name == 'traffic_limit_exceeded') \ .join(User._current_traffic_balance) \ .filter(or_(CurrentTrafficBalance.amount >= 0, CurrentProperty.property_name == 'traffic_limit_disabled')) \ .all() for user in users: remove_member_of(user, config.traffic_limit_exceeded_group, processor, closed(session.utcnow(), None)) print("Traffic-Limit no longer exceeded: " + user.name)
def end_payment_in_default_memberships(processor): users = User.q.join(User.current_properties) \ .filter(CurrentProperty.property_name == 'payment_in_default') \ .join(Account).filter(Account.balance <= 0).all() for user in users: if user.member_of(config.payment_in_default_group): remove_member_of(user, config.payment_in_default_group, processor, closedopen(session.utcnow() - timedelta(seconds=1), None)) return users
def end_payment_in_default_memberships(): processor = User.q.get(0) users = User.q.join(User.current_properties) \ .filter(CurrentProperty.property_name == 'payment_in_default') \ .join(Account).filter(Account.balance <= 0).all() for user in users: remove_member_of(user, config.payment_in_default_group, processor, closedopen(session.utcnow(), None)) return users
def post(self, user_id): parser = reqparse.RequestParser() parser.add_argument('use_cache', dest='use_cache', type=inputs.boolean, required=True) args = parser.parse_args() user = get_user_or_404(user_id) if args.use_cache != user.member_of(config.cache_group): if args.use_cache: make_member_of(user, config.cache_group, user) else: remove_member_of(user, config.cache_group, user) session.session.commit() return "Cache usage has been changed."
def handle_payments_in_default(): processor = User.q.get(0) # Add memberships and end "member" membership if threshold met users = User.q.join(User.current_properties)\ .filter(CurrentProperty.property_name == 'membership_fee') \ .join(Account).filter(Account.balance > 0).all() users_pid_membership = [] users_membership_terminated = [] ts_now = session.utcnow() for user in users: last_pid_membership = Membership.q.filter(Membership.user_id == user.id) \ .filter(Membership.group_id == config.payment_in_default_group.id) \ .order_by(Membership.ends_at.desc()) \ .first() if last_pid_membership is not None: if last_pid_membership.ends_at is not None and \ last_pid_membership.ends_at >= ts_now - timedelta(days=7): continue in_default_days = user.account.in_default_days try: fee = get_membership_fee_for_date( date.today() - timedelta(days=in_default_days)) except NoResultFound: fee = get_last_applied_membership_fee() if not fee: return [], [] if not user.has_property('payment_in_default'): if in_default_days >= fee.payment_deadline.days: make_member_of(user, config.payment_in_default_group, processor, closed(ts_now, None)) users_pid_membership.append(user) if in_default_days >= fee.payment_deadline_final.days: remove_member_of(user, config.member_group, processor, closedopen(ts_now, None)) log_user_event("Mitgliedschaftsende wegen Zahlungsrückstand ({})" .format(fee.name), processor, user) users_membership_terminated.append(user) return users_pid_membership, users_membership_terminated
def handle_payments_in_default(): processor = User.q.get(0) # Add memberships and end "member" membership if threshold met users = User.q.join(User.current_properties)\ .filter(CurrentProperty.property_name == 'membership_fee') \ .join(Account).filter(Account.balance > 0).all() users_pid_membership = [] users_membership_terminated = [] ts_now = session.utcnow() for user in users: last_pid_membership = Membership.q.filter(Membership.user_id == user.id) \ .filter(Membership.group_id == config.payment_in_default_group.id) \ .order_by(Membership.ends_at.desc()) \ .first() if last_pid_membership is not None: if last_pid_membership.ends_at is not None and \ last_pid_membership.ends_at >= ts_now - timedelta(days=7): continue in_default_days = user.account.in_default_days try: fee = get_membership_fee_for_date(date.today() - timedelta(days=in_default_days)) except NoResultFound: fee = get_last_applied_membership_fee() if not fee: return [], [] if not user.has_property('payment_in_default'): if in_default_days >= fee.payment_deadline.days: make_member_of(user, config.payment_in_default_group, processor, closed(ts_now, None)) users_pid_membership.append(user) if in_default_days >= fee.payment_deadline_final.days: remove_member_of(user, config.member_group, processor, closedopen(ts_now, None)) log_user_event( "Mitgliedschaftsende wegen Zahlungsrückstand ({})".format( fee.name), processor, user) users_membership_terminated.append(user) return users_pid_membership, users_membership_terminated
def move_out(user, comment, processor, when, end_membership=True): """Move out a user and may terminate relevant memberships. The user's room is set to ``None`` and all hosts are deleted. Memberships in :py:obj:`config.member_group` and :py:obj:`config.member_group` are terminated. A log message is created including the number of deleted hosts. :param User user: The user to move out. :param unicode|None comment: An optional comment :param User processor: The admin who is going to move out the user. :param datetime when: The time the user is going to move out. :param bool end_membership: Ends membership if true :return: The user that moved out. """ if when > session.utcnow(): raise NotImplementedError( "Moving out in the future is not supported yet.") if end_membership: for group in ({ config.member_group, config.network_access_group, config.external_group } | set(user.traffic_groups)): remove_member_of(user, group, processor, closedopen(when, None)) user.birthdate = None num_hosts = 0 # In case the chain is empty for num_hosts, h in enumerate(user.hosts, 1): session.session.delete(h) user.room = None if comment: message = deferred_gettext( u"Moved out: ({} hosts deleted). Comment: {}").format( num_hosts, comment) else: message = deferred_gettext(u"Moved out: ({} hosts deleted).").format( num_hosts) log_user_event(message=message.to_json(), author=processor, user=user) return user
def setup_traffic_group(user, processor, custom_group_id=None, terminate_other=False): """Add a user to a default or custom traffic group If neither a custom group is given, nor the corresponding building has a default traffic group, no membership is added. Group removal is executed independent of the latter. :param User user: the user :param User processor: the processor :param int custom_group_id: the id of a custom traffic group. if ``None``, the traffic group of the building is used. :param bool terminate_other: Whether to terminate current :py:cls:`TrafficGroup` memberships. Defaults to ``False`` """ now = session.utcnow() if terminate_other: for group in user.traffic_groups: remove_member_of(user, group, processor, closedopen(now, None)) traffic_group = determine_traffic_group(user, custom_group_id) if traffic_group is not None: make_member_of(user, traffic_group, processor, closedopen(now, None))
def end_membership(user_id, membership_id): user = get_user_or_404(user_id) membership = Membership.q.get(membership_id) if membership is None: flash( u"Gruppenmitgliedschaft mit ID {} existiert nicht!".format( membership.id), 'error') abort(404) if membership.user.id != user_id: flash( u"Gruppenmitgliedschaft {} gehört nicht zu Nutzer {}!".format( membership.id, user_id), 'error') return abort(404) remove_member_of(user, membership.group, current_user, closedopen(session.utcnow(), None)) session.session.commit() flash(u'Mitgliedschaft in Gruppe beendet', 'success') return redirect( url_for(".user_show", user_id=membership.user_id, _anchor='groups'))
def remove_membership(self, during=UnboundedInterval): remove_member_of(self.user, self.group, self.processor, during) session.session.commit()
def move_out(user, comment, processor, when, end_membership=True): """Move out a user and may terminate relevant memberships. The user's room is set to ``None`` and all hosts are deleted. Memberships in :py:obj:`config.member_group` and :py:obj:`config.member_group` are terminated. A log message is created including the number of deleted hosts. :param User user: The user to move out. :param unicode|None comment: An optional comment :param User processor: The admin who is going to move out the user. :param datetime when: The time the user is going to move out. :param bool end_membership: Ends membership if true :return: The user that moved out. """ if when > session.utcnow(): return schedule_user_task(task_type=TaskType.USER_MOVE_OUT, due=when, user=user, parameters={'comment': comment, 'end_membership': end_membership}, processor=processor) else: if end_membership: for group in {config.member_group, config.external_group, config.cache_group, config.network_access_group}: if user.member_of(group): remove_member_of(user, group, processor, closedopen(when, None)) user.birthdate = None deleted_interfaces = list() num_hosts = 0 for num_hosts, h in enumerate(user.hosts, 1): if not h.switch and (h.room == user.room or end_membership): for interface in h.interfaces: deleted_interfaces.append(interface.mac) session.session.delete(h) message = None if user.room is not None: message = u"Moved out of {room}: Deleted interfaces {interfaces} of {num_hosts} hosts."\ .format(room=user.room.short_name, num_hosts=num_hosts, interfaces=', '.join(deleted_interfaces)) had_custom_address = user.has_custom_address user.room = None else: if num_hosts: message = u"Deleted interfaces {interfaces} of {num_hosts} hosts." \ .format(num_hosts=num_hosts, interfaces=', '.join(deleted_interfaces)) if message is not None: if comment: message += u"\nComment: {}".format(comment) log_user_event( message=deferred_gettext(message).to_json(), author=processor, user=user ) return user
def move_in(user, building_id, level, room_number, mac, processor, birthdate=None, host_annex=False, begin_membership=True, when=None): """Move in a user in a given room and do some initialization. The user is given a new Host with an interface of the given mac, a UnixAccount, a finance Account, and is made member of important groups. Networking is set up. :param User user: The user to move in :param int building_id: :param int level: :param str room_number: :param str mac: The mac address of the users pc. :param User processor: :param Date birthdate: Date of birth` :param bool host_annex: when true: if MAC already in use, annex host to new user :param bool begin_membership: Starts a membership if true :param datetime when: The date at which the user should be moved in :return: The user object. """ if when and when > session.utcnow(): return schedule_user_task(task_type=TaskType.USER_MOVE_IN, due=when, user=user, parameters={'building_id': building_id, 'level': level, 'room_number': room_number, 'mac': mac, 'birthdate': birthdate, 'host_annex': host_annex, 'begin_membership': begin_membership}, processor=processor) else: room = get_room(building_id, level, room_number) if birthdate: user.birthdate = birthdate if begin_membership: if user.member_of(config.external_group): remove_member_of(user, config.external_group, processor, closedopen(session.utcnow(), None)) for group in {config.member_group, config.network_access_group}: if not user.member_of(group): make_member_of(user, group, processor, closed(session.utcnow(), None)) if room: user.room = room user.address = room.address if mac and user.birthdate: interface_existing = Interface.q.filter_by(mac=mac).first() if interface_existing is not None: if host_annex: host_existing = interface_existing.host host_existing.owner_id = user.id session.session.add(host_existing) migrate_user_host(host_existing, user.room, processor) else: raise MacExistsException else: new_host = Host(owner=user, room=room) session.session.add(new_host) session.session.add(Interface(mac=mac, host=new_host)) setup_ipv4_networking(new_host) msg = deferred_gettext(u"Moved in: {room}") log_user_event(author=processor, message=msg.format(room=room.short_name).to_json(), user=user) return user
def move_in(user, building, level, room_number, mac, processor, birthdate=None, traffic_group_id=None, host_annex=False, begin_membership=True): """Create a new user in a given room and do some initialization. The user is given a new Host with an interface of the given mac, a UnixAccount, a finance Account, and is made member of important groups. Networking is set up. :param User user: The user to move in :param Building building: See :py:func:`create_member` :param int level: See :py:func:`create_member` :param str room_number: See :py:func:`create_member` :param str mac: The mac address of the users pc. :param User processor: See :py:func:`create_member :param Date birthdate: Date of birth` :param int traffic_group_id: the id of the chosen traffic group to be used instead of the building's default one. :param bool host_annex: when true: if MAC already in use, annex host to new user :param bool begin_membership: Starts a membership if true :return: The user object. """ room = Room.q.filter_by(number=room_number, level=level, building=building).one() user.room = room if birthdate: user.birthdate = birthdate if begin_membership: if user.member_of(config.external_group): remove_member_of(user, config.external_group, processor, closedopen(session.utcnow(), None)) for group in {config.member_group, config.network_access_group}: if not user.member_of(group): make_member_of(user, group, processor, closed(session.utcnow(), None)) interface_existing = Interface.q.filter_by(mac=mac).first() if interface_existing is not None: if host_annex: host_existing = interface_existing.host host_existing.owner_id = user.id session.session.add(host_existing) migrate_user_host(host_existing, user.room, processor) else: raise MacExistsException else: new_host = Host(owner=user, room=room) session.session.add(new_host) session.session.add(Interface(mac=mac, host=new_host)) setup_ipv4_networking(new_host) setup_traffic_group(user, processor, traffic_group_id) try: grant_initial_credit(user) except NoTrafficGroup as e: raise ValueError("User {} could not be assigned a traffic group. " "Please specify one manually.".format(user)) from e msg = deferred_gettext(u"Moved in: {dorm} {level}-{room}") log_user_event(author=processor, message=msg.format(dorm=building.short_name, level=level, room=room_number).to_json(), user=user) return user
def remove_membership(self, during=UnboundedInterval): remove_member_of(self.user, self.group, self.processor, during) session.session.commit()
def move_in(user: User, building_id: int, level: int, room_number: str, mac: str | None, processor: User | None = None, birthdate: date = None, host_annex: bool = False, begin_membership: bool = True, when: datetime | None = None): """Move in a user in a given room and do some initialization. The user is given a new Host with an interface of the given mac, a UnixAccount, a finance Account, and is made member of important groups. Networking is set up. :param User user: The user to move in :param building_id: :param level: :param room_number: :param mac: The mac address of the users pc. :param processor: :param birthdate: Date of birth` :param host_annex: when true: if MAC already in use, annex host to new user :param begin_membership: Starts a membership if true :param when: The date at which the user should be moved in :return: The user object. """ if when and when > session.utcnow(): task_params = UserMoveInParams(building_id=building_id, level=level, room_number=room_number, mac=mac, birthdate=birthdate, host_annex=host_annex, begin_membership=begin_membership) return schedule_user_task(task_type=TaskType.USER_MOVE_IN, due=when, user=user, parameters=task_params, processor=processor) if user.room is not None: raise ValueError("user is already living in a room.") room = get_room(building_id, level, room_number) if birthdate: user.birthdate = birthdate if begin_membership: for group in {config.external_group, config.pre_member_group}: if user.member_of(group): remove_member_of(user, group, processor, closedopen(session.utcnow(), None)) for group in {config.member_group, config.network_access_group}: if not user.member_of(group): make_member_of(user, group, processor, closed(session.utcnow(), None)) if room: user.room = room user.address = room.address if mac and user.birthdate: interface_existing = Interface.q.filter_by(mac=mac).first() if interface_existing is not None: if host_annex: host_existing = interface_existing.host host_existing.owner_id = user.id session.session.add(host_existing) migrate_user_host(host_existing, user.room, processor) else: raise MacExistsException else: new_host = Host(owner=user, room=room) session.session.add(new_host) session.session.add(Interface(mac=mac, host=new_host)) setup_ipv4_networking(new_host) user_send_mail(user, UserMovedInTemplate(), True) msg = deferred_gettext("Moved in: {room}") log_user_event(author=processor if processor is not None else user, message=msg.format(room=room.short_name).to_json(), user=user) return user