def delete_user(identity: Identity, user_id: str, company_id: str = None, call: APICall = None): """ Delete an existing user from both the auth database and the backend database :param identity: Calling user identity :param user_id: ID of user to delete :param company_id: Company of user to delete :param call: API call that triggered this call. If not None, backend user deletion will be performed using a new call in the same transaction. """ if user_id == identity.user: raise errors.bad_request.FieldsValueError("cannot delete yourself", user=user_id) if not company_id: company_id = identity.company if (identity.role not in Role.get_system_roles() and company_id != identity.company): raise errors.bad_request.FieldsNotAllowedForRole( "must be empty or your own company", role=identity.role, field="company") with translate_errors_context(): query = dict(id=user_id, company=company_id) res = User.objects(**query).delete() if not res: raise errors.bad_request.InvalidUserId(**query) try: UserBLL.delete(user_id) except Exception as ex: log.error(f"Exception calling users.delete: {str(ex)}")
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 create(call: APICall): UserBLL.create(call.data_model)
def delete(call): assert isinstance(call, APICall) UserBLL.delete(call.data["user"])
def delete(call: APICall): UserBLL.delete(call.data["user"])