Skip to content

Commit

Permalink
Allow an user to set a preferred language (#3508)
Browse files Browse the repository at this point in the history
* update preferred user language

* add changelog and fix linter

* fix linter
  • Loading branch information
gmgigi96 authored Dec 1, 2022
1 parent 41d9411 commit 1949ac5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 1 deletion.
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
73 changes: 72 additions & 1 deletion internal/http/services/owncloud/ocs/handlers/cloud/user/user.go
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

0 comments on commit 1949ac5

Please sign in to comment.