Exemple #1
0
def create_user_open(
        *,
        db: Session = Depends(get_db),
        open_user_create: OpenUserCreate,
):
    """
    Create new user without the need to be logged in.
    """

    user = crud.user.get_by_email(db, email=open_user_create.email)
    if user:
        raise HTTPException(
            status_code=403,
            detail="A user with this email already exists in the system",
        )
    user = crud.user.get_by_email(db, email=open_user_create.full_name)
    if user:
        raise HTTPException(
            status_code=403,
            detail="A user with this full_name already exists in the system",
        )
    user_in = UserCreate(password=open_user_create.password,
                         email=open_user_create.email,
                         full_name=open_user_create.full_name)
    user = crud.user.create(db, obj_in=user_in)

    if config.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)
    return user
Exemple #2
0
def route_users_post(
    *, user_in: UserInCreate, current_user: UserInDB = Depends(get_current_user)
):
    """
    Create new user
    """
    if not check_if_user_is_active(current_user):
        raise HTTPException(status_code=400, detail="Inactive user")
    elif not check_if_user_is_superuser(current_user):
        raise HTTPException(
            status_code=400, detail="The user doesn't have enough privileges"
        )
    bucket = get_default_bucket()
    user = get_user(bucket, user_in.username)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )
    user = upsert_user(bucket, user_in, persist_to=1)
    if config.EMAILS_ENABLED and user_in.email:
        send_new_account_email(
            email_to=user_in.email, username=user_in.username, password=user_in.password
        )
    return user
Exemple #3
0
def route_users_post(
    *,
    username,
    password,
    admin_channels=[],
    admin_roles=[],
    disabled=False,
    email=None,
    full_name=None,
):
    current_user = get_current_user()

    if not current_user:
        abort(400, "Could not authenticate user with provided token")
    elif not check_if_user_is_active(current_user):
        abort(400, "Inactive user")
    elif not check_if_user_is_superuser(current_user):
        abort(400, "The user doesn't have enough privileges")
    bucket = get_default_bucket()
    user = get_user(bucket, username)
    if user:
        return abort(400, f"The user with this username already exists in the system.")
    user_in = UserInCreate(
        username=username,
        password=password,
        admin_channels=admin_channels,
        admin_roles=admin_roles,
        disabled=disabled,
        email=email,
        full_name=full_name,
    )
    user = upsert_user(bucket, user_in)
    if config.EMAILS_ENABLED:
        send_new_account_email(email_to=email, username=username, password=password)
    return user
Exemple #4
0
async def create_user_open(
        *,
        user_in: schemas.UnprivilegedUserCreate,
        db: Session = Depends(deps.get_db),
        redis: aioredis.Redis = Depends(deps.get_redis),
) -> Any:
    """
    Create new user without the need to be logged in.
    """
    if not settings.USERS_OPEN_REGISTRATION:
        raise HTTPException(
            status_code=403,
            detail="Open user registration is forbidden on this server",
        )
    user = crud.user.get_by_email(db, email=user_in.email)
    if user is not None:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system",
        )
    user_in = schemas.UserCreate(user_in.dict(exclude_unset=True))
    user = await crud.user_cachedb.create(db, redis, obj_in=user_in)
    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)
def create_user_open(
        *,
        username: str = Body(...),
        password: str = Body(...),
        email: EmailStr = Body(None),
        full_name: str = Body(None),
):
    """
    Create new user without the need to be logged in.
    """
    if not config.USERS_OPEN_REGISTRATION:
        raise HTTPException(
            status_code=403,
            detail="Open user resgistration is forbidden on this server",
        )
    bucket = get_default_bucket()
    user = crud.user.get(bucket, username=username)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system",
        )
    user_in = UserCreate(username=username,
                         password=password,
                         email=email,
                         full_name=full_name)
    user = crud.user.upsert(bucket, user_in=user_in, persist_to=1)
    if config.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.username,
                               password=user_in.password)
    return user
Exemple #6
0
def create_user(*, db: Session = Depends(get_db), user_in: UserCreate):
    """
    Create new user.
    """
    user = crud_user.get_by_email(db, email=user_in.email)
    if user:
        raise HTTPException(status_code=HTTP_400_BAD_REQUEST,
                            detail="Email already registered")
    user = crud_user.create(db=db, user_in=user_in)
    if config.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)
    return user
Exemple #7
0
def create_user(
    *,
    db: Session = Depends(deps.get_db),
    user_in: schema.UserCreate,
):
    """
    Create new user.
    """
    user = crud.user.get_by_email(db, email=user_in.email)
    user_phone = crud.user.get_by_phone(db,phone = user_in.phone)
    if user:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="The user with this username already exists in the system.",
        )

    if user_phone:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Phone number already exists in the system.",
        )
    user = crud.user.create(db, obj_in=user_in)


    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(
            email_to=user_in.email,
            username=user_in.name,
            password=user_in.password,
        )

    if settings.SMS_ENABLED and user_in.phone:
        send_new_account_sms(
            sms_to=user_in.phone,
            username=user_in.name,
            password=user_in.password,
        )

    data = user.__dict__
    data["user"] = dict(
        name=user.name,
        email=user.email,
        phone=user.phone,
    )
    return data
Exemple #8
0
def new_user():
    sources = Source.query.all()
    form = UserForm()
    if form.validate_on_submit():
        user = User(firstname=form.firstname.data,
                    lastname=form.lastname.data,
                    email=form.email.data.lower(),
                    phone=form.phone.data,
                    fax=form.fax.data,
                    role=form.role.data,
                    source_id=form.source_id.data)
        db.session.add(user)
        db.session.commit()
        send_new_account_email(user)
        flash('The User was created successfully.', 'success')
        return redirect(url_for('users.login'))
    return render_template('users/crud_user.html',
                           title='Create User',
                           form=form,
                           sources=sources)
def create_user(
        *,
        user_in: UserCreate,
        current_user: DBUser = Depends(get_current_active_superuser),
):
    """
    Create new user.
    """
    user = crud.user.get_by_email(email=user_in.email)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )
    user = crud.user.create(obj_in=user_in)
    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)
    return user
Exemple #10
0
def create_user(
    *,
    db: Session = Depends(get_db),
    user_in: UserCreate
):
    """
    Create new user.
    """
    user = crud.user.get_by_email(db, email=user_in.email)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )
    user = crud.user.create(db, user_in=user_in)
    if config.EMAILS_ENABLED and user_in.email:
        send_new_account_email(
            email_to=user_in.email, username=user_in.email, password=user_in.password
        )
    return user
def create_user(
    *,
    db: Session = Depends(deps.get_db),
    user_in: schemas.UserCreate,
    current_user: models.User = Depends(deps.get_current_active_superuser),
) -> Any:
    """
    Only active superuser should be able to create a new user.
    :return:
    """
    user = crud.user.get_by_email(db=db, email=user_in.email)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.")
    user = crud.user.create(db=db, obj_in=user_in)
    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)
    return user
def create_user(
        *,
        user_in: UserCreate,
        current_user: UserInDB = Depends(get_current_active_superuser),
):
    """
    Create new user.
    """
    bucket = get_default_bucket()
    user = crud.user.get(bucket, username=user_in.username)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )
    user = crud.user.upsert(bucket, user_in=user_in, persist_to=1)
    if config.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.username,
                               password=user_in.password)
    return user
Exemple #13
0
async def create_user(
    *,
    con: AsyncIOConnection = Depends(db.get_con),
    user_in: schemas.UserCreate,
    current_user: schemas.User = Depends(auth.get_current_active_superuser),
) -> Any:
    """
    Create new user.
    """
    user = await crud.user.get_by_email(con, email=user_in.email)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )
    user = await crud.user.create(con, obj_in=user_in)
    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)
    return user
Exemple #14
0
def create_user(
        *,
        db: Session = Depends(get_db),
        full_name: str = Body(None),
        email: EmailStr = Body(None),
        password: str = Body(None),
        current_user: DBUser = Depends(get_current_active_user),
):
    """
    Create new user
    """

    user = crud.user.get_by_email(db, email=email)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )
    try:
        user_in = UserInCreate(business_id=current_user.business_id,
                               email=email,
                               password=password,
                               full_name=full_name,
                               created_by=current_user.id,
                               modified_by=current_user.id,
                               is_active=True)

        user = crud.user.create(db, user_in=user_in)
        if config.EMAILS_ENABLED and user_in.email:
            send_new_account_email(email_to=user_in.email,
                                   username=user_in.email,
                                   password=user_in.password)
        return user
    except Exception as e:
        print(e)
        raise HTTPException(
            status_code=400,
            detail=str(e),
        )
Exemple #15
0
def create_user(
        *,
        user_in: schemas.UserCreate,
        user_uow: SqlAlchemyUnitOfWork = Depends(get_sqlalchemy_uow),
) -> User:
    """
    Create new user.
    """
    try:
        user = user_services.create_user(user_in, user_uow)
    except user_services.UserAlreadyExists:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )

    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(
            email_to=user_in.email,
            username=user_in.email,
            password=user_in.password,
        )
    return user
Exemple #16
0
def create_user(
    *,
    db: Session = Depends(deps.get_db),
    user_in: schemas.UserCreate,
    current_user: models.User = Depends(deps.get_current_active_user),
) -> models.User:
    """# Create a new user

    Create a new user in the database. Users are unique by email
    address (username). In order to create a new user, the username must
    be unique. Additionally, if the user performing the operation is not
    a superuser, a user group must be provided as a home for the new
    user. The indicated user group must exist in the database and the
    normal user must have create permissions for the user group
    indicated.

    ## Args:

    - user_in (schemas.UserCreate): Schema for creating a new user
    - db (Session, optional): SQLAlchemy Session. Defaults to
    Depends(deps.get_db).
    - current_user (models.User, optional): User object for the user
    accessing the endpoint. Defaults to
    Depends(deps.get_current_active_user).

    ## Raises:

    - HTTPException: 400 - When attempting to create a user with a
    username that already exists.
    - HTTPException: 403 - When a normal user attempts to create a user
    without providing a user group id.
    - HTTPException: 404 - When a normal user provides a user group id
    for a user group that doesn't exist.
    - HTTPException: 403 - When a normal user attempts to create a user
    in a user group without create permissions on the user group.

    Returns:

    - models.User: The created user
    """

    user_group = crud.user_group.get(db, user_in.user_group_id)
    if not current_user.is_superuser:
        if not user_in.user_group_id:
            raise HTTPException(
                status_code=403,
                detail="Non-superuser must provide a user group.")
        if not user_group:
            raise HTTPException(status_code=404,
                                detail="Can not find user group.")
        user_group_create_validator(resource_id=user_in.user_group_id,
                                    db=db,
                                    current_user=current_user)

    user = crud.user.get_by_email(db, email=user_in.email)
    if user:
        raise HTTPException(
            status_code=400,
            detail="The user with this username already exists in the system.",
        )

    user = crud.user.create(db, obj_in=user_in)
    if user_group:
        crud.user_group.add_user(db, user_group=user_group, user_id=user.id)

    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)
    return user
Exemple #17
0
def create_user(user_in: schemas.UserCreate, ) -> schemas.User:
    user = schemas.User.from_dto(services.user.create_user(user_in.to_dto()))
    assert user.email is not None
    ident: str = user.email
    send_new_account_email(email_to=ident, username=ident)
    return user
Exemple #18
0
                detail="Only superusers can create more superusers.", )

    # Check if the user already exists; raise Exception with error code 409 if it does
    user = crud.user.get_by_email(db, email=user_in.email)
    if user:
        raise ConflictException(
            detail="The user with this email already exists in the system.", )

    # Create new user
    logging.info(
        f"Admin {current_admin.user_id} ({current_admin.user.email}) is creating User {user_in.__dict__}"
    )
    user = crud.user.create(db, obj_in=user_in)
    if settings.EMAILS_ENABLED and user_in.email:
        send_new_account_email(email_to=user_in.email,
                               username=user_in.email,
                               password=user_in.password)

    return user


@router.put("/me", response_model=schemas.User)
def update_user_me(
        *,
        db: Session = Depends(deps.get_db),
        password: str = Body(None),
        full_name: str = Body(None),
        email: EmailStr = Body(None),
        current_user: models.User = Depends(deps.get_current_user),
) -> Any:
    """