A clean, Markdown-based publishing platform made for writers. Write together, and build a community. https://writefreely.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

128 lines
3.4 KiB

  1. /*
  2. * Copyright © 2018 A Bunch Tell LLC.
  3. *
  4. * This file is part of WriteFreely.
  5. *
  6. * WriteFreely is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Affero General Public License, included
  8. * in the LICENSE file in this source code package.
  9. */
  10. package author
  11. import (
  12. "github.com/writeas/writefreely/config"
  13. "os"
  14. "path/filepath"
  15. "regexp"
  16. )
  17. // Regex pattern for valid usernames
  18. var validUsernameReg = regexp.MustCompile("^[a-zA-Z0-9][a-zA-Z0-9-]*$")
  19. // List of reserved usernames
  20. var reservedUsernames = map[string]bool{
  21. "a": true,
  22. "about": true,
  23. "add": true,
  24. "admin": true,
  25. "administrator": true,
  26. "adminzone": true,
  27. "api": true,
  28. "article": true,
  29. "articles": true,
  30. "auth": true,
  31. "authenticate": true,
  32. "browse": true,
  33. "c": true,
  34. "categories": true,
  35. "category": true,
  36. "changes": true,
  37. "community": true,
  38. "create": true,
  39. "css": true,
  40. "data": true,
  41. "dev": true,
  42. "developers": true,
  43. "draft": true,
  44. "drafts": true,
  45. "edit": true,
  46. "edits": true,
  47. "faq": true,
  48. "feed": true,
  49. "feedback": true,
  50. "guide": true,
  51. "guides": true,
  52. "help": true,
  53. "index": true,
  54. "js": true,
  55. "login": true,
  56. "logout": true,
  57. "me": true,
  58. "media": true,
  59. "meta": true,
  60. "metadata": true,
  61. "new": true,
  62. "news": true,
  63. "post": true,
  64. "posts": true,
  65. "privacy": true,
  66. "publication": true,
  67. "publications": true,
  68. "publish": true,
  69. "random": true,
  70. "read": true,
  71. "reader": true,
  72. "register": true,
  73. "remove": true,
  74. "signin": true,
  75. "signout": true,
  76. "signup": true,
  77. "start": true,
  78. "status": true,
  79. "summary": true,
  80. "support": true,
  81. "tag": true,
  82. "tags": true,
  83. "team": true,
  84. "template": true,
  85. "templates": true,
  86. "terms": true,
  87. "terms-of-service": true,
  88. "termsofservice": true,
  89. "theme": true,
  90. "themes": true,
  91. "tips": true,
  92. "tos": true,
  93. "update": true,
  94. "updates": true,
  95. "user": true,
  96. "users": true,
  97. "yourname": true,
  98. }
  99. // IsValidUsername returns true if a given username is neither reserved nor
  100. // of the correct format.
  101. func IsValidUsername(cfg *config.Config, username string) bool {
  102. // Username has to be above a character limit
  103. if len(username) < cfg.App.MinUsernameLen {
  104. return false
  105. }
  106. // Username is invalid if page with the same name exists. So traverse
  107. // available pages, adding them to reservedUsernames map that'll be checked
  108. // later.
  109. // TODO: use pagesDir const
  110. filepath.Walk("pages/", func(path string, i os.FileInfo, err error) error {
  111. reservedUsernames[i.Name()] = true
  112. return nil
  113. })
  114. // Username is invalid if it is reserved!
  115. if _, reserved := reservedUsernames[username]; reserved {
  116. return false
  117. }
  118. // TODO: use correct regexp function here
  119. return len(validUsernameReg.FindStringSubmatch(username)) > 0
  120. }