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.
 
 
 
 
 

118 lines
3.1 KiB

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