コード例 #1
0
def group_by_id_put(group_id: str) -> Response:
    """
    Update a group based on the unique group id.
    :param group_id: Unique id which identifies a group.
    :return: A response object for the PUT API request.
    """
    old_group = GroupDao.get_group_by_id(int(group_id))

    jwt_claims: dict = get_claims(request)
    jwt_username = jwt_claims.get('sub')

    group_member: GroupMember = GroupMemberDao.get_group_member(
        group_id=int(group_id), username=jwt_username)

    if group_member is not None and group_member.user == 'admin' and group_member.status == 'accepted':
        current_app.logger.info(
            f'Admin user {jwt_username} is updating a group with id {group_id}.'
        )
    else:
        current_app.logger.info(
            f'User {jwt_username} is not authorized to update a group with id {group_id}.'
        )
        response = jsonify({
            'self':
            f'/v2/groups/{group_id}',
            'updated':
            False,
            'group':
            None,
            'error':
            f'User {jwt_username} is not authorized to update a group with id {group_id}.'
        })
        response.status_code = 400
        return response

    if old_group is None:
        response = jsonify({
            'self': f'/v2/groups/{group_id}',
            'updated': False,
            'group': None,
            'error': 'There is no existing group with this id.'
        })
        response.status_code = 400
        return response

    group_data: dict = request.get_json()
    new_group = Group(group_data)

    if new_group != old_group:
        new_group.modified_date = datetime.now()
        new_group.modified_app = 'saints-xctf-api'

        is_updated = GroupDao.update_group(group=new_group)

        if is_updated:
            updated_group = GroupDao.get_group_by_id(int(group_id))
            updated_group_dict: dict = GroupData(updated_group).__dict__

            response = jsonify({
                'self': f'/v2/groups/{group_id}',
                'updated': True,
                'group': updated_group_dict
            })
            response.status_code = 200
            return response
        else:
            response = jsonify({
                'self': f'/v2/groups/{group_id}',
                'updated': False,
                'group': None,
                'error': 'The group failed to update.'
            })
            response.status_code = 500
            return response
    else:
        response = jsonify({
            'self':
            f'/v2/groups/{group_id}',
            'updated':
            False,
            'group':
            None,
            'error':
            'The group submitted is equal to the existing group with the same id.'
        })
        response.status_code = 400
        return response
コード例 #2
0
def group_by_group_name_put(team_name: str, group_name: str) -> Response:
    """
    Update a group in the database.
    :param team_name: Unique name which identifies a team.
    :param group_name: Unique name which identifies a group within a team.
    :return: A response object for the PUT API request.
    """
    old_group_row: Optional[RowProxy] = GroupDao.get_group(
        team_name=team_name, group_name=group_name)

    if old_group_row is None:
        response = jsonify({
            'self': f'/v2/groups/{team_name}/{group_name}',
            'updated': False,
            'group': None,
            'error': 'there is no existing group with this name'
        })
        response.status_code = 400
        return response

    jwt_claims: dict = get_claims(request)
    jwt_username = jwt_claims.get('sub')

    group_member: GroupMember = GroupMemberDao.get_group_member_by_group_name(
        team_name=team_name, group_name=group_name, username=jwt_username)

    if group_member is not None and group_member.user == 'admin' and group_member.status == 'accepted':
        current_app.logger.info(
            f'Admin user {jwt_username} is updating a group with name {group_name} in team {team_name}.'
        )
    else:
        current_app.logger.info(
            f'User {jwt_username} is not authorized to update a group with name {group_name} in team {team_name}.'
        )
        response = jsonify({
            'self':
            f'/v2/groups/{team_name}/{group_name}',
            'updated':
            False,
            'group':
            None,
            'error':
            f'User {jwt_username} is not authorized to update a group with name {group_name} in team '
            f'{team_name}.'
        })
        response.status_code = 400
        return response

    old_group_dict = {key: value for key, value in old_group_row.items()}
    old_group = Group(old_group_dict)
    group_data: dict = request.get_json()
    new_group = Group(group_data)

    if old_group != new_group:

        new_group.modified_date = datetime.now()
        new_group.modified_app = 'saints-xctf-api'

        is_updated = GroupDao.update_group(group=new_group)

        if is_updated:
            updated_group_row: Optional[RowProxy] = GroupDao.get_group(
                team_name=team_name, group_name=new_group.group_name)

            updated_group_dict = {
                key: value
                for key, value in updated_group_row.items()
            }
            response = jsonify({
                'self': f'/v2/groups/{team_name}/{group_name}',
                'updated': True,
                'group': updated_group_dict
            })
            response.status_code = 200
            return response
        else:
            response = jsonify({
                'self': f'/v2/groups/{team_name}/{group_name}',
                'updated': False,
                'group': None,
                'error': 'the group failed to update'
            })
            response.status_code = 500
            return response
    else:
        response = jsonify({
            'self':
            f'/v2/groups/{team_name}/{group_name}',
            'updated':
            False,
            'group':
            None,
            'error':
            'the group submitted is equal to the existing group with the same name'
        })
        response.status_code = 400
        return response