feat(api): add update password
This commit is contained in:
parent
1eadd834e3
commit
ad0a4837fc
@ -2,7 +2,7 @@ from datetime import datetime, timedelta, timezone
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from sqlalchemy import insert, select
|
from sqlalchemy import insert, select, update
|
||||||
from sqlalchemy.ext.asyncio import AsyncConnection
|
from sqlalchemy.ext.asyncio import AsyncConnection
|
||||||
|
|
||||||
from api.db.tables.account import account_keyring_table, KeyStatus, KeyType
|
from api.db.tables.account import account_keyring_table, KeyStatus, KeyType
|
||||||
@ -80,7 +80,23 @@ async def create_password_key(connection: AsyncConnection, password: str | None,
|
|||||||
key_value=hasher.hash_data(password),
|
key_value=hasher.hash_data(password),
|
||||||
created_at=datetime.now(timezone.utc),
|
created_at=datetime.now(timezone.utc),
|
||||||
expiry=datetime.now(timezone.utc) + timedelta(days=365),
|
expiry=datetime.now(timezone.utc) + timedelta(days=365),
|
||||||
status=KeyStatus.ACTIVE.value,
|
status=KeyStatus.ACTIVE,
|
||||||
)
|
)
|
||||||
await connection.execute(stmt)
|
await connection.execute(stmt)
|
||||||
await connection.commit()
|
await connection.commit()
|
||||||
|
|
||||||
|
|
||||||
|
async def update_password_key(connection: AsyncConnection, owner_id: int, password: str):
|
||||||
|
stmt = select(account_keyring_table).where(account_keyring_table.c.owner_id == owner_id)
|
||||||
|
result = await connection.execute(stmt)
|
||||||
|
keyring = result.one_or_none()
|
||||||
|
if not keyring:
|
||||||
|
await create_password_key(connection, password, owner_id)
|
||||||
|
else:
|
||||||
|
stmt = (
|
||||||
|
update(account_keyring_table)
|
||||||
|
.values(key_value=hasher.hash_data(password), expiry=datetime.now(timezone.utc) + timedelta(days=365))
|
||||||
|
.where(account_keyring_table.c.owner_id == owner_id)
|
||||||
|
)
|
||||||
|
await connection.execute(stmt)
|
||||||
|
await connection.commit()
|
||||||
|
@ -14,7 +14,7 @@ from api.db.logic.account import (
|
|||||||
get_user_by_login,
|
get_user_by_login,
|
||||||
update_user_by_id,
|
update_user_by_id,
|
||||||
)
|
)
|
||||||
from api.db.logic.keyring import create_password_key
|
from api.db.logic.keyring import create_password_key, update_password_key
|
||||||
from api.db.tables.account import AccountStatus
|
from api.db.tables.account import AccountStatus
|
||||||
from api.schemas.account.account import User
|
from api.schemas.account.account import User
|
||||||
from api.schemas.base import bearer_schema
|
from api.schemas.base import bearer_schema
|
||||||
@ -95,6 +95,9 @@ async def update_account(
|
|||||||
if user is None:
|
if user is None:
|
||||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Account not found")
|
||||||
|
|
||||||
|
if user_update.password is not None:
|
||||||
|
await update_password_key(connection, user.id, user_update.password)
|
||||||
|
|
||||||
update_values = update_user_data_changes(user_update, user)
|
update_values = update_user_data_changes(user_update, user)
|
||||||
|
|
||||||
if update_values is None:
|
if update_values is None:
|
||||||
|
@ -12,6 +12,7 @@ class UserUpdate(Base):
|
|||||||
name: Optional[str] = Field(None, max_length=100)
|
name: Optional[str] = Field(None, max_length=100)
|
||||||
login: Optional[str] = Field(None, max_length=100)
|
login: Optional[str] = Field(None, max_length=100)
|
||||||
email: Optional[EmailStr] = None
|
email: Optional[EmailStr] = None
|
||||||
|
password: Optional[str] = None
|
||||||
bind_tenant_id: Optional[str] = Field(None, max_length=40)
|
bind_tenant_id: Optional[str] = Field(None, max_length=40)
|
||||||
role: Optional[AccountRole] = None
|
role: Optional[AccountRole] = None
|
||||||
meta: Optional[dict] = None
|
meta: Optional[dict] = None
|
||||||
|
@ -280,6 +280,8 @@ export interface components {
|
|||||||
login?: string | null;
|
login?: string | null;
|
||||||
/** Email */
|
/** Email */
|
||||||
email?: string | null;
|
email?: string | null;
|
||||||
|
/** Password */
|
||||||
|
password?: string | null;
|
||||||
/** Bindtenantid */
|
/** Bindtenantid */
|
||||||
bindTenantId?: string | null;
|
bindTenantId?: string | null;
|
||||||
role?: components["schemas"]["AccountRole"] | null;
|
role?: components["schemas"]["AccountRole"] | null;
|
||||||
@ -534,7 +536,7 @@ export interface operations {
|
|||||||
[name: string]: unknown;
|
[name: string]: unknown;
|
||||||
};
|
};
|
||||||
content: {
|
content: {
|
||||||
"application/json": components["schemas"]["User"];
|
"application/json": components["schemas"]["UserUpdate"];
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
/** @description Validation Error */
|
/** @description Validation Error */
|
||||||
|
Loading…
Reference in New Issue
Block a user