Esempio n. 1
0
File: demo.py Progetto: ttggaa/Y-VOD
def lesson():
    '''demo.lesson()'''
    if current_user.is_authenticated:
        return redirect(request.args.get('next') or current_user.index_url)
    mac_address = get_mac_address_from_ip(ip_address=request.headers\
        .get('X-Forwarded-For', request.remote_addr))
    if mac_address is None:
        flash('无法获取设备信息', category='error')
        return redirect(url_for('auth.login'))
    device = Device.query.filter_by(mac_address=mac_address).first()
    if device is None:
        flash('设备未授权(MAC地址:{})'.format(mac_address), category='error')
        return redirect(url_for('auth.login'))
    lesson_type = '体验课程'
    if not device.can_access_lesson_type(lesson_type=lesson_type):
        flash('该设备无法访问“{}”资源'.format(lesson_type), category='error')
        return redirect(url_for('auth.login'))
    lessons = Lesson.query\
        .join(LessonType, LessonType.id == Lesson.type_id)\
        .filter(LessonType.name == lesson_type)\
        .order_by(Lesson.id.asc())
    return minify(
        render_template('demo/lesson.html',
                        header=lesson_type,
                        lessons=lessons))
Esempio n. 2
0
File: auth.py Progetto: ttggaa/Y-VOD
def before_request():
    '''auth.before_request()'''
    if current_user.is_authenticated:
        current_user.ping()
        mac_address = get_mac_address_from_ip(ip_address=request.headers\
            .get('X-Forwarded-For', request.remote_addr))
        if mac_address is not None and mac_address != current_user.last_seen_mac:
            current_user.update_mac(mac_address=mac_address)
            add_user_log(user=current_user._get_current_object(),
                         event='请求访问(来源:{})'.format(
                             get_device_info(mac_address=mac_address,
                                             show_mac=True)),
                         category='access')
        db.session.commit()
Esempio n. 3
0
File: demo.py Progetto: ttggaa/Y-VOD
def video(id):
    '''demo.video(id)'''
    if current_user.is_authenticated:
        return redirect(request.args.get('next') or current_user.index_url)
    mac_address = get_mac_address_from_ip(ip_address=request.headers\
        .get('X-Forwarded-For', request.remote_addr))
    if mac_address is None:
        flash('无法获取设备信息', category='error')
        return redirect(url_for('auth.login'))
    device = Device.query.filter_by(mac_address=mac_address).first()
    if device is None:
        flash('设备未授权(MAC地址:{})'.format(mac_address), category='error')
        return redirect(url_for('auth.login'))
    video = Video.query.get_or_404(id)
    if not device.can_access_lesson_type(lesson_type=video.lesson.type):
        flash('该设备无法访问“{}”资源'.format(video.lesson.type.name), category='error')
        return redirect(url_for('auth.login'))
    return minify(render_template('demo/video.html', video=video))
Esempio n. 4
0
def demo_video(id):
    '''resource.demo_video(id)'''
    mac_address = get_mac_address_from_ip(ip_address=request.headers\
        .get('X-Forwarded-For', request.remote_addr))
    if mac_address is None:
        abort(403)
    device = Device.query.filter_by(mac_address=mac_address).first()
    if device is None:
        abort(403)
    if current_app.config['HLS_ENABLE']:
        abort(403)
    video = Video.query.get_or_404(id)
    if not device.can_access_lesson_type(lesson_type=video.lesson.type):
        abort(403)
    video_file = os.path.join(current_app.config['VIDEO_DIR'], video.file_name)
    if not os.path.exists(video_file):
        abort(404)
    if 'Range' in request.headers:
        return send_video_file(video_file=video_file, request=request)
    return send_file(video_file, mimetype='video/mp4')
Esempio n. 5
0
def y_gre_aw():
    '''study.y_gre_aw()'''
    mac_address = get_mac_address_from_ip(ip_address=request.headers\
        .get('X-Forwarded-For', request.remote_addr))
    if mac_address is None:
        flash('无法获取设备信息', category='error')
        return redirect(url_for('auth.login'))
    device = Device.query.filter_by(mac_address=mac_address).first()
    if device is None:
        flash('设备未授权(MAC地址:{})'.format(mac_address), category='error')
        return redirect(url_for('auth.login'))
    lesson_type = 'Y-GRE AW'
    if not device.can_access_lesson_type(lesson_type=lesson_type):
        flash('该设备无法访问“{}”资源'.format(lesson_type), category='error')
        return redirect(url_for('auth.login'))
    lessons = Lesson.query\
        .join(LessonType, LessonType.id == Lesson.type_id)\
        .filter(LessonType.name == lesson_type)\
        .order_by(Lesson.id.asc())
    return minify(
        render_template('study/lesson.html',
                        header=lesson_type,
                        lessons=lessons))
Esempio n. 6
0
File: auth.py Progetto: ttggaa/Y-VOD
def login():
    '''auth.login()'''
    if current_user.is_authenticated:
        return redirect(request.args.get('next') or current_user.index_url)
    form = LoginForm()
    if form.validate_on_submit():
        mac_address = get_mac_address_from_ip(ip_address=request.headers\
            .get('X-Forwarded-For', request.remote_addr))
        if mac_address is None:
            flash('无法获取设备信息', category='error')
            return redirect(
                url_for('auth.login', next=request.args.get('next')))
        device = Device.query.filter_by(mac_address=mac_address).first()
        if device is None:
            flash('设备未授权(MAC地址:{})'.format(mac_address), category='error')
            return redirect(
                url_for('auth.login', next=request.args.get('next')))
        # authenticate user via Y-System
        data = y_system_api_request(api='login-user',
                                    token_data={
                                        'email':
                                        form.email.data.strip().lower(),
                                        'password': form.password.data,
                                        'device': device.alias,
                                    })
        if data is None:
            flash('网络通信故障', category='error')
            return redirect(
                url_for('auth.login', next=request.args.get('next')))
        if verify_data_keys(data=data, keys=['error']):
            flash('登录失败:{}'.format(data.get('error')), category='error')
            return redirect(
                url_for('auth.login', next=request.args.get('next')))
        if not verify_data_keys(data=data, keys=['user_id']):
            flash('登录失败:用户信息无效', category='error')
            flash('初次登录时,请确认Y-System账号已经激活。', category='info')
            return redirect(
                url_for('auth.login', next=request.args.get('next')))
        user = User.query.get(data.get('user_id'))
        if user is None:
            # migrate user from Y-System
            data = y_system_api_request(api='migrate-user',
                                        token_data={
                                            'user_id': data.get('user_id'),
                                        })
            if data is None:
                flash('网络通信故障', category='error')
                return redirect(
                    url_for('auth.login', next=request.args.get('next')))
            if verify_data_keys(data=data, keys=['error']):
                flash('登录失败:{}'.format(data.get('error')), category='error')
                return redirect(
                    url_for('auth.login', next=request.args.get('next')))
            if not verify_data_keys(data=data,
                                    keys=['user_id', 'role', 'name']):
                flash('登录失败:用户信息无效', category='error')
                flash('初次登录时,请确认Y-System账号已经激活。', category='info')
                return redirect(
                    url_for('auth.login', next=request.args.get('next')))
            role = Role.query.filter_by(name=data.get('role')).first()
            if role is None:
                flash('登录失败:无效的用户角色“{}”'.format(data.get('role')),
                      category='error')
                return redirect(
                    url_for('auth.login', next=request.args.get('next')))
            user = User(id=data.get('user_id'),
                        role_id=role.id,
                        name=data.get('name'))
            db.session.add(user)
            db.session.commit()
            add_user_log(user=user, event='从Y-System导入用户信息', category='auth')
        if verify_data_keys(data=data, keys=['role', 'name']):
            if data.get('role') != user.role.name:
                role = Role.query.filter_by(name=data.get('role')).first()
                if role is not None:
                    user.role_id = role.id
                    db.session.add(user)
            if data.get('name') != user.name:
                user.name = data.get('name')
                db.session.add(user)
        if data.get('vb_progress') is not None:
            user.sync_punch(section=data.get('vb_progress'))
        if data.get('y_gre_progress') is not None:
            user.sync_punch(section=data.get('y_gre_progress'))
        if data.get('y_gre_aw_progress') is not None:
            user.sync_punch(section=data.get('y_gre_aw_progress'))
        login_user(user, remember=current_app.config['AUTH_REMEMBER_LOGIN'])
        add_user_log(user=user, event='登录系统', category='auth')
        db.session.commit()
        return redirect(request.args.get('next') or user.index_url)
    return minify(render_template('auth/login.html', form=form))