1
1
package controllers
2
2
3
3
import (
4
+ "context"
5
+ "fmt"
4
6
"html/template"
5
7
"io/ioutil"
6
8
"log"
@@ -15,6 +17,8 @@ import (
15
17
"github.com/gorilla/sessions"
16
18
"go.mongodb.org/mongo-driver/mongo"
17
19
"google.golang.org/grpc"
20
+
21
+ "github.com/go-stuff/grpc/api"
18
22
)
19
23
20
24
var (
@@ -65,27 +69,32 @@ func initTemplates() error {
65
69
return err
66
70
}
67
71
72
+ // // build noauth template
73
+ // err = initTemplateNoAuth()
74
+ // if err != nil {
75
+ // return err
76
+ // }
77
+
68
78
return nil
69
79
}
70
80
71
81
// <html> head, header, content, footer </html
72
82
func initTemplatesWithContent () error {
73
83
log .Println ("INFO > controllers/controllers.go > initTemplatesWithContent()" )
74
- var err error
84
+
85
+ layout = template .New ("mainContent.html" )
86
+
87
+ layout .Funcs (timestampFM ())
88
+ layout .Funcs (permissionFM (nil ))
75
89
76
90
// check the validity of login.html by parsing
77
- layout , err = template .ParseFiles (
91
+ layout .ParseFiles (
78
92
"./templates/layout/mainContent.html" ,
79
93
"./templates/layout/head.html" ,
80
94
"./templates/layout/header.html" ,
81
95
"./templates/layout/bypass.html" ,
82
96
"./templates/layout/footer.html" ,
83
97
)
84
- if err != nil {
85
- return err
86
- }
87
-
88
- layout .Funcs (timestampFM ())
89
98
90
99
// recurse content files templates and build separate templates for each of them
91
100
filepath .Walk ("./templates/mainContent" , walkTemplatesPath )
@@ -96,10 +105,15 @@ func initTemplatesWithContent() error {
96
105
// <html> head, header, menu, content, footer </html
97
106
func initTemplatesWithNavAndContent () error {
98
107
log .Println ("INFO > controllers/controllers.go > initTemplatesWithNavAndContent()" )
99
- var err error
108
+ //var err error
109
+
110
+ layout = template .New ("mainNavContent.html" )
111
+
112
+ layout .Funcs (timestampFM ())
113
+ layout .Funcs (permissionFM (nil ))
100
114
101
115
// check the validity of the files that make up layout.html by parsing
102
- layout , err = template .ParseFiles (
116
+ layout .ParseFiles (
103
117
"./templates/layout/mainNavContent.html" ,
104
118
"./templates/layout/head.html" ,
105
119
"./templates/layout/header.html" ,
@@ -108,18 +122,44 @@ func initTemplatesWithNavAndContent() error {
108
122
"./templates/layout/nav.html" ,
109
123
"./templates/layout/footer.html" ,
110
124
)
111
- if err != nil {
112
- return err
113
- }
114
-
115
- layout .Funcs (timestampFM ())
125
+ // if err != nil {
126
+ // return err
127
+ // }
116
128
117
129
// recurse content files templates and build separate templates for each of them
118
130
filepath .Walk ("./templates/mainMenuContent" , walkTemplatesPath )
119
131
120
132
return nil
121
133
}
122
134
135
+ // // <html> head, header, menu, content, footer </html
136
+ // func initTemplateNoAuth() error {
137
+ // log.Println("INFO > controllers/controllers.go > initTemplateNoAuth()")
138
+ // //var err error
139
+
140
+ // layout = template.New("mainContent.html")
141
+
142
+ // layout.Funcs(timestampFM())
143
+ // layout.Funcs(permissionFM(nil))
144
+
145
+ // // check the validity of the files that make up layout.html by parsing
146
+ // layout.ParseFiles(
147
+ // "./templates/layout/mainContent.html",
148
+ // "./templates/layout/head.html",
149
+ // "./templates/layout/header.html",
150
+ // "./templates/layout/bypass.html",
151
+ // "./templates/layout/footer.html",
152
+ // )
153
+ // // if err != nil {
154
+ // // return err
155
+ // // }
156
+
157
+ // // recurse content files templates and build separate templates for each of them
158
+ // filepath.Walk("./templates/mainNoAuth", walkTemplatesPath)
159
+
160
+ // return nil
161
+ // }
162
+
123
163
// recurse a directory and build templates
124
164
func walkTemplatesPath (path string , fileInfo os.FileInfo , err error ) error {
125
165
@@ -142,6 +182,7 @@ func walkTemplatesPath(path string, fileInfo os.FileInfo, err error) error {
142
182
// clone the base template
143
183
content := template .Must (layout .Clone ())
144
184
content .Funcs (timestampFM ())
185
+ content .Funcs (permissionFM (nil ))
145
186
146
187
// merge the base template and fileContents
147
188
_ , err = content .Parse (string (fileContents ))
@@ -175,7 +216,8 @@ func render(w http.ResponseWriter, r *http.Request, tmpl string, data interface{
175
216
// Set the content type.
176
217
w .Header ().Set ("Content-Type" , "text/html" )
177
218
178
- //templates[tmpl].Funcs(timestampFM())
219
+ templates [tmpl ].Funcs (timestampFM ())
220
+ templates [tmpl ].Funcs (permissionFM (r ))
179
221
180
222
// Execute the template.
181
223
err := templates [tmpl ].Execute (w , data )
@@ -199,8 +241,7 @@ func initRouter() *mux.Router {
199
241
router .HandleFunc ("/role/update/{id}" , roleUpdateHandler ).Methods ("GET" , "POST" )
200
242
router .HandleFunc ("/role/delete/{id}" , roleDeleteHandler ).Methods ("GET" )
201
243
202
- router .HandleFunc ("/route/list" , routeListHandler ).Methods ("GET" )
203
- router .HandleFunc ("/route/update" , routeUpdateHandler ).Methods ("GET" )
244
+ router .HandleFunc ("/route/list" , routeListHandler ).Methods ("GET" , "POST" )
204
245
205
246
router .HandleFunc ("/user/list" , userListHandler ).Methods ("GET" )
206
247
router .HandleFunc ("/user/update/{id}" , userUpdateHandler ).Methods ("GET" , "POST" )
@@ -209,14 +250,15 @@ func initRouter() *mux.Router {
209
250
router .HandleFunc ("/login" , loginHandler ).Methods ("GET" , "POST" )
210
251
router .HandleFunc ("/logout" , loginHandler ).Methods ("GET" )
211
252
253
+ router .HandleFunc ("/noauth" , noauthHandler ).Methods ("GET" )
254
+
212
255
// App Routes
213
256
router .HandleFunc ("/" , rootHandler ).Methods ("GET" , "POST" )
214
257
router .HandleFunc ("/home" , homeHandler ).Methods ("GET" )
215
258
216
259
router .HandleFunc ("/server/list" , serverListHandler ).Methods ("GET" )
217
260
router .HandleFunc ("/server/create" , serverCreateHandler ).Methods ("GET" , "POST" )
218
261
219
-
220
262
// Setup or static files.
221
263
router .PathPrefix ("/static/" ).Handler (http .StripPrefix ("/static/" , http .FileServer (http .Dir ("static" ))))
222
264
@@ -253,6 +295,60 @@ func timestampFM() template.FuncMap {
253
295
}
254
296
}
255
297
298
+ // funcMapPermissions allows us to inject our own way of using permissions in an html template.
299
+ func permissionFM (r * http.Request ) template.FuncMap {
300
+ // the first time the template is generated r will be nil
301
+ if r == nil {
302
+ return template.FuncMap {
303
+ "P" : func (route string ) bool {
304
+ return false
305
+ },
306
+ }
307
+ }
308
+
309
+ return template.FuncMap {
310
+ "P" : func (route string ) bool {
311
+ // get session
312
+ session , err := store .Get (r , "session" )
313
+ if err != nil {
314
+ log .Printf ("ERROR > controllers/controllers.go > permissionFM() > store.Get(): %s\n " , err .Error ())
315
+ // //http.Error(w, err.Error(), http.StatusInternalServerError)
316
+ return false
317
+ }
318
+
319
+ if session .Values ["roleid" ] == nil || session .Values ["roleid" ] == "" {
320
+ return false
321
+ }
322
+
323
+ //currentRoute := mux.CurrentRoute(r)
324
+ //pathTemplate, _ := currentRoute.GetPathTemplate()
325
+
326
+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
327
+ defer cancel ()
328
+
329
+ routeSvc := api .NewRouteServiceClient (apiClient )
330
+
331
+ // use the api to find a role
332
+ routeReq := new (api.RouteReadByRoleIDAndPathReq )
333
+
334
+ roleid := fmt .Sprintf ("%v" , session .Values ["roleid" ])
335
+
336
+ log .Printf ("INFO > controllers/controllers.go > get permissions for roleid: %v, pathtemplate: %v\n " , roleid , route )
337
+
338
+ routeReq .Route = new (api.Route )
339
+ routeReq .Route .RoleID = roleid
340
+ routeReq .Route .Path = route
341
+ routeRes , err := routeSvc .ReadByRoleIDAndPath (ctx , routeReq )
342
+ if err != nil {
343
+ log .Printf ("ERROR > controllers/controllers.go > permissionFM() > routeSvc.RouteReadByRoleIDAndPath(): %s\n " , err .Error ())
344
+ return false
345
+ }
346
+
347
+ return routeRes .Route .Permission
348
+ },
349
+ }
350
+ }
351
+
256
352
// addNotification adds a notification message to session.Values
257
353
func addNotification (session * sessions.Session , notification string ) {
258
354
session .Values ["notification" ] = notification
0 commit comments