Add a language selector for logged in users.
This commit is contained in:
parent
0b9afb1fa3
commit
dd759923f0
@ -323,7 +323,11 @@ async fn webservice(server_config: ServerConfig) -> std::io::Result<()> {
|
||||
web::post().to(views::process_edit_profile),
|
||||
),
|
||||
)
|
||||
.route("/set_admin/{user_id}", web::get().to(views::toggle_admin)),
|
||||
.route("/set_admin/{user_id}", web::get().to(views::toggle_admin))
|
||||
.route(
|
||||
"/set_language/{language}",
|
||||
web::get().to(views::set_language),
|
||||
),
|
||||
)
|
||||
.service(
|
||||
web::scope("/delete").service(
|
||||
|
@ -63,6 +63,7 @@ impl User {
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn toggle_admin(
|
||||
self,
|
||||
server_config: &ServerConfig,
|
||||
@ -74,6 +75,21 @@ impl User {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn set_language(
|
||||
self,
|
||||
server_config: &ServerConfig,
|
||||
new_language: &str,
|
||||
) -> Result<(), ServerError> {
|
||||
sqlx::query!(
|
||||
"UPDATE users SET language = ? where id = ?",
|
||||
new_language,
|
||||
self.id
|
||||
)
|
||||
.execute(&server_config.db_pool)
|
||||
.await?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub(crate) async fn count_admins(server_config: &ServerConfig) -> Result<Count, ServerError> {
|
||||
let num = sqlx::query_as!(Count, "select count(*) as number from users where role = 2")
|
||||
.fetch_one(&server_config.db_pool)
|
||||
|
@ -305,6 +305,26 @@ pub(crate) async fn toggle_admin(
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) async fn set_language(
|
||||
id: &Identity,
|
||||
lang_code: &str,
|
||||
server_config: &ServerConfig,
|
||||
) -> Result<(), ServerError> {
|
||||
match lang_code {
|
||||
"de" | "en" => match authenticate(id, server_config).await? {
|
||||
Role::Admin { user } | Role::Regular { user } => {
|
||||
user.set_language(server_config, lang_code).await
|
||||
}
|
||||
Role::Disabled | Role::NotAuthenticated => {
|
||||
Err(ServerError::User("Not Allowed".to_owned()))
|
||||
}
|
||||
},
|
||||
_ => Err(ServerError::User(
|
||||
"This language is not supported!".to_owned(),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get one link if permissions are accordingly.
|
||||
pub(crate) async fn get_link(
|
||||
id: &Identity,
|
||||
|
@ -282,6 +282,15 @@ pub(crate) async fn toggle_admin(
|
||||
)))
|
||||
}
|
||||
|
||||
pub(crate) async fn set_language(
|
||||
data: web::Path<String>,
|
||||
config: web::Data<crate::ServerConfig>,
|
||||
id: Identity,
|
||||
) -> Result<HttpResponse, ServerError> {
|
||||
queries::set_language(&id, &data.0, &config).await?;
|
||||
Ok(redirect_builder("/admin/index/"))
|
||||
}
|
||||
|
||||
pub(crate) async fn login(
|
||||
tera: web::Data<Tera>,
|
||||
id: Identity,
|
||||
|
@ -22,6 +22,8 @@
|
||||
<div class="willkommen">{{ fluent(key="welcome-user", lang=user.language, username=user.username) }}
|
||||
</div>
|
||||
</li>
|
||||
<li style="float:right"><a href="/admin/edit/set_language/en">en</a></li>
|
||||
<li style="float:right"><a href="/admin/edit/set_language/de">de</a></li>
|
||||
</ol>
|
||||
</nav>
|
||||
{% block admin %}
|
||||
|
Loading…
Reference in New Issue
Block a user