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.
 
 
 
 
 

129 lines
3.5 KiB

  1. /*
  2. * Copyright © 2018-2020 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. "invite": true,
  55. "js": true,
  56. "login": true,
  57. "logout": true,
  58. "me": true,
  59. "media": true,
  60. "meta": true,
  61. "metadata": true,
  62. "new": true,
  63. "news": true,
  64. "oauth": true,
  65. "post": true,
  66. "posts": true,
  67. "privacy": true,
  68. "publication": true,
  69. "publications": true,
  70. "publish": true,
  71. "random": true,
  72. "read": true,
  73. "reader": true,
  74. "register": true,
  75. "remove": true,
  76. "signin": true,
  77. "signout": true,
  78. "signup": true,
  79. "start": true,
  80. "status": true,
  81. "summary": true,
  82. "support": true,
  83. "tag": true,
  84. "tags": true,
  85. "team": true,
  86. "template": true,
  87. "templates": true,
  88. "terms": true,
  89. "terms-of-service": true,
  90. "termsofservice": true,
  91. "theme": true,
  92. "themes": true,
  93. "tips": true,
  94. "tos": true,
  95. "update": true,
  96. "updates": true,
  97. "user": true,
  98. "users": true,
  99. "yourname": true,
  100. }
  101. // IsValidUsername returns true if a given username is neither reserved nor
  102. // of the correct format.
  103. func IsValidUsername(cfg *config.Config, username string) bool {
  104. // Username has to be above a character limit
  105. if len(username) < cfg.App.MinUsernameLen {
  106. return false
  107. }
  108. // Username is invalid if page with the same name exists. So traverse
  109. // available pages, adding them to reservedUsernames map that'll be checked
  110. // later.
  111. filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, "pages"), func(path string, i os.FileInfo, err error) error {
  112. reservedUsernames[i.Name()] = true
  113. return nil
  114. })
  115. // Username is invalid if it is reserved!
  116. if _, reserved := reservedUsernames[username]; reserved {
  117. return false
  118. }
  119. // TODO: use correct regexp function here
  120. return len(validUsernameReg.FindStringSubmatch(username)) > 0
  121. }