-
Notifications
You must be signed in to change notification settings - Fork 2
/
app_data.go
90 lines (73 loc) · 2.24 KB
/
app_data.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
package page
import (
"context"
"net/http"
"net/url"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor"
"github.com/ministryofjustice/opg-modernising-lpa/internal/actor/actoruid"
"github.com/ministryofjustice/opg-modernising-lpa/internal/localize"
)
type AppData struct {
Page string
Path string
Query url.Values
Localizer Localizer
Lang localize.Lang
CookieConsentSet bool
CanGoBack bool
CanToggleWelsh bool
SessionID string
LpaID string
CsrfToken string
ActorType actor.Type
AttorneyUID actoruid.UID
LoginSessionEmail string
SupporterData *SupporterData
}
type SupporterData struct {
LpaType actor.LpaType
DonorFullName string
OrganisationName string
IsManageOrganisation bool
Permission actor.Permission
LoggedInSupporterID string
}
func (d AppData) Redirect(w http.ResponseWriter, r *http.Request, url string) error {
http.Redirect(w, r, d.Lang.URL(url), http.StatusFound)
return nil
}
func ContextWithAppData(ctx context.Context, appData AppData) context.Context {
return context.WithValue(ctx, contextKey("appData"), appData)
}
func AppDataFromContext(ctx context.Context) AppData {
appData, _ := ctx.Value(contextKey("appData")).(AppData)
return appData
}
func (d AppData) IsDonor() bool {
return d.ActorType == actor.TypeDonor
}
func (d AppData) IsCertificateProvider() bool {
return d.ActorType == actor.TypeCertificateProvider
}
func (d AppData) IsAttorneyType() bool {
return d.ActorType == actor.TypeAttorney ||
d.ActorType == actor.TypeReplacementAttorney ||
d.ActorType == actor.TypeTrustCorporation ||
d.ActorType == actor.TypeReplacementTrustCorporation
}
func (d AppData) IsReplacementAttorney() bool {
return d.ActorType == actor.TypeReplacementAttorney
}
func (d AppData) IsTrustCorporation() bool {
return d.ActorType == actor.TypeTrustCorporation || d.ActorType == actor.TypeReplacementTrustCorporation
}
func (d AppData) IsAdmin() bool {
return d.SupporterData != nil && d.SupporterData.Permission.IsAdmin()
}
func (d AppData) EncodeQuery() string {
query := ""
if d.Query.Encode() != "" {
query = "?" + d.Query.Encode()
}
return query
}