def status_query(): now = single(session.utcnow()) return session.session.query( User, User.member_of(config.member_group, now).label('member'), (Account.balance <= 0).label('account_balanced'), # a User.properties hybrid attribute would be preferrable (User.has_property('network_access', now)).label('network_access'), (User.has_property('violation', now)).label('violation'), (User.has_property('ldap', now)).label('ldap'), or_(*(User.has_property(prop, now) for prop in admin_properties)).label('admin') ).join(Account)
def merge_member_request(user: User, prm: PreMember, merge_name: bool, merge_email: bool, merge_person_id: bool, merge_room: bool, merge_password: bool, merge_birthdate: bool, processor: User): if prm.move_in_date is not None and prm.move_in_date < session.utcnow( ).date(): prm.move_in_date = session.utcnow().date() if merge_name: user = edit_name(user, prm.name, processor) if merge_email: user = edit_email(user, prm.email, user.email_forwarded, processor, is_confirmed=prm.email_confirmed) if merge_person_id: user = edit_person_id(user, prm.swdd_person_id, processor) move_in_datetime = datetime.combine(prm.move_in_date, utc.time_min()) if merge_room: if prm.room: if user.room: move(user, prm.room.building_id, prm.room.level, prm.room.number, processor=processor, when=move_in_datetime) else: move_in(user, prm.room.building_id, prm.room.level, prm.room.number, mac=None, processor=processor, when=move_in_datetime) if not user.member_of(config.member_group): make_member_of(user, config.member_group, processor, closed(move_in_datetime, None)) if merge_birthdate: user = edit_birthdate(user, prm.birthdate, processor) log_msg = "Merged information from registration {}." if merge_password: user.passwd_hash = prm.passwd_hash log_msg += " Password overridden." else: log_msg += " Kept old password." log_user_event( deferred_gettext(log_msg).format(encode_type2_user_id( prm.id)).to_json(), processor, user) session.session.delete(prm)
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