user
This commit is contained in:
parent
4193b37b6b
commit
95c5592170
4 changed files with 54 additions and 1 deletions
|
@ -27,7 +27,7 @@ impl Artist {
|
|||
}
|
||||
|
||||
/// Gets the image of an artist or `None` if it can't be found.
|
||||
///
|
||||
///
|
||||
/// This function gets a track from the artist. It then expects the folder structure to be `Artist/Album/Track.ext` and searches for an image file named `artist` in the artist folder.
|
||||
pub async fn get_image_of(id: &str) -> Option<String> {
|
||||
let track_path = Track::find_one(doc! { "artist_id": reference_of!(Artist, id)})
|
||||
|
|
|
@ -68,6 +68,23 @@ impl User {
|
|||
Some(u.session().await)
|
||||
}
|
||||
|
||||
/// Change the password of a `User`
|
||||
pub async fn passwd(&mut self, old: &str, new: &str) -> Result<(), ()> {
|
||||
if self.verify_pw(old) {
|
||||
self.update(&json!(
|
||||
{
|
||||
"password": bcrypt::hash(new, bcrypt::DEFAULT_COST).unwrap()
|
||||
}
|
||||
))
|
||||
.await
|
||||
.map_err(|_| ())?;
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Err(())
|
||||
}
|
||||
|
||||
pub async fn session(&self) -> Session {
|
||||
let s = Session {
|
||||
_id: uuid::Uuid::new_v4().to_string(),
|
||||
|
|
|
@ -48,6 +48,8 @@ async fn rocket() -> _ {
|
|||
route::track::track_audio_route,
|
||||
route::album::album_cover_route,
|
||||
route::user::login_route,
|
||||
route::user::passwd_route,
|
||||
route::user::user_create_route,
|
||||
route::track::track_audio_opus128_route
|
||||
],
|
||||
)
|
||||
|
|
|
@ -49,3 +49,37 @@ pub async fn login_route(login: Json<LoginData>) -> FallibleApiResponse {
|
|||
"token": ses.token
|
||||
}))
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct PasswdData {
|
||||
pub old: String,
|
||||
pub new: String,
|
||||
}
|
||||
|
||||
#[post("/passwd", data = "<passwd>")]
|
||||
pub async fn passwd_route(passwd: Json<PasswdData>, mut u: User) -> FallibleApiResponse {
|
||||
u.passwd(&passwd.old, &passwd.new)
|
||||
.await
|
||||
.map_err(|_| api_error("Password change failed"))?;
|
||||
|
||||
Ok(json!({
|
||||
"ok": 1
|
||||
}))
|
||||
}
|
||||
|
||||
#[post("/userCreate", data = "<user>")]
|
||||
pub async fn user_create_route(user: Json<LoginData>, u: User) -> FallibleApiResponse {
|
||||
if !u.is_admin() {
|
||||
return Err(api_error("Forbidden"));
|
||||
}
|
||||
|
||||
let new_user = User::create(
|
||||
&user.username,
|
||||
&user.password,
|
||||
crate::library::user::UserRole::Regular,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(json!({"created": new_user._id}))
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue