github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/api/request/context.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package request 13 14 import ( 15 "database/sql" 16 "fmt" 17 "net/http" 18 "time" 19 20 "github.com/gorilla/context" 21 "github.com/jmoiron/sqlx" 22 23 "github.com/documize/community/core/log" 24 ) 25 26 var rc = Context{} 27 28 // Context holds the context in which the client is dealing with Documize. 29 type Context struct { 30 AllowAnonymousAccess bool 31 Authenticated bool 32 Administrator bool 33 Guest bool 34 Editor bool 35 Global bool 36 UserID string 37 OrgID string 38 OrgName string 39 SSL bool 40 AppURL string // e.g. https://{url}.documize.com 41 Subdomain string 42 Expires time.Time 43 Transaction *sqlx.Tx 44 } 45 46 //GetAppURL returns full HTTP url for the app 47 func (c *Context) GetAppURL(endpoint string) string { 48 scheme := "http://" 49 50 if c.SSL { 51 scheme = "https://" 52 } 53 54 return fmt.Sprintf("%s%s/%s", scheme, c.AppURL, endpoint) 55 } 56 57 // NewContext simply returns a blank Context type. 58 func NewContext() Context { 59 return Context{} 60 } 61 62 func getContext(r *http.Request) Context { 63 64 if value := context.Get(r, rc); value != nil { 65 return value.(Context) 66 } 67 68 return Context{} 69 } 70 71 // SetContext simply calls the Set method on the passed context, using the empty context stored in rc as an extra parameter. 72 func SetContext(r *http.Request, c Context) { 73 c.AppURL = r.Host 74 c.Subdomain = GetSubdomainFromHost(r) 75 c.SSL = r.TLS != nil 76 77 context.Set(r, rc, c) 78 } 79 80 // Persister stores the Context of the client along with a baseManager instance. 81 type Persister struct { 82 Context Context 83 Base baseManager 84 } 85 86 // GetPersister reurns a Persister which contains a Context which is based on the incoming request. 87 func GetPersister(r *http.Request) Persister { 88 var p = Persister{} 89 p.Context = getContext(r) 90 p.Context.AppURL = r.Host 91 p.Context.SSL = r.TLS != nil 92 93 return p 94 } 95 96 // CanViewDocumentInFolder returns if the user has permission to view a document within the specified folder. 97 func (p *Persister) CanViewDocumentInFolder(labelID string) (hasPermission bool) { 98 roles, err := p.GetUserLabelRoles() 99 100 if err == sql.ErrNoRows { 101 err = nil 102 } 103 104 if err != nil { 105 log.Error(fmt.Sprintf("Unable to check folder %s for permission check", labelID), err) 106 return false 107 } 108 109 for _, role := range roles { 110 if role.LabelID == labelID && (role.CanView || role.CanEdit) { 111 return true 112 } 113 } 114 115 return false 116 } 117 118 // CanViewDocument returns if the clinet has permission to view a given document. 119 func (p *Persister) CanViewDocument(documentID string) (hasPermission bool) { 120 document, err := p.GetDocument(documentID) 121 122 if err == sql.ErrNoRows { 123 err = nil 124 } 125 126 if err != nil { 127 log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err) 128 return false 129 } 130 131 roles, err := p.GetUserLabelRoles() 132 133 if err == sql.ErrNoRows { 134 err = nil 135 } 136 137 if err != nil { 138 log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err) 139 return false 140 } 141 142 for _, role := range roles { 143 if role.LabelID == document.LabelID && (role.CanView || role.CanEdit) { 144 return true 145 } 146 } 147 148 return false 149 } 150 151 // CanChangeDocument returns if the clinet has permission to change a given document. 152 func (p *Persister) CanChangeDocument(documentID string) (hasPermission bool) { 153 document, err := p.GetDocument(documentID) 154 155 if err == sql.ErrNoRows { 156 err = nil 157 } 158 159 if err != nil { 160 log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err) 161 return false 162 } 163 164 roles, err := p.GetUserLabelRoles() 165 166 if err == sql.ErrNoRows { 167 err = nil 168 } 169 170 if err != nil { 171 log.Error(fmt.Sprintf("Unable to get document %s for permission check", documentID), err) 172 return false 173 } 174 175 for _, role := range roles { 176 if role.LabelID == document.LabelID && role.CanEdit { 177 return true 178 } 179 } 180 181 return false 182 } 183 184 // CanUploadDocument returns if the client has permission to upload documents to the given folderID. 185 func (p *Persister) CanUploadDocument(folderID string) (hasPermission bool) { 186 roles, err := p.GetUserLabelRoles() 187 188 if err == sql.ErrNoRows { 189 err = nil 190 } 191 192 if err != nil { 193 log.Error(fmt.Sprintf("Unable to check permission for folder %s", folderID), err) 194 return false 195 } 196 197 for _, role := range roles { 198 if role.LabelID == folderID && role.CanEdit { 199 return true 200 } 201 } 202 203 return false 204 } 205 206 // CanViewFolder returns if the user has permission to view the given folderID. 207 func (p *Persister) CanViewFolder(folderID string) (hasPermission bool) { 208 roles, err := p.GetUserLabelRoles() 209 210 if err == sql.ErrNoRows { 211 err = nil 212 } 213 214 if err != nil { 215 log.Error(fmt.Sprintf("Unable to check permission for folder %s", folderID), err) 216 return false 217 } 218 219 for _, role := range roles { 220 if role.LabelID == folderID && (role.CanView || role.CanEdit) { 221 return true 222 } 223 } 224 225 return false 226 }