def update_user_view(request): """ Update user information by user name. """ user_name = ar.get_value_matchdict_checked(request, key="user_name") ax.verify_param( user_name, paramCompare=get_constant("MAGPIE_LOGGED_USER"), notEqual=True, httpError=HTTPBadRequest, paramName="user_name", content={u"user_name": user_name}, msgOnFail=s.Service_PUT_BadRequestResponseSchema_ReservedKeyword. description) user = ar.get_user_matchdict_checked(request, user_name_key="user_name") new_user_name = ar.get_multiformat_post(request, "user_name", default=user.user_name) new_email = ar.get_multiformat_post(request, "email", default=user.email) new_password = ar.get_multiformat_post(request, "password", default=user.user_password) uu.check_user_info(new_user_name, new_email, new_password, group_name=new_user_name) update_username = user.user_name != new_user_name update_password = user.user_password != new_password update_email = user.email != new_email ax.verify_param(any([update_username, update_password, update_email]), isTrue=True, httpError=HTTPBadRequest, content={u"user_name": user.user_name}, msgOnFail=s.User_PUT_BadRequestResponseSchema.description) if user.user_name != new_user_name: existing_user = ax.evaluate_call( lambda: UserService.by_user_name(new_user_name, db_session=request.db), fallback=lambda: request.db.rollback(), httpError=HTTPForbidden, msgOnFail=s.User_PUT_ForbiddenResponseSchema.description) ax.verify_param( existing_user, isNone=True, httpError=HTTPConflict, msgOnFail=s.User_PUT_ConflictResponseSchema.description) user.user_name = new_user_name if user.email != new_email: user.email = new_email if user.user_password != new_password and new_password is not None: UserService.set_password(user, new_password) UserService.regenerate_security_code(user) return ax.valid_http(httpSuccess=HTTPOk, detail=s.Users_PUT_OkResponseSchema.description)
def create_user_view(request): """ Create a new user. """ user_name = ar.get_multiformat_post(request, "user_name") email = ar.get_multiformat_post(request, "email") password = ar.get_multiformat_post(request, "password") group_name = ar.get_multiformat_post(request, "group_name") uu.check_user_info(user_name, email, password, group_name) return uu.create_user(user_name, password, email, group_name, db_session=request.db)
def sign_in(request): """ Signs in a user session. """ provider_name = get_value_multiformat_post_checked(request, "provider_name", default=MAGPIE_DEFAULT_PROVIDER) provider_name = provider_name.lower() user_name = get_value_multiformat_post_checked(request, "user_name") password = get_multiformat_post(request, "password") # no check since password is None for external login verify_provider(provider_name) if provider_name in MAGPIE_INTERNAL_PROVIDERS.keys(): # obtain the raw path, without any '/magpie' prefix (if any), let 'application_url' handle it signin_internal_path = request.route_url("ziggurat.routes.sign_in", _app_url="") signin_internal_data = {u"user_name": user_name, u"password": password, u"provider_name": provider_name} signin_sub_request = Request.blank(signin_internal_path, base_url=request.application_url, headers={"Accept": CONTENT_TYPE_JSON}, POST=signin_internal_data) signin_response = request.invoke_subrequest(signin_sub_request, use_tweens=True) if signin_response.status_code == HTTPOk.code: return convert_response(signin_response) login_failure(request, s.Signin_POST_UnauthorizedResponseSchema.description) elif provider_name in MAGPIE_EXTERNAL_PROVIDERS.keys(): return ax.evaluate_call(lambda: process_sign_in_external(request, user_name, provider_name), httpError=HTTPInternalServerError, content={u"user_name": user_name, u"provider_name": provider_name}, msgOnFail=s.Signin_POST_External_InternalServerErrorResponseSchema.description)
def register_service_view(request): """ Registers a new service. """ service_name = ar.get_value_multiformat_post_checked( request, "service_name") service_url = ar.get_value_multiformat_post_checked(request, "service_url") service_type = ar.get_value_multiformat_post_checked( request, "service_type") service_push = asbool(ar.get_multiformat_post(request, "service_push")) ax.verify_param( service_type, isIn=True, paramCompare=SERVICE_TYPE_DICT.keys(), httpError=HTTPBadRequest, msgOnFail=s.Services_POST_BadRequestResponseSchema.description) ax.verify_param( models.Service.by_service_name(service_name, db_session=request.db), isNone=True, httpError=HTTPConflict, msgOnFail=s.Services_POST_ConflictResponseSchema.description, content={u"service_name": str(service_name)}, paramName=u"service_name") return su.create_service(service_name, service_type, service_url, service_push, db_session=request.db)
def create_service_direct_resource_view(request): """ Register a new resource directly under a service. """ service = ar.get_service_matchdict_checked(request) resource_name = ar.get_multiformat_post(request, "resource_name") resource_display_name = ar.get_multiformat_post(request, "resource_display_name", default=resource_name) resource_type = ar.get_multiformat_post(request, "resource_type") parent_id = ar.get_multiformat_post( request, "parent_id") # no check because None/empty is allowed if not parent_id: parent_id = service.resource_id return create_resource(resource_name, resource_display_name, resource_type, parent_id=parent_id, db_session=request.db)
def delete_resource(request): resource = ar.get_resource_matchdict_checked(request) service_push = asbool(ar.get_multiformat_post(request, "service_push")) res_content = {u"resource": format_resource(resource, basic_info=True)} ax.evaluate_call( lambda: models.resource_tree_service.delete_branch(resource_id=resource.resource_id, db_session=request.db), fallback=lambda: request.db.rollback(), httpError=HTTPForbidden, msgOnFail="Delete resource branch from tree service failed.", content=res_content ) def remove_service_magpie_and_phoenix(res, svc_push, db): if res.resource_type != "service": svc_push = False db.delete(res) if svc_push: sync_services_phoenix(db.query(models.Service)) ax.evaluate_call(lambda: remove_service_magpie_and_phoenix(resource, service_push, request.db), fallback=lambda: request.db.rollback(), httpError=HTTPForbidden, msgOnFail=s.Resource_DELETE_ForbiddenResponseSchema.description, content=res_content) return ax.valid_http(httpSuccess=HTTPOk, detail=s.Resource_DELETE_OkResponseSchema.description)
def update_resource(request): """ Update a resource information. """ resource = ar.get_resource_matchdict_checked(request, "resource_id") service_push = asbool(ar.get_multiformat_post(request, "service_push")) res_old_name = resource.resource_name res_new_name = ar.get_value_multiformat_post_checked(request, "resource_name") def rename_service_magpie_and_phoenix(res, new_name, svc_push, db): if res.resource_type != "service": svc_push = False res.resource_name = new_name if svc_push: sync_services_phoenix(db.query(models.Service)) ax.evaluate_call(lambda: rename_service_magpie_and_phoenix(resource, res_new_name, service_push, request.db), fallback=lambda: request.db.rollback(), httpError=HTTPForbidden, msgOnFail=s.Resource_PUT_ForbiddenResponseSchema.description, content={u"resource_id": resource.resource_id, u"resource_name": resource.resource_name, u"old_resource_name": res_old_name, u"new_resource_name": res_new_name}) return ax.valid_http(httpSuccess=HTTPOk, detail=s.Resource_PUT_OkResponseSchema.description, content={u"resource_id": resource.resource_id, u"resource_name": resource.resource_name, u"old_resource_name": res_old_name, u"new_resource_name": res_new_name})
def update_service_view(request): """ Update a service information. """ service = ar.get_service_matchdict_checked(request) service_push = asbool( ar.get_multiformat_post(request, "service_push", default=False)) def select_update(new_value, old_value): return new_value if new_value is not None and not new_value == "" else old_value # None/Empty values are accepted in case of unspecified svc_name = select_update(ar.get_multiformat_post(request, "service_name"), service.resource_name) svc_url = select_update(ar.get_multiformat_post(request, "service_url"), service.url) ax.verify_param( svc_name, paramCompare="types", notEqual=True, paramName="service_name", httpError=HTTPBadRequest, msgOnFail=s.Service_PUT_BadRequestResponseSchema_ReservedKeyword. description) ax.verify_param( svc_name == service.resource_name and svc_url == service.url, notEqual=True, paramCompare=True, paramName="service_name/service_url", httpError=HTTPBadRequest, msgOnFail=s.Service_PUT_BadRequestResponseSchema.description) if svc_name != service.resource_name: all_svc_names = list() for svc_type in SERVICE_TYPE_DICT: for svc in su.get_services_by_type(svc_type, db_session=request.db): all_svc_names.append(svc.resource_name) ax.verify_param( svc_name, notIn=True, paramCompare=all_svc_names, httpError=HTTPConflict, msgOnFail=s.Service_PUT_ConflictResponseSchema.description, content={u"service_name": str(svc_name)}) def update_service_magpie_and_phoenix(_svc, new_name, new_url, svc_push, db_session): _svc.resource_name = new_name _svc.url = new_url has_getcap = Permission.GET_CAPABILITIES in SERVICE_TYPE_DICT[ _svc.type].permissions if svc_push and svc.type in SERVICES_PHOENIX_ALLOWED and has_getcap: # (re)apply getcapabilities to updated service to ensure updated push su.add_service_getcapabilities_perms(_svc, db_session) sync_services_phoenix(db_session.query( models.Service)) # push all services old_svc_content = sf.format_service(service, show_private_url=True) err_svc_content = { u"service": old_svc_content, u"new_service_name": svc_name, u"new_service_url": svc_url } ax.evaluate_call( lambda: update_service_magpie_and_phoenix(service, svc_name, svc_url, service_push, request.db), fallback=lambda: request.db.rollback(), httpError=HTTPForbidden, msgOnFail=s.Service_PUT_ForbiddenResponseSchema.description, content=err_svc_content) return ax.valid_http(httpSuccess=HTTPOk, detail=s.Service_PUT_OkResponseSchema.description, content={ u"service": sf.format_service(service, show_private_url=True) })