forked from kabukky/journey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.go
835 lines (801 loc) · 26.3 KB
/
admin.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
package server
import (
"encoding/json"
"github.com/dimfeld/httptreemux"
"github.com/kabukky/journey/authentication"
"github.com/kabukky/journey/configuration"
"github.com/kabukky/journey/conversion"
"github.com/kabukky/journey/database"
"github.com/kabukky/journey/filenames"
"github.com/kabukky/journey/slug"
"github.com/kabukky/journey/structure"
"github.com/kabukky/journey/structure/methods"
"github.com/kabukky/journey/templates"
"github.com/twinj/uuid"
"io"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type JsonPost struct {
Id int64
Title string
Slug string
Markdown string
Html string
IsFeatured bool
IsPage bool
IsPublished bool
Image string
Date *time.Time
Tags string
}
type JsonBlog struct {
Url string
Title string
Description string
Logo string
Cover string
Themes []string
ActiveTheme string
PostsPerPage int64
NavigationItems []structure.Navigation
}
type JsonUser struct {
Id int64
Name string
Slug string
Email string
Image string
Cover string
Bio string
Website string
Location string
Password string
PasswordRepeated string
}
type JsonUserId struct {
Id int64
}
type JsonImage struct {
Filename string
}
// Function to serve the login page
func getLoginHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
if database.RetrieveUsersCount() == 0 {
http.Redirect(w, r, "/admin/register/", 302)
return
}
http.ServeFile(w, r, filepath.Join(filenames.AdminFilepath, "login.html"))
return
}
// Function to receive a login form
func postLoginHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
name := r.FormValue("name")
password := r.FormValue("password")
if name != "" && password != "" {
if authentication.LoginIsCorrect(name, password) {
logInUser(name, w)
} else {
log.Println("Failed login attempt for user " + name)
}
}
http.Redirect(w, r, "/admin/", 302)
return
}
// Function to serve the registration form
func getRegistrationHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
if database.RetrieveUsersCount() == 0 {
http.ServeFile(w, r, filepath.Join(filenames.AdminFilepath, "registration.html"))
return
}
http.Redirect(w, r, "/admin/", 302)
return
}
// Function to recieve a registration form.
func postRegistrationHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
if database.RetrieveUsersCount() == 0 { // TODO: Or check if authenticated user is admin when adding users from inside the admin area
name := r.FormValue("name")
email := r.FormValue("email")
password := r.FormValue("password")
if name != "" && password != "" {
hashedPassword, err := authentication.EncryptPassword(password)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
user := structure.User{Name: []byte(name), Slug: slug.Generate(name, "users"), Email: []byte(email), Image: []byte(filenames.DefaultUserImageFilename), Cover: []byte(filenames.DefaultUserCoverFilename), Role: 4}
err = methods.SaveUser(&user, hashedPassword, 1)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
http.Redirect(w, r, "/admin/", 302)
return
}
http.Redirect(w, r, "/admin/", 302)
return
} else {
// TODO: Handle creation of other users (not just the first one)
http.Error(w, "Not implemented yet.", http.StatusInternalServerError)
return
}
}
// Function to log out the user. Not used at the moment.
func logoutHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
authentication.ClearSession(w)
http.Redirect(w, r, "/admin/login/", 302)
return
}
// Function to route the /admin/ url accordingly. (Is user logged in? Is at least one user registered?)
func adminHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
if database.RetrieveUsersCount() == 0 {
http.Redirect(w, r, "/admin/register/", 302)
return
} else {
userName := authentication.GetUserName(r)
if userName != "" {
http.ServeFile(w, r, filepath.Join(filenames.AdminFilepath, "admin.html"))
return
} else {
http.Redirect(w, r, "/admin/login/", 302)
return
}
}
}
// Function to serve files belonging to the admin interface.
func adminFileHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
// Get arguments (files)
http.ServeFile(w, r, filepath.Join(filenames.AdminFilepath, params["filepath"]))
return
} else {
http.NotFound(w, r)
return
}
}
// API function to get all posts by pages
func apiPostsHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
number := params["number"]
page, err := strconv.Atoi(number)
if err != nil || page < 1 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
postsPerPage := int64(15)
posts, err := database.RetrievePostsForApi(postsPerPage, ((int64(page) - 1) * postsPerPage))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json, err := json.Marshal(postsToJson(posts))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to get a post by id
func getApiPostHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
id := params["id"]
// Get post
postId, err := strconv.ParseInt(id, 10, 64)
if err != nil || postId < 1 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
post, err := database.RetrievePostById(postId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json, err := json.Marshal(postToJson(post))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to create a post
func postApiPostHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
userId, err := getUserId(userName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create post
decoder := json.NewDecoder(r.Body)
var json JsonPost
err = decoder.Decode(&json)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var postSlug string
if json.Slug != "" { // Ceck if user has submitted a custom slug
postSlug = slug.Generate(json.Slug, "posts")
} else {
postSlug = slug.Generate(json.Title, "posts")
}
currentTime := time.Now()
post := structure.Post{Title: []byte(json.Title), Slug: postSlug, Markdown: []byte(json.Markdown), Html: conversion.GenerateHtmlFromMarkdown([]byte(json.Markdown)), IsFeatured: json.IsFeatured, IsPage: json.IsPage, IsPublished: json.IsPublished, Image: []byte(json.Image), Date: ¤tTime, Tags: methods.GenerateTagsFromCommaString(json.Tags), Author: &structure.User{Id: userId}}
err = methods.SavePost(&post)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Post created!"))
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to update a post.
func patchApiPostHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
userId, err := getUserId(userName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Update post
decoder := json.NewDecoder(r.Body)
var json JsonPost
err = decoder.Decode(&json)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
var postSlug string
// Get current slug of post
post, err := database.RetrievePostById(json.Id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if json.Slug != post.Slug { // Check if user has submitted a custom slug
postSlug = slug.Generate(json.Slug, "posts")
} else {
postSlug = post.Slug
}
currentTime := time.Now()
*post = structure.Post{Id: json.Id, Title: []byte(json.Title), Slug: postSlug, Markdown: []byte(json.Markdown), Html: conversion.GenerateHtmlFromMarkdown([]byte(json.Markdown)), IsFeatured: json.IsFeatured, IsPage: json.IsPage, IsPublished: json.IsPublished, Image: []byte(json.Image), Date: ¤tTime, Tags: methods.GenerateTagsFromCommaString(json.Tags), Author: &structure.User{Id: userId}}
err = methods.UpdatePost(post)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Post updated!"))
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to delete a post by id.
func deleteApiPostHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
id := params["id"]
// Delete post
postId, err := strconv.ParseInt(id, 10, 64)
if err != nil || postId < 1 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = methods.DeletePost(postId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Post deleted!"))
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to upload images
func apiUploadHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
// Create multipart reader
reader, err := r.MultipartReader()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Slice to hold all paths to the files
allFilePaths := make([]string, 0)
// Copy each part to destination.
for {
part, err := reader.NextPart()
if err == io.EOF {
break
}
// If part.FileName() is empty, skip this iteration.
if part.FileName() == "" {
continue
}
// Folder structure: year/month/randomname
filePath := filepath.Join(filenames.ImagesFilepath, time.Now().Format("2006"), time.Now().Format("01"))
if os.MkdirAll(filePath, 0777) != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dst, err := os.Create(filepath.Join(filePath, strconv.FormatInt(time.Now().Unix(), 10)+"_"+uuid.Formatter(uuid.NewV4(), uuid.Clean)+filepath.Ext(part.FileName())))
defer dst.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if _, err := io.Copy(dst, part); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Rewrite to file path on server
filePath = strings.Replace(dst.Name(), filenames.ImagesFilepath, "/images", 1)
// Make sure to always use "/" as path separator (to make a valid url that we can use on the blog)
filePath = filepath.ToSlash(filePath)
allFilePaths = append(allFilePaths, filePath)
}
json, err := json.Marshal(allFilePaths)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to get all images by pages
func apiImagesHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
number := params["number"]
page, err := strconv.Atoi(number)
if err != nil || page < 1 {
http.Error(w, "Not a valid api function!", http.StatusInternalServerError)
return
}
images := make([]string, 0)
// Walk all files in images folder
err = filepath.Walk(filenames.ImagesFilepath, func(filePath string, info os.FileInfo, err error) error {
if !info.IsDir() && (strings.EqualFold(filepath.Ext(filePath), ".jpg") || strings.EqualFold(filepath.Ext(filePath), ".jpeg") || strings.EqualFold(filepath.Ext(filePath), ".gif") || strings.EqualFold(filepath.Ext(filePath), ".png") || strings.EqualFold(filepath.Ext(filePath), ".svg")) {
// Rewrite to file path on server
filePath = strings.Replace(filePath, filenames.ImagesFilepath, "/images", 1)
// Make sure to always use "/" as path separator (to make a valid url that we can use on the blog)
filePath = filepath.ToSlash(filePath)
// Prepend file to slice (thus reversing the order)
images = append([]string{filePath}, images...)
}
return nil
})
if len(images) == 0 {
// Write empty json array
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("[]"))
return
}
imagesPerPage := 15
start := (page * imagesPerPage) - imagesPerPage
end := page * imagesPerPage
if start > (len(images) - 1) {
// Write empty json array
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("[]"))
return
}
if end > len(images) {
end = len(images)
}
json, err := json.Marshal(images[start:end])
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to delete an image by its filename.
func deleteApiImageHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" { // TODO: Check if the user has permissions to delete the image
// Get the file name from the json data
decoder := json.NewDecoder(r.Body)
var json JsonImage
err := decoder.Decode(&json)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = filepath.Walk(filenames.ImagesFilepath, func(filePath string, info os.FileInfo, err error) error {
if !info.IsDir() && filepath.Base(filePath) == filepath.Base(json.Filename) {
err := os.Remove(filePath)
if err != nil {
return err
}
}
return nil
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Image deleted!"))
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to get blog settings
func getApiBlogHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
// Read lock the global blog
methods.Blog.RLock()
defer methods.Blog.RUnlock()
blogJson := blogToJson(methods.Blog)
json, err := json.Marshal(blogJson)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to update blog settings
func patchApiBlogHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
userId, err := getUserId(userName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
decoder := json.NewDecoder(r.Body)
var json JsonBlog
err = decoder.Decode(&json)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Make sure postPerPage is over 0
if json.PostsPerPage < 1 {
json.PostsPerPage = 1
}
// Remove blog url in front of navigation urls
for index, _ := range json.NavigationItems {
if strings.HasPrefix(json.NavigationItems[index].Url, json.Url) {
json.NavigationItems[index].Url = strings.Replace(json.NavigationItems[index].Url, json.Url, "", 1)
// If we removed the blog url, there should be a / in front of the url
if !strings.HasPrefix(json.NavigationItems[index].Url, "/") {
json.NavigationItems[index].Url = "/" + json.NavigationItems[index].Url
}
}
}
// Retrieve old blog settings for comparison
blog, err := database.RetrieveBlog()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
tempBlog := structure.Blog{Url: []byte(configuration.Config.Url), Title: []byte(json.Title), Description: []byte(json.Description), Logo: []byte(json.Logo), Cover: []byte(json.Cover), AssetPath: []byte("/assets/"), PostCount: blog.PostCount, PostsPerPage: json.PostsPerPage, ActiveTheme: json.ActiveTheme, NavigationItems: json.NavigationItems}
err = methods.UpdateBlog(&tempBlog, userId)
// Check if active theme setting has been changed, if so, generate templates from new theme
if tempBlog.ActiveTheme != blog.ActiveTheme {
err = templates.Generate()
if err != nil {
// If there's an error while generating the new templates, the whole program must be stopped.
log.Fatal("Fatal error: Template data couldn't be generated from theme files: " + err.Error())
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("Blog settings updated!"))
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to get user settings
func getApiUserHandler(w http.ResponseWriter, r *http.Request, params map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
userId, err := getUserId(userName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
id := params["id"]
userIdToGet, err := strconv.ParseInt(id, 10, 64)
if err != nil || userIdToGet < 1 {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
} else if userIdToGet != userId { // Make sure the authenticated user is only accessing his/her own data. TODO: Make sure the user is admin when multiple users have been introduced
http.Error(w, "You don't have permission to access this data.", http.StatusForbidden)
return
}
user, err := database.RetrieveUser(userIdToGet)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
userJson := userToJson(user)
json, err := json.Marshal(userJson)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to patch user settings
func patchApiUserHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
userId, err := getUserId(userName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
decoder := json.NewDecoder(r.Body)
var json JsonUser
err = decoder.Decode(&json)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Make sure user id is over 0
if json.Id < 1 {
http.Error(w, "Wrong user id.", http.StatusInternalServerError)
return
} else if userId != json.Id { // Make sure the authenticated user is only changing his/her own data. TODO: Make sure the user is admin when multiple users have been introduced
http.Error(w, "You don't have permission to change this data.", http.StatusInternalServerError)
return
}
// Get old user data to compare
tempUser, err := database.RetrieveUser(json.Id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Make sure user email is provided
if json.Email == "" {
json.Email = string(tempUser.Email)
}
// Make sure user name is provided
if json.Name == "" {
json.Name = string(tempUser.Name)
}
// Make sure user slug is provided
if json.Slug == "" {
json.Slug = tempUser.Slug
}
// Check if new name is already taken
if json.Name != string(tempUser.Name) {
_, err = database.RetrieveUserByName([]byte(json.Name))
if err == nil {
// The new user name is already taken. Assign the old name.
// TODO: Return error that will be displayed in the admin interface.
json.Name = string(tempUser.Name)
}
}
// Check if new slug is already taken
if json.Slug != tempUser.Slug {
_, err = database.RetrieveUserBySlug(json.Slug)
if err == nil {
// The new user slug is already taken. Assign the old slug.
// TODO: Return error that will be displayed in the admin interface.
json.Slug = tempUser.Slug
}
}
user := structure.User{Id: json.Id, Name: []byte(json.Name), Slug: json.Slug, Email: []byte(json.Email), Image: []byte(json.Image), Cover: []byte(json.Cover), Bio: []byte(json.Bio), Website: []byte(json.Website), Location: []byte(json.Location)}
err = methods.UpdateUser(&user, userId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if json.Password != "" && (json.Password == json.PasswordRepeated) { // Update password if a new one was submitted
encryptedPassword, err := authentication.EncryptPassword(json.Password)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err = database.UpdateUserPassword(user.Id, encryptedPassword, time.Now(), json.Id)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
// Check if the user name was changed. If so, update the session cookie to the new user name.
if json.Name != string(tempUser.Name) {
logInUser(json.Name, w)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("User settings updated!"))
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
// API function to get the id of the currently authenticated user
func getApiUserIdHandler(w http.ResponseWriter, r *http.Request, _ map[string]string) {
userName := authentication.GetUserName(r)
if userName != "" {
userId, err := getUserId(userName)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
jsonUserId := JsonUserId{Id: userId}
json, err := json.Marshal(jsonUserId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(json)
return
} else {
http.Error(w, "Not logged in!", http.StatusInternalServerError)
return
}
}
func getUserId(userName string) (int64, error) {
user, err := database.RetrieveUserByName([]byte(userName))
if err != nil {
return 0, err
}
return user.Id, nil
}
func logInUser(name string, w http.ResponseWriter) {
authentication.SetSession(name, w)
userId, err := getUserId(name)
if err != nil {
log.Println("Couldn't get id of logged in user:", err)
}
err = database.UpdateLastLogin(time.Now(), userId)
if err != nil {
log.Println("Couldn't update last login date of a user:", err)
}
}
func postsToJson(posts []structure.Post) *[]JsonPost {
jsonPosts := make([]JsonPost, len(posts))
for index, _ := range posts {
jsonPosts[index] = *postToJson(&posts[index])
}
return &jsonPosts
}
func postToJson(post *structure.Post) *JsonPost {
var jsonPost JsonPost
jsonPost.Id = post.Id
jsonPost.Title = string(post.Title)
jsonPost.Slug = post.Slug
jsonPost.Markdown = string(post.Markdown)
jsonPost.Html = string(post.Html)
jsonPost.IsFeatured = post.IsFeatured
jsonPost.IsPage = post.IsPage
jsonPost.IsPublished = post.IsPublished
jsonPost.Image = string(post.Image)
jsonPost.Date = post.Date
tags := make([]string, len(post.Tags))
for index, _ := range post.Tags {
tags[index] = string(post.Tags[index].Name)
}
jsonPost.Tags = strings.Join(tags, ",")
return &jsonPost
}
func blogToJson(blog *structure.Blog) *JsonBlog {
var jsonBlog JsonBlog
jsonBlog.Url = string(blog.Url)
jsonBlog.Title = string(blog.Title)
jsonBlog.Description = string(blog.Description)
jsonBlog.Logo = string(blog.Logo)
jsonBlog.Cover = string(blog.Cover)
jsonBlog.PostsPerPage = blog.PostsPerPage
jsonBlog.Themes = templates.GetAllThemes()
jsonBlog.ActiveTheme = blog.ActiveTheme
jsonBlog.NavigationItems = blog.NavigationItems
return &jsonBlog
}
func userToJson(user *structure.User) *JsonUser {
var jsonUser JsonUser
jsonUser.Id = user.Id
jsonUser.Name = string(user.Name)
jsonUser.Slug = user.Slug
jsonUser.Email = string(user.Email)
jsonUser.Image = string(user.Image)
jsonUser.Cover = string(user.Cover)
jsonUser.Bio = string(user.Bio)
jsonUser.Website = string(user.Website)
jsonUser.Location = string(user.Location)
return &jsonUser
}
func InitializeAdmin(router *httptreemux.TreeMux) {
// For admin panel
router.GET("/admin/", adminHandler)
router.GET("/admin/login/", getLoginHandler)
router.POST("/admin/login/", postLoginHandler)
router.GET("/admin/register/", getRegistrationHandler)
router.POST("/admin/register/", postRegistrationHandler)
router.GET("/admin/logout/", logoutHandler)
router.GET("/admin/*filepath", adminFileHandler)
// For admin API (no trailing slash)
// Posts
router.GET("/admin/api/posts/:number", apiPostsHandler)
// Post
router.GET("/admin/api/post/:id", getApiPostHandler)
router.POST("/admin/api/post", postApiPostHandler)
router.PATCH("/admin/api/post", patchApiPostHandler)
router.DELETE("/admin/api/post/:id", deleteApiPostHandler)
// Upload
router.POST("/admin/api/upload", apiUploadHandler)
// Images
router.GET("/admin/api/images/:number", apiImagesHandler)
router.DELETE("/admin/api/image", deleteApiImageHandler)
// Blog
router.GET("/admin/api/blog", getApiBlogHandler)
router.PATCH("/admin/api/blog", patchApiBlogHandler)
// User
router.GET("/admin/api/user/:id", getApiUserHandler)
router.PATCH("/admin/api/user", patchApiUserHandler)
// User id
router.GET("/admin/api/userid", getApiUserIdHandler)
}