-
Notifications
You must be signed in to change notification settings - Fork 206
/
context.go
240 lines (190 loc) · 5.4 KB
/
context.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved.
//
// This software (Documize Community Edition) is licensed under
// GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html
//
// You can operate outside the AGPL restrictions by purchasing
// Documize Enterprise Edition and obtaining a commercial license
// by contacting <sales@documize.com>.
//
// https://documize.com
package request
import (
"database/sql"
"fmt"
"net/http"
"strings"
"time"
"github.com/gorilla/context"
"github.com/jmoiron/sqlx"
"github.com/documize/community/core/log"
)
var rc = Context{}
// Context holds the context in which the client is dealing with Documize.
type Context struct {
AllowAnonymousAccess bool
Authenticated bool
Administrator bool
Guest bool
Editor bool
Global bool
UserID string
OrgID string
OrgName string
SSL bool
AppURL string // e.g. https://{url}.documize.com
Subdomain string
ClientIP string
Expires time.Time
Transaction *sqlx.Tx
}
//GetAppURL returns full HTTP url for the app
func (c *Context) GetAppURL(endpoint string) string {
scheme := "http://"
if c.SSL {
scheme = "https://"
}
return fmt.Sprintf("%s%s/%s", scheme, c.AppURL, endpoint)
}
// NewContext simply returns a blank Context type.
func NewContext() Context {
return Context{}
}
func getContext(r *http.Request) Context {
if value := context.Get(r, rc); value != nil {
return value.(Context)
}
return Context{}
}
// SetContext simply calls the Set method on the passed context, using the empty context stored in rc as an extra parameter.
func SetContext(r *http.Request, c Context) {
c.AppURL = r.Host
c.Subdomain = GetSubdomainFromHost(r)
c.SSL = r.TLS != nil
// get user IP from request
i := strings.LastIndex(r.RemoteAddr, ":")
if i == -1 {
c.ClientIP = r.RemoteAddr
} else {
c.ClientIP = r.RemoteAddr[:i]
}
fip := r.Header.Get("X-Forwarded-For")
if len(fip) > 0 {
c.ClientIP = fip
}
context.Set(r, rc, c)
}
// Persister stores the Context of the client along with a baseManager instance.
type Persister struct {
Context Context
Base baseManager
}
// GetPersister reurns a Persister which contains a Context which is based on the incoming request.
func GetPersister(r *http.Request) Persister {
var p = Persister{}
p.Context = getContext(r)
p.Context.AppURL = r.Host
p.Context.SSL = r.TLS != nil
return p
}
// CanViewDocumentInFolder returns if the user has permission to view a document within the specified folder.
func (p *Persister) CanViewDocumentInFolder(labelID string) (hasPermission bool) {
roles, err := p.GetUserLabelRoles()
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
log.Error(fmt.Sprintf("Unable to check folder %s for permission check", labelID), err)
return false
}
for _, role := range roles {
if role.LabelID == labelID && (role.CanView || role.CanEdit) {
return true
}
}
return false
}
// CanViewDocument returns if the clinet has permission to view a given document.
func (p *Persister) CanViewDocument(documentID string) (hasPermission bool) {
document, err := p.GetDocument(documentID)
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err)
return false
}
roles, err := p.GetUserLabelRoles()
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err)
return false
}
for _, role := range roles {
if role.LabelID == document.LabelID && (role.CanView || role.CanEdit) {
return true
}
}
return false
}
// CanChangeDocument returns if the clinet has permission to change a given document.
func (p *Persister) CanChangeDocument(documentID string) (hasPermission bool) {
document, err := p.GetDocument(documentID)
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err)
return false
}
roles, err := p.GetUserLabelRoles()
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err)
return false
}
for _, role := range roles {
if role.LabelID == document.LabelID && role.CanEdit {
return true
}
}
return false
}
// CanUploadDocument returns if the client has permission to upload documents to the given folderID.
func (p *Persister) CanUploadDocument(folderID string) (hasPermission bool) {
roles, err := p.GetUserLabelRoles()
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
log.Error(fmt.Sprintf("Unable to check permission for folder %s", folderID), err)
return false
}
for _, role := range roles {
if role.LabelID == folderID && role.CanEdit {
return true
}
}
return false
}
// CanViewFolder returns if the user has permission to view the given folderID.
func (p *Persister) CanViewFolder(folderID string) (hasPermission bool) {
roles, err := p.GetUserLabelRoles()
if err == sql.ErrNoRows {
err = nil
}
if err != nil {
log.Error(fmt.Sprintf("Unable to check permission for folder %s", folderID), err)
return false
}
for _, role := range roles {
if role.LabelID == folderID && (role.CanView || role.CanEdit) {
return true
}
}
return false
}