def _ensure_auth_user(user_data: dict, company_id: str, log: Logger, revoke: bool = False): key, secret = user_data.get("key"), user_data.get("secret") if not (key and secret): credentials = None else: creds = Credentials(key=key, secret=secret) user = AuthUser.objects(credentials__match=creds).first() if user: if revoke: user.credentials = [] user.save() return user.id credentials = [] if revoke else [creds] user_id = user_data.get("id", f"__{user_data['name']}__") log.info(f"Creating user: {user_data['name']}") user = AuthUser( id=user_id, name=user_data["name"], company=company_id, role=user_data["role"], email=user_data["email"], created=datetime.utcnow(), credentials=credentials, ) user.save() return user.id
def _ensure_auth_user(user_data: dict, company_id: str, log: Logger, revoke: bool = False): ensure_credentials = {"key", "secret"}.issubset(user_data) if ensure_credentials: user = AuthUser.objects(credentials__match=Credentials( key=user_data["key"], secret=user_data["secret"])).first() if user: if revoke: user.credentials = [] user.save() return user.id user_id = user_data.get("id", f"__{user_data['name']}__") log.info(f"Creating user: {user_data['name']}") user = AuthUser( id=user_id, name=user_data["name"], company=company_id, role=user_data["role"], email=user_data["email"], created=datetime.utcnow(), credentials=[ Credentials(key=user_data["key"], secret=user_data["secret"]) ] if not revoke else [] if ensure_credentials else None, ) user.save() return user.id
def create_user(request: CreateUserRequest, call: APICall = None) -> str: """ Create a new user in both the auth database and the backend database :param request: New user details :param call: API call that triggered this call. If not None, new backend user creation will be performed using a new call in the same transaction. :return: The new user's ID """ with translate_errors_context(): if not Company.objects(id=request.company).only("id"): raise errors.bad_request.InvalidId(company=request.company) user = User( id=database.utils.id(), name=request.name, company=request.company, role=request.role or Role.user, email=request.email, created=datetime.utcnow(), ) user.save() users_create_request = Users_CreateRequest( id=user.id, name=request.name, company=request.company, family_name=request.family_name, given_name=request.given_name, avatar=request.avatar, ) try: UserBLL.create(users_create_request) except Exception as ex: user.delete() raise errors.server_error.GeneralError( "failed adding new user", ex=str(ex)) return user.id
def _ensure_user(user_data, company_id): user = User.objects(credentials__match=Credentials( key=user_data["key"], secret=user_data["secret"])).first() if user: return user.id log.info(f"Creating user: {user_data['name']}") user = User( id=f"__{user_data['name']}__", name=user_data["name"], company=company_id, role=user_data["role"], email=user_data["email"], created=datetime.utcnow(), credentials=[ Credentials(key=user_data["key"], secret=user_data["secret"]) ], ) user.save() return user.id
def _ensure_auth_user(user_data, company_id): ensure_credentials = {"key", "secret"}.issubset(user_data.keys()) if ensure_credentials: user = AuthUser.objects(credentials__match=Credentials( key=user_data["key"], secret=user_data["secret"])).first() if user: return user.id log.info(f"Creating user: {user_data['name']}") user = AuthUser( id=user_data.get("id", f"__{user_data['name']}__"), name=user_data["name"], company=company_id, role=user_data["role"], email=user_data["email"], created=datetime.utcnow(), credentials=[ Credentials(key=user_data["key"], secret=user_data["secret"]) ] if ensure_credentials else None, ) user.save() return user.id