Content-Length: 324676 | pFad | https://github.com/sebadob/rauthy/commit/1061fc212bb684b153ae99f1439ea312091e32cd

47 Merge pull request #25 from sebadob/update-language-endpoint · sebadob/rauthy@1061fc2 · GitHub
Skip to content

Commit

Permalink
Merge pull request #25 from sebadob/update-language-endpoint
Browse files Browse the repository at this point in the history
update user language for principal endpoint
  • Loading branch information
sebadob authored Aug 18, 2023
2 parents 77886a9 + 6fef29a commit 1061fc2
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
28 changes: 25 additions & 3 deletions rauthy-handlers/src/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use rauthy_models::entity::password::{PasswordHashTimes, PasswordPolicy};
use rauthy_models::entity::pow::Pow;
use rauthy_models::entity::principal::Principal;
use rauthy_models::entity::sessions::Session;
use rauthy_models::entity::users::User;
use rauthy_models::i18n::account::I18nAccount;
use rauthy_models::i18n::authorize::I18nAuthorize;
use rauthy_models::i18n::index::I18nIndex;
Expand All @@ -36,7 +37,6 @@ use rauthy_models::templates::{
use rauthy_service::encryption;
use redhac::{cache_get, cache_get_from, cache_get_value};
use std::borrow::Cow;
use tracing::debug;

#[get("/")]
#[has_permissions("all")]
Expand Down Expand Up @@ -89,8 +89,6 @@ pub async fn post_i18n(
req_data: Json<I18nRequest>,
) -> Result<HttpResponse, ErrorResponse> {
let lang = Language::try_from(&req).unwrap_or_default();
debug!("post_i18n lang: {:?} req: {:?}", lang, req_data);

let body = match req_data.content {
I18nContent::Authorize => I18nAuthorize::build(&lang).as_json(),
I18nContent::Account => I18nAccount::build(&lang).as_json(),
Expand Down Expand Up @@ -542,6 +540,30 @@ pub async fn get_pow(data: web::Data<AppState>) -> Result<HttpResponse, ErrorRes
Ok(HttpResponse::Ok().json(pow))
}

//github.com/ Updates the language for the logged in principal depending on the `locale` cookie
#[utoipa::path(
post,
path = "/update_language",
tag = "generic",
responses(
(status = 200, description = "Valid Token / Session"),
(status = 401, description = "Unauthorized"),
),
)]
#[post("/update_language")]
#[has_any_permission("token-auth", "session-auth")]
pub async fn post_update_language(
data: web::Data<AppState>,
principal: web::ReqData<Option<Principal>>,
req: HttpRequest,
) -> Result<HttpResponse, ErrorResponse> {
let principal = Principal::get_from_req(principal.into_inner())?;
let mut user = User::find(&data, principal.user_id).await?;
user.language = Language::try_from(&req).unwrap_or_default();
user.update_language(&data).await?;
Ok(HttpResponse::Ok().finish())
}

//github.com/ Backend health state
//github.com/
//github.com/ Health endpoint to get some additional information about the backend status, if it exists.
Expand Down
1 change: 1 addition & 0 deletions rauthy-main/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ async fn actix_main(app_state: web::Data<AppState>) -> std::io::Result<()> {
.service(generic::get_auth_check)
.service(generic::get_auth_check_admin)
.service(generic::post_i18n)
.service(generic::post_update_language)
.service(oidc::get_authorize)
.service(oidc::post_authorize)
.service(oidc::get_callback_html)
Expand Down
11 changes: 11 additions & 0 deletions rauthy-models/src/entity/users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,17 @@ impl User {
Ok(user)
}

pub async fn update_language(&self, data: &web::Data<AppState>) -> Result<(), ErrorResponse> {
let lang = self.language.as_str();
sqlx::query(r#"update users set language = $1 where id = $2"#)
.bind(lang)
.bind(&self.id)
.execute(&data.db)
.await?;

Ok(())
}

// Updates a user from himself. This is needed for the account page to make each user able to
// update its own data.
pub async fn update_self_req(
Expand Down

0 comments on commit 1061fc2

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/sebadob/rauthy/commit/1061fc212bb684b153ae99f1439ea312091e32cd

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy