IsValidUsername returns true if a given username is neither reserved nor of the correct format.
(cfg *config.Config, username string)
| 107 | // IsValidUsername returns true if a given username is neither reserved nor |
| 108 | // of the correct format. |
| 109 | func IsValidUsername(cfg *config.Config, username string) bool { |
| 110 | // Username has to be above a character limit |
| 111 | if len(username) < cfg.App.MinUsernameLen { |
| 112 | return false |
| 113 | } |
| 114 | // Username is invalid if page with the same name exists. So traverse |
| 115 | // available pages, adding them to reservedUsernames map that'll be checked |
| 116 | // later. |
| 117 | err := filepath.Walk(filepath.Join(cfg.Server.PagesParentDir, "pages"), func(path string, i os.FileInfo, err error) error { |
| 118 | if err != nil { |
| 119 | return err |
| 120 | } |
| 121 | reservedUsernames[i.Name()] = true |
| 122 | return nil |
| 123 | }) |
| 124 | if err != nil { |
| 125 | log.Error("[IMPORTANT WARNING]: Could not determine IsValidUsername! %s", err) |
| 126 | return false |
| 127 | } |
| 128 | |
| 129 | // Username is invalid if it is reserved! |
| 130 | if _, reserved := reservedUsernames[username]; reserved { |
| 131 | return false |
| 132 | } |
| 133 | |
| 134 | // TODO: use correct regexp function here |
| 135 | return len(validUsernameReg.FindStringSubmatch(username)) > 0 |
| 136 | } |
no outgoing calls
no test coverage detected