def on_delete(self, req: Request, resp: Response, scim_id: str): self.context.logger.info(f'Deleting invite {scim_id}') db_invite = ctx_invitedb(req).get_invite_by_scim_id(scim_id=scim_id) self.context.logger.debug(f'Found invite: {db_invite}') if not db_invite: raise NotFound(detail="Invite not found") # Check version if not self._check_version(req, db_invite): raise BadRequest(detail="Version mismatch") # Remove signup invite ref = self._create_signup_ref(req, db_invite) signup_invite = self.context.signup_invitedb.get_invite_by_reference( ref) self.context.signup_invitedb.remove_document(signup_invite.invite_id) # Remove scim invite res = ctx_invitedb(req).remove(db_invite) add_api_event( context=self.context, data_owner=req.context['data_owner'], db_obj=db_invite, resource_type=SCIMResourceType.INVITE, level=EventLevel.INFO, status=EventStatus.DELETED, message='Group was deleted', ) self.context.logger.debug(f'Remove invite result: {res}') resp.status = HTTP_204
def on_get(self, req: Request, resp: Response, scim_id: Optional[str] = None): if scim_id is None: raise BadRequest(detail='Not implemented') self.context.logger.info(f'Fetching invite {scim_id}') db_invite = ctx_invitedb(req).get_invite_by_scim_id(scim_id) if not db_invite: raise NotFound(detail='Invite not found') ref = self._create_signup_ref(req, db_invite) signup_invite = self.context.signup_invitedb.get_invite_by_reference( ref) self._db_invite_to_response(req, resp, db_invite, signup_invite)
def _get_scim_referenced( req: Request, resource: NutidEventResource) -> Optional[ScimApiResourceBase]: if resource.resource_type == SCIMResourceType.USER: return ctx_userdb(req).get_user_by_scim_id(str(resource.scim_id)) elif resource.resource_type == SCIMResourceType.GROUP: return ctx_groupdb(req).get_group_by_scim_id(str(resource.scim_id)) elif resource.resource_type == SCIMResourceType.INVITE: return ctx_invitedb(req).get_invite_by_scim_id(str(resource.scim_id)) elif resource.resource_type == SCIMResourceType.EVENT: raise BadRequest(detail=f'Events can not refer to other events') raise BadRequest( detail= f'Events for resource {resource.resource_type.value} not implemented')
def _filter_lastmodified( req: Request, filter: SearchFilter, skip: Optional[int] = None, limit: Optional[int] = None) -> Tuple[List[ScimApiInvite], int]: if filter.op not in ['gt', 'ge']: raise BadRequest(scim_type='invalidFilter', detail='Unsupported operator') if not isinstance(filter.val, str): raise BadRequest(scim_type='invalidFilter', detail='Invalid datetime') return ctx_invitedb(req).get_invites_by_last_modified( operator=filter.op, value=datetime.fromisoformat(filter.val), skip=skip, limit=limit)
def on_post(self, req: Request, resp: Response): """ POST /Invites HTTP/1.1 Host: example.com Accept: application/scim+json Content-Type: application/scim+json Authorization: Bearer h480djs93hd8 Content-Length: ... { 'schemas': ['https://scim.eduid.se/schema/nutid/invite/v1', 'https://scim.eduid.se/schema/nutid/user/v1'], 'expiresAt': '2021-03-02T14:35:52', 'groups': [], 'phoneNumbers': [ {'type': 'fax', 'value': 'tel:+461234567', 'primary': True}, {'type': 'home', 'value': 'tel:+5-555-555-5555', 'primary': False}, ], 'meta': { 'location': 'http://*****:*****@example.com', 'primary': True}, {'type': 'home', 'value': '*****@*****.**', 'primary': False}, ], } """ self.context.logger.info(f'Creating invite') try: create_request: InviteCreateRequest = InviteCreateRequestSchema( ).load(req.media) self.context.logger.debug(create_request) except ValidationError as e: raise BadRequest(detail=f"{e}") profiles = {} for profile_name, profile in create_request.nutid_user_v1.profiles.items( ): profiles[profile_name] = ScimApiProfile( attributes=profile.attributes, data=profile.data) db_invite = ScimApiInvite( external_id=create_request.external_id, name=ScimApiName(**asdict(create_request.nutid_invite_v1.name)), emails=[ ScimApiEmail(**asdict(email)) for email in create_request.nutid_invite_v1.emails ], phone_numbers=[ ScimApiPhoneNumber(**asdict(number)) for number in create_request.nutid_invite_v1.phone_numbers ], nin=create_request.nutid_invite_v1.national_identity_number, preferred_language=create_request.nutid_invite_v1. preferred_language, groups=create_request.nutid_invite_v1.groups, profiles=profiles, ) signup_invite = self._create_signup_invite(req, resp, create_request, db_invite) self.context.signup_invitedb.save(signup_invite) ctx_invitedb(req).save(db_invite) if signup_invite.send_email: self._send_invite_mail(signup_invite) add_api_event( context=self.context, data_owner=req.context['data_owner'], db_obj=db_invite, resource_type=SCIMResourceType.INVITE, level=EventLevel.INFO, status=EventStatus.CREATED, message='Invite was created', ) self._db_invite_to_response(req, resp, db_invite, signup_invite) resp.status = HTTP_201