def current_user_invenio_profile(): """Controller to get current user profile""" if current_user.is_authenticated: profile = UserProfile.get_by_userid(current_user.get_id()) return { "name": getattr(profile, "full_name", None), "email": getattr(current_user, "email", None), "is_authenticated": True } return { "is_authenticated": False }
def authorize(name): ui_flag = session.pop('ui', None) client = current_auth.create_client(name) try: token = client.authorize_access_token() except HTTPException: return render_template( current_app.config['AUTHENTICATION_POPUP_TEMPLATE'], msg=f'Access not provided to {name} service.'), 400 configs = OAUTH_SERVICES.get(name.upper(), {}) extra_data_method = configs.get('extra_data_method') # TOFIX Add error handlers for reject, auth errors, etc extra_data = {} if extra_data_method: extra_data = extra_data_method(client, token) _token = _create_or_update_token(name, token) _token.extra_data = extra_data db.session.add(_token) # Add extra data to user profile. # If user profile doesn't exist yet, it creates one. _profile = UserProfile.get_by_userid(current_user.id) if not _profile: _profile = UserProfile(user_id=current_user.id) db.session.add(_profile) profile_data = get_oauth_profile(name, token=_token, client=client) if _profile.extra_data: profile_services = _profile.extra_data.get("services", {}) else: profile_services = {} profile_services[name] = profile_data _profile.extra_data = {"services": profile_services} flag_modified(_profile, "extra_data") db.session.commit() if ui_flag: return render_template( current_app.config['AUTHENTICATION_POPUP_TEMPLATE'], msg=f'Authorization to {name} succeeded.'), 302 else: return jsonify({"message": f"Authorization to {name} succeeded."}), 200
def disconnect(name): _profile = UserProfile.get_by_userid(current_user.id) _token = OAuth2Token.get(name=name, user_id=current_user.id) if _profile and _token: del _profile.extra_data['services'][name] flag_modified(_profile, "extra_data") db.session.delete(_token) db.session.commit() return jsonify( {'message': 'Disconnected from {} ' 'successfully.'.format(name)}), 200 else: abort(403, "Unable to disconnect from {} service.".format(name))
def authorize(name): ui_flag = session.pop('ui', None) client = current_auth.create_client(name) token = client.authorize_access_token() configs = OAUTH_SERVICES.get(name.upper(), {}) extra_data_method = configs.get('extra_data_method') # TOFIX Add error handlers for reject, auth errors, etc extra_data = {} if extra_data_method: extra_data = extra_data_method(client, token) _token = _create_or_update_token(name, token) _token.extra_data = extra_data db.session.add(_token) # Add extra data to user profile. # If user profile doesn't exist yet, it creates one. _profile = UserProfile.get_by_userid(current_user.id) if not _profile: _profile = UserProfile(user_id=current_user.id) db.session.add(_profile) profile_data = get_oauth_profile(name, token=_token, client=client) if _profile.extra_data: profile_services = _profile.extra_data.get("services", {}) else: profile_services = {} profile_services[name] = profile_data _profile.extra_data = {"services": profile_services} flag_modified(_profile, "extra_data") db.session.commit() if ui_flag: if current_app.config['DEBUG']: redirect_url = "http://localhost:3000/settings/auth/connect" else: redirect_url = "/settings/auth/connect" return redirect(redirect_url) else: return jsonify( {"message": "Authorization to {} succeeded".format(name)}), 200
def get_user(): """Return logged in user.""" deposit_groups = get_user_deposit_groups() profile = UserProfile.get_by_userid(current_user.id) extra_data = {} if profile: extra_data = profile.extra_data _user = { "id": current_user.id, "email": current_user.email, "deposit_groups": deposit_groups, "profile": extra_data } response = jsonify(_user) response.status_code = 200 return response
def get_user(): """Return logged in user.""" deposit_groups = get_user_deposit_groups() profile = UserProfile.get_by_userid(current_user.id) extra_data = profile.extra_data if profile else {} cern_profile = get_remote_account_by_id(current_user.id)['profile'] if cern_profile: extra_data['cern'] = cern_profile _user = { "id": current_user.id, "email": current_user.email, "deposit_groups": deposit_groups, "profile": extra_data } response = jsonify(_user) response.status_code = 200 return response
def get_patron_activity(patron_pid): """Get activity related to the given patron pid.""" if patron_pid is None: raise ValueError("No patron pid was provided.") patron = get_patron_or_unknown_dump(patron_pid) def dump(search): return [hit.to_dict() for hit in search.scan()] DocumentRequestSearch = current_app_ils.document_request_search_cls patron_document_requests = dump( DocumentRequestSearch().search_by_patron_pid(patron_pid) ) BorrowingRequestsSearch = current_ils_ill.borrowing_request_search_cls patron_borrowing_requests = dump( BorrowingRequestsSearch().search_by_patron_pid(patron_pid) ) OrderSearch = current_ils_acq.order_search_cls patron_acquisitions = dump(OrderSearch().search_by_patron_pid(patron_pid)) patron_loans = dump(get_loans_by_patron_pid(patron_pid)) patron_profile = UserProfile.get_by_userid(patron_pid).__dict__ del patron_profile["_sa_instance_state"] patron_data = { "patron": patron, "profile": patron_profile, "document_requests": patron_document_requests, "borrowing_requests": patron_borrowing_requests, "acquisitions": patron_acquisitions, "loans": patron_loans, } return patron_data