Skip to content

Commit acc1474

Browse files
BlackDexdani-garcia
authored andcommitted
Add avatar color support
The new web-vault v2023.1.0 supports a custom color for the avatar. bitwarden/server#2330 This PR adds this feature.
1 parent c90b303 commit acc1474

File tree

12 files changed

+42
-1
lines changed

12 files changed

+42
-1
lines changed

migrations/mysql/2023-01-11-205851_add_avatar_color/down.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE users
2+
ADD COLUMN avatar_color VARCHAR(7);

migrations/postgresql/2023-01-11-205851_add_avatar_color/down.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE users
2+
ADD COLUMN avatar_color TEXT;

migrations/sqlite/2023-01-11-205851_add_avatar_color/down.sql

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE users
2+
ADD COLUMN avatar_color TEXT;

src/api/core/accounts.rs

+27
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub fn routes() -> Vec<rocket::Route> {
3939
api_key,
4040
rotate_api_key,
4141
get_known_device,
42+
put_avatar,
4243
]
4344
}
4445

@@ -228,6 +229,32 @@ async fn post_profile(data: JsonUpcase<ProfileData>, headers: Headers, mut conn:
228229
Ok(Json(user.to_json(&mut conn).await))
229230
}
230231

232+
#[derive(Deserialize)]
233+
#[allow(non_snake_case)]
234+
struct AvatarData {
235+
AvatarColor: Option<String>,
236+
}
237+
238+
#[put("/accounts/avatar", data = "<data>")]
239+
async fn put_avatar(data: JsonUpcase<AvatarData>, headers: Headers, mut conn: DbConn) -> JsonResult {
240+
let data: AvatarData = data.into_inner().data;
241+
242+
// It looks like it only supports the 6 hex color format.
243+
// If you try to add the short value it will not show that color.
244+
// Check and force 7 chars, including the #.
245+
if let Some(color) = &data.AvatarColor {
246+
if color.len() != 7 {
247+
err!("The field AvatarColor must be a HTML/Hex color code with a length of 7 characters")
248+
}
249+
}
250+
251+
let mut user = headers.user;
252+
user.avatar_color = data.AvatarColor;
253+
254+
user.save(&mut conn).await?;
255+
Ok(Json(user.to_json(&mut conn).await))
256+
}
257+
231258
#[get("/users/<uuid>/public-key")]
232259
async fn get_public_keys(uuid: String, _headers: Headers, mut conn: DbConn) -> JsonResult {
233260
let user = match User::find_by_uuid(&uuid, &mut conn).await {

src/db/models/user.rs

+5
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ db_object! {
4646
pub client_kdf_iter: i32,
4747

4848
pub api_key: Option<String>,
49+
50+
pub avatar_color: Option<String>,
4951
}
5052

5153
#[derive(Identifiable, Queryable, Insertable)]
@@ -113,6 +115,8 @@ impl User {
113115
client_kdf_iter: Self::CLIENT_KDF_ITER_DEFAULT,
114116

115117
api_key: None,
118+
119+
avatar_color: None,
116120
}
117121
}
118122

@@ -226,6 +230,7 @@ impl User {
226230
"Providers": [],
227231
"ProviderOrganizations": [],
228232
"ForcePasswordReset": false,
233+
"AvatarColor": self.avatar_color,
229234
"Object": "profile",
230235
})
231236
}

src/db/schemas/mysql/schema.rs

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ table! {
200200
client_kdf_type -> Integer,
201201
client_kdf_iter -> Integer,
202202
api_key -> Nullable<Text>,
203+
avatar_color -> Nullable<Text>,
203204
}
204205
}
205206

src/db/schemas/postgresql/schema.rs

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ table! {
200200
client_kdf_type -> Integer,
201201
client_kdf_iter -> Integer,
202202
api_key -> Nullable<Text>,
203+
avatar_color -> Nullable<Text>,
203204
}
204205
}
205206

src/db/schemas/sqlite/schema.rs

+1
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ table! {
200200
client_kdf_type -> Integer,
201201
client_kdf_iter -> Integer,
202202
api_key -> Nullable<Text>,
203+
avatar_color -> Nullable<Text>,
203204
}
204205
}
205206

src/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
// The more key/value pairs there are the more recursion occurs.
3535
// We want to keep this as low as possible, but not higher then 128.
3636
// If you go above 128 it will cause rust-analyzer to fail,
37-
#![recursion_limit = "94"]
37+
#![recursion_limit = "97"]
3838

3939
// When enabled use MiMalloc as malloc instead of the default malloc
4040
#[cfg(feature = "enable_mimalloc")]

0 commit comments

Comments
 (0)