-
Notifications
You must be signed in to change notification settings - Fork 5
/
domain_blacklist.go
83 lines (68 loc) · 2.38 KB
/
domain_blacklist.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
package impl
import (
"context"
"git.containerum.net/ch/user-manager/pkg/db"
"git.containerum.net/ch/user-manager/pkg/models"
cherry "git.containerum.net/ch/user-manager/pkg/umErrors"
"github.com/containerum/utils/httputil"
"github.com/pkg/errors"
)
func (u *serverImpl) GetBlacklistedDomain(ctx context.Context, domain string) (*models.Domain, error) {
u.log.WithField("domain", domain).Info("get domain info")
blacklistedDomain, err := u.svc.DB.GetBlacklistedDomain(ctx, domain)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return nil, cherry.ErrUnableGetDomainBlacklist()
}
if blacklistedDomain == nil {
u.log.WithError(errors.New("dom not blacklist"))
return nil, cherry.ErrDomainNotBlacklisted()
}
return &models.Domain{
Domain: blacklistedDomain.Domain,
AddedBy: blacklistedDomain.AddedBy.String,
CreatedAt: blacklistedDomain.CreatedAt,
}, nil
}
func (u *serverImpl) GetBlacklistedDomainsList(ctx context.Context) (*models.DomainListResponse, error) {
u.log.Info("get domains list")
blacklistedDomains, err := u.svc.DB.GetBlacklistedDomainsList(ctx)
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return nil, cherry.ErrUnableGetDomainBlacklist()
}
resp := models.DomainListResponse{
DomainList: []models.Domain{},
}
for _, v := range blacklistedDomains {
resp.DomainList = append(resp.DomainList, models.Domain{
Domain: v.Domain,
AddedBy: v.AddedBy.String,
CreatedAt: v.CreatedAt,
})
}
return &resp, nil
}
func (u *serverImpl) AddDomainToBlacklist(ctx context.Context, request models.Domain) error {
u.log.WithField("domain", request.Domain).Info("adding domain to blacklist")
userID := httputil.MustGetUserID(ctx)
err := u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
return tx.BlacklistDomain(ctx, request.Domain, userID)
})
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableBlacklistDomain()
}
return nil
}
func (u *serverImpl) RemoveDomainFromBlacklist(ctx context.Context, domain string) error {
u.log.WithField("domain", domain).Info("removing domain from blacklist")
err := u.svc.DB.Transactional(ctx, func(ctx context.Context, tx db.DB) error {
return tx.UnBlacklistDomain(ctx, domain)
})
if err := u.handleDBError(err); err != nil {
u.log.WithError(err)
return cherry.ErrUnableUnblacklistDomain()
}
return nil
}