Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow an user to set a preferred language #3508

Merged
merged 3 commits into from
Dec 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions changelog/unreleased/set-user-language.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Enhancement: Allow an user to set a preferred language

https://github.com/cs3org/reva/pull/3508
1 change: 1 addition & 0 deletions internal/http/services/owncloud/ocs/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type Config struct {
ResourceInfoCacheTTL int `mapstructure:"resource_info_cache_ttl"`
ResourceInfoCacheDrivers map[string]map[string]interface{} `mapstructure:"resource_info_caches"`
UserIdentifierCacheTTL int `mapstructure:"user_identifier_cache_ttl"`
AllowedLanguages []string `mapstructure:"allowed_languages"`
}

// Init sets sane defaults.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ package user

import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"

Expand All @@ -34,12 +36,17 @@ import (

// The Handler renders the user endpoint.
type Handler struct {
gatewayAddr string
gatewayAddr string
allowedLanguages []string
}

// Init initializes this and any contained handlers.
func (h *Handler) Init(c *config.Config) {
h.gatewayAddr = c.GatewaySvc
h.allowedLanguages = c.AllowedLanguages
if len(h.allowedLanguages) == 0 {
h.allowedLanguages = []string{"cs", "de", "en", "es", "fr", "it", "gl"}
}
}

const (
Expand Down Expand Up @@ -84,6 +91,70 @@ func (h *Handler) getLanguage(ctx context.Context) string {
return res.GetVal()
}

type updateSelfRequest struct {
Language string `json:"language"`
}

// UpdateSelf handles PATCH requests on /cloud/user.
func (h *Handler) UpdateSelf(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()

req, err := parseUpdateSelfRequest(r)
if err != nil {
response.WriteOCSError(w, r, response.MetaBadRequest.StatusCode, "body request not valid", err)
return
}

if req.Language != "" {
if !h.isLanguageAllowed(req.Language) {
response.WriteOCSError(w, r, response.MetaBadRequest.StatusCode, "language not allowed", fmt.Errorf("language not allowed"))
return
}
if err := h.updateLanguage(ctx, req.Language); err != nil {
response.WriteOCSError(w, r, response.MetaServerError.StatusCode, "error setting language", err)
return
}
}
}

func (h *Handler) updateLanguage(ctx context.Context, lang string) error {
gw, err := pool.GetGatewayServiceClient(pool.Endpoint(h.gatewayAddr))
if err != nil {
return err
}
res, err := gw.SetKey(ctx, &preferences.SetKeyRequest{
Key: &preferences.PreferenceKey{
Namespace: languageNamespace,
Key: languageKey,
},
Val: lang,
})
if err != nil {
return err
}
if res.Status.Code != rpc.Code_CODE_OK {
return errors.New(res.Status.Message)
}
return nil
}

func (h *Handler) isLanguageAllowed(lang string) bool {
for _, l := range h.allowedLanguages {
if l == lang {
return true
}
}
return false
}

func parseUpdateSelfRequest(r *http.Request) (updateSelfRequest, error) {
var req updateSelfRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
return updateSelfRequest{}, err
}
return req, nil
}

// User holds user data.
type User struct {
// TODO needs better naming, clarify if we need a userid, a username or both
Expand Down
1 change: 1 addition & 0 deletions internal/http/services/owncloud/ocs/ocs.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func (s *svc) routerInit() error {
r.Route("/cloud", func(r chi.Router) {
r.Get("/capabilities", capabilitiesHandler.GetCapabilities)
r.Get("/user", userHandler.GetSelf)
r.Patch("/user", userHandler.UpdateSelf)
r.Route("/users", func(r chi.Router) {
r.Get("/{userid}", usersHandler.GetUsers)
r.Get("/{userid}/groups", usersHandler.GetGroups)
Expand Down