-
Notifications
You must be signed in to change notification settings - Fork 5
/
checks.go
43 lines (36 loc) · 1.05 KB
/
checks.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
package impl
import (
"context"
cherry "git.containerum.net/ch/user-manager/pkg/umErrors"
"github.com/containerum/utils/httputil"
)
func (u *serverImpl) CheckUserExist(ctx context.Context) error {
userID := httputil.MustGetUserID(ctx)
u.log.WithField("user_id", userID).Info("checking if user exists")
user, err := u.svc.DB.GetUserByID(ctx, userID)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrPermissionsError()
}
if err := u.loginUserChecks(ctx, user); err != nil {
return err
}
return nil
}
func (u *serverImpl) CheckAdmin(ctx context.Context) error {
userID := httputil.MustGetUserID(ctx)
u.log.WithField("user_id", userID).Info("checking if user is admin")
user, err := u.svc.DB.GetUserByID(ctx, userID)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrPermissionsError()
}
if err := u.loginUserChecks(ctx, user); err != nil {
return err
}
if user.Role != "admin" {
u.log.WithError(cherry.ErrAdminRequired())
return cherry.ErrAdminRequired()
}
return nil
}