-
Notifications
You must be signed in to change notification settings - Fork 3
/
user_information_handler.go
95 lines (81 loc) · 3.18 KB
/
user_information_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2021 Monoskope Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package handler
import (
"context"
"github.com/finleap-connect/monoskope/pkg/domain/constants/users"
domainErrors "github.com/finleap-connect/monoskope/pkg/domain/errors"
metadata "github.com/finleap-connect/monoskope/pkg/domain/metadata"
"github.com/finleap-connect/monoskope/pkg/domain/projections"
"github.com/finleap-connect/monoskope/pkg/domain/repositories"
es "github.com/finleap-connect/monoskope/pkg/eventsourcing"
"github.com/finleap-connect/monoskope/pkg/logger"
)
type userInformationHandler struct {
log logger.Logger
userRepo repositories.ReadOnlyUserRepository
nextHandlerInChain es.CommandHandler
}
// NewUserInformationHandler creates a new CommandHandler which handles authorization.
func NewUserInformationHandler(userRepo repositories.ReadOnlyUserRepository) *userInformationHandler {
return &userInformationHandler{
log: logger.WithName("user-information-middleware"),
userRepo: userRepo,
}
}
func (m *userInformationHandler) Middleware(h es.CommandHandler) es.CommandHandler {
m.nextHandlerInChain = h
return m
}
// HandleCommand implements the CommandHandler interface
func (h *userInformationHandler) HandleCommand(ctx context.Context, cmd es.Command) (*es.CommandReply, error) {
var err error
// Gather context
metadataManager, err := metadata.NewDomainMetadataManager(ctx)
if err != nil {
return nil, err
}
userInfo := metadataManager.GetUserInformation()
if metadataManager.IsAuthorizationBypassed() {
h.log.V(logger.WarnLevel).Info("Authorization bypass enabled.", "CommandType", cmd.CommandType(), "AggregateType", cmd.AggregateType(), "User", userInfo.Email)
return h.nextHandlerInChain.HandleCommand(metadataManager.GetContext(), cmd)
}
var user *projections.User
// Check if system user
sysUser, isSysUser := users.AvailableSystemUsers[userInfo.Id]
if isSysUser {
user = sysUser
}
// Check that user exists
if !isSysUser {
user, err = h.userRepo.ByEmail(ctx, userInfo.Email)
if err != nil {
h.log.Info("User does not exist -> unauthorized.", "CommandType", cmd.CommandType(), "AggregateType", cmd.AggregateType(), "User", userInfo.Email)
return nil, domainErrors.ErrUnauthorized
}
}
// Enrich context with rolebindings and user information
userRoleBindings := user.GetRoles()
userInfo.Id = user.ID()
userInfo.Name = user.Name
userInfo.Email = user.Email
metadataManager.SetUserInformation(userInfo)
metadataManager.SetRoleBindings(userRoleBindings)
// Run next handler in chain
if h.nextHandlerInChain != nil {
return h.nextHandlerInChain.HandleCommand(metadataManager.GetContext(), cmd)
} else {
return nil, nil
}
}