Skip to content

Commit

Permalink
feat(db, webserver): Support user avatars
Browse files Browse the repository at this point in the history
  • Loading branch information
boxbeam committed Mar 18, 2024
1 parent 3c3f7d7 commit 1af6734
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions ee/tabby-db/migrations/0019_user-avatar.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users DROP COLUMN avatar;
1 change: 1 addition & 0 deletions ee/tabby-db/migrations/0019_user-avatar.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE users ADD COLUMN avatar TEXT;
Binary file modified ee/tabby-db/schema.sqlite
Binary file not shown.
13 changes: 13 additions & 0 deletions ee/tabby-db/src/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct UserDAO {
/// To authenticate IDE extensions / plugins to access code completion / chat api endpoints.
pub auth_token: String,
pub active: bool,
pub avatar: Option<String>,
}

static OWNER_USER_ID: i32 = 1;
Expand Down Expand Up @@ -130,6 +131,7 @@ impl DbConn {
"updated_at",
"auth_token",
"active",
"avatar",
],
limit,
skip_id,
Expand Down Expand Up @@ -221,6 +223,17 @@ impl DbConn {
Ok(())
}

pub async fn update_user_avatar(&self, id: i32, avatar_base64: Option<String>) -> Result<()> {
query!(
"UPDATE users SET avatar = ? WHERE id = ?;",
avatar_base64,
id
)
.execute(&self.pool)
.await?;
Ok(())
}

pub async fn count_active_users(&self) -> Result<usize> {
self.cache
.active_user_count
Expand Down
2 changes: 2 additions & 0 deletions ee/tabby-webserver/src/schema/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ pub struct User {
pub created_at: DateTime<Utc>,
pub active: bool,
pub is_password_set: bool,
pub avatar: Option<String>,
}

impl relay::NodeType for User {
Expand Down Expand Up @@ -467,6 +468,7 @@ pub trait AuthenticationService: Send + Sync {
async fn delete_oauth_credential(&self, provider: OAuthProvider) -> Result<()>;
async fn update_user_active(&self, id: &ID, active: bool) -> Result<()>;
async fn update_user_role(&self, id: &ID, is_admin: bool) -> Result<()>;
async fn update_user_avatar(&self, id: &ID, avatar: Option<String>) -> Result<()>;
}

fn validate_password(value: &str) -> Result<(), validator::ValidationError> {
Expand Down
11 changes: 11 additions & 0 deletions ee/tabby-webserver/src/schema/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,17 @@ impl Mutation {
Ok(true)
}

async fn update_user_avatar(ctx: &Context, id: ID, avatar: Option<String>) -> Result<bool> {
let claims = check_claims(ctx)?;
if claims.sub.0 != id && !check_admin(ctx).is_ok() {
return Err(CoreError::Unauthorized(
"You cannot change another user's avatar",
));
}
ctx.locator.auth().update_user_avatar(&id, avatar).await?;
Ok(true)
}

async fn register(
ctx: &Context,
email: String,
Expand Down
6 changes: 6 additions & 0 deletions ee/tabby-webserver/src/service/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ impl AuthenticationService for AuthenticationServiceImpl {
Ok(())
}

async fn update_user_avatar(&self, id: &ID, avatar: Option<String>) -> Result<()> {
let id = id.as_rowid()?;
self.db.update_user_avatar(id, avatar).await?;
Ok(())
}

async fn token_auth(&self, email: String, password: String) -> Result<TokenAuthResponse> {
let Some(user) = self.db.get_user_by_email(&email).await? else {
return Err(anyhow!("Invalid email address or password").into());
Expand Down
1 change: 1 addition & 0 deletions ee/tabby-webserver/src/service/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl From<UserDAO> for auth::User {
created_at: val.created_at,
active: val.active,
is_password_set: val.password_encrypted.is_some(),
avatar: val.avatar,
}
}
}
Expand Down

0 comments on commit 1af6734

Please sign in to comment.