users.py 6.64 KB
Newer Older
1
from pydantic import BaseModel, ConfigDict
Timothy J. Baek's avatar
Timothy J. Baek committed
2
3
from peewee import *
from playhouse.shortcuts import model_to_dict
4
5
from typing import List, Union, Optional
import time
Timothy J. Baek's avatar
Timothy J. Baek committed
6
7
from utils.misc import get_gravatar_url

8
from apps.webui.internal.db import DB, JSONField
9
from apps.webui.models.chats import Chats
10

11
12
13
14
15
####################
# User DB Schema
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
16
17
18
19
20
class User(Model):
    id = CharField(unique=True)
    name = CharField()
    email = CharField()
    role = CharField()
21
    profile_image_url = TextField()
Timothy J. Baek's avatar
Timothy J. Baek committed
22
23
24
25
26

    last_active_at = BigIntegerField()
    updated_at = BigIntegerField()
    created_at = BigIntegerField()

27
    api_key = CharField(null=True, unique=True)
28
    settings = JSONField(null=True)
Timothy J. Baek's avatar
Timothy J. Baek committed
29

30
31
    oauth_sub = TextField(null=True, unique=True)

Timothy J. Baek's avatar
Timothy J. Baek committed
32
33
34
35
    class Meta:
        database = DB


36
37
38
39
40
41
class UserSettings(BaseModel):
    ui: Optional[dict] = {}
    model_config = ConfigDict(extra="allow")
    pass


42
43
44
45
class UserModel(BaseModel):
    id: str
    name: str
    email: str
Timothy J. Baek's avatar
Timothy J. Baek committed
46
    role: str = "pending"
47
    profile_image_url: str
Timothy J. Baek's avatar
Timothy J. Baek committed
48
49
50
51
52

    last_active_at: int  # timestamp in epoch
    updated_at: int  # timestamp in epoch
    created_at: int  # timestamp in epoch

53
    api_key: Optional[str] = None
54
    settings: Optional[UserSettings] = None
55

56
57
    oauth_sub: Optional[str] = None

58
59
60
61
62
63

####################
# Forms
####################


Timothy J. Baek's avatar
Timothy J. Baek committed
64
65
66
67
68
class UserRoleUpdateForm(BaseModel):
    id: str
    role: str


Timothy J. Baek's avatar
Timothy J. Baek committed
69
70
71
72
73
74
class UserUpdateForm(BaseModel):
    name: str
    email: str
    profile_image_url: str
    password: Optional[str] = None

75

Timothy J. Baek's avatar
Timothy J. Baek committed
76
class UsersTable:
77
78
    def __init__(self, db):
        self.db = db
Timothy J. Baek's avatar
Timothy J. Baek committed
79
        self.db.create_tables([User])
80

Timothy J. Baek's avatar
Timothy J. Baek committed
81
    def insert_new_user(
Danny Liu's avatar
Danny Liu committed
82
83
84
85
        self,
        id: str,
        name: str,
        email: str,
Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
86
        profile_image_url: str = "/user.png",
Danny Liu's avatar
Danny Liu committed
87
        role: str = "pending",
88
        oauth_sub: Optional[str] = None,
Timothy J. Baek's avatar
Timothy J. Baek committed
89
    ) -> Optional[UserModel]:
90
91
92
93
94
95
        user = UserModel(
            **{
                "id": id,
                "name": name,
                "email": email,
                "role": role,
96
                "profile_image_url": profile_image_url,
Timothy J. Baek's avatar
Timothy J. Baek committed
97
98
99
                "last_active_at": int(time.time()),
                "created_at": int(time.time()),
                "updated_at": int(time.time()),
100
                "oauth_sub": oauth_sub,
Timothy J. Baek's avatar
Timothy J. Baek committed
101
102
            }
        )
Timothy J. Baek's avatar
Timothy J. Baek committed
103
        result = User.create(**user.model_dump())
104
105
106
107
108
        if result:
            return user
        else:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
109
110
111
112
113
114
    def get_user_by_id(self, id: str) -> Optional[UserModel]:
        try:
            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None
115

116
117
118
119
120
121
122
    def get_user_by_api_key(self, api_key: str) -> Optional[UserModel]:
        try:
            user = User.get(User.api_key == api_key)
            return UserModel(**model_to_dict(user))
        except:
            return None

123
124
125
    def get_user_by_email(
        self, email: str, oauth_user: bool = False
    ) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
126
        try:
127
128
129
130
131
132
            conditions = (
                (User.email == email, User.oauth_sub.is_null())
                if not oauth_user
                else (User.email == email)
            )
            user = User.get(conditions)
Timothy J. Baek's avatar
Timothy J. Baek committed
133
134
            return UserModel(**model_to_dict(user))
        except:
135
136
            return None

137
138
139
140
141
142
143
    def get_user_by_oauth_sub(self, sub: str) -> Optional[UserModel]:
        try:
            user = User.get(User.oauth_sub == sub)
            return UserModel(**model_to_dict(user))
        except:
            return None

144
    def get_users(self, skip: int = 0, limit: int = 50) -> List[UserModel]:
145
        return [
Timothy J. Baek's avatar
Timothy J. Baek committed
146
            UserModel(**model_to_dict(user))
Timothy J. Baek's avatar
Timothy J. Baek committed
147
148
            for user in User.select()
            # .limit(limit).offset(skip)
149
150
        ]

151
    def get_num_users(self) -> Optional[int]:
Timothy J. Baek's avatar
Timothy J. Baek committed
152
        return User.select().count()
Timothy J. Baek's avatar
Timothy J. Baek committed
153

154
155
156
157
158
159
160
    def get_first_user(self) -> UserModel:
        try:
            user = User.select().order_by(User.created_at).first()
            return UserModel(**model_to_dict(user))
        except:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
161
    def update_user_role_by_id(self, id: str, role: str) -> Optional[UserModel]:
Timothy J. Baek's avatar
Timothy J. Baek committed
162
163
164
165
166
167
168
169
        try:
            query = User.update(role=role).where(User.id == id)
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None
Timothy J. Baek's avatar
Timothy J. Baek committed
170

171
172
173
174
175
176
177
178
179
180
181
182
183
184
    def update_user_profile_image_url_by_id(
        self, id: str, profile_image_url: str
    ) -> Optional[UserModel]:
        try:
            query = User.update(profile_image_url=profile_image_url).where(
                User.id == id
            )
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
185
186
187
188
189
190
191
192
193
194
    def update_user_last_active_by_id(self, id: str) -> Optional[UserModel]:
        try:
            query = User.update(last_active_at=int(time.time())).where(User.id == id)
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None

195
196
197
198
199
200
201
202
203
204
205
206
    def update_user_oauth_sub_by_id(
        self, id: str, oauth_sub: str
    ) -> Optional[UserModel]:
        try:
            query = User.update(oauth_sub=oauth_sub).where(User.id == id)
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None

Timothy J. Baek's avatar
Timothy J. Baek committed
207
208
209
210
211
212
213
214
215
216
    def update_user_by_id(self, id: str, updated: dict) -> Optional[UserModel]:
        try:
            query = User.update(**updated).where(User.id == id)
            query.execute()

            user = User.get(User.id == id)
            return UserModel(**model_to_dict(user))
        except:
            return None

217
218
219
    def delete_user_by_id(self, id: str) -> bool:
        try:
            # Delete User Chats
Timothy J. Baek's avatar
Timothy J. Baek committed
220
            result = Chats.delete_chats_by_user_id(id)
221

222
223
224
            if result:
                # Delete User
                query = User.delete().where(User.id == id)
Timothy J. Baek's avatar
Timothy J. Baek committed
225
                query.execute()  # Remove the rows, return number of rows removed.
226

227
228
229
                return True
            else:
                return False
230
231
232
        except:
            return False

Timothy J. Baek's avatar
refac  
Timothy J. Baek committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
    def update_user_api_key_by_id(self, id: str, api_key: str) -> str:
        try:
            query = User.update(api_key=api_key).where(User.id == id)
            result = query.execute()

            return True if result == 1 else False
        except:
            return False

    def get_user_api_key_by_id(self, id: str) -> Optional[str]:
        try:
            user = User.get(User.id == id)
            return user.api_key
        except:
            return None

249
250

Users = UsersTable(DB)