async def update( self, id: OrganizationID, expiration_date: Union[UnsetType, Optional[DateTime]] = Unset, user_profile_outsider_allowed: Union[UnsetType, bool] = Unset, ) -> None: """ Raises: OrganizationNotFoundError """ if id not in self._organizations: raise OrganizationNotFoundError() organization = self._organizations[id] if expiration_date is not Unset: organization = organization.evolve(expiration_date=expiration_date) if user_profile_outsider_allowed is not Unset: organization = organization.evolve( user_profile_outsider_allowed=user_profile_outsider_allowed) self._organizations[id] = organization if self._organizations[id].is_expired: await self._send_event(BackendEvent.ORGANIZATION_EXPIRED, organization_id=id)
async def update( self, id: OrganizationID, is_expired: Union[UnsetType, bool] = Unset, active_users_limit: Union[UnsetType, Optional[int]] = Unset, user_profile_outsider_allowed: Union[UnsetType, bool] = Unset, ) -> None: """ Raises: OrganizationNotFoundError """ if id not in self._organizations: raise OrganizationNotFoundError() organization = self._organizations[id] if is_expired is not Unset: organization = organization.evolve(is_expired=is_expired) if active_users_limit is not Unset: organization = organization.evolve( active_users_limit=active_users_limit) if user_profile_outsider_allowed is not Unset: organization = organization.evolve( user_profile_outsider_allowed=user_profile_outsider_allowed) self._organizations[id] = organization if self._organizations[id].is_expired: await self._send_event(BackendEvent.ORGANIZATION_EXPIRED, organization_id=id)
async def set_expiration_date(self, id: OrganizationID, expiration_date: Pendulum = None) -> None: try: self._organizations[id] = self._organizations[id].evolve( expiration_date=expiration_date) except KeyError: raise OrganizationNotFoundError()
async def set_expiration_date( self, id: OrganizationID, expiration_date: DateTime = None ) -> None: try: self._organizations[id] = self._organizations[id].evolve( expiration_date=expiration_date ) if self._organizations[id].is_expired: await self._send_event(BackendEvent.ORGANIZATION_EXPIRED, organization_id=id) except KeyError: raise OrganizationNotFoundError()
async def _get(conn, id: OrganizationID) -> Organization: data = await conn.fetchrow(*_q_get_organization(organization_id=id)) if not data: raise OrganizationNotFoundError() rvk = VerifyKey(data[1]) if data[1] else None return Organization( organization_id=id, bootstrap_token=data[0], root_verify_key=rvk, expiration_date=data[2], )
async def _get(conn, id: OrganizationID) -> Organization: data = await conn.fetchrow( """ SELECT bootstrap_token, root_verify_key FROM organizations WHERE organization_id = $1 """, id, ) if not data: raise OrganizationNotFoundError() rvk = VerifyKey(data[1]) if data[1] else None return Organization(organization_id=id, bootstrap_token=data[0], root_verify_key=rvk)
async def _get(conn, id: OrganizationID, for_update: bool = False) -> Organization: if for_update: data = await conn.fetchrow(*_q_get_organization_for_update( organization_id=id.str)) else: data = await conn.fetchrow(*_q_get_organization( organization_id=id.str)) if not data: raise OrganizationNotFoundError() rvk = VerifyKey(data[1]) if data[1] else None return Organization( organization_id=id, bootstrap_token=data[0], root_verify_key=rvk, is_expired=data[2], active_users_limit=data[3], user_profile_outsider_allowed=data[4], )
async def stats(self, id: OrganizationID) -> OrganizationStats: async with self.dbh.pool.acquire() as conn, conn.transaction(): result = await conn.fetchrow(*_q_get_stats(organization_id=id.str)) if not result["exist"]: raise OrganizationNotFoundError() users = 0 active_users = 0 users_per_profile_detail = { p: { "active": 0, "revoked": 0 } for p in UserProfile } for u in result["users"]: is_revoked, profile = u users += 1 if is_revoked: users_per_profile_detail[ UserProfile[profile]]["revoked"] += 1 else: active_users += 1 users_per_profile_detail[ UserProfile[profile]]["active"] += 1 users_per_profile_detail = [ UsersPerProfileDetailItem(profile=profile, **data) for profile, data in users_per_profile_detail.items() ] return OrganizationStats( data_size=result["data_size"], metadata_size=result["metadata_size"], realms=result["realms"], users=users, active_users=active_users, users_per_profile_detail=users_per_profile_detail, )
async def get(self, id: OrganizationID) -> Organization: if id not in self._organizations: raise OrganizationNotFoundError() return self._organizations[id]