def _defaults(cls, user, course=None): caliper_actor = CaliperActor.generate_actor(user) if impersonation.is_impersonating() and user.id == current_user.id: caliper_actor = CaliperActor.generate_actor(impersonation.get_impersonation_original_user()) defaults = { 'context': CaliperSensor._version, 'eventTime': LearningRecord.generate_timestamp(), 'actor': caliper_actor, 'edApp': ResourceIRI.compair() } session = CaliperEntities.session(caliper_actor) if request: session.setdefault("extensions", {}).setdefault("browser-info", {}) if request.environ.get('HTTP_USER_AGENT'): session["extensions"]["browser-info"]["userAgent"] = request.environ.get('HTTP_USER_AGENT') if request.environ.get('HTTP_REFERER'): session["extensions"]["browser-info"]["referer"] = request.environ.get('HTTP_REFERER') if impersonation.is_impersonating() and user.id == current_user.id: session["extensions"]["impersonating-as"] = CaliperActor.generate_actor(user) defaults['session'] = session if course: defaults['membership'] = CaliperEntities.membership(course, user) return defaults
def _defaults(cls, user, course=None): caliper_actor = CaliperActor.generate_actor(user) if impersonation.is_impersonating() and user.id == current_user.id: caliper_actor = CaliperActor.generate_actor( impersonation.get_impersonation_original_user()) defaults = { 'context': CaliperSensor._version, 'eventTime': LearningRecord.generate_timestamp(), 'actor': caliper_actor, 'edApp': ResourceIRI.compair() } session = CaliperEntities.session(caliper_actor) if request: session.setdefault("extensions", {}).setdefault("browser-info", {}) if request.environ.get('HTTP_USER_AGENT'): session["extensions"]["browser-info"][ "userAgent"] = request.environ.get('HTTP_USER_AGENT') if request.environ.get('HTTP_REFERER'): session["extensions"]["browser-info"][ "referer"] = request.environ.get('HTTP_REFERER') if impersonation.is_impersonating() and user.id == current_user.id: session["extensions"][ "impersonating-as"] = CaliperActor.generate_actor(user) defaults['session'] = session if course: defaults['membership'] = CaliperEntities.membership(course, user) return defaults
def _add_default(cls, user, statement): if not statement.timestamp: statement.timestamp = LearningRecord.generate_timestamp() if impersonation.is_impersonating() and user.id == current_user.id: statement.actor = XAPIActor.generate_actor( impersonation.get_impersonation_original_user()) else: statement.actor = XAPIActor.generate_actor(user) # add default context info if not statement.context: statement.context = Context() if not statement.context.context_activities: statement.context.context_activities = ContextActivities() if not statement.context.context_activities.category: statement.context.context_activities.category = ActivityList() statement.context.context_activities.category.append( XAPIActivity.compair_source()) statement.context.platform = ResourceIRI.compair() if not statement.context.extensions: statement.context.extensions = Extensions() statement.context.extensions[ 'http://id.tincanapi.com/extension/session-info'] = { 'id': ResourceIRI.user_session(sess.get('session_id', '')), 'start_at': sess.get('start_at'), 'login_method': sess.get('login_method'), } if sess.get('end_at'): statement.context.extensions[ 'http://id.tincanapi.com/extension/session-info'][ 'end_at'] = sess.get('end_at') if impersonation.is_impersonating() and user.id == current_user.id: statement.context.extensions[ 'http://id.tincanapi.com/extension/session-info'][ 'impersonating-as'] = XAPIActor.generate_actor( user).as_version() statement.context.extensions[ 'http://id.tincanapi.com/extension/browser-info'] = {} if request and request.environ.get('HTTP_USER_AGENT'): statement.context.extensions[ 'http://id.tincanapi.com/extension/browser-info'][ 'user-agent'] = request.environ.get('HTTP_USER_AGENT') if request and request.environ.get('HTTP_REFERER'): statement.context.extensions[ 'http://id.tincanapi.com/extension/browser-info'][ 'referer'] = request.environ.get('HTTP_REFERER') return statement
def _add_default(cls, user, statement): if not statement.timestamp: statement.timestamp = datetime.datetime.now().replace( tzinfo=pytz.utc).isoformat() if impersonation.is_impersonating() and user.id == current_user.id: statement.actor = XAPIActor.generate_actor( impersonation.get_impersonation_original_user()) else: statement.actor = XAPIActor.generate_actor(user) # add default context info if not statement.context: statement.context = Context() if not statement.context.context_activities: statement.context.context_activities = ContextActivities() if not statement.context.context_activities.category: statement.context.context_activities.category = ActivityList() statement.context.context_activities.category.append( XAPIActivity.compair_source()) statement.context.platform = XAPIResourceIRI.compair() if request and request.environ.get('HTTP_USER_AGENT'): if not statement.context.extensions: statement.context.extensions = Extensions() browser_info_key = XAPIExtension.context_extensions.get( 'browser information') statement.context.extensions[ browser_info_key] = request.environ.get('HTTP_USER_AGENT') if request and request.environ.get('HTTP_REFERER'): if not statement.context.extensions: statement.context.extensions = Extensions() referer_key = XAPIExtension.context_extensions.get('referer') statement.context.extensions[referer_key] = request.environ.get( 'HTTP_REFERER') if impersonation.is_impersonating() and user.id == current_user.id: if not statement.context.extensions: statement.context.extensions = Extensions() impersonating_as_key = XAPIExtension.context_extensions.get( 'impersonating as') statement.context.extensions[ impersonating_as_key] = XAPIActor.generate_actor(user) return statement
def delete(self): if not current_app.config.get( 'IMPERSONATION_ENABLED', False) or not impersonation.is_impersonating(): abort(404, title="Cannot stop impersonation", message="Sorry, you can't perform that action") original_user_id = impersonation.get_impersonation_original_user_id() impersonate_as_user_id = impersonation.get_impersonation_act_as_user_id( ) original_user = User.query.get(original_user_id) impersonate_as_user = User.query.get(impersonate_as_user_id) if original_user is None or impersonate_as_user is None: abort(404, title="Cannot stop impersonation", \ message="Cannot find corresponding information to stop impersonation") on_impersonation_stopped.send( self, event_name=on_impersonation_stopped.name, user=original_user, data={'impersonate_as': impersonate_as_user.id}) impersonation.end_impersonation() return {'success': True}
def _add_default(cls, user, statement): if not statement.timestamp: statement.timestamp = LearningRecord.generate_timestamp() if impersonation.is_impersonating() and user.id == current_user.id: statement.actor = XAPIActor.generate_actor(impersonation.get_impersonation_original_user()) else: statement.actor = XAPIActor.generate_actor(user) # add default context info if not statement.context: statement.context = Context() if not statement.context.context_activities: statement.context.context_activities = ContextActivities() if not statement.context.context_activities.category: statement.context.context_activities.category = ActivityList() statement.context.context_activities.category.append( XAPIActivity.compair_source() ) statement.context.platform = ResourceIRI.compair() if not statement.context.extensions: statement.context.extensions = Extensions() statement.context.extensions['http://id.tincanapi.com/extension/session-info'] = { 'id': ResourceIRI.user_session(sess.get('session_id', '')), 'start_at': sess.get('start_at'), 'login_method': sess.get('login_method'), } if sess.get('end_at'): statement.context.extensions['http://id.tincanapi.com/extension/session-info']['end_at'] = sess.get('end_at') if impersonation.is_impersonating() and user.id == current_user.id: statement.context.extensions['http://id.tincanapi.com/extension/session-info']['impersonating-as'] = XAPIActor.generate_actor(user) statement.context.extensions['http://id.tincanapi.com/extension/browser-info'] = {} if request and request.environ.get('HTTP_USER_AGENT'): statement.context.extensions['http://id.tincanapi.com/extension/browser-info']['user-agent'] = request.environ.get('HTTP_USER_AGENT') if request and request.environ.get('HTTP_REFERER'): statement.context.extensions['http://id.tincanapi.com/extension/browser-info']['referer'] = request.environ.get('HTTP_REFERER') return statement
def marshal_user_data(user): if impersonation.is_impersonating() and current_user.id == user.id: # when retrieving the profile of the student being impersonated, # don't include full profile (i.e. no email) return marshal(user, dataformat.get_user(False)) elif allow(MANAGE, user) or current_user.id == user.id: return marshal(user, dataformat.get_full_user()) else: return marshal(user, dataformat.get_user(is_user_access_restricted(user)))
def marshal_user_data(user): if impersonation.is_impersonating() and current_user.id == user.id: # when retrieving the profile of the student being impersonated, # don't include full profile (i.e. no email) return marshal(user, dataformat.get_user(False)) elif can(MANAGE, user) or current_user.id == user.id: return marshal(user, dataformat.get_full_user()) else: return marshal(user, dataformat.get_user(is_user_access_restricted(user)))
def _defaults(cls, user, course=None): caliper_actor = CaliperActor.generate_actor(user) if impersonation.is_impersonating() and user.id == current_user.id: caliper_actor = CaliperActor.generate_actor(impersonation.get_impersonation_original_user()) defaults = { 'context': CaliperSensor._core_context, 'eventTime': LearningRecord.generate_timestamp(), 'actor': caliper_actor, 'edApp': CaliperEntities.compair_app() } session_extensions = {} if impersonation.is_impersonating() and user.id == current_user.id: session_extensions["impersonating-as"] = CaliperActor.generate_actor(user).as_dict() defaults['session'] = CaliperEntities.session(caliper_actor, request, session_extensions) if course: #todo add 'Group' which is the course or the group within the course defaults['membership'] = CaliperEntities.membership(course, user) return defaults
def get(self): if not current_app.config.get('IMPERSONATION_ENABLED', False) or not impersonation.is_impersonating(): return {'impersonating' : False} original_user = impersonation.get_impersonation_original_user() impersonate_as_user = current_user if original_user is None or impersonate_as_user is None: # something went wrong... abort(503, title="Cannot check impersonation status", \ message="Sorry, cannot find information on impersonation status") # user is impersonating. treat as restrict_user when calling dataformat return { \ 'impersonating' : True, 'original_user' : marshal(original_user, dataformat.get_user(True)) }
def _checkImpersonation(): if not current_app.config.get( 'IMPERSONATION_ENABLED', False) or not impersonation.is_impersonating(): return {'impersonating': False} original_user = impersonation.get_impersonation_original_user() impersonate_as_user = current_user if original_user is None or impersonate_as_user is None: # something went wrong... abort(503, title="Cannot check impersonation status", \ message="Sorry, cannot find information on impersonation status") # user is impersonating. treat as restrict_user when calling dataformat return { \ 'impersonating' : True, 'original_user' : marshal(original_user, dataformat.get_user(True)) }
def delete(self): if not current_app.config.get('IMPERSONATION_ENABLED', False) or not impersonation.is_impersonating(): abort(404, title="Cannot stop impersonation", message="Sorry, you can't perform that action") original_user_id = impersonation.get_impersonation_original_user_id() impersonate_as_user_id = impersonation.get_impersonation_act_as_user_id() original_user = User.query.get(original_user_id) impersonate_as_user = User.query.get(impersonate_as_user_id) if original_user is None or impersonate_as_user is None: abort(404, title="Cannot stop impersonation", \ message="Cannot find corresponding information to stop impersonation") on_impersonation_stopped.send( self, event_name=on_impersonation_stopped.name, user=original_user, data={ 'impersonate_as': impersonate_as_user.id }) impersonation.end_impersonation() return {'success': True}