async def retrieve_user( request: Request, payload: Dict, allow_api_token: bool, extract_user_from_jwt: bool, *args: Any, **kwargs: Any, ) -> Optional[Dict]: if extract_user_from_jwt and payload and has_role(payload.get("user"), GUEST): return payload["user"] user_service = UserService(request[REQUEST_DB_SESSION_KEY]) if allow_api_token: api_token = rasa_x_utils.default_arg(request, "api_token") if api_token: return user_service.api_token_auth(api_token) if payload: username = payload.get("username", None) if username is not None: return user_service.fetch_user(username) else: # user is first-time enterprise user and has username None # in this case we'll fetch the profile using name_id name_id = payload.get("user", {}).get("name_id", None) return user_service.fetch_user(name_id) return None
def update_role_users( self, role: Text, username_to_exclude: Text, users_to_update: List[Text], user_service: UserService, ) -> List[Dict[Text, Any]]: """Update all users associated with this role. Exclude user `username_to_exclude` from this operation. Returns a list of users associated with `role`. """ # delete this role from all users for user in self.fetch_role_users(role): username = user["username"] # exclude `username_to_exclude` if username == username_to_exclude: continue user_service.delete_user_role(username, role) # add role to all users_to_update for username in users_to_update: # exclude `username_to_exclude` if username == username_to_exclude: continue existing_user = user_service.fetch_user(username) if existing_user: user_service.add_role_to_user(username, role) return self.fetch_role_users(role)
def get_user_permissions( self, username: Text, user_service: UserService, project_id: Text = config.project_name, ) -> List[Text]: """Retrieve list of API permissions associated with `username`.""" user = user_service.fetch_user(username) return self._permissions_for(user, project_id)
def _create_system_user(session: Session) -> None: user_service = UserService(session) if user_service.fetch_user(rasa_x_config.SYSTEM_USER): logger.debug( f"Found existing system system user '{rasa_x_config.SYSTEM_USER}'." ) return user_service.create_user( rasa_x_config.SYSTEM_USER, None, rasa_x_config.team_name, ADMIN ) logger.debug(f"Created new system user '{rasa_x_config.SYSTEM_USER}'.")
async def list_users(request: Request) -> HTTPResponse: user_service = UserService(request[REQUEST_DB_SESSION_KEY]) username_query = utils.default_arg(request, "username", None) role_query = utils.default_arg(request, "role", None) users = user_service.fetch_all_users(config.team_name, username_query, role_query) if not users: return rasa_x_utils.error(404, "NoUsersFound", "No users found") profiles = [user_service.fetch_user(u["username"]) for u in users] return response.json(profiles, headers={"X-Total-Count": len(profiles)})
async def profile(request: Request, user: Dict) -> HTTPResponse: user_service = UserService(request[REQUEST_DB_SESSION_KEY]) return response.json( user_service.fetch_user(user["username"], return_api_token=True))