2023-11-19 00:47:12 +00:00
|
|
|
import time
|
2024-08-27 22:10:27 +00:00
|
|
|
from typing import Optional
|
2024-06-18 13:03:31 +00:00
|
|
|
|
2024-09-04 14:54:48 +00:00
|
|
|
from open_webui.apps.webui.internal.db import Base, JSONField, get_db
|
|
|
|
from open_webui.apps.webui.models.chats import Chats
|
2024-08-27 22:10:27 +00:00
|
|
|
from pydantic import BaseModel, ConfigDict
|
|
|
|
from sqlalchemy import BigInteger, Column, String, Text
|
2023-12-29 07:02:49 +00:00
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
####################
|
|
|
|
# User DB Schema
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
class User(Base):
|
|
|
|
__tablename__ = "user"
|
2024-04-27 23:38:51 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
id = Column(String, primary_key=True)
|
|
|
|
name = Column(String)
|
|
|
|
email = Column(String)
|
|
|
|
role = Column(String)
|
2024-06-24 11:21:51 +00:00
|
|
|
profile_image_url = Column(Text)
|
2024-04-27 23:38:51 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
last_active_at = Column(BigInteger)
|
|
|
|
updated_at = Column(BigInteger)
|
|
|
|
created_at = Column(BigInteger)
|
2023-12-26 05:44:28 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
api_key = Column(String, nullable=True, unique=True)
|
|
|
|
settings = Column(JSONField, nullable=True)
|
|
|
|
info = Column(JSONField, nullable=True)
|
2024-05-26 07:37:09 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
oauth_sub = Column(Text, unique=True)
|
2023-12-26 05:44:28 +00:00
|
|
|
|
|
|
|
|
2024-05-27 05:47:42 +00:00
|
|
|
class UserSettings(BaseModel):
|
|
|
|
ui: Optional[dict] = {}
|
|
|
|
model_config = ConfigDict(extra="allow")
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
class UserModel(BaseModel):
|
|
|
|
id: str
|
|
|
|
name: str
|
|
|
|
email: str
|
2023-11-19 05:41:43 +00:00
|
|
|
role: str = "pending"
|
2024-04-04 05:36:27 +00:00
|
|
|
profile_image_url: str
|
2024-04-27 23:38:51 +00:00
|
|
|
|
|
|
|
last_active_at: int # timestamp in epoch
|
|
|
|
updated_at: int # timestamp in epoch
|
|
|
|
created_at: int # timestamp in epoch
|
|
|
|
|
2024-04-02 16:23:55 +00:00
|
|
|
api_key: Optional[str] = None
|
2024-05-27 05:47:42 +00:00
|
|
|
settings: Optional[UserSettings] = None
|
2024-06-16 22:32:26 +00:00
|
|
|
info: Optional[dict] = None
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-05-26 07:37:09 +00:00
|
|
|
oauth_sub: Optional[str] = None
|
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
model_config = ConfigDict(from_attributes=True)
|
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
|
|
|
|
####################
|
|
|
|
# Forms
|
|
|
|
####################
|
|
|
|
|
|
|
|
|
2024-11-18 13:37:04 +00:00
|
|
|
class UserResponse(BaseModel):
|
|
|
|
id: str
|
|
|
|
name: str
|
|
|
|
email: str
|
|
|
|
role: str
|
|
|
|
profile_image_url: str
|
|
|
|
|
|
|
|
|
2023-11-19 08:13:59 +00:00
|
|
|
class UserRoleUpdateForm(BaseModel):
|
|
|
|
id: str
|
|
|
|
role: str
|
|
|
|
|
|
|
|
|
2024-01-06 04:59:56 +00:00
|
|
|
class UserUpdateForm(BaseModel):
|
|
|
|
name: str
|
|
|
|
email: str
|
|
|
|
profile_image_url: str
|
|
|
|
password: Optional[str] = None
|
|
|
|
|
2024-01-03 22:33:57 +00:00
|
|
|
|
2024-01-06 04:59:56 +00:00
|
|
|
class UsersTable:
|
|
|
|
def insert_new_user(
|
2024-04-04 08:10:51 +00:00
|
|
|
self,
|
|
|
|
id: str,
|
|
|
|
name: str,
|
|
|
|
email: str,
|
2024-04-07 06:16:29 +00:00
|
|
|
profile_image_url: str = "/user.png",
|
2024-04-04 08:10:51 +00:00
|
|
|
role: str = "pending",
|
2024-05-26 07:37:09 +00:00
|
|
|
oauth_sub: Optional[str] = None,
|
2024-01-06 04:59:56 +00:00
|
|
|
) -> Optional[UserModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
user = UserModel(
|
|
|
|
**{
|
|
|
|
"id": id,
|
|
|
|
"name": name,
|
|
|
|
"email": email,
|
|
|
|
"role": role,
|
|
|
|
"profile_image_url": profile_image_url,
|
|
|
|
"last_active_at": int(time.time()),
|
|
|
|
"created_at": int(time.time()),
|
|
|
|
"updated_at": int(time.time()),
|
|
|
|
"oauth_sub": oauth_sub,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
result = User(**user.model_dump())
|
|
|
|
db.add(result)
|
|
|
|
db.commit()
|
|
|
|
db.refresh(result)
|
|
|
|
if result:
|
|
|
|
return user
|
|
|
|
else:
|
|
|
|
return None
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def get_user_by_id(self, id: str) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-27 22:10:27 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def get_user_by_api_key(self, api_key: str) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
user = db.query(User).filter_by(api_key=api_key).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def get_user_by_email(self, email: str) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
user = db.query(User).filter_by(email=email).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-05-26 07:37:09 +00:00
|
|
|
def get_user_by_oauth_sub(self, sub: str) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
user = db.query(User).filter_by(oauth_sub=sub).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-06-21 12:58:57 +00:00
|
|
|
|
2024-08-14 12:46:31 +00:00
|
|
|
def get_users(self, skip: int = 0, limit: int = 50) -> list[UserModel]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
users = (
|
|
|
|
db.query(User)
|
|
|
|
# .offset(skip).limit(limit)
|
|
|
|
.all()
|
|
|
|
)
|
|
|
|
return [UserModel.model_validate(user) for user in users]
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def get_num_users(self) -> Optional[int]:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
return db.query(User).count()
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def get_first_user(self) -> UserModel:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
user = db.query(User).order_by(User.created_at).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-05-02 02:59:05 +00:00
|
|
|
|
2024-06-24 07:57:08 +00:00
|
|
|
def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
db.query(User).filter_by(id=id).update({"role": role})
|
|
|
|
db.commit()
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2023-11-19 08:13:59 +00:00
|
|
|
|
2024-01-27 04:27:45 +00:00
|
|
|
def update_user_profile_image_url_by_id(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, id: str, profile_image_url: str
|
2024-01-27 04:27:45 +00:00
|
|
|
) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
db.query(User).filter_by(id=id).update(
|
|
|
|
{"profile_image_url": profile_image_url}
|
|
|
|
)
|
|
|
|
db.commit()
|
|
|
|
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-01-27 04:27:45 +00:00
|
|
|
|
2024-06-24 07:57:08 +00:00
|
|
|
def update_user_last_active_by_id(self, id: str) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
db.query(User).filter_by(id=id).update(
|
|
|
|
{"last_active_at": int(time.time())}
|
|
|
|
)
|
|
|
|
db.commit()
|
2024-04-27 23:38:51 +00:00
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-04-27 23:38:51 +00:00
|
|
|
|
2024-05-27 17:07:38 +00:00
|
|
|
def update_user_oauth_sub_by_id(
|
2024-06-21 12:58:57 +00:00
|
|
|
self, id: str, oauth_sub: str
|
2024-05-27 17:07:38 +00:00
|
|
|
) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
db.query(User).filter_by(id=id).update({"oauth_sub": oauth_sub})
|
2024-07-08 06:01:15 +00:00
|
|
|
db.commit()
|
2024-05-27 17:07:38 +00:00
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
return UserModel.model_validate(user)
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-05-27 17:07:38 +00:00
|
|
|
|
2024-06-24 07:57:08 +00:00
|
|
|
def update_user_by_id(self, id: str, updated: dict) -> Optional[UserModel]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
db.query(User).filter_by(id=id).update(updated)
|
|
|
|
db.commit()
|
2024-01-06 04:59:56 +00:00
|
|
|
|
2024-07-04 06:32:39 +00:00
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
return UserModel.model_validate(user)
|
|
|
|
# return UserModel(**user.dict())
|
2024-08-27 22:10:27 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-06-21 12:58:57 +00:00
|
|
|
|
|
|
|
def delete_user_by_id(self, id: str) -> bool:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
|
|
|
# Delete User Chats
|
|
|
|
result = Chats.delete_chats_by_user_id(id)
|
|
|
|
|
|
|
|
if result:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
# Delete User
|
|
|
|
db.query(User).filter_by(id=id).delete()
|
|
|
|
db.commit()
|
2024-06-24 11:06:15 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
else:
|
2024-06-21 12:58:57 +00:00
|
|
|
return False
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return False
|
2023-12-29 07:02:49 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def update_user_api_key_by_id(self, id: str, api_key: str) -> str:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
result = db.query(User).filter_by(id=id).update({"api_key": api_key})
|
|
|
|
db.commit()
|
|
|
|
return True if result == 1 else False
|
2024-08-14 12:38:19 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return False
|
2023-12-29 07:02:49 +00:00
|
|
|
|
2024-06-21 12:58:57 +00:00
|
|
|
def get_user_api_key_by_id(self, id: str) -> Optional[str]:
|
2024-06-24 11:06:15 +00:00
|
|
|
try:
|
2024-07-04 06:32:39 +00:00
|
|
|
with get_db() as db:
|
|
|
|
user = db.query(User).filter_by(id=id).first()
|
|
|
|
return user.api_key
|
2024-08-27 22:10:27 +00:00
|
|
|
except Exception:
|
2024-06-24 11:06:15 +00:00
|
|
|
return None
|
2024-04-02 16:27:35 +00:00
|
|
|
|
2023-11-19 00:47:12 +00:00
|
|
|
|
2024-06-18 13:03:31 +00:00
|
|
|
Users = UsersTable()
|