Пример #1
0
async def create_admin() -> None:
    """Create admin user if it's not exits."""
    hashed_pass = await hash_password("admin")
    find_query = AopiUser.find("admin")
    insert_query = AopiUser.create("admin", hashed_pass)
    if await database.fetch_one(find_query):
        logger.debug("Admin user already exists")
        return
    logger.debug("Creating admin user")
    await database.execute(insert_query)
Пример #2
0
 def check_user_in_group(cls, user: Union[str, int],
                         group: Union[str, int]) -> sa.sql.Select:
     user_id = AopiUser.get_id(user)
     group_id = AopiGroup.get_id(group)
     return sa.select([
         sa.exists(
             cls.select_query(cls.id).where(
                 sa.and_(cls.group_id == group_id, cls.user_id == user_id)))
     ])
Пример #3
0
async def get_user_id(username: str, password: str) -> Optional[int]:
    query = AopiUser.find(username, AopiUser.id, AopiUser.password)
    user = await database.fetch_one(query)
    if user is None:
        return None
    user_model = AopiUserModel.from_orm(user)
    if user_model.password is None:
        return None
    if not await verify_password(user_model.password, password):
        return None
    return user.id
Пример #4
0
async def get_current_user_info(
    user_id: int = Depends(get_current_user_id),
) -> AopiUserModel:
    user_not_found = HTTPException(
        status_code=status.HTTP_404_NOT_FOUND, detail="User not found"
    )
    if user_id is None:
        raise user_not_found
    find_query = AopiUser.select_query().where(AopiUser.id == user_id)
    if user_dict := await database.fetch_one(find_query):
        return AopiUserModel.from_orm(user_dict)
Пример #5
0
async def authenticate_user(username: str,
                            password: str) -> Optional[AopiUserModel]:
    user_query = AopiUser.find(username=username)
    found_users = await database.fetch_all(user_query)
    if len(found_users) != 1:
        return None
    user = AopiUserModel.from_orm(found_users[0])
    if user.password is None:
        return None
    if not await verify_password(user.password, password):
        return None
    return user
Пример #6
0
 def create(
     cls, *, name: str, deletable: bool, user: Union[str, int]
 ) -> sa.sql.Insert:
     return cls.insert_query(
         name=name, deletable=deletable, created_by=AopiUser.get_id(user)
     )
Пример #7
0
 def link(cls, user: Union[str, int], group: Union[str,
                                                   int]) -> sa.sql.Insert:
     user_id = AopiUser.get_id(user)
     group_id = AopiGroup.get_id(group)
     return cls.insert_query(group_id=group_id, user_id=user_id)
Пример #8
0
 def get_user_groups(cls, user: Union[str, int]) -> sa.sql.Select:
     user_id = AopiUser.get_id(user)
     return cls.select_query(cls.group_id).where(cls.user_id == user_id)